org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714
  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 'cl)
  68. (require 'cl-extra)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. ;;;; Customization variables
  81. ;;; Version
  82. (defconst org-version "5.22a+"
  83. "The version number of the file org.el.")
  84. (defun org-version (&optional here)
  85. "Show the org-mode version in the echo area.
  86. With prefix arg HERE, insert it at point."
  87. (interactive "P")
  88. (let ((version (format "Org-mode version %s" org-version)))
  89. (message version)
  90. (if here
  91. (insert version))))
  92. ;;; Compatibility constants
  93. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  94. (defconst org-format-transports-properties-p
  95. (let ((x "a"))
  96. (add-text-properties 0 1 '(test t) x)
  97. (get-text-property 0 'test (format "%s" x)))
  98. "Does format transport text properties?")
  99. (defmacro org-bound-and-true-p (var)
  100. "Return the value of symbol VAR if it is bound, else nil."
  101. `(and (boundp (quote ,var)) ,var))
  102. (defmacro org-unmodified (&rest body)
  103. "Execute body without changing `buffer-modified-p'."
  104. `(set-buffer-modified-p
  105. (prog1 (buffer-modified-p) ,@body)))
  106. (defmacro org-re (s)
  107. "Replace posix classes in regular expression."
  108. (if (featurep 'xemacs)
  109. (let ((ss s))
  110. (save-match-data
  111. (while (string-match "\\[:alnum:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  113. (while (string-match "\\[:alpha:\\]" ss)
  114. (setq ss (replace-match "a-zA-Z" t t ss)))
  115. ss))
  116. s))
  117. (defmacro org-preserve-lc (&rest body)
  118. `(let ((_line (org-current-line))
  119. (_col (current-column)))
  120. (unwind-protect
  121. (progn ,@body)
  122. (goto-line _line)
  123. (move-to-column _col))))
  124. (defmacro org-without-partial-completion (&rest body)
  125. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  126. partial-completion-mode)))
  127. (unwind-protect
  128. (progn
  129. (if pc-mode (partial-completion-mode -1))
  130. ,@body)
  131. (if pc-mode (partial-completion-mode 1)))))
  132. ;;; The custom variables
  133. (defgroup org nil
  134. "Outline-based notes management and organizer."
  135. :tag "Org"
  136. :group 'outlines
  137. :group 'hypermedia
  138. :group 'calendar)
  139. (defcustom org-load-hook '(org-load-default-extensions)
  140. "Hook that is run after org.el has been loaded.
  141. This happens also after `org' has been provided, so
  142. requiring something in this hook that does a (require 'org) is ok."
  143. :group 'org
  144. :type 'hook)
  145. (defcustom org-default-extensions '(org-irc)
  146. "Extensions that should always be loaded together with org.el.
  147. If the description starts with <A>, this means the extension
  148. will be autoloaded when needed, preloading is not necessary."
  149. :group 'org
  150. :type
  151. '(set :greedy t
  152. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  153. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  154. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  155. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  156. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  157. (defun org-load-default-extensions ()
  158. "Load all extensions listed in `org-default-extensions'."
  159. (mapc (lambda (ext)
  160. (condition-case nil (require ext)
  161. (error (message "Problems while trying to load feature `%s'" ext))))
  162. org-default-extensions))
  163. ;; FIXME: Needs a separate group...
  164. (defcustom org-completion-fallback-command 'hippie-expand
  165. "The expansion command called by \\[org-complete] in normal context.
  166. Normal means, no org-mode-specific context."
  167. :group 'org
  168. :type 'function)
  169. (defgroup org-startup nil
  170. "Options concerning startup of Org-mode."
  171. :tag "Org Startup"
  172. :group 'org)
  173. (defcustom org-startup-folded t
  174. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: fold
  178. #+STARTUP: nofold
  179. #+STARTUP: content"
  180. :group 'org-startup
  181. :type '(choice
  182. (const :tag "nofold: show all" nil)
  183. (const :tag "fold: overview" t)
  184. (const :tag "content: all headlines" content)))
  185. (defcustom org-startup-truncated t
  186. "Non-nil means, entering Org-mode will set `truncate-lines'.
  187. This is useful since some lines containing links can be very long and
  188. uninteresting. Also tables look terrible when wrapped."
  189. :group 'org-startup
  190. :type 'boolean)
  191. (defcustom org-startup-align-all-tables nil
  192. "Non-nil means, align all tables when visiting a file.
  193. This is useful when the column width in tables is forced with <N> cookies
  194. in table fields. Such tables will look correct only after the first re-align.
  195. This can also be configured on a per-file basis by adding one of
  196. the following lines anywhere in the buffer:
  197. #+STARTUP: align
  198. #+STARTUP: noalign"
  199. :group 'org-startup
  200. :type 'boolean)
  201. (defcustom org-insert-mode-line-in-empty-file nil
  202. "Non-nil means insert the first line setting Org-mode in empty files.
  203. When the function `org-mode' is called interactively in an empty file, this
  204. normally means that the file name does not automatically trigger Org-mode.
  205. To ensure that the file will always be in Org-mode in the future, a
  206. line enforcing Org-mode will be inserted into the buffer, if this option
  207. has been set."
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-replace-disputed-keys nil
  211. "Non-nil means use alternative key bindings for some keys.
  212. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  213. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  214. If you want to use Org-mode together with one of these other modes,
  215. or more generally if you would like to move some Org-mode commands to
  216. other keys, set this variable and configure the keys with the variable
  217. `org-disputed-keys'.
  218. This option is only relevant at load-time of Org-mode, and must be set
  219. *before* org.el is loaded. Changing it requires a restart of Emacs to
  220. become effective."
  221. :group 'org-startup
  222. :type 'boolean)
  223. (if (fboundp 'defvaralias)
  224. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  225. (defcustom org-disputed-keys
  226. '(([(shift up)] . [(meta p)])
  227. ([(shift down)] . [(meta n)])
  228. ([(shift left)] . [(meta -)])
  229. ([(shift right)] . [(meta +)])
  230. ([(control shift right)] . [(meta shift +)])
  231. ([(control shift left)] . [(meta shift -)]))
  232. "Keys for which Org-mode and other modes compete.
  233. This is an alist, cars are the default keys, second element specifies
  234. the alternative to use when `org-replace-disputed-keys' is t.
  235. Keys can be specified in any syntax supported by `define-key'.
  236. The value of this option takes effect only at Org-mode's startup,
  237. therefore you'll have to restart Emacs to apply it after changing."
  238. :group 'org-startup
  239. :type 'alist)
  240. (defun org-key (key)
  241. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  242. Or return the original if not disputed."
  243. (if org-replace-disputed-keys
  244. (let* ((nkey (key-description key))
  245. (x (org-find-if (lambda (x)
  246. (equal (key-description (car x)) nkey))
  247. org-disputed-keys)))
  248. (if x (cdr x) key))
  249. key))
  250. (defun org-find-if (predicate seq)
  251. (catch 'exit
  252. (while seq
  253. (if (funcall predicate (car seq))
  254. (throw 'exit (car seq))
  255. (pop seq)))))
  256. (defun org-defkey (keymap key def)
  257. "Define a key, possibly translated, as returned by `org-key'."
  258. (define-key keymap (org-key key) def))
  259. (defcustom org-ellipsis nil
  260. "The ellipsis to use in the Org-mode outline.
  261. When nil, just use the standard three dots. When a string, use that instead,
  262. When a face, use the standart 3 dots, but with the specified face.
  263. The change affects only Org-mode (which will then use its own display table).
  264. Changing this requires executing `M-x org-mode' in a buffer to become
  265. effective."
  266. :group 'org-startup
  267. :type '(choice (const :tag "Default" nil)
  268. (face :tag "Face" :value org-warning)
  269. (string :tag "String" :value "...#")))
  270. (defvar org-display-table nil
  271. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  272. (defgroup org-keywords nil
  273. "Keywords in Org-mode."
  274. :tag "Org Keywords"
  275. :group 'org)
  276. (defcustom org-deadline-string "DEADLINE:"
  277. "String to mark deadline entries.
  278. A deadline is this string, followed by a time stamp. Should be a word,
  279. terminated by a colon. You can insert a schedule keyword and
  280. a timestamp with \\[org-deadline].
  281. Changes become only effective after restarting Emacs."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defcustom org-scheduled-string "SCHEDULED:"
  285. "String to mark scheduled TODO entries.
  286. A schedule is this string, followed by a time stamp. Should be a word,
  287. terminated by a colon. You can insert a schedule keyword and
  288. a timestamp with \\[org-schedule].
  289. Changes become only effective after restarting Emacs."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-closed-string "CLOSED:"
  293. "String used as the prefix for timestamps logging closing a TODO entry."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-clock-string "CLOCK:"
  297. "String used as prefix for timestamps clocking work hours on an item."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-comment-string "COMMENT"
  301. "Entries starting with this keyword will never be exported.
  302. An entry can be toggled between COMMENT and normal with
  303. \\[org-toggle-comment].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-quote-string "QUOTE"
  308. "Entries starting with this keyword will be exported in fixed-width font.
  309. Quoting applies only to the text in the entry following the headline, and does
  310. not extend beyond the next headline, even if that is lower level.
  311. An entry can be toggled between QUOTE and normal with
  312. \\[org-toggle-fixed-width-section]."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defconst org-repeat-re
  316. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  317. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  318. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  319. "Regular expression for specifying repeated events.
  320. After a match, group 1 contains the repeat expression.")
  321. (defgroup org-structure nil
  322. "Options concerning the general structure of Org-mode files."
  323. :tag "Org Structure"
  324. :group 'org)
  325. (defgroup org-reveal-location nil
  326. "Options about how to make context of a location visible."
  327. :tag "Org Reveal Location"
  328. :group 'org-structure)
  329. (defconst org-context-choice
  330. '(choice
  331. (const :tag "Always" t)
  332. (const :tag "Never" nil)
  333. (repeat :greedy t :tag "Individual contexts"
  334. (cons
  335. (choice :tag "Context"
  336. (const agenda)
  337. (const org-goto)
  338. (const occur-tree)
  339. (const tags-tree)
  340. (const link-search)
  341. (const mark-goto)
  342. (const bookmark-jump)
  343. (const isearch)
  344. (const default))
  345. (boolean))))
  346. "Contexts for the reveal options.")
  347. (defcustom org-show-hierarchy-above '((default . t))
  348. "Non-nil means, show full hierarchy when revealing a location.
  349. Org-mode often shows locations in an org-mode file which might have
  350. been invisible before. When this is set, the hierarchy of headings
  351. above the exposed location is shown.
  352. Turning this off for example for sparse trees makes them very compact.
  353. Instead of t, this can also be an alist specifying this option for different
  354. contexts. Valid contexts are
  355. agenda when exposing an entry from the agenda
  356. org-goto when using the command `org-goto' on key C-c C-j
  357. occur-tree when using the command `org-occur' on key C-c /
  358. tags-tree when constructing a sparse tree based on tags matches
  359. link-search when exposing search matches associated with a link
  360. mark-goto when exposing the jump goal of a mark
  361. bookmark-jump when exposing a bookmark location
  362. isearch when exiting from an incremental search
  363. default default for all contexts not set explicitly"
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-following-heading '((default . nil))
  367. "Non-nil means, show following heading when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the heading following the
  370. match is shown.
  371. Turning this off for example for sparse trees makes them very compact,
  372. but makes it harder to edit the location of the match. In such a case,
  373. use the command \\[org-reveal] to show more context.
  374. Instead of t, this can also be an alist specifying this option for different
  375. contexts. See `org-show-hierarchy-above' for valid contexts."
  376. :group 'org-reveal-location
  377. :type org-context-choice)
  378. (defcustom org-show-siblings '((default . nil) (isearch t))
  379. "Non-nil means, show all sibling heading when revealing a location.
  380. Org-mode often shows locations in an org-mode file which might have
  381. been invisible before. When this is set, the sibling of the current entry
  382. heading are all made visible. If `org-show-hierarchy-above' is t,
  383. the same happens on each level of the hierarchy above the current entry.
  384. By default this is on for the isearch context, off for all other contexts.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-entry-below '((default . nil))
  393. "Non-nil means, show the entry below a headline when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the text below the headline that is
  396. exposed is also shown.
  397. By default this is off for all contexts.
  398. Instead of t, this can also be an alist specifying this option for different
  399. contexts. See `org-show-hierarchy-above' for valid contexts."
  400. :group 'org-reveal-location
  401. :type org-context-choice)
  402. (defgroup org-cycle nil
  403. "Options concerning visibility cycling in Org-mode."
  404. :tag "Org Cycle"
  405. :group 'org-structure)
  406. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  407. "Names of drawers. Drawers are not opened by cycling on the headline above.
  408. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  409. this:
  410. :DRAWERNAME:
  411. .....
  412. :END:
  413. The drawer \"PROPERTIES\" is special for capturing properties through
  414. the property API.
  415. Drawers can be defined on the per-file basis with a line like:
  416. #+DRAWERS: HIDDEN STATE PROPERTIES"
  417. :group 'org-structure
  418. :type '(repeat (string :tag "Drawer Name")))
  419. (defcustom org-cycle-global-at-bob nil
  420. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  421. This makes it possible to do global cycling without having to use S-TAB or
  422. C-u TAB. For this special case to work, the first line of the buffer
  423. must not be a headline - it may be empty ot some other text. When used in
  424. this way, `org-cycle-hook' is disables temporarily, to make sure the
  425. cursor stays at the beginning of the buffer.
  426. When this option is nil, don't do anything special at the beginning
  427. of the buffer."
  428. :group 'org-cycle
  429. :type 'boolean)
  430. (defcustom org-cycle-emulate-tab t
  431. "Where should `org-cycle' emulate TAB.
  432. nil Never
  433. white Only in completely white lines
  434. whitestart Only at the beginning of lines, before the first non-white char
  435. t Everywhere except in headlines
  436. exc-hl-bol Everywhere except at the start of a headline
  437. If TAB is used in a place where it does not emulate TAB, the current subtree
  438. visibility is cycled."
  439. :group 'org-cycle
  440. :type '(choice (const :tag "Never" nil)
  441. (const :tag "Only in completely white lines" white)
  442. (const :tag "Before first char in a line" whitestart)
  443. (const :tag "Everywhere except in headlines" t)
  444. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  445. ))
  446. (defcustom org-cycle-separator-lines 2
  447. "Number of empty lines needed to keep an empty line between collapsed trees.
  448. If you leave an empty line between the end of a subtree and the following
  449. headline, this empty line is hidden when the subtree is folded.
  450. Org-mode will leave (exactly) one empty line visible if the number of
  451. empty lines is equal or larger to the number given in this variable.
  452. So the default 2 means, at least 2 empty lines after the end of a subtree
  453. are needed to produce free space between a collapsed subtree and the
  454. following headline.
  455. Special case: when 0, never leave empty lines in collapsed view."
  456. :group 'org-cycle
  457. :type 'integer)
  458. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  459. org-cycle-hide-drawers
  460. org-cycle-show-empty-lines
  461. org-optimize-window-after-visibility-change)
  462. "Hook that is run after `org-cycle' has changed the buffer visibility.
  463. The function(s) in this hook must accept a single argument which indicates
  464. the new state that was set by the most recent `org-cycle' command. The
  465. argument is a symbol. After a global state change, it can have the values
  466. `overview', `content', or `all'. After a local state change, it can have
  467. the values `folded', `children', or `subtree'."
  468. :group 'org-cycle
  469. :type 'hook)
  470. (defgroup org-edit-structure nil
  471. "Options concerning structure editing in Org-mode."
  472. :tag "Org Edit Structure"
  473. :group 'org-structure)
  474. (defcustom org-odd-levels-only nil
  475. "Non-nil means, skip even levels and only use odd levels for the outline.
  476. This has the effect that two stars are being added/taken away in
  477. promotion/demotion commands. It also influences how levels are
  478. handled by the exporters.
  479. Changing it requires restart of `font-lock-mode' to become effective
  480. for fontification also in regions already fontified.
  481. You may also set this on a per-file basis by adding one of the following
  482. lines to the buffer:
  483. #+STARTUP: odd
  484. #+STARTUP: oddeven"
  485. :group 'org-edit-structure
  486. :group 'org-font-lock
  487. :type 'boolean)
  488. (defcustom org-adapt-indentation t
  489. "Non-nil means, adapt indentation when promoting and demoting.
  490. When this is set and the *entire* text in an entry is indented, the
  491. indentation is increased by one space in a demotion command, and
  492. decreased by one in a promotion command. If any line in the entry
  493. body starts at column 0, indentation is not changed at all."
  494. :group 'org-edit-structure
  495. :type 'boolean)
  496. (defcustom org-special-ctrl-a/e nil
  497. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  498. When t, `C-a' will bring back the cursor to the beginning of the
  499. headline text, i.e. after the stars and after a possible TODO keyword.
  500. In an item, this will be the position after the bullet.
  501. When the cursor is already at that position, another `C-a' will bring
  502. it to the beginning of the line.
  503. `C-e' will jump to the end of the headline, ignoring the presence of tags
  504. in the headline. A second `C-e' will then jump to the true end of the
  505. line, after any tags.
  506. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  507. and only a directly following, identical keypress will bring the cursor
  508. to the special positions."
  509. :group 'org-edit-structure
  510. :type '(choice
  511. (const :tag "off" nil)
  512. (const :tag "after bullet first" t)
  513. (const :tag "border first" reversed)))
  514. (if (fboundp 'defvaralias)
  515. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  516. (defcustom org-special-ctrl-k nil
  517. "Non-nil means `C-k' will behave specially in headlines.
  518. When nil, `C-k' will call the default `kill-line' command.
  519. When t, the following will happen while the cursor is in the headline:
  520. - When the cursor is at the beginning of a headline, kill the entire
  521. line and possible the folded subtree below the line.
  522. - When in the middle of the headline text, kill the headline up to the tags.
  523. - When after the headline text, kill the tags."
  524. :group 'org-edit-structure
  525. :type 'boolean)
  526. (defcustom org-M-RET-may-split-line '((default . t))
  527. "Non-nil means, M-RET will split the line at the cursor position.
  528. When nil, it will go to the end of the line before making a
  529. new line.
  530. You may also set this option in a different way for different
  531. contexts. Valid contexts are:
  532. headline when creating a new headline
  533. item when creating a new item
  534. table in a table field
  535. default the value to be used for all contexts not explicitly
  536. customized"
  537. :group 'org-structure
  538. :group 'org-table
  539. :type '(choice
  540. (const :tag "Always" t)
  541. (const :tag "Never" nil)
  542. (repeat :greedy t :tag "Individual contexts"
  543. (cons
  544. (choice :tag "Context"
  545. (const headline)
  546. (const item)
  547. (const table)
  548. (const default))
  549. (boolean)))))
  550. (defcustom org-blank-before-new-entry '((heading . nil)
  551. (plain-list-item . nil))
  552. "Should `org-insert-heading' leave a blank line before new heading/item?
  553. The value is an alist, with `heading' and `plain-list-item' as car,
  554. and a boolean flag as cdr."
  555. :group 'org-edit-structure
  556. :type '(list
  557. (cons (const heading) (boolean))
  558. (cons (const plain-list-item) (boolean))))
  559. (defcustom org-insert-heading-hook nil
  560. "Hook being run after inserting a new heading."
  561. :group 'org-edit-structure
  562. :type 'hook)
  563. (defcustom org-enable-fixed-width-editor t
  564. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  565. This currently only means, they are never auto-wrapped.
  566. When nil, such lines will be treated like ordinary lines.
  567. See also the QUOTE keyword."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-goto-auto-isearch t
  571. "Non-nil means, typing characters in org-goto starts incremental search."
  572. :group 'org-edit-structure
  573. :type 'boolean)
  574. (defgroup org-sparse-trees nil
  575. "Options concerning sparse trees in Org-mode."
  576. :tag "Org Sparse Trees"
  577. :group 'org-structure)
  578. (defcustom org-highlight-sparse-tree-matches t
  579. "Non-nil means, highlight all matches that define a sparse tree.
  580. The highlights will automatically disappear the next time the buffer is
  581. changed by an edit command."
  582. :group 'org-sparse-trees
  583. :type 'boolean)
  584. (defcustom org-remove-highlights-with-change t
  585. "Non-nil means, any change to the buffer will remove temporary highlights.
  586. Such highlights are created by `org-occur' and `org-clock-display'.
  587. When nil, `C-c C-c needs to be used to get rid of the highlights.
  588. The highlights created by `org-preview-latex-fragment' always need
  589. `C-c C-c' to be removed."
  590. :group 'org-sparse-trees
  591. :group 'org-time
  592. :type 'boolean)
  593. (defcustom org-occur-hook '(org-first-headline-recenter)
  594. "Hook that is run after `org-occur' has constructed a sparse tree.
  595. This can be used to recenter the window to show as much of the structure
  596. as possible."
  597. :group 'org-sparse-trees
  598. :type 'hook)
  599. (defgroup org-plain-lists nil
  600. "Options concerning plain lists in Org-mode."
  601. :tag "Org Plain lists"
  602. :group 'org-structure)
  603. (defcustom org-cycle-include-plain-lists nil
  604. "Non-nil means, include plain lists into visibility cycling.
  605. This means that during cycling, plain list items will *temporarily* be
  606. interpreted as outline headlines with a level given by 1000+i where i is the
  607. indentation of the bullet. In all other operations, plain list items are
  608. not seen as headlines. For example, you cannot assign a TODO keyword to
  609. such an item."
  610. :group 'org-plain-lists
  611. :type 'boolean)
  612. (defcustom org-plain-list-ordered-item-terminator t
  613. "The character that makes a line with leading number an ordered list item.
  614. Valid values are ?. and ?\). To get both terminators, use t. While
  615. ?. may look nicer, it creates the danger that a line with leading
  616. number may be incorrectly interpreted as an item. ?\) therefore is
  617. the safe choice."
  618. :group 'org-plain-lists
  619. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  620. (const :tag "paren like in \"2)\"" ?\))
  621. (const :tab "both" t)))
  622. (defcustom org-auto-renumber-ordered-lists t
  623. "Non-nil means, automatically renumber ordered plain lists.
  624. Renumbering happens when the sequence have been changed with
  625. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  626. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  627. :group 'org-plain-lists
  628. :type 'boolean)
  629. (defcustom org-provide-checkbox-statistics t
  630. "Non-nil means, update checkbox statistics after insert and toggle.
  631. When this is set, checkbox statistics is updated each time you either insert
  632. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  633. with \\[org-ctrl-c-ctrl-c\\]."
  634. :group 'org-plain-lists
  635. :type 'boolean)
  636. (defgroup org-archive nil
  637. "Options concerning archiving in Org-mode."
  638. :tag "Org Archive"
  639. :group 'org-structure)
  640. (defcustom org-archive-tag "ARCHIVE"
  641. "The tag that marks a subtree as archived.
  642. An archived subtree does not open during visibility cycling, and does
  643. not contribute to the agenda listings.
  644. After changing this, font-lock must be restarted in the relevant buffers to
  645. get the proper fontification."
  646. :group 'org-archive
  647. :group 'org-keywords
  648. :type 'string)
  649. (defcustom org-agenda-skip-archived-trees t
  650. "Non-nil means, the agenda will skip any items located in archived trees.
  651. An archived tree is a tree marked with the tag ARCHIVE."
  652. :group 'org-archive
  653. :group 'org-agenda-skip
  654. :type 'boolean)
  655. (defcustom org-cycle-open-archived-trees nil
  656. "Non-nil means, `org-cycle' will open archived trees.
  657. An archived tree is a tree marked with the tag ARCHIVE.
  658. When nil, archived trees will stay folded. You can still open them with
  659. normal outline commands like `show-all', but not with the cycling commands."
  660. :group 'org-archive
  661. :group 'org-cycle
  662. :type 'boolean)
  663. (defcustom org-sparse-tree-open-archived-trees nil
  664. "Non-nil means sparse tree construction shows matches in archived trees.
  665. When nil, matches in these trees are highlighted, but the trees are kept in
  666. collapsed state."
  667. :group 'org-archive
  668. :group 'org-sparse-trees
  669. :type 'boolean)
  670. (defcustom org-archive-location "%s_archive::"
  671. "The location where subtrees should be archived.
  672. This string consists of two parts, separated by a double-colon.
  673. The first part is a file name - when omitted, archiving happens in the same
  674. file. %s will be replaced by the current file name (without directory part).
  675. Archiving to a different file is useful to keep archived entries from
  676. contributing to the Org-mode Agenda.
  677. The part after the double colon is a headline. The archived entries will be
  678. filed under that headline. When omitted, the subtrees are simply filed away
  679. at the end of the file, as top-level entries.
  680. Here are a few examples:
  681. \"%s_archive::\"
  682. If the current file is Projects.org, archive in file
  683. Projects.org_archive, as top-level trees. This is the default.
  684. \"::* Archived Tasks\"
  685. Archive in the current file, under the top-level headline
  686. \"* Archived Tasks\".
  687. \"~/org/archive.org::\"
  688. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  689. \"basement::** Finished Tasks\"
  690. Archive in file ./basement (relative path), as level 3 trees
  691. below the level 2 heading \"** Finished Tasks\".
  692. You may set this option on a per-file basis by adding to the buffer a
  693. line like
  694. #+ARCHIVE: basement::** Finished Tasks"
  695. :group 'org-archive
  696. :type 'string)
  697. (defcustom org-archive-mark-done t
  698. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  699. This can be a string to set the keyword to use. When t, Org-mode will
  700. use the first keyword in its list that means done."
  701. :group 'org-archive
  702. :type '(choice
  703. (const :tag "No" nil)
  704. (const :tag "Yes" t)
  705. (string :tag "Use this keyword")))
  706. (defcustom org-archive-stamp-time t
  707. "Non-nil means, add a time stamp to entries moved to an archive file.
  708. This variable is obsolete and has no effect anymore, instead add ot remove
  709. `time' from the variablle `org-archive-save-context-info'."
  710. :group 'org-archive
  711. :type 'boolean)
  712. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  713. "Parts of context info that should be stored as properties when archiving.
  714. When a subtree is moved to an archive file, it looses information given by
  715. context, like inherited tags, the category, and possibly also the TODO
  716. state (depending on the variable `org-archive-mark-done').
  717. This variable can be a list of any of the following symbols:
  718. time The time of archiving.
  719. file The file where the entry originates.
  720. itags The local tags, in the headline of the subtree.
  721. ltags The tags the subtree inherits from further up the hierarchy.
  722. todo The pre-archive TODO state.
  723. category The category, taken from file name or #+CATEGORY lines.
  724. olpath The outline path to the item. These are all headlines above
  725. the current item, separated by /, like a file path.
  726. For each symbol present in the list, a property will be created in
  727. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  728. information."
  729. :group 'org-archive
  730. :type '(set :greedy t
  731. (const :tag "Time" time)
  732. (const :tag "File" file)
  733. (const :tag "Category" category)
  734. (const :tag "TODO state" todo)
  735. (const :tag "TODO state" priority)
  736. (const :tag "Inherited tags" itags)
  737. (const :tag "Outline path" olpath)
  738. (const :tag "Local tags" ltags)))
  739. (defgroup org-imenu-and-speedbar nil
  740. "Options concerning imenu and speedbar in Org-mode."
  741. :tag "Org Imenu and Speedbar"
  742. :group 'org-structure)
  743. (defcustom org-imenu-depth 2
  744. "The maximum level for Imenu access to Org-mode headlines.
  745. This also applied for speedbar access."
  746. :group 'org-imenu-and-speedbar
  747. :type 'number)
  748. (defgroup org-table nil
  749. "Options concerning tables in Org-mode."
  750. :tag "Org Table"
  751. :group 'org)
  752. (defcustom org-enable-table-editor 'optimized
  753. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  754. When nil, such lines will be treated like ordinary lines.
  755. When equal to the symbol `optimized', the table editor will be optimized to
  756. do the following:
  757. - Automatic overwrite mode in front of whitespace in table fields.
  758. This makes the structure of the table stay in tact as long as the edited
  759. field does not exceed the column width.
  760. - Minimize the number of realigns. Normally, the table is aligned each time
  761. TAB or RET are pressed to move to another field. With optimization this
  762. happens only if changes to a field might have changed the column width.
  763. Optimization requires replacing the functions `self-insert-command',
  764. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  765. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  766. very good at guessing when a re-align will be necessary, but you can always
  767. force one with \\[org-ctrl-c-ctrl-c].
  768. If you would like to use the optimized version in Org-mode, but the
  769. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  770. This variable can be used to turn on and off the table editor during a session,
  771. but in order to toggle optimization, a restart is required.
  772. See also the variable `org-table-auto-blank-field'."
  773. :group 'org-table
  774. :type '(choice
  775. (const :tag "off" nil)
  776. (const :tag "on" t)
  777. (const :tag "on, optimized" optimized)))
  778. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  779. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  780. In the optimized version, the table editor takes over all simple keys that
  781. normally just insert a character. In tables, the characters are inserted
  782. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  783. for empty fields). Outside tables, the correct binding of the keys is
  784. restored.
  785. The default for this option is t if the optimized version is also used in
  786. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  787. this variable requires a restart of Emacs to become effective."
  788. :group 'org-table
  789. :type 'boolean)
  790. (defcustom orgtbl-radio-table-templates
  791. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  792. % END RECEIVE ORGTBL %n
  793. \\begin{comment}
  794. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  795. | | |
  796. \\end{comment}\n")
  797. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  798. @c END RECEIVE ORGTBL %n
  799. @ignore
  800. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  801. | | |
  802. @end ignore\n")
  803. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  804. <!-- END RECEIVE ORGTBL %n -->
  805. <!--
  806. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  807. | | |
  808. -->\n"))
  809. "Templates for radio tables in different major modes.
  810. All occurrences of %n in a template will be replaced with the name of the
  811. table, obtained by prompting the user."
  812. :group 'org-table
  813. :type '(repeat
  814. (list (symbol :tag "Major mode")
  815. (string :tag "Format"))))
  816. (defgroup org-table-settings nil
  817. "Settings for tables in Org-mode."
  818. :tag "Org Table Settings"
  819. :group 'org-table)
  820. (defcustom org-table-default-size "5x2"
  821. "The default size for newly created tables, Columns x Rows."
  822. :group 'org-table-settings
  823. :type 'string)
  824. (defcustom org-table-number-regexp
  825. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  826. "Regular expression for recognizing numbers in table columns.
  827. If a table column contains mostly numbers, it will be aligned to the
  828. right. If not, it will be aligned to the left.
  829. The default value of this option is a regular expression which allows
  830. anything which looks remotely like a number as used in scientific
  831. context. For example, all of the following will be considered a
  832. number:
  833. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  834. Other options offered by the customize interface are more restrictive."
  835. :group 'org-table-settings
  836. :type '(choice
  837. (const :tag "Positive Integers"
  838. "^[0-9]+$")
  839. (const :tag "Integers"
  840. "^[-+]?[0-9]+$")
  841. (const :tag "Floating Point Numbers"
  842. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  843. (const :tag "Floating Point Number or Integer"
  844. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  845. (const :tag "Exponential, Floating point, Integer"
  846. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  847. (const :tag "Very General Number-Like, including hex"
  848. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  849. (string :tag "Regexp:")))
  850. (defcustom org-table-number-fraction 0.5
  851. "Fraction of numbers in a column required to make the column align right.
  852. In a column all non-white fields are considered. If at least this
  853. fraction of fields is matched by `org-table-number-fraction',
  854. alignment to the right border applies."
  855. :group 'org-table-settings
  856. :type 'number)
  857. (defgroup org-table-editing nil
  858. "Behavior of tables during editing in Org-mode."
  859. :tag "Org Table Editing"
  860. :group 'org-table)
  861. (defcustom org-table-automatic-realign t
  862. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  863. When nil, aligning is only done with \\[org-table-align], or after column
  864. removal/insertion."
  865. :group 'org-table-editing
  866. :type 'boolean)
  867. (defcustom org-table-auto-blank-field t
  868. "Non-nil means, automatically blank table field when starting to type into it.
  869. This only happens when typing immediately after a field motion
  870. command (TAB, S-TAB or RET).
  871. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  872. :group 'org-table-editing
  873. :type 'boolean)
  874. (defcustom org-table-tab-jumps-over-hlines t
  875. "Non-nil means, tab in the last column of a table with jump over a hline.
  876. If a horizontal separator line is following the current line,
  877. `org-table-next-field' can either create a new row before that line, or jump
  878. over the line. When this option is nil, a new line will be created before
  879. this line."
  880. :group 'org-table-editing
  881. :type 'boolean)
  882. (defcustom org-table-tab-recognizes-table.el t
  883. "Non-nil means, TAB will automatically notice a table.el table.
  884. When it sees such a table, it moves point into it and - if necessary -
  885. calls `table-recognize-table'."
  886. :group 'org-table-editing
  887. :type 'boolean)
  888. (defgroup org-table-calculation nil
  889. "Options concerning tables in Org-mode."
  890. :tag "Org Table Calculation"
  891. :group 'org-table)
  892. (defcustom org-table-use-standard-references t
  893. "Should org-mode work with table refrences like B3 instead of @3$2?
  894. Possible values are:
  895. nil never use them
  896. from accept as input, do not present for editing
  897. t: accept as input and present for editing"
  898. :group 'org-table-calculation
  899. :type '(choice
  900. (const :tag "Never, don't even check unser input for them" nil)
  901. (const :tag "Always, both as user input, and when editing" t)
  902. (const :tag "Convert user input, don't offer during editing" 'from)))
  903. (defcustom org-table-copy-increment t
  904. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  905. :group 'org-table-calculation
  906. :type 'boolean)
  907. (defcustom org-calc-default-modes
  908. '(calc-internal-prec 12
  909. calc-float-format (float 5)
  910. calc-angle-mode deg
  911. calc-prefer-frac nil
  912. calc-symbolic-mode nil
  913. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  914. calc-display-working-message t
  915. )
  916. "List with Calc mode settings for use in calc-eval for table formulas.
  917. The list must contain alternating symbols (Calc modes variables and values).
  918. Don't remove any of the default settings, just change the values. Org-mode
  919. relies on the variables to be present in the list."
  920. :group 'org-table-calculation
  921. :type 'plist)
  922. (defcustom org-table-formula-evaluate-inline t
  923. "Non-nil means, TAB and RET evaluate a formula in current table field.
  924. If the current field starts with an equal sign, it is assumed to be a formula
  925. which should be evaluated as described in the manual and in the documentation
  926. string of the command `org-table-eval-formula'. This feature requires the
  927. Emacs calc package.
  928. When this variable is nil, formula calculation is only available through
  929. the command \\[org-table-eval-formula]."
  930. :group 'org-table-calculation
  931. :type 'boolean)
  932. (defcustom org-table-formula-use-constants t
  933. "Non-nil means, interpret constants in formulas in tables.
  934. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  935. by the value given in `org-table-formula-constants', or by a value obtained
  936. from the `constants.el' package."
  937. :group 'org-table-calculation
  938. :type 'boolean)
  939. (defcustom org-table-formula-constants nil
  940. "Alist with constant names and values, for use in table formulas.
  941. The car of each element is a name of a constant, without the `$' before it.
  942. The cdr is the value as a string. For example, if you'd like to use the
  943. speed of light in a formula, you would configure
  944. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  945. and then use it in an equation like `$1*$c'.
  946. Constants can also be defined on a per-file basis using a line like
  947. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  948. :group 'org-table-calculation
  949. :type '(repeat
  950. (cons (string :tag "name")
  951. (string :tag "value"))))
  952. (defvar org-table-formula-constants-local nil
  953. "Local version of `org-table-formula-constants'.")
  954. (make-variable-buffer-local 'org-table-formula-constants-local)
  955. (defcustom org-table-allow-automatic-line-recalculation t
  956. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  957. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  958. :group 'org-table-calculation
  959. :type 'boolean)
  960. (defgroup org-link nil
  961. "Options concerning links in Org-mode."
  962. :tag "Org Link"
  963. :group 'org)
  964. (defvar org-link-abbrev-alist-local nil
  965. "Buffer-local version of `org-link-abbrev-alist', which see.
  966. The value of this is taken from the #+LINK lines.")
  967. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  968. (defcustom org-link-abbrev-alist nil
  969. "Alist of link abbreviations.
  970. The car of each element is a string, to be replaced at the start of a link.
  971. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  972. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  973. [[linkkey:tag][description]]
  974. If REPLACE is a string, the tag will simply be appended to create the link.
  975. If the string contains \"%s\", the tag will be inserted there.
  976. REPLACE may also be a function that will be called with the tag as the
  977. only argument to create the link, which should be returned as a string.
  978. See the manual for examples."
  979. :group 'org-link
  980. :type 'alist)
  981. (defcustom org-descriptive-links t
  982. "Non-nil means, hide link part and only show description of bracket links.
  983. Bracket links are like [[link][descritpion]]. This variable sets the initial
  984. state in new org-mode buffers. The setting can then be toggled on a
  985. per-buffer basis from the Org->Hyperlinks menu."
  986. :group 'org-link
  987. :type 'boolean)
  988. (defcustom org-link-file-path-type 'adaptive
  989. "How the path name in file links should be stored.
  990. Valid values are:
  991. relative Relative to the current directory, i.e. the directory of the file
  992. into which the link is being inserted.
  993. absolute Absolute path, if possible with ~ for home directory.
  994. noabbrev Absolute path, no abbreviation of home directory.
  995. adaptive Use relative path for files in the current directory and sub-
  996. directories of it. For other files, use an absolute path."
  997. :group 'org-link
  998. :type '(choice
  999. (const relative)
  1000. (const absolute)
  1001. (const noabbrev)
  1002. (const adaptive)))
  1003. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1004. "Types of links that should be activated in Org-mode files.
  1005. This is a list of symbols, each leading to the activation of a certain link
  1006. type. In principle, it does not hurt to turn on most link types - there may
  1007. be a small gain when turning off unused link types. The types are:
  1008. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1009. angular Links in angular brackes that may contain whitespace like
  1010. <bbdb:Carsten Dominik>.
  1011. plain Plain links in normal text, no whitespace, like http://google.com.
  1012. radio Text that is matched by a radio target, see manual for details.
  1013. tag Tag settings in a headline (link to tag search).
  1014. date Time stamps (link to calendar).
  1015. Changing this variable requires a restart of Emacs to become effective."
  1016. :group 'org-link
  1017. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1018. (const :tag "Angular bracket links (old style)" angular)
  1019. (const :tag "Plain text links" plain)
  1020. (const :tag "Radio target matches" radio)
  1021. (const :tag "Tags" tag)
  1022. (const :tag "Timestamps" date)))
  1023. (defgroup org-link-store nil
  1024. "Options concerning storing links in Org-mode"
  1025. :tag "Org Store Link"
  1026. :group 'org-link)
  1027. (defcustom org-email-link-description-format "Email %c: %.30s"
  1028. "Format of the description part of a link to an email or usenet message.
  1029. The following %-excapes will be replaced by corresponding information:
  1030. %F full \"From\" field
  1031. %f name, taken from \"From\" field, address if no name
  1032. %T full \"To\" field
  1033. %t first name in \"To\" field, address if no name
  1034. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1035. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1036. %s subject
  1037. %m message-id.
  1038. You may use normal field width specification between the % and the letter.
  1039. This is for example useful to limit the length of the subject.
  1040. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1041. :group 'org-link-store
  1042. :type 'string)
  1043. (defcustom org-from-is-user-regexp
  1044. (let (r1 r2)
  1045. (when (and user-mail-address (not (string= user-mail-address "")))
  1046. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1047. (when (and user-full-name (not (string= user-full-name "")))
  1048. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1049. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1050. "Regexp mached against the \"From:\" header of an email or usenet message.
  1051. It should match if the message is from the user him/herself."
  1052. :group 'org-link-store
  1053. :type 'regexp)
  1054. (defcustom org-context-in-file-links t
  1055. "Non-nil means, file links from `org-store-link' contain context.
  1056. A search string will be added to the file name with :: as separator and
  1057. used to find the context when the link is activated by the command
  1058. `org-open-at-point'.
  1059. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1060. negates this setting for the duration of the command."
  1061. :group 'org-link-store
  1062. :type 'boolean)
  1063. (defcustom org-keep-stored-link-after-insertion nil
  1064. "Non-nil means, keep link in list for entire session.
  1065. The command `org-store-link' adds a link pointing to the current
  1066. location to an internal list. These links accumulate during a session.
  1067. The command `org-insert-link' can be used to insert links into any
  1068. Org-mode file (offering completion for all stored links). When this
  1069. option is nil, every link which has been inserted once using \\[org-insert-link]
  1070. will be removed from the list, to make completing the unused links
  1071. more efficient."
  1072. :group 'org-link-store
  1073. :type 'boolean)
  1074. (defcustom org-usenet-links-prefer-google nil
  1075. "Non-nil means, `org-store-link' will create web links to Google groups.
  1076. When nil, Gnus will be used for such links.
  1077. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1078. negates this setting for the duration of the command."
  1079. :group 'org-link-store
  1080. :type 'boolean)
  1081. (defgroup org-link-follow nil
  1082. "Options concerning following links in Org-mode"
  1083. :tag "Org Follow Link"
  1084. :group 'org-link)
  1085. (defcustom org-tab-follows-link nil
  1086. "Non-nil means, on links TAB will follow the link.
  1087. Needs to be set before org.el is loaded."
  1088. :group 'org-link-follow
  1089. :type 'boolean)
  1090. (defcustom org-return-follows-link nil
  1091. "Non-nil means, on links RET will follow the link.
  1092. Needs to be set before org.el is loaded."
  1093. :group 'org-link-follow
  1094. :type 'boolean)
  1095. (defcustom org-mouse-1-follows-link
  1096. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1097. "Non-nil means, mouse-1 on a link will follow the link.
  1098. A longer mouse click will still set point. Does not work on XEmacs.
  1099. Needs to be set before org.el is loaded."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-mark-ring-length 4
  1103. "Number of different positions to be recorded in the ring
  1104. Changing this requires a restart of Emacs to work correctly."
  1105. :group 'org-link-follow
  1106. :type 'interger)
  1107. (defcustom org-link-frame-setup
  1108. '((vm . vm-visit-folder-other-frame)
  1109. (gnus . gnus-other-frame)
  1110. (file . find-file-other-window))
  1111. "Setup the frame configuration for following links.
  1112. When following a link with Emacs, it may often be useful to display
  1113. this link in another window or frame. This variable can be used to
  1114. set this up for the different types of links.
  1115. For VM, use any of
  1116. `vm-visit-folder'
  1117. `vm-visit-folder-other-frame'
  1118. For Gnus, use any of
  1119. `gnus'
  1120. `gnus-other-frame'
  1121. For FILE, use any of
  1122. `find-file'
  1123. `find-file-other-window'
  1124. `find-file-other-frame'
  1125. For the calendar, use the variable `calendar-setup'.
  1126. For BBDB, it is currently only possible to display the matches in
  1127. another window."
  1128. :group 'org-link-follow
  1129. :type '(list
  1130. (cons (const vm)
  1131. (choice
  1132. (const vm-visit-folder)
  1133. (const vm-visit-folder-other-window)
  1134. (const vm-visit-folder-other-frame)))
  1135. (cons (const gnus)
  1136. (choice
  1137. (const gnus)
  1138. (const gnus-other-frame)))
  1139. (cons (const file)
  1140. (choice
  1141. (const find-file)
  1142. (const find-file-other-window)
  1143. (const find-file-other-frame)))))
  1144. (defcustom org-display-internal-link-with-indirect-buffer nil
  1145. "Non-nil means, use indirect buffer to display infile links.
  1146. Activating internal links (from one location in a file to another location
  1147. in the same file) normally just jumps to the location. When the link is
  1148. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1149. another window. When this option is set, the other window actually displays
  1150. an indirect buffer clone of the current buffer, to avoid any visibility
  1151. changes to the current buffer."
  1152. :group 'org-link-follow
  1153. :type 'boolean)
  1154. (defcustom org-open-non-existing-files nil
  1155. "Non-nil means, `org-open-file' will open non-existing files.
  1156. When nil, an error will be generated."
  1157. :group 'org-link-follow
  1158. :type 'boolean)
  1159. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1160. "Function and arguments to call for following mailto links.
  1161. This is a list with the first element being a lisp function, and the
  1162. remaining elements being arguments to the function. In string arguments,
  1163. %a will be replaced by the address, and %s will be replaced by the subject
  1164. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1165. :group 'org-link-follow
  1166. :type '(choice
  1167. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1168. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1169. (const :tag "message-mail" (message-mail "%a" "%s"))
  1170. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1171. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1172. "Non-nil means, ask for confirmation before executing shell links.
  1173. Shell links can be dangerous: just think about a link
  1174. [[shell:rm -rf ~/*][Google Search]]
  1175. This link would show up in your Org-mode document as \"Google Search\",
  1176. but really it would remove your entire home directory.
  1177. Therefore we advise against setting this variable to nil.
  1178. Just change it to `y-or-n-p' of you want to confirm with a
  1179. single keystroke rather than having to type \"yes\"."
  1180. :group 'org-link-follow
  1181. :type '(choice
  1182. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1183. (const :tag "with y-or-n (faster)" y-or-n-p)
  1184. (const :tag "no confirmation (dangerous)" nil)))
  1185. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1186. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1187. Elisp links can be dangerous: just think about a link
  1188. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1189. This link would show up in your Org-mode document as \"Google Search\",
  1190. but really it would remove your entire home directory.
  1191. Therefore we advise against setting this variable to nil.
  1192. Just change it to `y-or-n-p' of you want to confirm with a
  1193. single keystroke rather than having to type \"yes\"."
  1194. :group 'org-link-follow
  1195. :type '(choice
  1196. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1197. (const :tag "with y-or-n (faster)" y-or-n-p)
  1198. (const :tag "no confirmation (dangerous)" nil)))
  1199. (defconst org-file-apps-defaults-gnu
  1200. '((remote . emacs)
  1201. (t . mailcap))
  1202. "Default file applications on a UNIX or GNU/Linux system.
  1203. See `org-file-apps'.")
  1204. (defconst org-file-apps-defaults-macosx
  1205. '((remote . emacs)
  1206. (t . "open %s")
  1207. ("ps" . "gv %s")
  1208. ("ps.gz" . "gv %s")
  1209. ("eps" . "gv %s")
  1210. ("eps.gz" . "gv %s")
  1211. ("dvi" . "xdvi %s")
  1212. ("fig" . "xfig %s"))
  1213. "Default file applications on a MacOS X system.
  1214. The system \"open\" is known as a default, but we use X11 applications
  1215. for some files for which the OS does not have a good default.
  1216. See `org-file-apps'.")
  1217. (defconst org-file-apps-defaults-windowsnt
  1218. (list
  1219. '(remote . emacs)
  1220. (cons t
  1221. (list (if (featurep 'xemacs)
  1222. 'mswindows-shell-execute
  1223. 'w32-shell-execute)
  1224. "open" 'file)))
  1225. "Default file applications on a Windows NT system.
  1226. The system \"open\" is used for most files.
  1227. See `org-file-apps'.")
  1228. (defcustom org-file-apps
  1229. '(
  1230. ("txt" . emacs)
  1231. ("tex" . emacs)
  1232. ("ltx" . emacs)
  1233. ("org" . emacs)
  1234. ("el" . emacs)
  1235. ("bib" . emacs)
  1236. )
  1237. "External applications for opening `file:path' items in a document.
  1238. Org-mode uses system defaults for different file types, but
  1239. you can use this variable to set the application for a given file
  1240. extension. The entries in this list are cons cells where the car identifies
  1241. files and the cdr the corresponding command. Possible values for the
  1242. file identifier are
  1243. \"ext\" A string identifying an extension
  1244. `directory' Matches a directory
  1245. `remote' Matches a remote file, accessible through tramp or efs.
  1246. Remote files most likely should be visited through Emacs
  1247. because external applications cannot handle such paths.
  1248. t Default for all remaining files
  1249. Possible values for the command are:
  1250. `emacs' The file will be visited by the current Emacs process.
  1251. `default' Use the default application for this file type.
  1252. string A command to be executed by a shell; %s will be replaced
  1253. by the path to the file.
  1254. sexp A Lisp form which will be evaluated. The file path will
  1255. be available in the Lisp variable `file'.
  1256. For more examples, see the system specific constants
  1257. `org-file-apps-defaults-macosx'
  1258. `org-file-apps-defaults-windowsnt'
  1259. `org-file-apps-defaults-gnu'."
  1260. :group 'org-link-follow
  1261. :type '(repeat
  1262. (cons (choice :value ""
  1263. (string :tag "Extension")
  1264. (const :tag "Default for unrecognized files" t)
  1265. (const :tag "Remote file" remote)
  1266. (const :tag "Links to a directory" directory))
  1267. (choice :value ""
  1268. (const :tag "Visit with Emacs" emacs)
  1269. (const :tag "Use system default" default)
  1270. (string :tag "Command")
  1271. (sexp :tag "Lisp form")))))
  1272. (defcustom org-mhe-search-all-folders nil
  1273. "Non-nil means, that the search for the mh-message will be extended to
  1274. all folders if the message cannot be found in the folder given in the link.
  1275. Searching all folders is very efficient with one of the search engines
  1276. supported by MH-E, but will be slow with pick."
  1277. :group 'org-link-follow
  1278. :type 'boolean)
  1279. (defgroup org-remember nil
  1280. "Options concerning interaction with remember.el."
  1281. :tag "Org Remember"
  1282. :group 'org)
  1283. (defcustom org-directory "~/org"
  1284. "Directory with org files.
  1285. This directory will be used as default to prompt for org files.
  1286. Used by the hooks for remember.el."
  1287. :group 'org-remember
  1288. :type 'directory)
  1289. (defcustom org-default-notes-file "~/.notes"
  1290. "Default target for storing notes.
  1291. Used by the hooks for remember.el. This can be a string, or nil to mean
  1292. the value of `remember-data-file'.
  1293. You can set this on a per-template basis with the variable
  1294. `org-remember-templates'."
  1295. :group 'org-remember
  1296. :type '(choice
  1297. (const :tag "Default from remember-data-file" nil)
  1298. file))
  1299. (defcustom org-remember-store-without-prompt t
  1300. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1301. In this case, you need `C-u C-c C-c' to get the prompts for
  1302. note file and headline.
  1303. When this variable is nil, `C-c C-c' give you the prompts, and
  1304. `C-u C-c C-c' trigger the fasttrack."
  1305. :group 'org-remember
  1306. :type 'boolean)
  1307. (defcustom org-remember-interactive-interface 'refile
  1308. "The interface to be used for interactive filing of remember notes.
  1309. This is only used when the interactive mode for selecting a filing
  1310. location is used (see the variable `org-remember-store-without-prompt').
  1311. Allowed vaues are:
  1312. outline The interface shows an outline of the relevant file
  1313. and the correct heading is found by moving through
  1314. the outline or by searching with incremental search.
  1315. outline-path-completion Headlines in the current buffer are offered via
  1316. completion.
  1317. refile Use the refile interface, and offer headlines,
  1318. possibly from different buffers."
  1319. :group 'org-remember
  1320. :type '(choice
  1321. (const :tag "Refile" refile)
  1322. (const :tag "Outline" outline)
  1323. (const :tag "Outline-path-completion" outline-path-completion)))
  1324. (defcustom org-goto-interface 'outline
  1325. "The default interface to be used for `org-goto'.
  1326. Allowed vaues are:
  1327. outline The interface shows an outline of the relevant file
  1328. and the correct heading is found by moving through
  1329. the outline or by searching with incremental search.
  1330. outline-path-completion Headlines in the current buffer are offered via
  1331. completion."
  1332. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1333. :type '(choice
  1334. (const :tag "Outline" outline)
  1335. (const :tag "Outline-path-completion" outline-path-completion)))
  1336. (defcustom org-remember-default-headline ""
  1337. "The headline that should be the default location in the notes file.
  1338. When filing remember notes, the cursor will start at that position.
  1339. You can set this on a per-template basis with the variable
  1340. `org-remember-templates'."
  1341. :group 'org-remember
  1342. :type 'string)
  1343. (defcustom org-remember-templates nil
  1344. "Templates for the creation of remember buffers.
  1345. When nil, just let remember make the buffer.
  1346. When not nil, this is a list of 5-element lists. In each entry, the first
  1347. element is the name of the template, which should be a single short word.
  1348. The second element is a character, a unique key to select this template.
  1349. The third element is the template. The fourth element is optional and can
  1350. specify a destination file for remember items created with this template.
  1351. The default file is given by `org-default-notes-file'. An optional fifth
  1352. element can specify the headline in that file that should be offered
  1353. first when the user is asked to file the entry. The default headline is
  1354. given in the variable `org-remember-default-headline'.
  1355. The template specifies the structure of the remember buffer. It should have
  1356. a first line starting with a star, to act as the org-mode headline.
  1357. Furthermore, the following %-escapes will be replaced with content:
  1358. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1359. A default value and a completion table ca be specified like this:
  1360. %^{prompt|default|completion2|completion3|...}
  1361. %t time stamp, date only
  1362. %T time stamp with date and time
  1363. %u, %U like the above, but inactive time stamps
  1364. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1365. You may define a prompt like %^{Please specify birthday}t
  1366. %n user name (taken from `user-full-name')
  1367. %a annotation, normally the link created with org-store-link
  1368. %i initial content, the region active. If %i is indented,
  1369. the entire inserted text will be indented as well.
  1370. %c content of the clipboard, or current kill ring head
  1371. %^g prompt for tags, with completion on tags in target file
  1372. %^G prompt for tags, with completion all tags in all agenda files
  1373. %:keyword specific information for certain link types, see below
  1374. %[pathname] insert the contents of the file given by `pathname'
  1375. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1376. %! Store this note immediately after filling the template
  1377. %? After completing the template, position cursor here.
  1378. Apart from these general escapes, you can access information specific to the
  1379. link type that is created. For example, calling `remember' in emails or gnus
  1380. will record the author and the subject of the message, which you can access
  1381. with %:author and %:subject, respectively. Here is a complete list of what
  1382. is recorded for each link type.
  1383. Link type | Available information
  1384. -------------------+------------------------------------------------------
  1385. bbdb | %:type %:name %:company
  1386. vm, wl, mh, rmail | %:type %:subject %:message-id
  1387. | %:from %:fromname %:fromaddress
  1388. | %:to %:toname %:toaddress
  1389. | %:fromto (either \"to NAME\" or \"from NAME\")
  1390. gnus | %:group, for messages also all email fields
  1391. w3, w3m | %:type %:url
  1392. info | %:type %:file %:node
  1393. calendar | %:type %:date"
  1394. :group 'org-remember
  1395. :get (lambda (var) ; Make sure all entries have 5 elements
  1396. (mapcar (lambda (x)
  1397. (if (not (stringp (car x))) (setq x (cons "" x)))
  1398. (cond ((= (length x) 4) (append x '("")))
  1399. ((= (length x) 3) (append x '("" "")))
  1400. (t x)))
  1401. (default-value var)))
  1402. :type '(repeat
  1403. :tag "enabled"
  1404. (list :value ("" ?a "\n" nil nil)
  1405. (string :tag "Name")
  1406. (character :tag "Selection Key")
  1407. (string :tag "Template")
  1408. (choice
  1409. (file :tag "Destination file")
  1410. (const :tag "Prompt for file" nil))
  1411. (choice
  1412. (string :tag "Destination headline")
  1413. (const :tag "Selection interface for heading")))))
  1414. (defcustom org-reverse-note-order nil
  1415. "Non-nil means, store new notes at the beginning of a file or entry.
  1416. When nil, new notes will be filed to the end of a file or entry.
  1417. This can also be a list with cons cells of regular expressions that
  1418. are matched against file names, and values."
  1419. :group 'org-remember
  1420. :type '(choice
  1421. (const :tag "Reverse always" t)
  1422. (const :tag "Reverse never" nil)
  1423. (repeat :tag "By file name regexp"
  1424. (cons regexp boolean))))
  1425. (defcustom org-refile-targets nil
  1426. "Targets for refiling entries with \\[org-refile].
  1427. This is list of cons cells. Each cell contains:
  1428. - a specification of the files to be considered, either a list of files,
  1429. or a symbol whose function or value fields will be used to retrieve
  1430. a file name or a list of file names. Nil means, refile to a different
  1431. heading in the current buffer.
  1432. - A specification of how to find candidate refile targets. This may be
  1433. any of
  1434. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1435. This tag has to be present in all target headlines, inheritance will
  1436. not be considered.
  1437. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1438. todo keyword.
  1439. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1440. headlines that are refiling targets.
  1441. - a cons cell (:level . N). Any headline of level N is considered a target.
  1442. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1443. ;; FIXME: what if there are a var and func with same name???
  1444. :group 'org-remember
  1445. :type '(repeat
  1446. (cons
  1447. (choice :value org-agenda-files
  1448. (const :tag "All agenda files" org-agenda-files)
  1449. (const :tag "Current buffer" nil)
  1450. (function) (variable) (file))
  1451. (choice :tag "Identify target headline by"
  1452. (cons :tag "Specific tag" (const :tag) (string))
  1453. (cons :tag "TODO keyword" (const :todo) (string))
  1454. (cons :tag "Regular expression" (const :regexp) (regexp))
  1455. (cons :tag "Level number" (const :level) (integer))
  1456. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1457. (defcustom org-refile-use-outline-path nil
  1458. "Non-nil means, provide refile targets as paths.
  1459. So a level 3 headline will be available as level1/level2/level3.
  1460. When the value is `file', also include the file name (without directory)
  1461. into the path. When `full-file-path', include the full file path."
  1462. :group 'org-remember
  1463. :type '(choice
  1464. (const :tag "Not" nil)
  1465. (const :tag "Yes" t)
  1466. (const :tag "Start with file name" file)
  1467. (const :tag "Start with full file path" full-file-path)))
  1468. (defgroup org-todo nil
  1469. "Options concerning TODO items in Org-mode."
  1470. :tag "Org TODO"
  1471. :group 'org)
  1472. (defgroup org-progress nil
  1473. "Options concerning Progress logging in Org-mode."
  1474. :tag "Org Progress"
  1475. :group 'org-time)
  1476. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1477. "List of TODO entry keyword sequences and their interpretation.
  1478. \\<org-mode-map>This is a list of sequences.
  1479. Each sequence starts with a symbol, either `sequence' or `type',
  1480. indicating if the keywords should be interpreted as a sequence of
  1481. action steps, or as different types of TODO items. The first
  1482. keywords are states requiring action - these states will select a headline
  1483. for inclusion into the global TODO list Org-mode produces. If one of
  1484. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1485. signify that no further action is necessary. If \"|\" is not found,
  1486. the last keyword is treated as the only DONE state of the sequence.
  1487. The command \\[org-todo] cycles an entry through these states, and one
  1488. additional state where no keyword is present. For details about this
  1489. cycling, see the manual.
  1490. TODO keywords and interpretation can also be set on a per-file basis with
  1491. the special #+SEQ_TODO and #+TYP_TODO lines.
  1492. Each keyword can optionally specify a character for fast state selection
  1493. \(in combination with the variable `org-use-fast-todo-selection')
  1494. and specifiers for state change logging, using the same syntax
  1495. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1496. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1497. indicates to record a time stamp each time this state is selected.
  1498. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1499. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1500. entering and when leaving this state.
  1501. For backward compatibility, this variable may also be just a list
  1502. of keywords - in this case the interptetation (sequence or type) will be
  1503. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1504. :group 'org-todo
  1505. :group 'org-keywords
  1506. :type '(choice
  1507. (repeat :tag "Old syntax, just keywords"
  1508. (string :tag "Keyword"))
  1509. (repeat :tag "New syntax"
  1510. (cons
  1511. (choice
  1512. :tag "Interpretation"
  1513. (const :tag "Sequence (cycling hits every state)" sequence)
  1514. (const :tag "Type (cycling directly to DONE)" type))
  1515. (repeat
  1516. (string :tag "Keyword"))))))
  1517. (defvar org-todo-keywords-1 nil
  1518. "All TODO and DONE keywords active in a buffer.")
  1519. (make-variable-buffer-local 'org-todo-keywords-1)
  1520. (defvar org-todo-keywords-for-agenda nil)
  1521. (defvar org-done-keywords-for-agenda nil)
  1522. (defvar org-not-done-keywords nil)
  1523. (make-variable-buffer-local 'org-not-done-keywords)
  1524. (defvar org-done-keywords nil)
  1525. (make-variable-buffer-local 'org-done-keywords)
  1526. (defvar org-todo-heads nil)
  1527. (make-variable-buffer-local 'org-todo-heads)
  1528. (defvar org-todo-sets nil)
  1529. (make-variable-buffer-local 'org-todo-sets)
  1530. (defvar org-todo-log-states nil)
  1531. (make-variable-buffer-local 'org-todo-log-states)
  1532. (defvar org-todo-kwd-alist nil)
  1533. (make-variable-buffer-local 'org-todo-kwd-alist)
  1534. (defvar org-todo-key-alist nil)
  1535. (make-variable-buffer-local 'org-todo-key-alist)
  1536. (defvar org-todo-key-trigger nil)
  1537. (make-variable-buffer-local 'org-todo-key-trigger)
  1538. (defcustom org-todo-interpretation 'sequence
  1539. "Controls how TODO keywords are interpreted.
  1540. This variable is in principle obsolete and is only used for
  1541. backward compatibility, if the interpretation of todo keywords is
  1542. not given already in `org-todo-keywords'. See that variable for
  1543. more information."
  1544. :group 'org-todo
  1545. :group 'org-keywords
  1546. :type '(choice (const sequence)
  1547. (const type)))
  1548. (defcustom org-use-fast-todo-selection 'prefix
  1549. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1550. This variable describes if and under what circumstances the cycling
  1551. mechanism for TODO keywords will be replaced by a single-key, direct
  1552. selection scheme.
  1553. When nil, fast selection is never used.
  1554. When the symbol `prefix', it will be used when `org-todo' is called with
  1555. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1556. in an agenda buffer.
  1557. When t, fast selection is used by default. In this case, the prefix
  1558. argument forces cycling instead.
  1559. In all cases, the special interface is only used if access keys have actually
  1560. been assigned by the user, i.e. if keywords in the configuration are followed
  1561. by a letter in parenthesis, like TODO(t)."
  1562. :group 'org-todo
  1563. :type '(choice
  1564. (const :tag "Never" nil)
  1565. (const :tag "By default" t)
  1566. (const :tag "Only with C-u C-c C-t" prefix)))
  1567. (defcustom org-after-todo-state-change-hook nil
  1568. "Hook which is run after the state of a TODO item was changed.
  1569. The new state (a string with a TODO keyword, or nil) is available in the
  1570. Lisp variable `state'."
  1571. :group 'org-todo
  1572. :type 'hook)
  1573. (defcustom org-log-done nil
  1574. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1575. When equal to the list (done), also prompt for a closing note.
  1576. This can also be configured on a per-file basis by adding one of
  1577. the following lines anywhere in the buffer:
  1578. #+STARTUP: logdone
  1579. #+STARTUP: lognotedone
  1580. #+STARTUP: nologdone"
  1581. :group 'org-todo
  1582. :group 'org-progress
  1583. :type '(choice
  1584. (const :tag "No logging" nil)
  1585. (const :tag "Record CLOSED timestamp" time)
  1586. (const :tag "Record CLOSED timestamp with closing note." note)))
  1587. ;; Normalize old uses of org-log-done.
  1588. (cond
  1589. ((eq org-log-done t) (setq org-log-done 'time))
  1590. ((and (listp org-log-done) (memq 'done org-log-done))
  1591. (setq org-log-done 'note)))
  1592. ;; FIXME: document
  1593. (defcustom org-log-note-clock-out nil
  1594. "Non-nil means, recored a note when clocking out of an item.
  1595. This can also be configured on a per-file basis by adding one of
  1596. the following lines anywhere in the buffer:
  1597. #+STARTUP: lognoteclock-out
  1598. #+STARTUP: nolognoteclock-out"
  1599. :group 'org-todo
  1600. :group 'org-progress
  1601. :type 'boolean)
  1602. (defcustom org-log-done-with-time t
  1603. "Non-nil means, the CLOSED time stamp will contain date and time.
  1604. When nil, only the date will be recorded."
  1605. :group 'org-progress
  1606. :type 'boolean)
  1607. (defcustom org-log-note-headings
  1608. '((done . "CLOSING NOTE %t")
  1609. (state . "State %-12s %t")
  1610. (clock-out . ""))
  1611. "Headings for notes added when clocking out or closing TODO items.
  1612. The value is an alist, with the car being a symbol indicating the note
  1613. context, and the cdr is the heading to be used. The heading may also be the
  1614. empty string.
  1615. %t in the heading will be replaced by a time stamp.
  1616. %s will be replaced by the new TODO state, in double quotes.
  1617. %u will be replaced by the user name.
  1618. %U will be replaced by the full user name."
  1619. :group 'org-todo
  1620. :group 'org-progress
  1621. :type '(list :greedy t
  1622. (cons (const :tag "Heading when closing an item" done) string)
  1623. (cons (const :tag
  1624. "Heading when changing todo state (todo sequence only)"
  1625. state) string)
  1626. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1627. (defcustom org-log-states-order-reversed t
  1628. "Non-nil means, the latest state change note will be directly after heading.
  1629. When nil, the notes will be orderer according to time."
  1630. :group 'org-todo
  1631. :group 'org-progress
  1632. :type 'boolean)
  1633. (defcustom org-log-repeat 'time
  1634. "Non-nil means, record moving through the DONE state when triggering repeat.
  1635. An auto-repeating tasks is immediately switched back to TODO when marked
  1636. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1637. the TODO keyword definition, or recording a cloing note by setting
  1638. `org-log-done', there will be no record of the task moving trhough DONE.
  1639. This variable forces taking a note anyway. Possible values are:
  1640. nil Don't force a record
  1641. time Record a time stamp
  1642. note Record a note
  1643. This option can also be set with on a per-file-basis with
  1644. #+STARTUP: logrepeat
  1645. #+STARTUP: lognoterepeat
  1646. #+STARTUP: nologrepeat
  1647. You can have local logging settings for a subtree by setting the LOGGING
  1648. property to one or more of these keywords."
  1649. :group 'org-todo
  1650. :group 'org-progress
  1651. :type '(choice
  1652. (const :tag "Don't force a record" nil)
  1653. (const :tag "Force recording the DONE state" time)
  1654. (const :tag "Force recording a note with the DONE state" note)))
  1655. (defcustom org-clock-into-drawer 2
  1656. "Should clocking info be wrapped into a drawer?
  1657. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1658. If necessary, the drawer will be created.
  1659. When nil, the drawer will not be created, but used when present.
  1660. When an integer and the number of clocking entries in an item
  1661. reaches or exceeds this number, a drawer will be created."
  1662. :group 'org-todo
  1663. :group 'org-progress
  1664. :type '(choice
  1665. (const :tag "Always" t)
  1666. (const :tag "Only when drawer exists" nil)
  1667. (integer :tag "When at least N clock entries")))
  1668. (defcustom org-clock-out-when-done t
  1669. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1670. Nil means, clock will keep running until stopped explicitly with
  1671. `C-c C-x C-o', or until the clock is started in a different item."
  1672. :group 'org-progress
  1673. :type 'boolean)
  1674. (defcustom org-clock-in-switch-to-state nil
  1675. "Set task to a special todo state while clocking it.
  1676. The value should be the state to which the entry should be switched."
  1677. :group 'org-progress
  1678. :group 'org-todo
  1679. :type '(choice
  1680. (const :tag "Don't force a state" nil)
  1681. (string :tag "State")))
  1682. (defgroup org-priorities nil
  1683. "Priorities in Org-mode."
  1684. :tag "Org Priorities"
  1685. :group 'org-todo)
  1686. (defcustom org-highest-priority ?A
  1687. "The highest priority of TODO items. A character like ?A, ?B etc.
  1688. Must have a smaller ASCII number than `org-lowest-priority'."
  1689. :group 'org-priorities
  1690. :type 'character)
  1691. (defcustom org-lowest-priority ?C
  1692. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1693. Must have a larger ASCII number than `org-highest-priority'."
  1694. :group 'org-priorities
  1695. :type 'character)
  1696. (defcustom org-default-priority ?B
  1697. "The default priority of TODO items.
  1698. This is the priority an item get if no explicit priority is given."
  1699. :group 'org-priorities
  1700. :type 'character)
  1701. (defcustom org-priority-start-cycle-with-default t
  1702. "Non-nil means, start with default priority when starting to cycle.
  1703. When this is nil, the first step in the cycle will be (depending on the
  1704. command used) one higher or lower that the default priority."
  1705. :group 'org-priorities
  1706. :type 'boolean)
  1707. (defgroup org-time nil
  1708. "Options concerning time stamps and deadlines in Org-mode."
  1709. :tag "Org Time"
  1710. :group 'org)
  1711. (defcustom org-insert-labeled-timestamps-at-point nil
  1712. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1713. When nil, these labeled time stamps are forces into the second line of an
  1714. entry, just after the headline. When scheduling from the global TODO list,
  1715. the time stamp will always be forced into the second line."
  1716. :group 'org-time
  1717. :type 'boolean)
  1718. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1719. "Formats for `format-time-string' which are used for time stamps.
  1720. It is not recommended to change this constant.")
  1721. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1722. "Number of minutes to round time stamps to.
  1723. These are two values, the first applies when first creating a time stamp.
  1724. The second applies when changing it with the commands `S-up' and `S-down'.
  1725. When changing the time stamp, this means that it will change in steps
  1726. of N minues, as given by the second value.
  1727. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1728. numbers should be factors of 60, so for example 5, 10, 15.
  1729. When this is larger than 1, you can still force an exact time-stamp by using
  1730. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1731. and by using a prefix arg to `S-up/down' to specify the exact number
  1732. of minutes to shift."
  1733. :group 'org-time
  1734. :get '(lambda (var) ; Make sure all entries have 5 elements
  1735. (if (integerp (default-value var))
  1736. (list (default-value var) 5)
  1737. (default-value var)))
  1738. :type '(list
  1739. (integer :tag "when inserting times")
  1740. (integer :tag "when modifying times")))
  1741. (defcustom org-display-custom-times nil
  1742. "Non-nil means, overlay custom formats over all time stamps.
  1743. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1744. To turn this on on a per-file basis, insert anywhere in the file:
  1745. #+STARTUP: customtime"
  1746. :group 'org-time
  1747. :set 'set-default
  1748. :type 'sexp)
  1749. (make-variable-buffer-local 'org-display-custom-times)
  1750. (defcustom org-time-stamp-custom-formats
  1751. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1752. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1753. These are overlayed over the default ISO format if the variable
  1754. `org-display-custom-times' is set. Time like %H:%M should be at the
  1755. end of the second format."
  1756. :group 'org-time
  1757. :type 'sexp)
  1758. (defun org-time-stamp-format (&optional long inactive)
  1759. "Get the right format for a time string."
  1760. (let ((f (if long (cdr org-time-stamp-formats)
  1761. (car org-time-stamp-formats))))
  1762. (if inactive
  1763. (concat "[" (substring f 1 -1) "]")
  1764. f)))
  1765. (defcustom org-read-date-prefer-future t
  1766. "Non-nil means, assume future for incomplete date input from user.
  1767. This affects the following situations:
  1768. 1. The user gives a day, but no month.
  1769. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1770. read this as the third of *next* month. However, if you enter \"17\",
  1771. it will be considered as *this* month.
  1772. 2. The user gives a month but not a year.
  1773. For example, if it is april and you enter \"feb 2\", this will be read
  1774. as feb 2, *next* year. \"May 5\", however, will be this year.
  1775. When this option is nil, the current month and year will always be used
  1776. as defaults."
  1777. :group 'org-time
  1778. :type 'boolean)
  1779. (defcustom org-read-date-display-live t
  1780. "Non-nil means, display current interpretation of date prompt live.
  1781. This display will be in an overlay, in the minibuffer."
  1782. :group 'org-time
  1783. :type 'boolean)
  1784. (defcustom org-read-date-popup-calendar t
  1785. "Non-nil means, pop up a calendar when prompting for a date.
  1786. In the calendar, the date can be selected with mouse-1. However, the
  1787. minibuffer will also be active, and you can simply enter the date as well.
  1788. When nil, only the minibuffer will be available."
  1789. :group 'org-time
  1790. :type 'boolean)
  1791. (if (fboundp 'defvaralias)
  1792. (defvaralias 'org-popup-calendar-for-date-prompt
  1793. 'org-read-date-popup-calendar))
  1794. (defcustom org-extend-today-until 0
  1795. "The hour when your day really ends.
  1796. This has influence for the following applications:
  1797. - When switching the agenda to \"today\". It it is still earlier than
  1798. the time given here, the day recognized as TODAY is actually yesterday.
  1799. - When a date is read from the user and it is still before the time given
  1800. here, the current date and time will be assumed to be yesterday, 23:59.
  1801. FIXME:
  1802. IMPORTANT: This is still a very experimental feature, it may disappear
  1803. again or it may be extended to mean more things."
  1804. :group 'org-time
  1805. :type 'number)
  1806. (defcustom org-edit-timestamp-down-means-later nil
  1807. "Non-nil means, S-down will increase the time in a time stamp.
  1808. When nil, S-up will increase."
  1809. :group 'org-time
  1810. :type 'boolean)
  1811. (defcustom org-calendar-follow-timestamp-change t
  1812. "Non-nil means, make the calendar window follow timestamp changes.
  1813. When a timestamp is modified and the calendar window is visible, it will be
  1814. moved to the new date."
  1815. :group 'org-time
  1816. :type 'boolean)
  1817. (defcustom org-clock-heading-function nil
  1818. "When non-nil, should be a function to create `org-clock-heading'.
  1819. This is the string shown in the mode line when a clock is running.
  1820. The function is called with point at the beginning of the headline."
  1821. :group 'org-time ; FIXME: Should we have a separate group????
  1822. :type 'function)
  1823. (defgroup org-tags nil
  1824. "Options concerning tags in Org-mode."
  1825. :tag "Org Tags"
  1826. :group 'org)
  1827. (defcustom org-tag-alist nil
  1828. "List of tags allowed in Org-mode files.
  1829. When this list is nil, Org-mode will base TAG input on what is already in the
  1830. buffer.
  1831. The value of this variable is an alist, the car of each entry must be a
  1832. keyword as a string, the cdr may be a character that is used to select
  1833. that tag through the fast-tag-selection interface.
  1834. See the manual for details."
  1835. :group 'org-tags
  1836. :type '(repeat
  1837. (choice
  1838. (cons (string :tag "Tag name")
  1839. (character :tag "Access char"))
  1840. (const :tag "Start radio group" (:startgroup))
  1841. (const :tag "End radio group" (:endgroup)))))
  1842. (defcustom org-use-fast-tag-selection 'auto
  1843. "Non-nil means, use fast tag selection scheme.
  1844. This is a special interface to select and deselect tags with single keys.
  1845. When nil, fast selection is never used.
  1846. When the symbol `auto', fast selection is used if and only if selection
  1847. characters for tags have been configured, either through the variable
  1848. `org-tag-alist' or through a #+TAGS line in the buffer.
  1849. When t, fast selection is always used and selection keys are assigned
  1850. automatically if necessary."
  1851. :group 'org-tags
  1852. :type '(choice
  1853. (const :tag "Always" t)
  1854. (const :tag "Never" nil)
  1855. (const :tag "When selection characters are configured" 'auto)))
  1856. (defcustom org-fast-tag-selection-single-key nil
  1857. "Non-nil means, fast tag selection exits after first change.
  1858. When nil, you have to press RET to exit it.
  1859. During fast tag selection, you can toggle this flag with `C-c'.
  1860. This variable can also have the value `expert'. In this case, the window
  1861. displaying the tags menu is not even shown, until you press C-c again."
  1862. :group 'org-tags
  1863. :type '(choice
  1864. (const :tag "No" nil)
  1865. (const :tag "Yes" t)
  1866. (const :tag "Expert" expert)))
  1867. (defvar org-fast-tag-selection-include-todo nil
  1868. "Non-nil means, fast tags selection interface will also offer TODO states.
  1869. This is an undocumented feature, you should not rely on it.")
  1870. (defcustom org-tags-column -80
  1871. "The column to which tags should be indented in a headline.
  1872. If this number is positive, it specifies the column. If it is negative,
  1873. it means that the tags should be flushright to that column. For example,
  1874. -80 works well for a normal 80 character screen."
  1875. :group 'org-tags
  1876. :type 'integer)
  1877. (defcustom org-auto-align-tags t
  1878. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1879. These operations change the length of a headline and therefore shift
  1880. the tags around. With this options turned on, after each such operation
  1881. the tags are again aligned to `org-tags-column'."
  1882. :group 'org-tags
  1883. :type 'boolean)
  1884. (defcustom org-use-tag-inheritance t
  1885. "Non-nil means, tags in levels apply also for sublevels.
  1886. When nil, only the tags directly given in a specific line apply there.
  1887. If you turn off this option, you very likely want to turn on the
  1888. companion option `org-tags-match-list-sublevels'."
  1889. :group 'org-tags
  1890. :type 'boolean)
  1891. (defcustom org-tags-match-list-sublevels nil
  1892. "Non-nil means list also sublevels of headlines matching tag search.
  1893. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1894. the sublevels of a headline matching a tag search often also match
  1895. the same search. Listing all of them can create very long lists.
  1896. Setting this variable to nil causes subtrees of a match to be skipped.
  1897. This option is off by default, because inheritance in on. If you turn
  1898. inheritance off, you very likely want to turn this option on.
  1899. As a special case, if the tag search is restricted to TODO items, the
  1900. value of this variable is ignored and sublevels are always checked, to
  1901. make sure all corresponding TODO items find their way into the list."
  1902. :group 'org-tags
  1903. :type 'boolean)
  1904. (defvar org-tags-history nil
  1905. "History of minibuffer reads for tags.")
  1906. (defvar org-last-tags-completion-table nil
  1907. "The last used completion table for tags.")
  1908. (defvar org-after-tags-change-hook nil
  1909. "Hook that is run after the tags in a line have changed.")
  1910. (defgroup org-properties nil
  1911. "Options concerning properties in Org-mode."
  1912. :tag "Org Properties"
  1913. :group 'org)
  1914. (defcustom org-property-format "%-10s %s"
  1915. "How property key/value pairs should be formatted by `indent-line'.
  1916. When `indent-line' hits a property definition, it will format the line
  1917. according to this format, mainly to make sure that the values are
  1918. lined-up with respect to each other."
  1919. :group 'org-properties
  1920. :type 'string)
  1921. (defcustom org-use-property-inheritance nil
  1922. "Non-nil means, properties apply also for sublevels.
  1923. This setting is only relevant during property searches, not when querying
  1924. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1925. you need to call `org-entry-get' with the inheritance flag.
  1926. Turning this on can cause significant overhead when doing a search, so
  1927. this is turned off by default.
  1928. When nil, only the properties directly given in the current entry count.
  1929. The value may also be a list of properties that shouldhave inheritance.
  1930. However, note that some special properties use inheritance under special
  1931. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1932. and the properties ending in \"_ALL\" when they are used as descriptor
  1933. for valid values of a property."
  1934. :group 'org-properties
  1935. :type '(choice
  1936. (const :tag "Not" nil)
  1937. (const :tag "Always" nil)
  1938. (repeat :tag "Specific properties" (string :tag "Property"))))
  1939. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1940. "The default column format, if no other format has been defined.
  1941. This variable can be set on the per-file basis by inserting a line
  1942. #+COLUMNS: %25ITEM ....."
  1943. :group 'org-properties
  1944. :type 'string)
  1945. (defcustom org-global-properties nil
  1946. "List of property/value pairs that can be inherited by any entry.
  1947. You can set buffer-local values for this by adding lines like
  1948. #+PROPERTY: NAME VALUE"
  1949. :group 'org-properties
  1950. :type '(repeat
  1951. (cons (string :tag "Property")
  1952. (string :tag "Value"))))
  1953. (defvar org-local-properties nil
  1954. "List of property/value pairs that can be inherited by any entry.
  1955. Valid for the current buffer.
  1956. This variable is populated from #+PROPERTY lines.")
  1957. (defgroup org-agenda nil
  1958. "Options concerning agenda views in Org-mode."
  1959. :tag "Org Agenda"
  1960. :group 'org)
  1961. (defvar org-category nil
  1962. "Variable used by org files to set a category for agenda display.
  1963. Such files should use a file variable to set it, for example
  1964. # -*- mode: org; org-category: \"ELisp\"
  1965. or contain a special line
  1966. #+CATEGORY: ELisp
  1967. If the file does not specify a category, then file's base name
  1968. is used instead.")
  1969. (make-variable-buffer-local 'org-category)
  1970. (defcustom org-agenda-files nil
  1971. "The files to be used for agenda display.
  1972. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1973. \\[org-remove-file]. You can also use customize to edit the list.
  1974. If an entry is a directory, all files in that directory that are matched by
  1975. `org-agenda-file-regexp' will be part of the file list.
  1976. If the value of the variable is not a list but a single file name, then
  1977. the list of agenda files is actually stored and maintained in that file, one
  1978. agenda file per line."
  1979. :group 'org-agenda
  1980. :type '(choice
  1981. (repeat :tag "List of files and directories" file)
  1982. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1983. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1984. "Regular expression to match files for `org-agenda-files'.
  1985. If any element in the list in that variable contains a directory instead
  1986. of a normal file, all files in that directory that are matched by this
  1987. regular expression will be included."
  1988. :group 'org-agenda
  1989. :type 'regexp)
  1990. (defcustom org-agenda-skip-unavailable-files nil
  1991. "t means to just skip non-reachable files in `org-agenda-files'.
  1992. Nil means to remove them, after a query, from the list."
  1993. :group 'org-agenda
  1994. :type 'boolean)
  1995. (defcustom org-agenda-text-search-extra-files nil
  1996. "List of extra files to be searched by text search commands.
  1997. These files will be search in addition to the agenda files bu the
  1998. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1999. Note that these files will only be searched for text search commands,
  2000. not for the other agenda views like todo lists, tag earches or the weekly
  2001. agenda. This variable is intended to list notes and possibly archive files
  2002. that should also be searched by these two commands."
  2003. :group 'org-agenda
  2004. :type '(repeat file))
  2005. (if (fboundp 'defvaralias)
  2006. (defvaralias 'org-agenda-multi-occur-extra-files
  2007. 'org-agenda-text-search-extra-files))
  2008. (defcustom org-agenda-confirm-kill 1
  2009. "When set, remote killing from the agenda buffer needs confirmation.
  2010. When t, a confirmation is always needed. When a number N, confirmation is
  2011. only needed when the text to be killed contains more than N non-white lines."
  2012. :group 'org-agenda
  2013. :type '(choice
  2014. (const :tag "Never" nil)
  2015. (const :tag "Always" t)
  2016. (number :tag "When more than N lines")))
  2017. (defcustom org-calendar-to-agenda-key [?c]
  2018. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2019. The command `org-calendar-goto-agenda' will be bound to this key. The
  2020. default is the character `c' because then `c' can be used to switch back and
  2021. forth between agenda and calendar."
  2022. :group 'org-agenda
  2023. :type 'sexp)
  2024. (defcustom org-agenda-compact-blocks nil
  2025. "Non-nil means, make the block agenda more compact.
  2026. This is done by leaving out unnecessary lines."
  2027. :group 'org-agenda
  2028. :type nil)
  2029. (defgroup org-agenda-export nil
  2030. "Options concerning exporting agenda views in Org-mode."
  2031. :tag "Org Agenda Export"
  2032. :group 'org-agenda)
  2033. (defcustom org-agenda-with-colors t
  2034. "Non-nil means, use colors in agenda views."
  2035. :group 'org-agenda-export
  2036. :type 'boolean)
  2037. (defcustom org-agenda-exporter-settings nil
  2038. "Alist of variable/value pairs that should be active during agenda export.
  2039. This is a good place to set uptions for ps-print and for htmlize."
  2040. :group 'org-agenda-export
  2041. :type '(repeat
  2042. (list
  2043. (variable)
  2044. (sexp :tag "Value"))))
  2045. (defcustom org-agenda-export-html-style ""
  2046. "The style specification for exported HTML Agenda files.
  2047. If this variable contains a string, it will replace the default <style>
  2048. section as produced by `htmlize'.
  2049. Since there are different ways of setting style information, this variable
  2050. needs to contain the full HTML structure to provide a style, including the
  2051. surrounding HTML tags. The style specifications should include definitions
  2052. the fonts used by the agenda, here is an example:
  2053. <style type=\"text/css\">
  2054. p { font-weight: normal; color: gray; }
  2055. .org-agenda-structure {
  2056. font-size: 110%;
  2057. color: #003399;
  2058. font-weight: 600;
  2059. }
  2060. .org-todo {
  2061. color: #cc6666;Week-agenda:
  2062. font-weight: bold;
  2063. }
  2064. .org-done {
  2065. color: #339933;
  2066. }
  2067. .title { text-align: center; }
  2068. .todo, .deadline { color: red; }
  2069. .done { color: green; }
  2070. </style>
  2071. or, if you want to keep the style in a file,
  2072. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2073. As the value of this option simply gets inserted into the HTML <head> header,
  2074. you can \"misuse\" it to also add other text to the header. However,
  2075. <style>...</style> is required, if not present the variable will be ignored."
  2076. :group 'org-agenda-export
  2077. :group 'org-export-html
  2078. :type 'string)
  2079. (defgroup org-agenda-custom-commands nil
  2080. "Options concerning agenda views in Org-mode."
  2081. :tag "Org Agenda Custom Commands"
  2082. :group 'org-agenda)
  2083. (defcustom org-agenda-custom-commands nil
  2084. "Custom commands for the agenda.
  2085. These commands will be offered on the splash screen displayed by the
  2086. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2087. (key desc type match options files)
  2088. key The key (one or more characters as a string) to be associated
  2089. with the command.
  2090. desc A description of the commend, when omitted or nil, a default
  2091. description is built using MATCH.
  2092. type The command type, any of the following symbols:
  2093. agenda The daily/weekly agenda.
  2094. todo Entries with a specific TODO keyword, in all agenda files.
  2095. search Entries containing search words entry or headline.
  2096. tags Tags/Property/TODO match in all agenda files.
  2097. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2098. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2099. tags-tree Sparse tree with all tags matches in *current* file.
  2100. occur-tree Occur sparse tree for *current* file.
  2101. ... A user-defined function.
  2102. match What to search for:
  2103. - a single keyword for TODO keyword searches
  2104. - a tags match expression for tags searches
  2105. - a regular expression for occur searches
  2106. options A list of option settings, similar to that in a let form, so like
  2107. this: ((opt1 val1) (opt2 val2) ...)
  2108. files A list of files file to write the produced agenda buffer to
  2109. with the command `org-store-agenda-views'.
  2110. If a file name ends in \".html\", an HTML version of the buffer
  2111. is written out. If it ends in \".ps\", a postscript version is
  2112. produced. Otherwide, only the plain text is written to the file.
  2113. You can also define a set of commands, to create a composite agenda buffer.
  2114. In this case, an entry looks like this:
  2115. (key desc (cmd1 cmd2 ...) general-options file)
  2116. where
  2117. desc A description string to be displayed in the dispatcher menu.
  2118. cmd An agenda command, similar to the above. However, tree commands
  2119. are no allowed, but instead you can get agenda and global todo list.
  2120. So valid commands for a set are:
  2121. (agenda)
  2122. (alltodo)
  2123. (stuck)
  2124. (todo \"match\" options files)
  2125. (search \"match\" options files)
  2126. (tags \"match\" options files)
  2127. (tags-todo \"match\" options files)
  2128. Each command can carry a list of options, and another set of options can be
  2129. given for the whole set of commands. Individual command options take
  2130. precedence over the general options.
  2131. When using several characters as key to a command, the first characters
  2132. are prefix commands. For the dispatcher to display useful information, you
  2133. should provide a description for the prefix, like
  2134. (setq org-agenda-custom-commands
  2135. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2136. (\"hl\" tags \"+HOME+Lisa\")
  2137. (\"hp\" tags \"+HOME+Peter\")
  2138. (\"hk\" tags \"+HOME+Kim\")))"
  2139. :group 'org-agenda-custom-commands
  2140. :type '(repeat
  2141. (choice :value ("a" "" tags "" nil)
  2142. (list :tag "Single command"
  2143. (string :tag "Access Key(s) ")
  2144. (option (string :tag "Description"))
  2145. (choice
  2146. (const :tag "Agenda" agenda)
  2147. (const :tag "TODO list" alltodo)
  2148. (const :tag "Search words" search)
  2149. (const :tag "Stuck projects" stuck)
  2150. (const :tag "Tags search (all agenda files)" tags)
  2151. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2152. (const :tag "TODO keyword search (all agenda files)" todo)
  2153. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2154. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2155. (const :tag "Occur tree (current buffer)" occur-tree)
  2156. (sexp :tag "Other, user-defined function"))
  2157. (string :tag "Match")
  2158. (repeat :tag "Local options"
  2159. (list (variable :tag "Option") (sexp :tag "Value")))
  2160. (option (repeat :tag "Export" (file :tag "Export to"))))
  2161. (list :tag "Command series, all agenda files"
  2162. (string :tag "Access Key(s)")
  2163. (string :tag "Description ")
  2164. (repeat
  2165. (choice
  2166. (const :tag "Agenda" (agenda))
  2167. (const :tag "TODO list" (alltodo))
  2168. (list :tag "Search words"
  2169. (const :format "" search)
  2170. (string :tag "Match")
  2171. (repeat :tag "Local options"
  2172. (list (variable :tag "Option")
  2173. (sexp :tag "Value"))))
  2174. (const :tag "Stuck projects" (stuck))
  2175. (list :tag "Tags search"
  2176. (const :format "" tags)
  2177. (string :tag "Match")
  2178. (repeat :tag "Local options"
  2179. (list (variable :tag "Option")
  2180. (sexp :tag "Value"))))
  2181. (list :tag "Tags search, TODO entries only"
  2182. (const :format "" tags-todo)
  2183. (string :tag "Match")
  2184. (repeat :tag "Local options"
  2185. (list (variable :tag "Option")
  2186. (sexp :tag "Value"))))
  2187. (list :tag "TODO keyword search"
  2188. (const :format "" todo)
  2189. (string :tag "Match")
  2190. (repeat :tag "Local options"
  2191. (list (variable :tag "Option")
  2192. (sexp :tag "Value"))))
  2193. (list :tag "Other, user-defined function"
  2194. (symbol :tag "function")
  2195. (string :tag "Match")
  2196. (repeat :tag "Local options"
  2197. (list (variable :tag "Option")
  2198. (sexp :tag "Value"))))))
  2199. (repeat :tag "General options"
  2200. (list (variable :tag "Option")
  2201. (sexp :tag "Value")))
  2202. (option (repeat :tag "Export" (file :tag "Export to"))))
  2203. (cons :tag "Prefix key documentation"
  2204. (string :tag "Access Key(s)")
  2205. (string :tag "Description ")))))
  2206. (defcustom org-agenda-query-register ?o
  2207. "The register holding the current query string.
  2208. The prupose of this is that if you construct a query string interactively,
  2209. you can then use it to define a custom command."
  2210. :group 'org-agenda-custom-commands
  2211. :type 'character)
  2212. (defcustom org-stuck-projects
  2213. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2214. "How to identify stuck projects.
  2215. This is a list of four items:
  2216. 1. A tags/todo matcher string that is used to identify a project.
  2217. The entire tree below a headline matched by this is considered one project.
  2218. 2. A list of TODO keywords identifying non-stuck projects.
  2219. If the project subtree contains any headline with one of these todo
  2220. keywords, the project is considered to be not stuck. If you specify
  2221. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2222. 3. A list of tags identifying non-stuck projects.
  2223. If the project subtree contains any headline with one of these tags,
  2224. the project is considered to be not stuck. If you specify \"*\" as
  2225. a tag, any tag will mark the project unstuck.
  2226. 4. An arbitrary regular expression matching non-stuck projects.
  2227. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2228. or `C-c a #' to produce the list."
  2229. :group 'org-agenda-custom-commands
  2230. :type '(list
  2231. (string :tag "Tags/TODO match to identify a project")
  2232. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2233. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2234. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2235. (defgroup org-agenda-skip nil
  2236. "Options concerning skipping parts of agenda files."
  2237. :tag "Org Agenda Skip"
  2238. :group 'org-agenda)
  2239. (defcustom org-agenda-todo-list-sublevels t
  2240. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2241. When nil, the sublevels of a TODO entry are not checked, resulting in
  2242. potentially much shorter TODO lists."
  2243. :group 'org-agenda-skip
  2244. :group 'org-todo
  2245. :type 'boolean)
  2246. (defcustom org-agenda-todo-ignore-with-date nil
  2247. "Non-nil means, don't show entries with a date in the global todo list.
  2248. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2249. but don't want them to show up in the TODO list.
  2250. When this is set, it also covers deadlines and scheduled items, the settings
  2251. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2252. will be ignored."
  2253. :group 'org-agenda-skip
  2254. :group 'org-todo
  2255. :type 'boolean)
  2256. (defcustom org-agenda-todo-ignore-scheduled nil
  2257. "Non-nil means, don't show scheduled entries in the global todo list.
  2258. The idea behind this is that by scheduling it, you have already taken care
  2259. of this item.
  2260. See also `org-agenda-todo-ignore-with-date'."
  2261. :group 'org-agenda-skip
  2262. :group 'org-todo
  2263. :type 'boolean)
  2264. (defcustom org-agenda-todo-ignore-deadlines nil
  2265. "Non-nil means, don't show near deadline entries in the global todo list.
  2266. Near means closer than `org-deadline-warning-days' days.
  2267. The idea behind this is that such items will appear in the agenda anyway.
  2268. See also `org-agenda-todo-ignore-with-date'."
  2269. :group 'org-agenda-skip
  2270. :group 'org-todo
  2271. :type 'boolean)
  2272. (defcustom org-agenda-skip-scheduled-if-done nil
  2273. "Non-nil means don't show scheduled items in agenda when they are done.
  2274. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2275. it applies only to the actual date of the scheduling. Warnings about
  2276. an item with a past scheduling dates are always turned off when the item
  2277. is DONE."
  2278. :group 'org-agenda-skip
  2279. :type 'boolean)
  2280. (defcustom org-agenda-skip-deadline-if-done nil
  2281. "Non-nil means don't show deadines when the corresponding item is done.
  2282. When nil, the deadline is still shown and should give you a happy feeling.
  2283. This is relevant for the daily/weekly agenda. And it applied only to the
  2284. actualy date of the deadline. Warnings about approching and past-due
  2285. deadlines are always turned off when the item is DONE."
  2286. :group 'org-agenda-skip
  2287. :type 'boolean)
  2288. (defcustom org-agenda-skip-timestamp-if-done nil
  2289. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2290. :group 'org-agenda-skip
  2291. :type 'boolean)
  2292. (defcustom org-timeline-show-empty-dates 3
  2293. "Non-nil means, `org-timeline' also shows dates without an entry.
  2294. When nil, only the days which actually have entries are shown.
  2295. When t, all days between the first and the last date are shown.
  2296. When an integer, show also empty dates, but if there is a gap of more than
  2297. N days, just insert a special line indicating the size of the gap."
  2298. :group 'org-agenda-skip
  2299. :type '(choice
  2300. (const :tag "None" nil)
  2301. (const :tag "All" t)
  2302. (number :tag "at most")))
  2303. (defgroup org-agenda-startup nil
  2304. "Options concerning initial settings in the Agenda in Org Mode."
  2305. :tag "Org Agenda Startup"
  2306. :group 'org-agenda)
  2307. (defcustom org-finalize-agenda-hook nil
  2308. "Hook run just before displaying an agenda buffer."
  2309. :group 'org-agenda-startup
  2310. :type 'hook)
  2311. (defcustom org-agenda-mouse-1-follows-link nil
  2312. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2313. A longer mouse click will still set point. Does not work on XEmacs.
  2314. Needs to be set before org.el is loaded."
  2315. :group 'org-agenda-startup
  2316. :type 'boolean)
  2317. (defcustom org-agenda-start-with-follow-mode nil
  2318. "The initial value of follow-mode in a newly created agenda window."
  2319. :group 'org-agenda-startup
  2320. :type 'boolean)
  2321. (defgroup org-agenda-windows nil
  2322. "Options concerning the windows used by the Agenda in Org Mode."
  2323. :tag "Org Agenda Windows"
  2324. :group 'org-agenda)
  2325. (defcustom org-agenda-window-setup 'reorganize-frame
  2326. "How the agenda buffer should be displayed.
  2327. Possible values for this option are:
  2328. current-window Show agenda in the current window, keeping all other windows.
  2329. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2330. other-window Use `switch-to-buffer-other-window' to display agenda.
  2331. reorganize-frame Show only two windows on the current frame, the current
  2332. window and the agenda.
  2333. See also the variable `org-agenda-restore-windows-after-quit'."
  2334. :group 'org-agenda-windows
  2335. :type '(choice
  2336. (const current-window)
  2337. (const other-frame)
  2338. (const other-window)
  2339. (const reorganize-frame)))
  2340. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2341. "The min and max height of the agenda window as a fraction of frame height.
  2342. The value of the variable is a cons cell with two numbers between 0 and 1.
  2343. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2344. :group 'org-agenda-windows
  2345. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2346. (defcustom org-agenda-restore-windows-after-quit nil
  2347. "Non-nil means, restore window configuration open exiting agenda.
  2348. Before the window configuration is changed for displaying the agenda,
  2349. the current status is recorded. When the agenda is exited with
  2350. `q' or `x' and this option is set, the old state is restored. If
  2351. `org-agenda-window-setup' is `other-frame', the value of this
  2352. option will be ignored.."
  2353. :group 'org-agenda-windows
  2354. :type 'boolean)
  2355. (defcustom org-indirect-buffer-display 'other-window
  2356. "How should indirect tree buffers be displayed?
  2357. This applies to indirect buffers created with the commands
  2358. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2359. Valid values are:
  2360. current-window Display in the current window
  2361. other-window Just display in another window.
  2362. dedicated-frame Create one new frame, and re-use it each time.
  2363. new-frame Make a new frame each time. Note that in this case
  2364. previously-made indirect buffers are kept, and you need to
  2365. kill these buffers yourself."
  2366. :group 'org-structure
  2367. :group 'org-agenda-windows
  2368. :type '(choice
  2369. (const :tag "In current window" current-window)
  2370. (const :tag "In current frame, other window" other-window)
  2371. (const :tag "Each time a new frame" new-frame)
  2372. (const :tag "One dedicated frame" dedicated-frame)))
  2373. (defgroup org-agenda-daily/weekly nil
  2374. "Options concerning the daily/weekly agenda."
  2375. :tag "Org Agenda Daily/Weekly"
  2376. :group 'org-agenda)
  2377. (defcustom org-agenda-ndays 7
  2378. "Number of days to include in overview display.
  2379. Should be 1 or 7."
  2380. :group 'org-agenda-daily/weekly
  2381. :type 'number)
  2382. (defcustom org-agenda-start-on-weekday 1
  2383. "Non-nil means, start the overview always on the specified weekday.
  2384. 0 denotes Sunday, 1 denotes Monday etc.
  2385. When nil, always start on the current day."
  2386. :group 'org-agenda-daily/weekly
  2387. :type '(choice (const :tag "Today" nil)
  2388. (number :tag "Weekday No.")))
  2389. (defcustom org-agenda-show-all-dates t
  2390. "Non-nil means, `org-agenda' shows every day in the selected range.
  2391. When nil, only the days which actually have entries are shown."
  2392. :group 'org-agenda-daily/weekly
  2393. :type 'boolean)
  2394. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2395. "Format string for displaying dates in the agenda.
  2396. Used by the daily/weekly agenda and by the timeline. This should be
  2397. a format string understood by `format-time-string', or a function returning
  2398. the formatted date as a string. The function must take a single argument,
  2399. a calendar-style date list like (month day year)."
  2400. :group 'org-agenda-daily/weekly
  2401. :type '(choice
  2402. (string :tag "Format string")
  2403. (function :tag "Function")))
  2404. (defun org-agenda-format-date-aligned (date)
  2405. "Format a date string for display in the daily/weekly agenda, or timeline.
  2406. This function makes sure that dates are aligned for easy reading."
  2407. (format "%-9s %2d %s %4d"
  2408. (calendar-day-name date)
  2409. (extract-calendar-day date)
  2410. (calendar-month-name (extract-calendar-month date))
  2411. (extract-calendar-year date)))
  2412. (defcustom org-agenda-include-diary nil
  2413. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2414. :group 'org-agenda-daily/weekly
  2415. :type 'boolean)
  2416. (defcustom org-agenda-include-all-todo nil
  2417. "Set means weekly/daily agenda will always contain all TODO entries.
  2418. The TODO entries will be listed at the top of the agenda, before
  2419. the entries for specific days."
  2420. :group 'org-agenda-daily/weekly
  2421. :type 'boolean)
  2422. (defcustom org-agenda-repeating-timestamp-show-all t
  2423. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2424. When nil, only one occurence is shown, either today or the
  2425. nearest into the future."
  2426. :group 'org-agenda-daily/weekly
  2427. :type 'boolean)
  2428. (defcustom org-deadline-warning-days 14
  2429. "No. of days before expiration during which a deadline becomes active.
  2430. This variable governs the display in sparse trees and in the agenda.
  2431. When 0 or negative, it means use this number (the absolute value of it)
  2432. even if a deadline has a different individual lead time specified."
  2433. :group 'org-time
  2434. :group 'org-agenda-daily/weekly
  2435. :type 'number)
  2436. (defcustom org-scheduled-past-days 10000
  2437. "No. of days to continue listing scheduled items that are not marked DONE.
  2438. When an item is scheduled on a date, it shows up in the agenda on this
  2439. day and will be listed until it is marked done for the number of days
  2440. given here."
  2441. :group 'org-agenda-daily/weekly
  2442. :type 'number)
  2443. (defgroup org-agenda-time-grid nil
  2444. "Options concerning the time grid in the Org-mode Agenda."
  2445. :tag "Org Agenda Time Grid"
  2446. :group 'org-agenda)
  2447. (defcustom org-agenda-use-time-grid t
  2448. "Non-nil means, show a time grid in the agenda schedule.
  2449. A time grid is a set of lines for specific times (like every two hours between
  2450. 8:00 and 20:00). The items scheduled for a day at specific times are
  2451. sorted in between these lines.
  2452. For details about when the grid will be shown, and what it will look like, see
  2453. the variable `org-agenda-time-grid'."
  2454. :group 'org-agenda-time-grid
  2455. :type 'boolean)
  2456. (defcustom org-agenda-time-grid
  2457. '((daily today require-timed)
  2458. "----------------"
  2459. (800 1000 1200 1400 1600 1800 2000))
  2460. "The settings for time grid for agenda display.
  2461. This is a list of three items. The first item is again a list. It contains
  2462. symbols specifying conditions when the grid should be displayed:
  2463. daily if the agenda shows a single day
  2464. weekly if the agenda shows an entire week
  2465. today show grid on current date, independent of daily/weekly display
  2466. require-timed show grid only if at least one item has a time specification
  2467. The second item is a string which will be places behing the grid time.
  2468. The third item is a list of integers, indicating the times that should have
  2469. a grid line."
  2470. :group 'org-agenda-time-grid
  2471. :type
  2472. '(list
  2473. (set :greedy t :tag "Grid Display Options"
  2474. (const :tag "Show grid in single day agenda display" daily)
  2475. (const :tag "Show grid in weekly agenda display" weekly)
  2476. (const :tag "Always show grid for today" today)
  2477. (const :tag "Show grid only if any timed entries are present"
  2478. require-timed)
  2479. (const :tag "Skip grid times already present in an entry"
  2480. remove-match))
  2481. (string :tag "Grid String")
  2482. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2483. (defgroup org-agenda-sorting nil
  2484. "Options concerning sorting in the Org-mode Agenda."
  2485. :tag "Org Agenda Sorting"
  2486. :group 'org-agenda)
  2487. (defconst org-sorting-choice
  2488. '(choice
  2489. (const time-up) (const time-down)
  2490. (const category-keep) (const category-up) (const category-down)
  2491. (const tag-down) (const tag-up)
  2492. (const priority-up) (const priority-down))
  2493. "Sorting choices.")
  2494. (defcustom org-agenda-sorting-strategy
  2495. '((agenda time-up category-keep priority-down)
  2496. (todo category-keep priority-down)
  2497. (tags category-keep priority-down)
  2498. (search category-keep))
  2499. "Sorting structure for the agenda items of a single day.
  2500. This is a list of symbols which will be used in sequence to determine
  2501. if an entry should be listed before another entry. The following
  2502. symbols are recognized:
  2503. time-up Put entries with time-of-day indications first, early first
  2504. time-down Put entries with time-of-day indications first, late first
  2505. category-keep Keep the default order of categories, corresponding to the
  2506. sequence in `org-agenda-files'.
  2507. category-up Sort alphabetically by category, A-Z.
  2508. category-down Sort alphabetically by category, Z-A.
  2509. tag-up Sort alphabetically by last tag, A-Z.
  2510. tag-down Sort alphabetically by last tag, Z-A.
  2511. priority-up Sort numerically by priority, high priority last.
  2512. priority-down Sort numerically by priority, high priority first.
  2513. The different possibilities will be tried in sequence, and testing stops
  2514. if one comparison returns a \"not-equal\". For example, the default
  2515. '(time-up category-keep priority-down)
  2516. means: Pull out all entries having a specified time of day and sort them,
  2517. in order to make a time schedule for the current day the first thing in the
  2518. agenda listing for the day. Of the entries without a time indication, keep
  2519. the grouped in categories, don't sort the categories, but keep them in
  2520. the sequence given in `org-agenda-files'. Within each category sort by
  2521. priority.
  2522. Leaving out `category-keep' would mean that items will be sorted across
  2523. categories by priority.
  2524. Instead of a single list, this can also be a set of list for specific
  2525. contents, with a context symbol in the car of the list, any of
  2526. `agenda', `todo', `tags' for the corresponding agenda views."
  2527. :group 'org-agenda-sorting
  2528. :type `(choice
  2529. (repeat :tag "General" ,org-sorting-choice)
  2530. (list :tag "Individually"
  2531. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2532. (repeat ,org-sorting-choice))
  2533. (cons (const :tag "Strategy for TODO lists" todo)
  2534. (repeat ,org-sorting-choice))
  2535. (cons (const :tag "Strategy for Tags matches" tags)
  2536. (repeat ,org-sorting-choice)))))
  2537. (defcustom org-sort-agenda-notime-is-late t
  2538. "Non-nil means, items without time are considered late.
  2539. This is only relevant for sorting. When t, items which have no explicit
  2540. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2541. do have a time. When nil, the default time is before 0:00. You can use this
  2542. option to decide if the schedule for today should come before or after timeless
  2543. agenda entries."
  2544. :group 'org-agenda-sorting
  2545. :type 'boolean)
  2546. (defgroup org-agenda-line-format nil
  2547. "Options concerning the entry prefix in the Org-mode agenda display."
  2548. :tag "Org Agenda Line Format"
  2549. :group 'org-agenda)
  2550. (defcustom org-agenda-prefix-format
  2551. '((agenda . " %-12:c%?-12t% s")
  2552. (timeline . " % s")
  2553. (todo . " %-12:c")
  2554. (tags . " %-12:c")
  2555. (search . " %-12:c"))
  2556. "Format specifications for the prefix of items in the agenda views.
  2557. An alist with four entries, for the different agenda types. The keys to the
  2558. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2559. are format strings.
  2560. This format works similar to a printf format, with the following meaning:
  2561. %c the category of the item, \"Diary\" for entries from the diary, or
  2562. as given by the CATEGORY keyword or derived from the file name.
  2563. %T the *last* tag of the item. Last because inherited tags come
  2564. first in the list.
  2565. %t the time-of-day specification if one applies to the entry, in the
  2566. format HH:MM
  2567. %s Scheduling/Deadline information, a short string
  2568. All specifiers work basically like the standard `%s' of printf, but may
  2569. contain two additional characters: A question mark just after the `%' and
  2570. a whitespace/punctuation character just before the final letter.
  2571. If the first character after `%' is a question mark, the entire field
  2572. will only be included if the corresponding value applies to the
  2573. current entry. This is useful for fields which should have fixed
  2574. width when present, but zero width when absent. For example,
  2575. \"%?-12t\" will result in a 12 character time field if a time of the
  2576. day is specified, but will completely disappear in entries which do
  2577. not contain a time.
  2578. If there is punctuation or whitespace character just before the final
  2579. format letter, this character will be appended to the field value if
  2580. the value is not empty. For example, the format \"%-12:c\" leads to
  2581. \"Diary: \" if the category is \"Diary\". If the category were be
  2582. empty, no additional colon would be interted.
  2583. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2584. - Indent the line with two space characters
  2585. - Give the category in a 12 chars wide field, padded with whitespace on
  2586. the right (because of `-'). Append a colon if there is a category
  2587. (because of `:').
  2588. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2589. time, don't put in an empty field, just skip it (because of '?').
  2590. - Finally, put the scheduling information and append a whitespace.
  2591. As another example, if you don't want the time-of-day of entries in
  2592. the prefix, you could use:
  2593. (setq org-agenda-prefix-format \" %-11:c% s\")
  2594. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2595. `org-agenda-remove-tags'."
  2596. :type '(choice
  2597. (string :tag "General format")
  2598. (list :greedy t :tag "View dependent"
  2599. (cons (const agenda) (string :tag "Format"))
  2600. (cons (const timeline) (string :tag "Format"))
  2601. (cons (const todo) (string :tag "Format"))
  2602. (cons (const tags) (string :tag "Format"))
  2603. (cons (const search) (string :tag "Format"))))
  2604. :group 'org-agenda-line-format)
  2605. (defvar org-prefix-format-compiled nil
  2606. "The compiled version of the most recently used prefix format.
  2607. See the variable `org-agenda-prefix-format'.")
  2608. (defcustom org-agenda-todo-keyword-format "%-1s"
  2609. "Format for the TODO keyword in agenda lines.
  2610. Set this to something like \"%-12s\" if you want all TODO keywords
  2611. to occupy a fixed space in the agenda display."
  2612. :group 'org-agenda-line-format
  2613. :type 'string)
  2614. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2615. "Text preceeding scheduled items in the agenda view.
  2616. This is a list with two strings. The first applies when the item is
  2617. scheduled on the current day. The second applies when it has been scheduled
  2618. previously, it may contain a %d to capture how many days ago the item was
  2619. scheduled."
  2620. :group 'org-agenda-line-format
  2621. :type '(list
  2622. (string :tag "Scheduled today ")
  2623. (string :tag "Scheduled previously")))
  2624. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2625. "Text preceeding deadline items in the agenda view.
  2626. This is a list with two strings. The first applies when the item has its
  2627. deadline on the current day. The second applies when it is in the past or
  2628. in the future, it may contain %d to capture how many days away the deadline
  2629. is (was)."
  2630. :group 'org-agenda-line-format
  2631. :type '(list
  2632. (string :tag "Deadline today ")
  2633. (string :tag "Deadline relative")))
  2634. (defcustom org-agenda-remove-times-when-in-prefix t
  2635. "Non-nil means, remove duplicate time specifications in agenda items.
  2636. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2637. time-of-day specification in a headline or diary entry is extracted and
  2638. placed into the prefix. If this option is non-nil, the original specification
  2639. \(a timestamp or -range, or just a plain time(range) specification like
  2640. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2641. cluttered.
  2642. The option can be t or nil. It may also be the symbol `beg', indicating
  2643. that the time should only be removed what it is located at the beginning of
  2644. the headline/diary entry."
  2645. :group 'org-agenda-line-format
  2646. :type '(choice
  2647. (const :tag "Always" t)
  2648. (const :tag "Never" nil)
  2649. (const :tag "When at beginning of entry" beg)))
  2650. (defcustom org-agenda-default-appointment-duration nil
  2651. "Default duration for appointments that only have a starting time.
  2652. When nil, no duration is specified in such cases.
  2653. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2654. :group 'org-agenda-line-format
  2655. :type '(choice
  2656. (integer :tag "Minutes")
  2657. (const :tag "No default duration")))
  2658. (defcustom org-agenda-remove-tags nil
  2659. "Non-nil means, remove the tags from the headline copy in the agenda.
  2660. When this is the symbol `prefix', only remove tags when
  2661. `org-agenda-prefix-format' contains a `%T' specifier."
  2662. :group 'org-agenda-line-format
  2663. :type '(choice
  2664. (const :tag "Always" t)
  2665. (const :tag "Never" nil)
  2666. (const :tag "When prefix format contains %T" prefix)))
  2667. (if (fboundp 'defvaralias)
  2668. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2669. 'org-agenda-remove-tags))
  2670. (defcustom org-agenda-tags-column -80
  2671. "Shift tags in agenda items to this column.
  2672. If this number is positive, it specifies the column. If it is negative,
  2673. it means that the tags should be flushright to that column. For example,
  2674. -80 works well for a normal 80 character screen."
  2675. :group 'org-agenda-line-format
  2676. :type 'integer)
  2677. (if (fboundp 'defvaralias)
  2678. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2679. (defcustom org-agenda-fontify-priorities t
  2680. "Non-nil means, highlight low and high priorities in agenda.
  2681. When t, the highest priority entries are bold, lowest priority italic.
  2682. This may also be an association list of priority faces. The face may be
  2683. a names face, or a list like `(:background \"Red\")'."
  2684. :group 'org-agenda-line-format
  2685. :type '(choice
  2686. (const :tag "Never" nil)
  2687. (const :tag "Defaults" t)
  2688. (repeat :tag "Specify"
  2689. (list (character :tag "Priority" :value ?A)
  2690. (sexp :tag "face")))))
  2691. (defgroup org-latex nil
  2692. "Options for embedding LaTeX code into Org-mode"
  2693. :tag "Org LaTeX"
  2694. :group 'org)
  2695. (defcustom org-format-latex-options
  2696. '(:foreground default :background default :scale 1.0
  2697. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2698. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2699. "Options for creating images from LaTeX fragments.
  2700. This is a property list with the following properties:
  2701. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2702. `default' means use the forground of the default face.
  2703. :background the background color, or \"Transparent\".
  2704. `default' means use the background of the default face.
  2705. :scale a scaling factor for the size of the images
  2706. :html-foreground, :html-background, :html-scale
  2707. The same numbers for HTML export.
  2708. :matchers a list indicating which matchers should be used to
  2709. find LaTeX fragments. Valid members of this list are:
  2710. \"begin\" find environments
  2711. \"$\" find math expressions surrounded by $...$
  2712. \"$$\" find math expressions surrounded by $$....$$
  2713. \"\\(\" find math expressions surrounded by \\(...\\)
  2714. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2715. :group 'org-latex
  2716. :type 'plist)
  2717. (defcustom org-format-latex-header "\\documentclass{article}
  2718. \\usepackage{fullpage} % do not remove
  2719. \\usepackage{amssymb}
  2720. \\usepackage[usenames]{color}
  2721. \\usepackage{amsmath}
  2722. \\usepackage{latexsym}
  2723. \\usepackage[mathscr]{eucal}
  2724. \\pagestyle{empty} % do not remove"
  2725. "The document header used for processing LaTeX fragments."
  2726. :group 'org-latex
  2727. :type 'string)
  2728. (defgroup org-export nil
  2729. "Options for exporting org-listings."
  2730. :tag "Org Export"
  2731. :group 'org)
  2732. (defgroup org-export-general nil
  2733. "General options for exporting Org-mode files."
  2734. :tag "Org Export General"
  2735. :group 'org-export)
  2736. ;; FIXME
  2737. (defvar org-export-publishing-directory nil)
  2738. (defcustom org-export-with-special-strings t
  2739. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2740. When this option is turned on, these strings will be exported as:
  2741. Org HTML LaTeX
  2742. -----+----------+--------
  2743. \\- &shy; \\-
  2744. -- &ndash; --
  2745. --- &mdash; ---
  2746. ... &hellip; \ldots
  2747. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2748. :group 'org-export-translation
  2749. :type 'boolean)
  2750. (defcustom org-export-language-setup
  2751. '(("en" "Author" "Date" "Table of Contents")
  2752. ("cs" "Autor" "Datum" "Obsah")
  2753. ("da" "Ophavsmand" "Dato" "Indhold")
  2754. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2755. ("es" "Autor" "Fecha" "\xcdndice")
  2756. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2757. ("it" "Autore" "Data" "Indice")
  2758. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2759. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2760. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2761. "Terms used in export text, translated to different languages.
  2762. Use the variable `org-export-default-language' to set the language,
  2763. or use the +OPTION lines for a per-file setting."
  2764. :group 'org-export-general
  2765. :type '(repeat
  2766. (list
  2767. (string :tag "HTML language tag")
  2768. (string :tag "Author")
  2769. (string :tag "Date")
  2770. (string :tag "Table of Contents"))))
  2771. (defcustom org-export-default-language "en"
  2772. "The default language of HTML export, as a string.
  2773. This should have an association in `org-export-language-setup'."
  2774. :group 'org-export-general
  2775. :type 'string)
  2776. (defcustom org-export-skip-text-before-1st-heading t
  2777. "Non-nil means, skip all text before the first headline when exporting.
  2778. When nil, that text is exported as well."
  2779. :group 'org-export-general
  2780. :type 'boolean)
  2781. (defcustom org-export-headline-levels 3
  2782. "The last level which is still exported as a headline.
  2783. Inferior levels will produce itemize lists when exported.
  2784. Note that a numeric prefix argument to an exporter function overrides
  2785. this setting.
  2786. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2787. :group 'org-export-general
  2788. :type 'number)
  2789. (defcustom org-export-with-section-numbers t
  2790. "Non-nil means, add section numbers to headlines when exporting.
  2791. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2792. :group 'org-export-general
  2793. :type 'boolean)
  2794. (defcustom org-export-with-toc t
  2795. "Non-nil means, create a table of contents in exported files.
  2796. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2797. When an integer, include levels up to N in the toc, this may then be
  2798. different from `org-export-headline-levels', but it will not be allowed
  2799. to be larger than the number of headline levels.
  2800. When nil, no table of contents is made.
  2801. Headlines which contain any TODO items will be marked with \"(*)\" in
  2802. ASCII export, and with red color in HTML output, if the option
  2803. `org-export-mark-todo-in-toc' is set.
  2804. In HTML output, the TOC will be clickable.
  2805. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2806. or \"toc:3\"."
  2807. :group 'org-export-general
  2808. :type '(choice
  2809. (const :tag "No Table of Contents" nil)
  2810. (const :tag "Full Table of Contents" t)
  2811. (integer :tag "TOC to level")))
  2812. (defcustom org-export-mark-todo-in-toc nil
  2813. "Non-nil means, mark TOC lines that contain any open TODO items."
  2814. :group 'org-export-general
  2815. :type 'boolean)
  2816. (defcustom org-export-preserve-breaks nil
  2817. "Non-nil means, preserve all line breaks when exporting.
  2818. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2819. export, line breaks will always be preserved, regardless of this variable.
  2820. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2821. :group 'org-export-general
  2822. :type 'boolean)
  2823. (defcustom org-export-with-archived-trees 'headline
  2824. "Whether subtrees with the ARCHIVE tag should be exported.
  2825. This can have three different values
  2826. nil Do not export, pretend this tree is not present
  2827. t Do export the entire tree
  2828. headline Only export the headline, but skip the tree below it."
  2829. :group 'org-export-general
  2830. :group 'org-archive
  2831. :type '(choice
  2832. (const :tag "not at all" nil)
  2833. (const :tag "headline only" 'headline)
  2834. (const :tag "entirely" t)))
  2835. (defcustom org-export-author-info t
  2836. "Non-nil means, insert author name and email into the exported file.
  2837. This option can also be set with the +OPTIONS line,
  2838. e.g. \"author-info:nil\"."
  2839. :group 'org-export-general
  2840. :type 'boolean)
  2841. (defcustom org-export-time-stamp-file t
  2842. "Non-nil means, insert a time stamp into the exported file.
  2843. The time stamp shows when the file was created.
  2844. This option can also be set with the +OPTIONS line,
  2845. e.g. \"timestamp:nil\"."
  2846. :group 'org-export-general
  2847. :type 'boolean)
  2848. (defcustom org-export-with-timestamps t
  2849. "If nil, do not export time stamps and associated keywords."
  2850. :group 'org-export-general
  2851. :type 'boolean)
  2852. (defcustom org-export-remove-timestamps-from-toc t
  2853. "If nil, remove timestamps from the table of contents entries."
  2854. :group 'org-export-general
  2855. :type 'boolean)
  2856. (defcustom org-export-with-tags 'not-in-toc
  2857. "If nil, do not export tags, just remove them from headlines.
  2858. If this is the symbol `not-in-toc', tags will be removed from table of
  2859. contents entries, but still be shown in the headlines of the document.
  2860. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2861. :group 'org-export-general
  2862. :type '(choice
  2863. (const :tag "Off" nil)
  2864. (const :tag "Not in TOC" not-in-toc)
  2865. (const :tag "On" t)))
  2866. (defcustom org-export-with-drawers nil
  2867. "Non-nil means, export with drawers like the property drawer.
  2868. When t, all drawers are exported. This may also be a list of
  2869. drawer names to export."
  2870. :group 'org-export-general
  2871. :type '(choice
  2872. (const :tag "All drawers" t)
  2873. (const :tag "None" nil)
  2874. (repeat :tag "Selected drawers"
  2875. (string :tag "Drawer name"))))
  2876. (defgroup org-export-translation nil
  2877. "Options for translating special ascii sequences for the export backends."
  2878. :tag "Org Export Translation"
  2879. :group 'org-export)
  2880. (defcustom org-export-with-emphasize t
  2881. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2882. If the export target supports emphasizing text, the word will be
  2883. typeset in bold, italic, or underlined, respectively. Works only for
  2884. single words, but you can say: I *really* *mean* *this*.
  2885. Not all export backends support this.
  2886. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2887. :group 'org-export-translation
  2888. :type 'boolean)
  2889. (defcustom org-export-with-footnotes t
  2890. "If nil, export [1] as a footnote marker.
  2891. Lines starting with [1] will be formatted as footnotes.
  2892. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2893. :group 'org-export-translation
  2894. :type 'boolean)
  2895. (defcustom org-export-with-sub-superscripts t
  2896. "Non-nil means, interpret \"_\" and \"^\" for export.
  2897. When this option is turned on, you can use TeX-like syntax for sub- and
  2898. superscripts. Several characters after \"_\" or \"^\" will be
  2899. considered as a single item - so grouping with {} is normally not
  2900. needed. For example, the following things will be parsed as single
  2901. sub- or superscripts.
  2902. 10^24 or 10^tau several digits will be considered 1 item.
  2903. 10^-12 or 10^-tau a leading sign with digits or a word
  2904. x^2-y^3 will be read as x^2 - y^3, because items are
  2905. terminated by almost any nonword/nondigit char.
  2906. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2907. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2908. sub/superscript. If you set this variable to the symbol `{}',
  2909. the braces are *required* in order to trigger interpretations as
  2910. sub/superscript. This can be helpful in documents that need \"_\"
  2911. frequently in plain text.
  2912. Not all export backends support this, but HTML does.
  2913. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2914. :group 'org-export-translation
  2915. :type '(choice
  2916. (const :tag "Always interpret" t)
  2917. (const :tag "Only with braces" {})
  2918. (const :tag "Never interpret" nil)))
  2919. (defcustom org-export-with-special-strings t
  2920. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2921. When this option is turned on, these strings will be exported as:
  2922. \\- : &shy;
  2923. -- : &ndash;
  2924. --- : &mdash;
  2925. Not all export backends support this, but HTML does.
  2926. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2927. :group 'org-export-translation
  2928. :type 'boolean)
  2929. (defcustom org-export-with-TeX-macros t
  2930. "Non-nil means, interpret simple TeX-like macros when exporting.
  2931. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2932. No only real TeX macros will work here, but the standard HTML entities
  2933. for math can be used as macro names as well. For a list of supported
  2934. names in HTML export, see the constant `org-html-entities'.
  2935. Not all export backends support this.
  2936. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2937. :group 'org-export-translation
  2938. :group 'org-export-latex
  2939. :type 'boolean)
  2940. (defcustom org-export-with-LaTeX-fragments nil
  2941. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2942. When set, the exporter will find LaTeX environments if the \\begin line is
  2943. the first non-white thing on a line. It will also find the math delimiters
  2944. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2945. display math.
  2946. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2947. :group 'org-export-translation
  2948. :group 'org-export-latex
  2949. :type 'boolean)
  2950. (defcustom org-export-with-fixed-width t
  2951. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2952. This can be used to have pre-formatted text, fragments of code etc. For
  2953. example:
  2954. : ;; Some Lisp examples
  2955. : (while (defc cnt)
  2956. : (ding))
  2957. will be looking just like this in also HTML. See also the QUOTE keyword.
  2958. Not all export backends support this.
  2959. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2960. :group 'org-export-translation
  2961. :type 'boolean)
  2962. (defcustom org-match-sexp-depth 3
  2963. "Number of stacked braces for sub/superscript matching.
  2964. This has to be set before loading org.el to be effective."
  2965. :group 'org-export-translation
  2966. :type 'integer)
  2967. (defgroup org-export-tables nil
  2968. "Options for exporting tables in Org-mode."
  2969. :tag "Org Export Tables"
  2970. :group 'org-export)
  2971. (defcustom org-export-with-tables t
  2972. "If non-nil, lines starting with \"|\" define a table.
  2973. For example:
  2974. | Name | Address | Birthday |
  2975. |-------------+----------+-----------|
  2976. | Arthur Dent | England | 29.2.2100 |
  2977. Not all export backends support this.
  2978. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2979. :group 'org-export-tables
  2980. :type 'boolean)
  2981. (defcustom org-export-highlight-first-table-line t
  2982. "Non-nil means, highlight the first table line.
  2983. In HTML export, this means use <th> instead of <td>.
  2984. In tables created with table.el, this applies to the first table line.
  2985. In Org-mode tables, all lines before the first horizontal separator
  2986. line will be formatted with <th> tags."
  2987. :group 'org-export-tables
  2988. :type 'boolean)
  2989. (defcustom org-export-table-remove-special-lines t
  2990. "Remove special lines and marking characters in calculating tables.
  2991. This removes the special marking character column from tables that are set
  2992. up for spreadsheet calculations. It also removes the entire lines
  2993. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2994. the values of constants may be useful to have."
  2995. :group 'org-export-tables
  2996. :type 'boolean)
  2997. (defcustom org-export-prefer-native-exporter-for-tables nil
  2998. "Non-nil means, always export tables created with table.el natively.
  2999. Natively means, use the HTML code generator in table.el.
  3000. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  3001. the table does not use row- or column-spanning). This has the
  3002. advantage, that the automatic HTML conversions for math symbols and
  3003. sub/superscripts can be applied. Org-mode's HTML generator is also
  3004. much faster."
  3005. :group 'org-export-tables
  3006. :type 'boolean)
  3007. (defgroup org-export-ascii nil
  3008. "Options specific for ASCII export of Org-mode files."
  3009. :tag "Org Export ASCII"
  3010. :group 'org-export)
  3011. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  3012. "Characters for underlining headings in ASCII export.
  3013. In the given sequence, these characters will be used for level 1, 2, ..."
  3014. :group 'org-export-ascii
  3015. :type '(repeat character))
  3016. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3017. "Bullet characters for headlines converted to lists in ASCII export.
  3018. The first character is used for the first lest level generated in this
  3019. way, and so on. If there are more levels than characters given here,
  3020. the list will be repeated.
  3021. Note that plain lists will keep the same bullets as the have in the
  3022. Org-mode file."
  3023. :group 'org-export-ascii
  3024. :type '(repeat character))
  3025. (defgroup org-export-xml nil
  3026. "Options specific for XML export of Org-mode files."
  3027. :tag "Org Export XML"
  3028. :group 'org-export)
  3029. (defgroup org-export-html nil
  3030. "Options specific for HTML export of Org-mode files."
  3031. :tag "Org Export HTML"
  3032. :group 'org-export)
  3033. (defcustom org-export-html-coding-system nil
  3034. ""
  3035. :group 'org-export-html
  3036. :type 'coding-system)
  3037. (defcustom org-export-html-extension "html"
  3038. "The extension for exported HTML files."
  3039. :group 'org-export-html
  3040. :type 'string)
  3041. (defcustom org-export-html-style
  3042. "<style type=\"text/css\">
  3043. html {
  3044. font-family: Times, serif;
  3045. font-size: 12pt;
  3046. }
  3047. .title { text-align: center; }
  3048. .todo { color: red; }
  3049. .done { color: green; }
  3050. .timestamp { color: grey }
  3051. .timestamp-kwd { color: CadetBlue }
  3052. .tag { background-color:lightblue; font-weight:normal }
  3053. .target { background-color: lavender; }
  3054. pre {
  3055. border: 1pt solid #AEBDCC;
  3056. background-color: #F3F5F7;
  3057. padding: 5pt;
  3058. font-family: courier, monospace;
  3059. }
  3060. table { border-collapse: collapse; }
  3061. td, th {
  3062. vertical-align: top;
  3063. <!--border: 1pt solid #ADB9CC;-->
  3064. }
  3065. </style>"
  3066. "The default style specification for exported HTML files.
  3067. Since there are different ways of setting style information, this variable
  3068. needs to contain the full HTML structure to provide a style, including the
  3069. surrounding HTML tags. The style specifications should include definitions
  3070. for new classes todo, done, title, and deadline. For example, legal values
  3071. would be:
  3072. <style type=\"text/css\">
  3073. p { font-weight: normal; color: gray; }
  3074. h1 { color: black; }
  3075. .title { text-align: center; }
  3076. .todo, .deadline { color: red; }
  3077. .done { color: green; }
  3078. </style>
  3079. or, if you want to keep the style in a file,
  3080. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3081. As the value of this option simply gets inserted into the HTML <head> header,
  3082. you can \"misuse\" it to add arbitrary text to the header."
  3083. :group 'org-export-html
  3084. :type 'string)
  3085. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3086. "Format for typesetting the document title in HTML export."
  3087. :group 'org-export-html
  3088. :type 'string)
  3089. (defcustom org-export-html-toplevel-hlevel 2
  3090. "The <H> level for level 1 headings in HTML export."
  3091. :group 'org-export-html
  3092. :type 'string)
  3093. (defcustom org-export-html-link-org-files-as-html t
  3094. "Non-nil means, make file links to `file.org' point to `file.html'.
  3095. When org-mode is exporting an org-mode file to HTML, links to
  3096. non-html files are directly put into a href tag in HTML.
  3097. However, links to other Org-mode files (recognized by the
  3098. extension `.org.) should become links to the corresponding html
  3099. file, assuming that the linked org-mode file will also be
  3100. converted to HTML.
  3101. When nil, the links still point to the plain `.org' file."
  3102. :group 'org-export-html
  3103. :type 'boolean)
  3104. (defcustom org-export-html-inline-images 'maybe
  3105. "Non-nil means, inline images into exported HTML pages.
  3106. This is done using an <img> tag. When nil, an anchor with href is used to
  3107. link to the image. If this option is `maybe', then images in links with
  3108. an empty description will be inlined, while images with a description will
  3109. be linked only."
  3110. :group 'org-export-html
  3111. :type '(choice (const :tag "Never" nil)
  3112. (const :tag "Always" t)
  3113. (const :tag "When there is no description" maybe)))
  3114. ;; FIXME: rename
  3115. (defcustom org-export-html-expand t
  3116. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3117. When nil, these tags will be exported as plain text and therefore
  3118. not be interpreted by a browser.
  3119. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3120. :group 'org-export-html
  3121. :type 'boolean)
  3122. (defcustom org-export-html-table-tag
  3123. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3124. "The HTML tag that is used to start a table.
  3125. This must be a <table> tag, but you may change the options like
  3126. borders and spacing."
  3127. :group 'org-export-html
  3128. :type 'string)
  3129. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3130. "The opening tag for table header fields.
  3131. This is customizable so that alignment options can be specified."
  3132. :group 'org-export-tables
  3133. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3134. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3135. "The opening tag for table data fields.
  3136. This is customizable so that alignment options can be specified."
  3137. :group 'org-export-tables
  3138. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3139. (defcustom org-export-html-with-timestamp nil
  3140. "If non-nil, write `org-export-html-html-helper-timestamp'
  3141. into the exported HTML text. Otherwise, the buffer will just be saved
  3142. to a file."
  3143. :group 'org-export-html
  3144. :type 'boolean)
  3145. (defcustom org-export-html-html-helper-timestamp
  3146. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3147. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3148. :group 'org-export-html
  3149. :type 'string)
  3150. (defgroup org-export-icalendar nil
  3151. "Options specific for iCalendar export of Org-mode files."
  3152. :tag "Org Export iCalendar"
  3153. :group 'org-export)
  3154. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3155. "The file name for the iCalendar file covering all agenda files.
  3156. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3157. The file name should be absolute, the file will be overwritten without warning."
  3158. :group 'org-export-icalendar
  3159. :type 'file)
  3160. (defcustom org-icalendar-include-todo nil
  3161. "Non-nil means, export to iCalendar files should also cover TODO items."
  3162. :group 'org-export-icalendar
  3163. :type '(choice
  3164. (const :tag "None" nil)
  3165. (const :tag "Unfinished" t)
  3166. (const :tag "All" all)))
  3167. (defcustom org-icalendar-include-sexps t
  3168. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3169. These are entries like in the diary, but directly in an Org-mode file."
  3170. :group 'org-export-icalendar
  3171. :type 'boolean)
  3172. (defcustom org-icalendar-include-body 100
  3173. "Amount of text below headline to be included in iCalendar export.
  3174. This is a number of characters that should maximally be included.
  3175. Properties, scheduling and clocking lines will always be removed.
  3176. The text will be inserted into the DESCRIPTION field."
  3177. :group 'org-export-icalendar
  3178. :type '(choice
  3179. (const :tag "Nothing" nil)
  3180. (const :tag "Everything" t)
  3181. (integer :tag "Max characters")))
  3182. (defcustom org-icalendar-combined-name "OrgMode"
  3183. "Calendar name for the combined iCalendar representing all agenda files."
  3184. :group 'org-export-icalendar
  3185. :type 'string)
  3186. (defgroup org-font-lock nil
  3187. "Font-lock settings for highlighting in Org-mode."
  3188. :tag "Org Font Lock"
  3189. :group 'org)
  3190. (defcustom org-level-color-stars-only nil
  3191. "Non-nil means fontify only the stars in each headline.
  3192. When nil, the entire headline is fontified.
  3193. Changing it requires restart of `font-lock-mode' to become effective
  3194. also in regions already fontified."
  3195. :group 'org-font-lock
  3196. :type 'boolean)
  3197. (defcustom org-hide-leading-stars nil
  3198. "Non-nil means, hide the first N-1 stars in a headline.
  3199. This works by using the face `org-hide' for these stars. This
  3200. face is white for a light background, and black for a dark
  3201. background. You may have to customize the face `org-hide' to
  3202. make this work.
  3203. Changing it requires restart of `font-lock-mode' to become effective
  3204. also in regions already fontified.
  3205. You may also set this on a per-file basis by adding one of the following
  3206. lines to the buffer:
  3207. #+STARTUP: hidestars
  3208. #+STARTUP: showstars"
  3209. :group 'org-font-lock
  3210. :type 'boolean)
  3211. (defcustom org-fontify-done-headline nil
  3212. "Non-nil means, change the face of a headline if it is marked DONE.
  3213. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3214. When this is non-nil, the headline after the keyword is set to the
  3215. `org-headline-done' as an additional indication."
  3216. :group 'org-font-lock
  3217. :type 'boolean)
  3218. (defcustom org-fontify-emphasized-text t
  3219. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3220. Changing this variable requires a restart of Emacs to take effect."
  3221. :group 'org-font-lock
  3222. :type 'boolean)
  3223. (defcustom org-highlight-latex-fragments-and-specials nil
  3224. "Non-nil means, fontify what is treated specially by the exporters."
  3225. :group 'org-font-lock
  3226. :type 'boolean)
  3227. (defcustom org-hide-emphasis-markers nil
  3228. "Non-nil mean font-lock should hide the emphasis marker characters."
  3229. :group 'org-font-lock
  3230. :type 'boolean)
  3231. (defvar org-emph-re nil
  3232. "Regular expression for matching emphasis.")
  3233. (defvar org-verbatim-re nil
  3234. "Regular expression for matching verbatim text.")
  3235. (defvar org-emphasis-regexp-components) ; defined just below
  3236. (defvar org-emphasis-alist) ; defined just below
  3237. (defun org-set-emph-re (var val)
  3238. "Set variable and compute the emphasis regular expression."
  3239. (set var val)
  3240. (when (and (boundp 'org-emphasis-alist)
  3241. (boundp 'org-emphasis-regexp-components)
  3242. org-emphasis-alist org-emphasis-regexp-components)
  3243. (let* ((e org-emphasis-regexp-components)
  3244. (pre (car e))
  3245. (post (nth 1 e))
  3246. (border (nth 2 e))
  3247. (body (nth 3 e))
  3248. (nl (nth 4 e))
  3249. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3250. (body1 (concat body "*?"))
  3251. (markers (mapconcat 'car org-emphasis-alist ""))
  3252. (vmarkers (mapconcat
  3253. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3254. org-emphasis-alist "")))
  3255. ;; make sure special characters appear at the right position in the class
  3256. (if (string-match "\\^" markers)
  3257. (setq markers (concat (replace-match "" t t markers) "^")))
  3258. (if (string-match "-" markers)
  3259. (setq markers (concat (replace-match "" t t markers) "-")))
  3260. (if (string-match "\\^" vmarkers)
  3261. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3262. (if (string-match "-" vmarkers)
  3263. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3264. (if (> nl 0)
  3265. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3266. (int-to-string nl) "\\}")))
  3267. ;; Make the regexp
  3268. (setq org-emph-re
  3269. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3270. "\\("
  3271. "\\([" markers "]\\)"
  3272. "\\("
  3273. "[^" border "]\\|"
  3274. "[^" border (if (and nil stacked) markers) "]"
  3275. body1
  3276. "[^" border (if (and nil stacked) markers) "]"
  3277. "\\)"
  3278. "\\3\\)"
  3279. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3280. (setq org-verbatim-re
  3281. (concat "\\([" pre "]\\|^\\)"
  3282. "\\("
  3283. "\\([" vmarkers "]\\)"
  3284. "\\("
  3285. "[^" border "]\\|"
  3286. "[^" border "]"
  3287. body1
  3288. "[^" border "]"
  3289. "\\)"
  3290. "\\3\\)"
  3291. "\\([" post "]\\|$\\)")))))
  3292. (defcustom org-emphasis-regexp-components
  3293. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3294. "Components used to build the regular expression for emphasis.
  3295. This is a list with 6 entries. Terminology: In an emphasis string
  3296. like \" *strong word* \", we call the initial space PREMATCH, the final
  3297. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3298. and \"trong wor\" is the body. The different components in this variable
  3299. specify what is allowed/forbidden in each part:
  3300. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3301. post Chars allowed as postmatch. End of line will be allowed too.
  3302. border The chars *forbidden* as border characters.
  3303. body-regexp A regexp like \".\" to match a body character. Don't use
  3304. non-shy groups here, and don't allow newline here.
  3305. newline The maximum number of newlines allowed in an emphasis exp.
  3306. Use customize to modify this, or restart Emacs after changing it."
  3307. :group 'org-font-lock
  3308. :set 'org-set-emph-re
  3309. :type '(list
  3310. (sexp :tag "Allowed chars in pre ")
  3311. (sexp :tag "Allowed chars in post ")
  3312. (sexp :tag "Forbidden chars in border ")
  3313. (sexp :tag "Regexp for body ")
  3314. (integer :tag "number of newlines allowed")
  3315. (option (boolean :tag "Stacking (DISABLED) "))))
  3316. (defcustom org-emphasis-alist
  3317. '(("*" bold "<b>" "</b>")
  3318. ("/" italic "<i>" "</i>")
  3319. ("_" underline "<u>" "</u>")
  3320. ("=" org-code "<code>" "</code>" verbatim)
  3321. ("~" org-verbatim "" "" verbatim)
  3322. ("+" (:strike-through t) "<del>" "</del>")
  3323. )
  3324. "Special syntax for emphasized text.
  3325. Text starting and ending with a special character will be emphasized, for
  3326. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3327. characters, the face to be used by font-lock for highlighting in Org-mode
  3328. Emacs buffers, and the HTML tags to be used for this.
  3329. Use customize to modify this, or restart Emacs after changing it."
  3330. :group 'org-font-lock
  3331. :set 'org-set-emph-re
  3332. :type '(repeat
  3333. (list
  3334. (string :tag "Marker character")
  3335. (choice
  3336. (face :tag "Font-lock-face")
  3337. (plist :tag "Face property list"))
  3338. (string :tag "HTML start tag")
  3339. (string :tag "HTML end tag")
  3340. (option (const verbatim)))))
  3341. ;;; The faces
  3342. (defgroup org-faces nil
  3343. "Faces in Org-mode."
  3344. :tag "Org Faces"
  3345. :group 'org-font-lock)
  3346. (defun org-compatible-face (inherits specs)
  3347. "Make a compatible face specification.
  3348. If INHERITS is an existing face and if the Emacs version supports it,
  3349. just inherit the face. If not, use SPECS to define the face.
  3350. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3351. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3352. to the top of the list. The `min-colors' attribute will be removed from
  3353. any other entries, and any resulting duplicates will be removed entirely."
  3354. (cond
  3355. ((and inherits (facep inherits)
  3356. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3357. ;; In Emacs 23, we use inheritance where possible.
  3358. ;; We only do this in Emacs 23, because only there the outline
  3359. ;; faces have been changed to the original org-mode-level-faces.
  3360. (list (list t :inherit inherits)))
  3361. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3362. ;; These do not understand the `min-colors' attribute.
  3363. (let (r e a)
  3364. (while (setq e (pop specs))
  3365. (cond
  3366. ((memq (car e) '(t default)) (push e r))
  3367. ((setq a (member '(min-colors 8) (car e)))
  3368. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3369. (cdr e)))))
  3370. ((setq a (assq 'min-colors (car e)))
  3371. (setq e (cons (delq a (car e)) (cdr e)))
  3372. (or (assoc (car e) r) (push e r)))
  3373. (t (or (assoc (car e) r) (push e r)))))
  3374. (nreverse r)))
  3375. (t specs)))
  3376. (put 'org-compatible-face 'lisp-indent-function 1)
  3377. (defface org-hide
  3378. '((((background light)) (:foreground "white"))
  3379. (((background dark)) (:foreground "black")))
  3380. "Face used to hide leading stars in headlines.
  3381. The forground color of this face should be equal to the background
  3382. color of the frame."
  3383. :group 'org-faces)
  3384. (defface org-level-1 ;; font-lock-function-name-face
  3385. (org-compatible-face 'outline-1
  3386. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3387. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3388. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3389. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3390. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3391. (t (:bold t))))
  3392. "Face used for level 1 headlines."
  3393. :group 'org-faces)
  3394. (defface org-level-2 ;; font-lock-variable-name-face
  3395. (org-compatible-face 'outline-2
  3396. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3397. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3398. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3399. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3400. (t (:bold t))))
  3401. "Face used for level 2 headlines."
  3402. :group 'org-faces)
  3403. (defface org-level-3 ;; font-lock-keyword-face
  3404. (org-compatible-face 'outline-3
  3405. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3406. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3407. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3408. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3409. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3410. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3411. (t (:bold t))))
  3412. "Face used for level 3 headlines."
  3413. :group 'org-faces)
  3414. (defface org-level-4 ;; font-lock-comment-face
  3415. (org-compatible-face 'outline-4
  3416. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3417. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3418. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3419. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3420. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3421. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3422. (t (:bold t))))
  3423. "Face used for level 4 headlines."
  3424. :group 'org-faces)
  3425. (defface org-level-5 ;; font-lock-type-face
  3426. (org-compatible-face 'outline-5
  3427. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3428. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3429. (((class color) (min-colors 8)) (:foreground "green"))))
  3430. "Face used for level 5 headlines."
  3431. :group 'org-faces)
  3432. (defface org-level-6 ;; font-lock-constant-face
  3433. (org-compatible-face 'outline-6
  3434. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3435. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3436. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3437. "Face used for level 6 headlines."
  3438. :group 'org-faces)
  3439. (defface org-level-7 ;; font-lock-builtin-face
  3440. (org-compatible-face 'outline-7
  3441. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3442. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3443. (((class color) (min-colors 8)) (:foreground "blue"))))
  3444. "Face used for level 7 headlines."
  3445. :group 'org-faces)
  3446. (defface org-level-8 ;; font-lock-string-face
  3447. (org-compatible-face 'outline-8
  3448. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3449. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3450. (((class color) (min-colors 8)) (:foreground "green"))))
  3451. "Face used for level 8 headlines."
  3452. :group 'org-faces)
  3453. (defface org-special-keyword ;; font-lock-string-face
  3454. (org-compatible-face nil
  3455. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3456. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3457. (t (:italic t))))
  3458. "Face used for special keywords."
  3459. :group 'org-faces)
  3460. (defface org-drawer ;; font-lock-function-name-face
  3461. (org-compatible-face nil
  3462. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3463. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3464. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3465. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3466. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3467. (t (:bold t))))
  3468. "Face used for drawers."
  3469. :group 'org-faces)
  3470. (defface org-property-value nil
  3471. "Face used for the value of a property."
  3472. :group 'org-faces)
  3473. (defface org-column
  3474. (org-compatible-face nil
  3475. '((((class color) (min-colors 16) (background light))
  3476. (:background "grey90"))
  3477. (((class color) (min-colors 16) (background dark))
  3478. (:background "grey30"))
  3479. (((class color) (min-colors 8))
  3480. (:background "cyan" :foreground "black"))
  3481. (t (:inverse-video t))))
  3482. "Face for column display of entry properties."
  3483. :group 'org-faces)
  3484. (when (fboundp 'set-face-attribute)
  3485. ;; Make sure that a fixed-width face is used when we have a column table.
  3486. (set-face-attribute 'org-column nil
  3487. :height (face-attribute 'default :height)
  3488. :family (face-attribute 'default :family)))
  3489. (defface org-warning
  3490. (org-compatible-face 'font-lock-warning-face
  3491. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3492. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3493. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3494. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3495. (t (:bold t))))
  3496. "Face for deadlines and TODO keywords."
  3497. :group 'org-faces)
  3498. (defface org-archived ; similar to shadow
  3499. (org-compatible-face 'shadow
  3500. '((((class color grayscale) (min-colors 88) (background light))
  3501. (:foreground "grey50"))
  3502. (((class color grayscale) (min-colors 88) (background dark))
  3503. (:foreground "grey70"))
  3504. (((class color) (min-colors 8) (background light))
  3505. (:foreground "green"))
  3506. (((class color) (min-colors 8) (background dark))
  3507. (:foreground "yellow"))))
  3508. "Face for headline with the ARCHIVE tag."
  3509. :group 'org-faces)
  3510. (defface org-link
  3511. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3512. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3513. (t (:underline t)))
  3514. "Face for links."
  3515. :group 'org-faces)
  3516. (defface org-ellipsis
  3517. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3518. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3519. (t (:strike-through t)))
  3520. "Face for the ellipsis in folded text."
  3521. :group 'org-faces)
  3522. (defface org-target
  3523. '((((class color) (background light)) (:underline t))
  3524. (((class color) (background dark)) (:underline t))
  3525. (t (:underline t)))
  3526. "Face for links."
  3527. :group 'org-faces)
  3528. (defface org-date
  3529. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3530. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3531. (t (:underline t)))
  3532. "Face for links."
  3533. :group 'org-faces)
  3534. (defface org-sexp-date
  3535. '((((class color) (background light)) (:foreground "Purple"))
  3536. (((class color) (background dark)) (:foreground "Cyan"))
  3537. (t (:underline t)))
  3538. "Face for links."
  3539. :group 'org-faces)
  3540. (defface org-tag
  3541. '((t (:bold t)))
  3542. "Face for tags."
  3543. :group 'org-faces)
  3544. (defface org-todo ; font-lock-warning-face
  3545. (org-compatible-face nil
  3546. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3547. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3548. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3549. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3550. (t (:inverse-video t :bold t))))
  3551. "Face for TODO keywords."
  3552. :group 'org-faces)
  3553. (defface org-done ;; font-lock-type-face
  3554. (org-compatible-face nil
  3555. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3556. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3557. (((class color) (min-colors 8)) (:foreground "green"))
  3558. (t (:bold t))))
  3559. "Face used for todo keywords that indicate DONE items."
  3560. :group 'org-faces)
  3561. (defface org-headline-done ;; font-lock-string-face
  3562. (org-compatible-face nil
  3563. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3564. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3565. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3566. "Face used to indicate that a headline is DONE.
  3567. This face is only used if `org-fontify-done-headline' is set. If applies
  3568. to the part of the headline after the DONE keyword."
  3569. :group 'org-faces)
  3570. (defcustom org-todo-keyword-faces nil
  3571. "Faces for specific TODO keywords.
  3572. This is a list of cons cells, with TODO keywords in the car
  3573. and faces in the cdr. The face can be a symbol, or a property
  3574. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3575. :group 'org-faces
  3576. :group 'org-todo
  3577. :type '(repeat
  3578. (cons
  3579. (string :tag "keyword")
  3580. (sexp :tag "face"))))
  3581. (defface org-table ;; font-lock-function-name-face
  3582. (org-compatible-face nil
  3583. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3584. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3585. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3586. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3587. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3588. (((class color) (min-colors 8) (background dark)))))
  3589. "Face used for tables."
  3590. :group 'org-faces)
  3591. (defface org-formula
  3592. (org-compatible-face nil
  3593. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3594. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3595. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3596. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3597. (t (:bold t :italic t))))
  3598. "Face for formulas."
  3599. :group 'org-faces)
  3600. (defface org-code
  3601. (org-compatible-face nil
  3602. '((((class color grayscale) (min-colors 88) (background light))
  3603. (:foreground "grey50"))
  3604. (((class color grayscale) (min-colors 88) (background dark))
  3605. (:foreground "grey70"))
  3606. (((class color) (min-colors 8) (background light))
  3607. (:foreground "green"))
  3608. (((class color) (min-colors 8) (background dark))
  3609. (:foreground "yellow"))))
  3610. "Face for fixed-with text like code snippets."
  3611. :group 'org-faces
  3612. :version "22.1")
  3613. (defface org-verbatim
  3614. (org-compatible-face nil
  3615. '((((class color grayscale) (min-colors 88) (background light))
  3616. (:foreground "grey50" :underline t))
  3617. (((class color grayscale) (min-colors 88) (background dark))
  3618. (:foreground "grey70" :underline t))
  3619. (((class color) (min-colors 8) (background light))
  3620. (:foreground "green" :underline t))
  3621. (((class color) (min-colors 8) (background dark))
  3622. (:foreground "yellow" :underline t))))
  3623. "Face for fixed-with text like code snippets."
  3624. :group 'org-faces
  3625. :version "22.1")
  3626. (defface org-agenda-structure ;; font-lock-function-name-face
  3627. (org-compatible-face nil
  3628. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3629. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3630. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3631. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3632. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3633. (t (:bold t))))
  3634. "Face used in agenda for captions and dates."
  3635. :group 'org-faces)
  3636. (defface org-scheduled-today
  3637. (org-compatible-face nil
  3638. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3639. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3640. (((class color) (min-colors 8)) (:foreground "green"))
  3641. (t (:bold t :italic t))))
  3642. "Face for items scheduled for a certain day."
  3643. :group 'org-faces)
  3644. (defface org-scheduled-previously
  3645. (org-compatible-face nil
  3646. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3647. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3648. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3649. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3650. (t (:bold t))))
  3651. "Face for items scheduled previously, and not yet done."
  3652. :group 'org-faces)
  3653. (defface org-upcoming-deadline
  3654. (org-compatible-face nil
  3655. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3656. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3657. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3658. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3659. (t (:bold t))))
  3660. "Face for items scheduled previously, and not yet done."
  3661. :group 'org-faces)
  3662. (defcustom org-agenda-deadline-faces
  3663. '((1.0 . org-warning)
  3664. (0.5 . org-upcoming-deadline)
  3665. (0.0 . default))
  3666. "Faces for showing deadlines in the agenda.
  3667. This is a list of cons cells. The cdr of each cell is a face to be used,
  3668. and it can also just be like '(:foreground \"yellow\").
  3669. Each car is a fraction of the head-warning time that must have passed for
  3670. this the face in the cdr to be used for display. The numbers must be
  3671. given in descending order. The head-warning time is normally taken
  3672. from `org-deadline-warning-days', but can also be specified in the deadline
  3673. timestamp itself, like this:
  3674. DEADLINE: <2007-08-13 Mon -8d>
  3675. You may use d for days, w for weeks, m for months and y for years. Months
  3676. and years will only be treated in an approximate fashion (30.4 days for a
  3677. month and 365.24 days for a year)."
  3678. :group 'org-faces
  3679. :group 'org-agenda-daily/weekly
  3680. :type '(repeat
  3681. (cons
  3682. (number :tag "Fraction of head-warning time passed")
  3683. (sexp :tag "Face"))))
  3684. ;; FIXME: this is not a good face yet.
  3685. (defface org-agenda-restriction-lock
  3686. (org-compatible-face nil
  3687. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3688. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3689. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3690. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3691. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3692. (t (:inverse-video t))))
  3693. "Face for showing the agenda restriction lock."
  3694. :group 'org-faces)
  3695. (defface org-time-grid ;; font-lock-variable-name-face
  3696. (org-compatible-face nil
  3697. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3698. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3699. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3700. "Face used for time grids."
  3701. :group 'org-faces)
  3702. (defconst org-level-faces
  3703. '(org-level-1 org-level-2 org-level-3 org-level-4
  3704. org-level-5 org-level-6 org-level-7 org-level-8
  3705. ))
  3706. (defcustom org-n-level-faces (length org-level-faces)
  3707. "The number of different faces to be used for headlines.
  3708. Org-mode defines 8 different headline faces, so this can be at most 8.
  3709. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3710. :type 'number
  3711. :group 'org-faces)
  3712. ;;; Functions and variables from ther packages
  3713. ;; Declared here to avoid compiler warnings
  3714. (eval-and-compile
  3715. (unless (fboundp 'declare-function)
  3716. (defmacro declare-function (fn file &optional arglist fileonly))))
  3717. ;; XEmacs only
  3718. (defvar outline-mode-menu-heading)
  3719. (defvar outline-mode-menu-show)
  3720. (defvar outline-mode-menu-hide)
  3721. (defvar zmacs-regions) ; XEmacs regions
  3722. ;; Emacs only
  3723. (defvar mark-active)
  3724. ;; Various packages
  3725. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3726. (declare-function add-to-diary-list "diary-lib"
  3727. (date string specifier &optional marker globcolor literal))
  3728. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3729. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3730. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3731. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3732. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3733. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3734. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3735. (declare-function bbdb-record-name "ext:bbdb" (record))
  3736. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3737. (declare-function bibtex-generate-autokey "bibtex" ())
  3738. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3739. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3740. (defvar calc-embedded-close-formula)
  3741. (defvar calc-embedded-open-formula)
  3742. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3743. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3744. (declare-function calendar-check-holidays "holidays" (date))
  3745. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3746. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3747. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3748. (declare-function calendar-forward-day "cal-move" (arg))
  3749. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3750. (declare-function calendar-goto-date "cal-move" (date))
  3751. (declare-function calendar-goto-today "cal-move" ())
  3752. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3753. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3754. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3755. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3756. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3757. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3758. (defvar calendar-mode-map)
  3759. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3760. (declare-function cdlatex-tab "ext:cdlatex" ())
  3761. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3762. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3763. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3764. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3765. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3766. (defvar font-lock-unfontify-region-function)
  3767. (declare-function gnus-article-show-summary "gnus-art" ())
  3768. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3769. (defvar gnus-other-frame-object)
  3770. (defvar gnus-group-name)
  3771. (defvar gnus-article-current)
  3772. (defvar Info-current-file)
  3773. (defvar Info-current-node)
  3774. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3775. (declare-function mh-find-path "mh-utils" ())
  3776. (declare-function mh-get-header-field "mh-utils" (field))
  3777. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3778. (declare-function mh-header-display "mh-show" ())
  3779. (declare-function mh-index-previous-folder "mh-search" ())
  3780. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3781. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3782. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3783. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3784. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3785. (declare-function mh-show-header-display "mh-show" t t)
  3786. (declare-function mh-show-msg "mh-show" (msg))
  3787. (declare-function mh-show-show "mh-show" t t)
  3788. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3789. (defvar mh-progs)
  3790. (defvar mh-current-folder)
  3791. (defvar mh-show-folder-buffer)
  3792. (defvar mh-index-folder)
  3793. (defvar mh-searcher)
  3794. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3795. (declare-function parse-time-string "parse-time" (string))
  3796. (declare-function remember "remember" (&optional initial))
  3797. (declare-function remember-buffer-desc "remember" ())
  3798. (declare-function remember-finalize "remember" ())
  3799. (defvar remember-save-after-remembering)
  3800. (defvar remember-data-file)
  3801. (defvar remember-register)
  3802. (defvar remember-buffer)
  3803. (defvar remember-handler-functions)
  3804. (defvar remember-annotation-functions)
  3805. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3806. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3807. (declare-function rmail-what-message "rmail" ())
  3808. (defvar rmail-current-message)
  3809. (defvar texmathp-why)
  3810. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3811. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3812. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3813. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3814. (declare-function vm-isearch-update "ext:vm-search" ())
  3815. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3816. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3817. (declare-function vm-su-subject "ext:vm-summary" (m))
  3818. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3819. (defvar vm-message-pointer)
  3820. (defvar vm-folder-directory)
  3821. (defvar w3m-current-url)
  3822. (defvar w3m-current-title)
  3823. ;; backward compatibility to old version of wl
  3824. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3825. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3826. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3827. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3828. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3829. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3830. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3831. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3832. (defvar wl-summary-buffer-elmo-folder)
  3833. (defvar wl-summary-buffer-folder-name)
  3834. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3835. (defvar org-latex-regexps)
  3836. (defvar constants-unit-system)
  3837. ;;; Variables for pre-computed regular expressions, all buffer local
  3838. (defvar org-drawer-regexp nil
  3839. "Matches first line of a hidden block.")
  3840. (make-variable-buffer-local 'org-drawer-regexp)
  3841. (defvar org-todo-regexp nil
  3842. "Matches any of the TODO state keywords.")
  3843. (make-variable-buffer-local 'org-todo-regexp)
  3844. (defvar org-not-done-regexp nil
  3845. "Matches any of the TODO state keywords except the last one.")
  3846. (make-variable-buffer-local 'org-not-done-regexp)
  3847. (defvar org-todo-line-regexp nil
  3848. "Matches a headline and puts TODO state into group 2 if present.")
  3849. (make-variable-buffer-local 'org-todo-line-regexp)
  3850. (defvar org-complex-heading-regexp nil
  3851. "Matches a headline and puts everything into groups:
  3852. group 1: the stars
  3853. group 2: The todo keyword, maybe
  3854. group 3: Priority cookie
  3855. group 4: True headline
  3856. group 5: Tags")
  3857. (make-variable-buffer-local 'org-complex-heading-regexp)
  3858. (defvar org-todo-line-tags-regexp nil
  3859. "Matches a headline and puts TODO state into group 2 if present.
  3860. Also put tags into group 4 if tags are present.")
  3861. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3862. (defvar org-nl-done-regexp nil
  3863. "Matches newline followed by a headline with the DONE keyword.")
  3864. (make-variable-buffer-local 'org-nl-done-regexp)
  3865. (defvar org-looking-at-done-regexp nil
  3866. "Matches the DONE keyword a point.")
  3867. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3868. (defvar org-ds-keyword-length 12
  3869. "Maximum length of the Deadline and SCHEDULED keywords.")
  3870. (make-variable-buffer-local 'org-ds-keyword-length)
  3871. (defvar org-deadline-regexp nil
  3872. "Matches the DEADLINE keyword.")
  3873. (make-variable-buffer-local 'org-deadline-regexp)
  3874. (defvar org-deadline-time-regexp nil
  3875. "Matches the DEADLINE keyword together with a time stamp.")
  3876. (make-variable-buffer-local 'org-deadline-time-regexp)
  3877. (defvar org-deadline-line-regexp nil
  3878. "Matches the DEADLINE keyword and the rest of the line.")
  3879. (make-variable-buffer-local 'org-deadline-line-regexp)
  3880. (defvar org-scheduled-regexp nil
  3881. "Matches the SCHEDULED keyword.")
  3882. (make-variable-buffer-local 'org-scheduled-regexp)
  3883. (defvar org-scheduled-time-regexp nil
  3884. "Matches the SCHEDULED keyword together with a time stamp.")
  3885. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3886. (defvar org-closed-time-regexp nil
  3887. "Matches the CLOSED keyword together with a time stamp.")
  3888. (make-variable-buffer-local 'org-closed-time-regexp)
  3889. (defvar org-keyword-time-regexp nil
  3890. "Matches any of the 4 keywords, together with the time stamp.")
  3891. (make-variable-buffer-local 'org-keyword-time-regexp)
  3892. (defvar org-keyword-time-not-clock-regexp nil
  3893. "Matches any of the 3 keywords, together with the time stamp.")
  3894. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3895. (defvar org-maybe-keyword-time-regexp nil
  3896. "Matches a timestamp, possibly preceeded by a keyword.")
  3897. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3898. (defvar org-planning-or-clock-line-re nil
  3899. "Matches a line with planning or clock info.")
  3900. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3901. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3902. rear-nonsticky t mouse-map t fontified t)
  3903. "Properties to remove when a string without properties is wanted.")
  3904. (defsubst org-match-string-no-properties (num &optional string)
  3905. (if (featurep 'xemacs)
  3906. (let ((s (match-string num string)))
  3907. (remove-text-properties 0 (length s) org-rm-props s)
  3908. s)
  3909. (match-string-no-properties num string)))
  3910. (defsubst org-no-properties (s)
  3911. (if (fboundp 'set-text-properties)
  3912. (set-text-properties 0 (length s) nil s)
  3913. (remove-text-properties 0 (length s) org-rm-props s))
  3914. s)
  3915. (defsubst org-get-alist-option (option key)
  3916. (cond ((eq key t) t)
  3917. ((eq option t) t)
  3918. ((assoc key option) (cdr (assoc key option)))
  3919. (t (cdr (assq 'default option)))))
  3920. (defsubst org-inhibit-invisibility ()
  3921. "Modified `buffer-invisibility-spec' for Emacs 21.
  3922. Some ops with invisible text do not work correctly on Emacs 21. For these
  3923. we turn off invisibility temporarily. Use this in a `let' form."
  3924. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3925. (defsubst org-set-local (var value)
  3926. "Make VAR local in current buffer and set it to VALUE."
  3927. (set (make-variable-buffer-local var) value))
  3928. (defsubst org-mode-p ()
  3929. "Check if the current buffer is in Org-mode."
  3930. (eq major-mode 'org-mode))
  3931. (defsubst org-last (list)
  3932. "Return the last element of LIST."
  3933. (car (last list)))
  3934. (defun org-let (list &rest body)
  3935. (eval (cons 'let (cons list body))))
  3936. (put 'org-let 'lisp-indent-function 1)
  3937. (defun org-let2 (list1 list2 &rest body)
  3938. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3939. (put 'org-let2 'lisp-indent-function 2)
  3940. (defconst org-startup-options
  3941. '(("fold" org-startup-folded t)
  3942. ("overview" org-startup-folded t)
  3943. ("nofold" org-startup-folded nil)
  3944. ("showall" org-startup-folded nil)
  3945. ("content" org-startup-folded content)
  3946. ("hidestars" org-hide-leading-stars t)
  3947. ("showstars" org-hide-leading-stars nil)
  3948. ("odd" org-odd-levels-only t)
  3949. ("oddeven" org-odd-levels-only nil)
  3950. ("align" org-startup-align-all-tables t)
  3951. ("noalign" org-startup-align-all-tables nil)
  3952. ("customtime" org-display-custom-times t)
  3953. ("logdone" org-log-done time)
  3954. ("lognotedone" org-log-done note)
  3955. ("nologdone" org-log-done nil)
  3956. ("lognoteclock-out" org-log-note-clock-out t)
  3957. ("nolognoteclock-out" org-log-note-clock-out nil)
  3958. ("logrepeat" org-log-repeat state)
  3959. ("lognoterepeat" org-log-repeat note)
  3960. ("nologrepeat" org-log-repeat nil)
  3961. ("constcgs" constants-unit-system cgs)
  3962. ("constSI" constants-unit-system SI))
  3963. "Variable associated with STARTUP options for org-mode.
  3964. Each element is a list of three items: The startup options as written
  3965. in the #+STARTUP line, the corresponding variable, and the value to
  3966. set this variable to if the option is found. An optional forth element PUSH
  3967. means to push this value onto the list in the variable.")
  3968. (defun org-set-regexps-and-options ()
  3969. "Precompute regular expressions for current buffer."
  3970. (when (org-mode-p)
  3971. (org-set-local 'org-todo-kwd-alist nil)
  3972. (org-set-local 'org-todo-key-alist nil)
  3973. (org-set-local 'org-todo-key-trigger nil)
  3974. (org-set-local 'org-todo-keywords-1 nil)
  3975. (org-set-local 'org-done-keywords nil)
  3976. (org-set-local 'org-todo-heads nil)
  3977. (org-set-local 'org-todo-sets nil)
  3978. (org-set-local 'org-todo-log-states nil)
  3979. (let ((re (org-make-options-regexp
  3980. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3981. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3982. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3983. (splitre "[ \t]+")
  3984. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3985. tail sep kws1 prio props drawers)
  3986. (save-excursion
  3987. (save-restriction
  3988. (widen)
  3989. (goto-char (point-min))
  3990. (while (re-search-forward re nil t)
  3991. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3992. (cond
  3993. ((equal key "CATEGORY")
  3994. (if (string-match "[ \t]+$" value)
  3995. (setq value (replace-match "" t t value)))
  3996. (setq cat value))
  3997. ((member key '("SEQ_TODO" "TODO"))
  3998. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3999. ((equal key "TYP_TODO")
  4000. (push (cons 'type (org-split-string value splitre)) kwds))
  4001. ((equal key "TAGS")
  4002. (setq tags (append tags (org-split-string value splitre))))
  4003. ((equal key "COLUMNS")
  4004. (org-set-local 'org-columns-default-format value))
  4005. ((equal key "LINK")
  4006. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4007. (push (cons (match-string 1 value)
  4008. (org-trim (match-string 2 value)))
  4009. links)))
  4010. ((equal key "PRIORITIES")
  4011. (setq prio (org-split-string value " +")))
  4012. ((equal key "PROPERTY")
  4013. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4014. (push (cons (match-string 1 value) (match-string 2 value))
  4015. props)))
  4016. ((equal key "DRAWERS")
  4017. (setq drawers (org-split-string value splitre)))
  4018. ((equal key "CONSTANTS")
  4019. (setq const (append const (org-split-string value splitre))))
  4020. ((equal key "STARTUP")
  4021. (let ((opts (org-split-string value splitre))
  4022. l var val)
  4023. (while (setq l (pop opts))
  4024. (when (setq l (assoc l org-startup-options))
  4025. (setq var (nth 1 l) val (nth 2 l))
  4026. (if (not (nth 3 l))
  4027. (set (make-local-variable var) val)
  4028. (if (not (listp (symbol-value var)))
  4029. (set (make-local-variable var) nil))
  4030. (set (make-local-variable var) (symbol-value var))
  4031. (add-to-list var val))))))
  4032. ((equal key "ARCHIVE")
  4033. (string-match " *$" value)
  4034. (setq arch (replace-match "" t t value))
  4035. (remove-text-properties 0 (length arch)
  4036. '(face t fontified t) arch)))
  4037. )))
  4038. (when cat
  4039. (org-set-local 'org-category (intern cat))
  4040. (push (cons "CATEGORY" cat) props))
  4041. (when prio
  4042. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4043. (setq prio (mapcar 'string-to-char prio))
  4044. (org-set-local 'org-highest-priority (nth 0 prio))
  4045. (org-set-local 'org-lowest-priority (nth 1 prio))
  4046. (org-set-local 'org-default-priority (nth 2 prio)))
  4047. (and props (org-set-local 'org-local-properties (nreverse props)))
  4048. (and drawers (org-set-local 'org-drawers drawers))
  4049. (and arch (org-set-local 'org-archive-location arch))
  4050. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4051. ;; Process the TODO keywords
  4052. (unless kwds
  4053. ;; Use the global values as if they had been given locally.
  4054. (setq kwds (default-value 'org-todo-keywords))
  4055. (if (stringp (car kwds))
  4056. (setq kwds (list (cons org-todo-interpretation
  4057. (default-value 'org-todo-keywords)))))
  4058. (setq kwds (reverse kwds)))
  4059. (setq kwds (nreverse kwds))
  4060. (let (inter kws kw)
  4061. (while (setq kws (pop kwds))
  4062. (setq inter (pop kws) sep (member "|" kws)
  4063. kws0 (delete "|" (copy-sequence kws))
  4064. kwsa nil
  4065. kws1 (mapcar
  4066. (lambda (x)
  4067. ;; 1 2
  4068. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4069. (progn
  4070. (setq kw (match-string 1 x)
  4071. key (and (match-end 2) (match-string 2 x))
  4072. log (org-extract-log-state-settings x))
  4073. (push (cons kw (and key (string-to-char key))) kwsa)
  4074. (and log (push log org-todo-log-states))
  4075. kw)
  4076. (error "Invalid TODO keyword %s" x)))
  4077. kws0)
  4078. kwsa (if kwsa (append '((:startgroup))
  4079. (nreverse kwsa)
  4080. '((:endgroup))))
  4081. hw (car kws1)
  4082. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4083. tail (list inter hw (car dws) (org-last dws)))
  4084. (add-to-list 'org-todo-heads hw 'append)
  4085. (push kws1 org-todo-sets)
  4086. (setq org-done-keywords (append org-done-keywords dws nil))
  4087. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4088. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4089. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4090. (setq org-todo-sets (nreverse org-todo-sets)
  4091. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4092. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4093. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4094. ;; Process the constants
  4095. (when const
  4096. (let (e cst)
  4097. (while (setq e (pop const))
  4098. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4099. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4100. (setq org-table-formula-constants-local cst)))
  4101. ;; Process the tags.
  4102. (when tags
  4103. (let (e tgs)
  4104. (while (setq e (pop tags))
  4105. (cond
  4106. ((equal e "{") (push '(:startgroup) tgs))
  4107. ((equal e "}") (push '(:endgroup) tgs))
  4108. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4109. (push (cons (match-string 1 e)
  4110. (string-to-char (match-string 2 e)))
  4111. tgs))
  4112. (t (push (list e) tgs))))
  4113. (org-set-local 'org-tag-alist nil)
  4114. (while (setq e (pop tgs))
  4115. (or (and (stringp (car e))
  4116. (assoc (car e) org-tag-alist))
  4117. (push e org-tag-alist))))))
  4118. ;; Compute the regular expressions and other local variables
  4119. (if (not org-done-keywords)
  4120. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4121. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4122. (length org-scheduled-string)))
  4123. org-drawer-regexp
  4124. (concat "^[ \t]*:\\("
  4125. (mapconcat 'regexp-quote org-drawers "\\|")
  4126. "\\):[ \t]*$")
  4127. org-not-done-keywords
  4128. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4129. org-todo-regexp
  4130. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4131. "\\|") "\\)\\>")
  4132. org-not-done-regexp
  4133. (concat "\\<\\("
  4134. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4135. "\\)\\>")
  4136. org-todo-line-regexp
  4137. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4138. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4139. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4140. org-complex-heading-regexp
  4141. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4142. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4143. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4144. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4145. org-nl-done-regexp
  4146. (concat "\n\\*+[ \t]+"
  4147. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4148. "\\)" "\\>")
  4149. org-todo-line-tags-regexp
  4150. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4151. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4152. (org-re
  4153. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4154. org-looking-at-done-regexp
  4155. (concat "^" "\\(?:"
  4156. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4157. "\\>")
  4158. org-deadline-regexp (concat "\\<" org-deadline-string)
  4159. org-deadline-time-regexp
  4160. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4161. org-deadline-line-regexp
  4162. (concat "\\<\\(" org-deadline-string "\\).*")
  4163. org-scheduled-regexp
  4164. (concat "\\<" org-scheduled-string)
  4165. org-scheduled-time-regexp
  4166. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4167. org-closed-time-regexp
  4168. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4169. org-keyword-time-regexp
  4170. (concat "\\<\\(" org-scheduled-string
  4171. "\\|" org-deadline-string
  4172. "\\|" org-closed-string
  4173. "\\|" org-clock-string "\\)"
  4174. " *[[<]\\([^]>]+\\)[]>]")
  4175. org-keyword-time-not-clock-regexp
  4176. (concat "\\<\\(" org-scheduled-string
  4177. "\\|" org-deadline-string
  4178. "\\|" org-closed-string
  4179. "\\)"
  4180. " *[[<]\\([^]>]+\\)[]>]")
  4181. org-maybe-keyword-time-regexp
  4182. (concat "\\(\\<\\(" org-scheduled-string
  4183. "\\|" org-deadline-string
  4184. "\\|" org-closed-string
  4185. "\\|" org-clock-string "\\)\\)?"
  4186. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4187. org-planning-or-clock-line-re
  4188. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4189. "\\|" org-deadline-string
  4190. "\\|" org-closed-string "\\|" org-clock-string
  4191. "\\)\\>\\)")
  4192. )
  4193. (org-compute-latex-and-specials-regexp)
  4194. (org-set-font-lock-defaults)))
  4195. (defun org-extract-log-state-settings (x)
  4196. "Extract the log state setting from a TODO keyword string.
  4197. This will extract info from a string like \"WAIT(w@/!)\"."
  4198. (let (kw key log1 log2)
  4199. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4200. (setq kw (match-string 1 x)
  4201. key (and (match-end 2) (match-string 2 x))
  4202. log1 (and (match-end 3) (match-string 3 x))
  4203. log2 (and (match-end 4) (match-string 4 x)))
  4204. (and (or log1 log2)
  4205. (list kw
  4206. (and log1 (if (equal log1 "!") 'time 'note))
  4207. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4208. (defun org-remove-keyword-keys (list)
  4209. "Remove a pair of parenthesis at the end of each string in LIST."
  4210. (mapcar (lambda (x)
  4211. (if (string-match "(.*)$" x)
  4212. (substring x 0 (match-beginning 0))
  4213. x))
  4214. list))
  4215. ;; FIXME: this could be done much better, using second characters etc.
  4216. (defun org-assign-fast-keys (alist)
  4217. "Assign fast keys to a keyword-key alist.
  4218. Respect keys that are already there."
  4219. (let (new e k c c1 c2 (char ?a))
  4220. (while (setq e (pop alist))
  4221. (cond
  4222. ((equal e '(:startgroup)) (push e new))
  4223. ((equal e '(:endgroup)) (push e new))
  4224. (t
  4225. (setq k (car e) c2 nil)
  4226. (if (cdr e)
  4227. (setq c (cdr e))
  4228. ;; automatically assign a character.
  4229. (setq c1 (string-to-char
  4230. (downcase (substring
  4231. k (if (= (string-to-char k) ?@) 1 0)))))
  4232. (if (or (rassoc c1 new) (rassoc c1 alist))
  4233. (while (or (rassoc char new) (rassoc char alist))
  4234. (setq char (1+ char)))
  4235. (setq c2 c1))
  4236. (setq c (or c2 char)))
  4237. (push (cons k c) new))))
  4238. (nreverse new)))
  4239. ;;; Some variables ujsed in various places
  4240. (defvar org-window-configuration nil
  4241. "Used in various places to store a window configuration.")
  4242. (defvar org-finish-function nil
  4243. "Function to be called when `C-c C-c' is used.
  4244. This is for getting out of special buffers like remember.")
  4245. ;; FIXME: Occasionally check by commenting these, to make sure
  4246. ;; no other functions uses these, forgetting to let-bind them.
  4247. (defvar entry)
  4248. (defvar state)
  4249. (defvar last-state)
  4250. (defvar date)
  4251. (defvar description)
  4252. ;; Defined somewhere in this file, but used before definition.
  4253. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4254. (defvar org-agenda-buffer-name)
  4255. (defvar org-agenda-undo-list)
  4256. (defvar org-agenda-pending-undo-list)
  4257. (defvar org-agenda-overriding-header)
  4258. (defvar orgtbl-mode)
  4259. (defvar org-html-entities)
  4260. (defvar org-struct-menu)
  4261. (defvar org-org-menu)
  4262. (defvar org-tbl-menu)
  4263. (defvar org-agenda-keymap)
  4264. ;;;; Emacs/XEmacs compatibility
  4265. ;; Overlay compatibility functions
  4266. (defun org-make-overlay (beg end &optional buffer)
  4267. (if (featurep 'xemacs)
  4268. (make-extent beg end buffer)
  4269. (make-overlay beg end buffer)))
  4270. (defun org-delete-overlay (ovl)
  4271. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4272. (defun org-detach-overlay (ovl)
  4273. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4274. (defun org-move-overlay (ovl beg end &optional buffer)
  4275. (if (featurep 'xemacs)
  4276. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4277. (move-overlay ovl beg end buffer)))
  4278. (defun org-overlay-put (ovl prop value)
  4279. (if (featurep 'xemacs)
  4280. (set-extent-property ovl prop value)
  4281. (overlay-put ovl prop value)))
  4282. (defun org-overlay-display (ovl text &optional face evap)
  4283. "Make overlay OVL display TEXT with face FACE."
  4284. (if (featurep 'xemacs)
  4285. (let ((gl (make-glyph text)))
  4286. (and face (set-glyph-face gl face))
  4287. (set-extent-property ovl 'invisible t)
  4288. (set-extent-property ovl 'end-glyph gl))
  4289. (overlay-put ovl 'display text)
  4290. (if face (overlay-put ovl 'face face))
  4291. (if evap (overlay-put ovl 'evaporate t))))
  4292. (defun org-overlay-before-string (ovl text &optional face evap)
  4293. "Make overlay OVL display TEXT with face FACE."
  4294. (if (featurep 'xemacs)
  4295. (let ((gl (make-glyph text)))
  4296. (and face (set-glyph-face gl face))
  4297. (set-extent-property ovl 'begin-glyph gl))
  4298. (if face (org-add-props text nil 'face face))
  4299. (overlay-put ovl 'before-string text)
  4300. (if evap (overlay-put ovl 'evaporate t))))
  4301. (defun org-overlay-get (ovl prop)
  4302. (if (featurep 'xemacs)
  4303. (extent-property ovl prop)
  4304. (overlay-get ovl prop)))
  4305. (defun org-overlays-at (pos)
  4306. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4307. (defun org-overlays-in (&optional start end)
  4308. (if (featurep 'xemacs)
  4309. (extent-list nil start end)
  4310. (overlays-in start end)))
  4311. (defun org-overlay-start (o)
  4312. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4313. (defun org-overlay-end (o)
  4314. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4315. (defun org-find-overlays (prop &optional pos delete)
  4316. "Find all overlays specifying PROP at POS or point.
  4317. If DELETE is non-nil, delete all those overlays."
  4318. (let ((overlays (org-overlays-at (or pos (point))))
  4319. ov found)
  4320. (while (setq ov (pop overlays))
  4321. (if (org-overlay-get ov prop)
  4322. (if delete (org-delete-overlay ov) (push ov found))))
  4323. found))
  4324. ;; Region compatibility
  4325. (defun org-add-hook (hook function &optional append local)
  4326. "Add-hook, compatible with both Emacsen."
  4327. (if (and local (featurep 'xemacs))
  4328. (add-local-hook hook function append)
  4329. (add-hook hook function append local)))
  4330. (defvar org-ignore-region nil
  4331. "To temporarily disable the active region.")
  4332. (defun org-region-active-p ()
  4333. "Is `transient-mark-mode' on and the region active?
  4334. Works on both Emacs and XEmacs."
  4335. (if org-ignore-region
  4336. nil
  4337. (if (featurep 'xemacs)
  4338. (and zmacs-regions (region-active-p))
  4339. (if (fboundp 'use-region-p)
  4340. (use-region-p)
  4341. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4342. ;; Invisibility compatibility
  4343. (defun org-add-to-invisibility-spec (arg)
  4344. "Add elements to `buffer-invisibility-spec'.
  4345. See documentation for `buffer-invisibility-spec' for the kind of elements
  4346. that can be added."
  4347. (cond
  4348. ((fboundp 'add-to-invisibility-spec)
  4349. (add-to-invisibility-spec arg))
  4350. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4351. (setq buffer-invisibility-spec (list arg)))
  4352. (t
  4353. (setq buffer-invisibility-spec
  4354. (cons arg buffer-invisibility-spec)))))
  4355. (defun org-remove-from-invisibility-spec (arg)
  4356. "Remove elements from `buffer-invisibility-spec'."
  4357. (if (fboundp 'remove-from-invisibility-spec)
  4358. (remove-from-invisibility-spec arg)
  4359. (if (consp buffer-invisibility-spec)
  4360. (setq buffer-invisibility-spec
  4361. (delete arg buffer-invisibility-spec)))))
  4362. (defun org-in-invisibility-spec-p (arg)
  4363. "Is ARG a member of `buffer-invisibility-spec'?"
  4364. (if (consp buffer-invisibility-spec)
  4365. (member arg buffer-invisibility-spec)
  4366. nil))
  4367. ;;;; Define the Org-mode
  4368. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4369. (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."))
  4370. ;; We use a before-change function to check if a table might need
  4371. ;; an update.
  4372. (defvar org-table-may-need-update t
  4373. "Indicates that a table might need an update.
  4374. This variable is set by `org-before-change-function'.
  4375. `org-table-align' sets it back to nil.")
  4376. (defvar org-mode-map)
  4377. (defvar org-mode-hook nil)
  4378. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4379. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4380. (defvar org-table-buffer-is-an nil)
  4381. (defconst org-outline-regexp "\\*+ ")
  4382. ;;;###autoload
  4383. (define-derived-mode org-mode outline-mode "Org"
  4384. "Outline-based notes management and organizer, alias
  4385. \"Carsten's outline-mode for keeping track of everything.\"
  4386. Org-mode develops organizational tasks around a NOTES file which
  4387. contains information about projects as plain text. Org-mode is
  4388. implemented on top of outline-mode, which is ideal to keep the content
  4389. of large files well structured. It supports ToDo items, deadlines and
  4390. time stamps, which magically appear in the diary listing of the Emacs
  4391. calendar. Tables are easily created with a built-in table editor.
  4392. Plain text URL-like links connect to websites, emails (VM), Usenet
  4393. messages (Gnus), BBDB entries, and any files related to the project.
  4394. For printing and sharing of notes, an Org-mode file (or a part of it)
  4395. can be exported as a structured ASCII or HTML file.
  4396. The following commands are available:
  4397. \\{org-mode-map}"
  4398. ;; Get rid of Outline menus, they are not needed
  4399. ;; Need to do this here because define-derived-mode sets up
  4400. ;; the keymap so late. Still, it is a waste to call this each time
  4401. ;; we switch another buffer into org-mode.
  4402. (if (featurep 'xemacs)
  4403. (when (boundp 'outline-mode-menu-heading)
  4404. ;; Assume this is Greg's port, it used easymenu
  4405. (easy-menu-remove outline-mode-menu-heading)
  4406. (easy-menu-remove outline-mode-menu-show)
  4407. (easy-menu-remove outline-mode-menu-hide))
  4408. (define-key org-mode-map [menu-bar headings] 'undefined)
  4409. (define-key org-mode-map [menu-bar hide] 'undefined)
  4410. (define-key org-mode-map [menu-bar show] 'undefined))
  4411. (easy-menu-add org-org-menu)
  4412. (easy-menu-add org-tbl-menu)
  4413. (org-install-agenda-files-menu)
  4414. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4415. (org-add-to-invisibility-spec '(org-cwidth))
  4416. (when (featurep 'xemacs)
  4417. (org-set-local 'line-move-ignore-invisible t))
  4418. (org-set-local 'outline-regexp org-outline-regexp)
  4419. (org-set-local 'outline-level 'org-outline-level)
  4420. (when (and org-ellipsis
  4421. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4422. (fboundp 'make-glyph-code))
  4423. (unless org-display-table
  4424. (setq org-display-table (make-display-table)))
  4425. (set-display-table-slot
  4426. org-display-table 4
  4427. (vconcat (mapcar
  4428. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4429. org-ellipsis)))
  4430. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4431. (setq buffer-display-table org-display-table))
  4432. (org-set-regexps-and-options)
  4433. ;; Calc embedded
  4434. (org-set-local 'calc-embedded-open-mode "# ")
  4435. (modify-syntax-entry ?# "<")
  4436. (modify-syntax-entry ?@ "w")
  4437. (if org-startup-truncated (setq truncate-lines t))
  4438. (org-set-local 'font-lock-unfontify-region-function
  4439. 'org-unfontify-region)
  4440. ;; Activate before-change-function
  4441. (org-set-local 'org-table-may-need-update t)
  4442. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4443. 'local)
  4444. ;; Check for running clock before killing a buffer
  4445. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4446. ;; Paragraphs and auto-filling
  4447. (org-set-autofill-regexps)
  4448. (setq indent-line-function 'org-indent-line-function)
  4449. (org-update-radio-target-regexp)
  4450. ;; Comment characters
  4451. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4452. (org-set-local 'comment-padding " ")
  4453. ;; Align options lines
  4454. (org-set-local
  4455. 'align-mode-rules-list
  4456. '((org-in-buffer-settings
  4457. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4458. (modes . '(org-mode)))))
  4459. ;; Imenu
  4460. (org-set-local 'imenu-create-index-function
  4461. 'org-imenu-get-tree)
  4462. ;; Make isearch reveal context
  4463. (if (or (featurep 'xemacs)
  4464. (not (boundp 'outline-isearch-open-invisible-function)))
  4465. ;; Emacs 21 and XEmacs make use of the hook
  4466. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4467. ;; Emacs 22 deals with this through a special variable
  4468. (org-set-local 'outline-isearch-open-invisible-function
  4469. (lambda (&rest ignore) (org-show-context 'isearch))))
  4470. ;; If empty file that did not turn on org-mode automatically, make it to.
  4471. (if (and org-insert-mode-line-in-empty-file
  4472. (interactive-p)
  4473. (= (point-min) (point-max)))
  4474. (insert "# -*- mode: org -*-\n\n"))
  4475. (unless org-inhibit-startup
  4476. (when org-startup-align-all-tables
  4477. (let ((bmp (buffer-modified-p)))
  4478. (org-table-map-tables 'org-table-align)
  4479. (set-buffer-modified-p bmp)))
  4480. (org-cycle-hide-drawers 'all)
  4481. (cond
  4482. ((eq org-startup-folded t)
  4483. (org-cycle '(4)))
  4484. ((eq org-startup-folded 'content)
  4485. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4486. (org-cycle '(4)) (org-cycle '(4)))))))
  4487. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4488. (defsubst org-call-with-arg (command arg)
  4489. "Call COMMAND interactively, but pretend prefix are was ARG."
  4490. (let ((current-prefix-arg arg)) (call-interactively command)))
  4491. (defsubst org-current-line (&optional pos)
  4492. (save-excursion
  4493. (and pos (goto-char pos))
  4494. ;; works also in narrowed buffer, because we start at 1, not point-min
  4495. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4496. (defun org-current-time ()
  4497. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4498. (if (> (car org-time-stamp-rounding-minutes) 1)
  4499. (let ((r (car org-time-stamp-rounding-minutes))
  4500. (time (decode-time)))
  4501. (apply 'encode-time
  4502. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4503. (nthcdr 2 time))))
  4504. (current-time)))
  4505. (defun org-add-props (string plist &rest props)
  4506. "Add text properties to entire string, from beginning to end.
  4507. PLIST may be a list of properties, PROPS are individual properties and values
  4508. that will be added to PLIST. Returns the string that was modified."
  4509. (add-text-properties
  4510. 0 (length string) (if props (append plist props) plist) string)
  4511. string)
  4512. (put 'org-add-props 'lisp-indent-function 2)
  4513. ;;;; Font-Lock stuff, including the activators
  4514. (defvar org-mouse-map (make-sparse-keymap))
  4515. (org-defkey org-mouse-map
  4516. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4517. (org-defkey org-mouse-map
  4518. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4519. (when org-mouse-1-follows-link
  4520. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4521. (when org-tab-follows-link
  4522. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4523. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4524. (when org-return-follows-link
  4525. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4526. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4527. (require 'font-lock)
  4528. (defconst org-non-link-chars "]\t\n\r<>")
  4529. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4530. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4531. (defvar org-link-re-with-space nil
  4532. "Matches a link with spaces, optional angular brackets around it.")
  4533. (defvar org-link-re-with-space2 nil
  4534. "Matches a link with spaces, optional angular brackets around it.")
  4535. (defvar org-angle-link-re nil
  4536. "Matches link with angular brackets, spaces are allowed.")
  4537. (defvar org-plain-link-re nil
  4538. "Matches plain link, without spaces.")
  4539. (defvar org-bracket-link-regexp nil
  4540. "Matches a link in double brackets.")
  4541. (defvar org-bracket-link-analytic-regexp nil
  4542. "Regular expression used to analyze links.
  4543. Here is what the match groups contain after a match:
  4544. 1: http:
  4545. 2: http
  4546. 3: path
  4547. 4: [desc]
  4548. 5: desc")
  4549. (defvar org-any-link-re nil
  4550. "Regular expression matching any link.")
  4551. (defun org-make-link-regexps ()
  4552. "Update the link regular expressions.
  4553. This should be called after the variable `org-link-types' has changed."
  4554. (setq org-link-re-with-space
  4555. (concat
  4556. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4557. "\\([^" org-non-link-chars " ]"
  4558. "[^" org-non-link-chars "]*"
  4559. "[^" org-non-link-chars " ]\\)>?")
  4560. org-link-re-with-space2
  4561. (concat
  4562. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4563. "\\([^" org-non-link-chars " ]"
  4564. "[^]\t\n\r]*"
  4565. "[^" org-non-link-chars " ]\\)>?")
  4566. org-angle-link-re
  4567. (concat
  4568. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4569. "\\([^" org-non-link-chars " ]"
  4570. "[^" org-non-link-chars "]*"
  4571. "\\)>")
  4572. org-plain-link-re
  4573. (concat
  4574. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4575. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4576. org-bracket-link-regexp
  4577. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4578. org-bracket-link-analytic-regexp
  4579. (concat
  4580. "\\[\\["
  4581. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4582. "\\([^]]+\\)"
  4583. "\\]"
  4584. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4585. "\\]")
  4586. org-any-link-re
  4587. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4588. org-angle-link-re "\\)\\|\\("
  4589. org-plain-link-re "\\)")))
  4590. (org-make-link-regexps)
  4591. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4592. "Regular expression for fast time stamp matching.")
  4593. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4594. "Regular expression for fast time stamp matching.")
  4595. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4596. "Regular expression matching time strings for analysis.
  4597. This one does not require the space after the date.")
  4598. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4599. "Regular expression matching time strings for analysis.")
  4600. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4601. "Regular expression matching time stamps, with groups.")
  4602. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4603. "Regular expression matching time stamps (also [..]), with groups.")
  4604. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4605. "Regular expression matching a time stamp range.")
  4606. (defconst org-tr-regexp-both
  4607. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4608. "Regular expression matching a time stamp range.")
  4609. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4610. org-ts-regexp "\\)?")
  4611. "Regular expression matching a time stamp or time stamp range.")
  4612. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4613. org-ts-regexp-both "\\)?")
  4614. "Regular expression matching a time stamp or time stamp range.
  4615. The time stamps may be either active or inactive.")
  4616. (defvar org-emph-face nil)
  4617. (defun org-do-emphasis-faces (limit)
  4618. "Run through the buffer and add overlays to links."
  4619. (let (rtn)
  4620. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4621. (if (not (= (char-after (match-beginning 3))
  4622. (char-after (match-beginning 4))))
  4623. (progn
  4624. (setq rtn t)
  4625. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4626. 'face
  4627. (nth 1 (assoc (match-string 3)
  4628. org-emphasis-alist)))
  4629. (add-text-properties (match-beginning 2) (match-end 2)
  4630. '(font-lock-multiline t))
  4631. (when org-hide-emphasis-markers
  4632. (add-text-properties (match-end 4) (match-beginning 5)
  4633. '(invisible org-link))
  4634. (add-text-properties (match-beginning 3) (match-end 3)
  4635. '(invisible org-link)))))
  4636. (backward-char 1))
  4637. rtn))
  4638. (defun org-emphasize (&optional char)
  4639. "Insert or change an emphasis, i.e. a font like bold or italic.
  4640. If there is an active region, change that region to a new emphasis.
  4641. If there is no region, just insert the marker characters and position
  4642. the cursor between them.
  4643. CHAR should be either the marker character, or the first character of the
  4644. HTML tag associated with that emphasis. If CHAR is a space, the means
  4645. to remove the emphasis of the selected region.
  4646. If char is not given (for example in an interactive call) it
  4647. will be prompted for."
  4648. (interactive)
  4649. (let ((eal org-emphasis-alist) e det
  4650. (erc org-emphasis-regexp-components)
  4651. (prompt "")
  4652. (string "") beg end move tag c s)
  4653. (if (org-region-active-p)
  4654. (setq beg (region-beginning) end (region-end)
  4655. string (buffer-substring beg end))
  4656. (setq move t))
  4657. (while (setq e (pop eal))
  4658. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4659. c (aref tag 0))
  4660. (push (cons c (string-to-char (car e))) det)
  4661. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4662. (substring tag 1)))))
  4663. (unless char
  4664. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4665. (setq char (read-char-exclusive)))
  4666. (setq char (or (cdr (assoc char det)) char))
  4667. (if (equal char ?\ )
  4668. (setq s "" move nil)
  4669. (unless (assoc (char-to-string char) org-emphasis-alist)
  4670. (error "No such emphasis marker: \"%c\"" char))
  4671. (setq s (char-to-string char)))
  4672. (while (and (> (length string) 1)
  4673. (equal (substring string 0 1) (substring string -1))
  4674. (assoc (substring string 0 1) org-emphasis-alist))
  4675. (setq string (substring string 1 -1)))
  4676. (setq string (concat s string s))
  4677. (if beg (delete-region beg end))
  4678. (unless (or (bolp)
  4679. (string-match (concat "[" (nth 0 erc) "\n]")
  4680. (char-to-string (char-before (point)))))
  4681. (insert " "))
  4682. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4683. (char-to-string (char-after (point))))
  4684. (insert " ") (backward-char 1))
  4685. (insert string)
  4686. (and move (backward-char 1))))
  4687. (defconst org-nonsticky-props
  4688. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4689. (defun org-activate-plain-links (limit)
  4690. "Run through the buffer and add overlays to links."
  4691. (catch 'exit
  4692. (let (f)
  4693. (while (re-search-forward org-plain-link-re limit t)
  4694. (setq f (get-text-property (match-beginning 0) 'face))
  4695. (if (or (eq f 'org-tag)
  4696. (and (listp f) (memq 'org-tag f)))
  4697. nil
  4698. (add-text-properties (match-beginning 0) (match-end 0)
  4699. (list 'mouse-face 'highlight
  4700. 'rear-nonsticky org-nonsticky-props
  4701. 'keymap org-mouse-map
  4702. ))
  4703. (throw 'exit t))))))
  4704. (defun org-activate-code (limit)
  4705. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4706. (unless (get-text-property (match-beginning 1) 'face)
  4707. (remove-text-properties (match-beginning 0) (match-end 0)
  4708. '(display t invisible t intangible t))
  4709. t)))
  4710. (defun org-activate-angle-links (limit)
  4711. "Run through the buffer and add overlays to links."
  4712. (if (re-search-forward org-angle-link-re limit t)
  4713. (progn
  4714. (add-text-properties (match-beginning 0) (match-end 0)
  4715. (list 'mouse-face 'highlight
  4716. 'rear-nonsticky org-nonsticky-props
  4717. 'keymap org-mouse-map
  4718. ))
  4719. t)))
  4720. (defmacro org-maybe-intangible (props)
  4721. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4722. In emacs 21, invisible text is not avoided by the command loop, so the
  4723. intangible property is needed to make sure point skips this text.
  4724. In Emacs 22, this is not necessary. The intangible text property has
  4725. led to problems with flyspell. These problems are fixed in flyspell.el,
  4726. but we still avoid setting the property in Emacs 22 and later.
  4727. We use a macro so that the test can happen at compilation time."
  4728. (if (< emacs-major-version 22)
  4729. `(append '(intangible t) ,props)
  4730. props))
  4731. (defun org-activate-bracket-links (limit)
  4732. "Run through the buffer and add overlays to bracketed links."
  4733. (if (re-search-forward org-bracket-link-regexp limit t)
  4734. (let* ((help (concat "LINK: "
  4735. (org-match-string-no-properties 1)))
  4736. ;; FIXME: above we should remove the escapes.
  4737. ;; but that requires another match, protecting match data,
  4738. ;; a lot of overhead for font-lock.
  4739. (ip (org-maybe-intangible
  4740. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4741. 'keymap org-mouse-map 'mouse-face 'highlight
  4742. 'font-lock-multiline t 'help-echo help)))
  4743. (vp (list 'rear-nonsticky org-nonsticky-props
  4744. 'keymap org-mouse-map 'mouse-face 'highlight
  4745. ' font-lock-multiline t 'help-echo help)))
  4746. ;; We need to remove the invisible property here. Table narrowing
  4747. ;; may have made some of this invisible.
  4748. (remove-text-properties (match-beginning 0) (match-end 0)
  4749. '(invisible nil))
  4750. (if (match-end 3)
  4751. (progn
  4752. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4753. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4754. (add-text-properties (match-end 3) (match-end 0) ip))
  4755. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4756. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4757. (add-text-properties (match-end 1) (match-end 0) ip))
  4758. t)))
  4759. (defun org-activate-dates (limit)
  4760. "Run through the buffer and add overlays to dates."
  4761. (if (re-search-forward org-tsr-regexp-both limit t)
  4762. (progn
  4763. (add-text-properties (match-beginning 0) (match-end 0)
  4764. (list 'mouse-face 'highlight
  4765. 'rear-nonsticky org-nonsticky-props
  4766. 'keymap org-mouse-map))
  4767. (when org-display-custom-times
  4768. (if (match-end 3)
  4769. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4770. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4771. t)))
  4772. (defvar org-target-link-regexp nil
  4773. "Regular expression matching radio targets in plain text.")
  4774. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4775. "Regular expression matching a link target.")
  4776. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4777. "Regular expression matching a radio target.")
  4778. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4779. "Regular expression matching any target.")
  4780. (defun org-activate-target-links (limit)
  4781. "Run through the buffer and add overlays to target matches."
  4782. (when org-target-link-regexp
  4783. (let ((case-fold-search t))
  4784. (if (re-search-forward org-target-link-regexp limit t)
  4785. (progn
  4786. (add-text-properties (match-beginning 0) (match-end 0)
  4787. (list 'mouse-face 'highlight
  4788. 'rear-nonsticky org-nonsticky-props
  4789. 'keymap org-mouse-map
  4790. 'help-echo "Radio target link"
  4791. 'org-linked-text t))
  4792. t)))))
  4793. (defun org-update-radio-target-regexp ()
  4794. "Find all radio targets in this file and update the regular expression."
  4795. (interactive)
  4796. (when (memq 'radio org-activate-links)
  4797. (setq org-target-link-regexp
  4798. (org-make-target-link-regexp (org-all-targets 'radio)))
  4799. (org-restart-font-lock)))
  4800. (defun org-hide-wide-columns (limit)
  4801. (let (s e)
  4802. (setq s (text-property-any (point) (or limit (point-max))
  4803. 'org-cwidth t))
  4804. (when s
  4805. (setq e (next-single-property-change s 'org-cwidth))
  4806. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4807. (goto-char e)
  4808. t)))
  4809. (defvar org-latex-and-specials-regexp nil
  4810. "Regular expression for highlighting export special stuff.")
  4811. (defvar org-match-substring-regexp)
  4812. (defvar org-match-substring-with-braces-regexp)
  4813. (defvar org-export-html-special-string-regexps)
  4814. (defun org-compute-latex-and-specials-regexp ()
  4815. "Compute regular expression for stuff treated specially by exporters."
  4816. (if (not org-highlight-latex-fragments-and-specials)
  4817. (org-set-local 'org-latex-and-specials-regexp nil)
  4818. (let*
  4819. ((matchers (plist-get org-format-latex-options :matchers))
  4820. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4821. org-latex-regexps)))
  4822. (options (org-combine-plists (org-default-export-plist)
  4823. (org-infile-export-plist)))
  4824. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4825. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4826. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4827. (org-export-html-expand (plist-get options :expand-quoted-html))
  4828. (org-export-with-special-strings (plist-get options :special-strings))
  4829. (re-sub
  4830. (cond
  4831. ((equal org-export-with-sub-superscripts '{})
  4832. (list org-match-substring-with-braces-regexp))
  4833. (org-export-with-sub-superscripts
  4834. (list org-match-substring-regexp))
  4835. (t nil)))
  4836. (re-latex
  4837. (if org-export-with-LaTeX-fragments
  4838. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4839. (re-macros
  4840. (if org-export-with-TeX-macros
  4841. (list (concat "\\\\"
  4842. (regexp-opt
  4843. (append (mapcar 'car org-html-entities)
  4844. (if (boundp 'org-latex-entities)
  4845. org-latex-entities nil))
  4846. 'words))) ; FIXME
  4847. ))
  4848. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4849. (re-special (if org-export-with-special-strings
  4850. (mapcar (lambda (x) (car x))
  4851. org-export-html-special-string-regexps)))
  4852. (re-rest
  4853. (delq nil
  4854. (list
  4855. (if org-export-html-expand "@<[^>\n]+>")
  4856. ))))
  4857. (org-set-local
  4858. 'org-latex-and-specials-regexp
  4859. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4860. re-rest) "\\|")))))
  4861. (defface org-latex-and-export-specials
  4862. (let ((font (cond ((assq :inherit custom-face-attributes)
  4863. '(:inherit underline))
  4864. (t '(:underline t)))))
  4865. `((((class grayscale) (background light))
  4866. (:foreground "DimGray" ,@font))
  4867. (((class grayscale) (background dark))
  4868. (:foreground "LightGray" ,@font))
  4869. (((class color) (background light))
  4870. (:foreground "SaddleBrown"))
  4871. (((class color) (background dark))
  4872. (:foreground "burlywood"))
  4873. (t (,@font))))
  4874. "Face used to highlight math latex and other special exporter stuff."
  4875. :group 'org-faces)
  4876. (defun org-do-latex-and-special-faces (limit)
  4877. "Run through the buffer and add overlays to links."
  4878. (when org-latex-and-specials-regexp
  4879. (let (rtn d)
  4880. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4881. limit t))
  4882. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4883. 'face))
  4884. '(org-code org-verbatim underline)))
  4885. (progn
  4886. (setq rtn t
  4887. d (cond ((member (char-after (1+ (match-beginning 0)))
  4888. '(?_ ?^)) 1)
  4889. (t 0)))
  4890. (font-lock-prepend-text-property
  4891. (+ d (match-beginning 0)) (match-end 0)
  4892. 'face 'org-latex-and-export-specials)
  4893. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4894. '(font-lock-multiline t)))))
  4895. rtn)))
  4896. (defun org-restart-font-lock ()
  4897. "Restart font-lock-mode, to force refontification."
  4898. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4899. (font-lock-mode -1)
  4900. (font-lock-mode 1)))
  4901. (defun org-all-targets (&optional radio)
  4902. "Return a list of all targets in this file.
  4903. With optional argument RADIO, only find radio targets."
  4904. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4905. rtn)
  4906. (save-excursion
  4907. (goto-char (point-min))
  4908. (while (re-search-forward re nil t)
  4909. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4910. rtn)))
  4911. (defun org-make-target-link-regexp (targets)
  4912. "Make regular expression matching all strings in TARGETS.
  4913. The regular expression finds the targets also if there is a line break
  4914. between words."
  4915. (and targets
  4916. (concat
  4917. "\\<\\("
  4918. (mapconcat
  4919. (lambda (x)
  4920. (while (string-match " +" x)
  4921. (setq x (replace-match "\\s-+" t t x)))
  4922. x)
  4923. targets
  4924. "\\|")
  4925. "\\)\\>")))
  4926. (defun org-activate-tags (limit)
  4927. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4928. (progn
  4929. (add-text-properties (match-beginning 1) (match-end 1)
  4930. (list 'mouse-face 'highlight
  4931. 'rear-nonsticky org-nonsticky-props
  4932. 'keymap org-mouse-map))
  4933. t)))
  4934. (defun org-outline-level ()
  4935. (save-excursion
  4936. (looking-at outline-regexp)
  4937. (if (match-beginning 1)
  4938. (+ (org-get-string-indentation (match-string 1)) 1000)
  4939. (1- (- (match-end 0) (match-beginning 0))))))
  4940. (defvar org-font-lock-keywords nil)
  4941. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4942. "Regular expression matching a property line.")
  4943. (defun org-set-font-lock-defaults ()
  4944. (let* ((em org-fontify-emphasized-text)
  4945. (lk org-activate-links)
  4946. (org-font-lock-extra-keywords
  4947. (list
  4948. ;; Headlines
  4949. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4950. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4951. ;; Table lines
  4952. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4953. (1 'org-table t))
  4954. ;; Table internals
  4955. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4956. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4957. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4958. ;; Drawers
  4959. (list org-drawer-regexp '(0 'org-special-keyword t))
  4960. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4961. ;; Properties
  4962. (list org-property-re
  4963. '(1 'org-special-keyword t)
  4964. '(3 'org-property-value t))
  4965. (if org-format-transports-properties-p
  4966. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4967. ;; Links
  4968. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4969. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4970. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4971. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4972. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4973. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4974. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4975. '(org-hide-wide-columns (0 nil append))
  4976. ;; TODO lines
  4977. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4978. '(1 (org-get-todo-face 1) t))
  4979. ;; DONE
  4980. (if org-fontify-done-headline
  4981. (list (concat "^[*]+ +\\<\\("
  4982. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4983. "\\)\\(.*\\)")
  4984. '(2 'org-headline-done t))
  4985. nil)
  4986. ;; Priorities
  4987. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4988. ;; Special keywords
  4989. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4990. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4991. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4992. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4993. ;; Emphasis
  4994. (if em
  4995. (if (featurep 'xemacs)
  4996. '(org-do-emphasis-faces (0 nil append))
  4997. '(org-do-emphasis-faces)))
  4998. ;; Checkboxes
  4999. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  5000. 2 'bold prepend)
  5001. (if org-provide-checkbox-statistics
  5002. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5003. (0 (org-get-checkbox-statistics-face) t)))
  5004. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5005. '(1 'org-archived prepend))
  5006. ;; Specials
  5007. '(org-do-latex-and-special-faces)
  5008. ;; Code
  5009. '(org-activate-code (1 'org-code t))
  5010. ;; COMMENT
  5011. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5012. "\\|" org-quote-string "\\)\\>")
  5013. '(1 'org-special-keyword t))
  5014. '("^#.*" (0 'font-lock-comment-face t))
  5015. )))
  5016. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5017. ;; Now set the full font-lock-keywords
  5018. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5019. (org-set-local 'font-lock-defaults
  5020. '(org-font-lock-keywords t nil nil backward-paragraph))
  5021. (kill-local-variable 'font-lock-keywords) nil))
  5022. (defvar org-m nil)
  5023. (defvar org-l nil)
  5024. (defvar org-f nil)
  5025. (defun org-get-level-face (n)
  5026. "Get the right face for match N in font-lock matching of healdines."
  5027. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5028. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5029. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5030. (cond
  5031. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5032. ((eq n 2) org-f)
  5033. (t (if org-level-color-stars-only nil org-f))))
  5034. (defun org-get-todo-face (kwd)
  5035. "Get the right face for a TODO keyword KWD.
  5036. If KWD is a number, get the corresponding match group."
  5037. (if (numberp kwd) (setq kwd (match-string kwd)))
  5038. (or (cdr (assoc kwd org-todo-keyword-faces))
  5039. (and (member kwd org-done-keywords) 'org-done)
  5040. 'org-todo))
  5041. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5042. "Remove fontification and activation overlays from links."
  5043. (font-lock-default-unfontify-region beg end)
  5044. (let* ((buffer-undo-list t)
  5045. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5046. (inhibit-modification-hooks t)
  5047. deactivate-mark buffer-file-name buffer-file-truename)
  5048. (remove-text-properties beg end
  5049. '(mouse-face t keymap t org-linked-text t
  5050. invisible t intangible t))))
  5051. ;;;; Visibility cycling, including org-goto and indirect buffer
  5052. ;;; Cycling
  5053. (defvar org-cycle-global-status nil)
  5054. (make-variable-buffer-local 'org-cycle-global-status)
  5055. (defvar org-cycle-subtree-status nil)
  5056. (make-variable-buffer-local 'org-cycle-subtree-status)
  5057. ;;;###autoload
  5058. (defun org-cycle (&optional arg)
  5059. "Visibility cycling for Org-mode.
  5060. - When this function is called with a prefix argument, rotate the entire
  5061. buffer through 3 states (global cycling)
  5062. 1. OVERVIEW: Show only top-level headlines.
  5063. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5064. 3. SHOW ALL: Show everything.
  5065. - When point is at the beginning of a headline, rotate the subtree started
  5066. by this line through 3 different states (local cycling)
  5067. 1. FOLDED: Only the main headline is shown.
  5068. 2. CHILDREN: The main headline and the direct children are shown.
  5069. From this state, you can move to one of the children
  5070. and zoom in further.
  5071. 3. SUBTREE: Show the entire subtree, including body text.
  5072. - When there is a numeric prefix, go up to a heading with level ARG, do
  5073. a `show-subtree' and return to the previous cursor position. If ARG
  5074. is negative, go up that many levels.
  5075. - When point is not at the beginning of a headline, execute
  5076. `indent-relative', like TAB normally does. See the option
  5077. `org-cycle-emulate-tab' for details.
  5078. - Special case: if point is at the beginning of the buffer and there is
  5079. no headline in line 1, this function will act as if called with prefix arg.
  5080. But only if also the variable `org-cycle-global-at-bob' is t."
  5081. (interactive "P")
  5082. (let* ((outline-regexp
  5083. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5084. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5085. outline-regexp))
  5086. (bob-special (and org-cycle-global-at-bob (bobp)
  5087. (not (looking-at outline-regexp))))
  5088. (org-cycle-hook
  5089. (if bob-special
  5090. (delq 'org-optimize-window-after-visibility-change
  5091. (copy-sequence org-cycle-hook))
  5092. org-cycle-hook))
  5093. (pos (point)))
  5094. (if (or bob-special (equal arg '(4)))
  5095. ;; special case: use global cycling
  5096. (setq arg t))
  5097. (cond
  5098. ((org-at-table-p 'any)
  5099. ;; Enter the table or move to the next field in the table
  5100. (or (org-table-recognize-table.el)
  5101. (progn
  5102. (if arg (org-table-edit-field t)
  5103. (org-table-justify-field-maybe)
  5104. (call-interactively 'org-table-next-field)))))
  5105. ((eq arg t) ;; Global cycling
  5106. (cond
  5107. ((and (eq last-command this-command)
  5108. (eq org-cycle-global-status 'overview))
  5109. ;; We just created the overview - now do table of contents
  5110. ;; This can be slow in very large buffers, so indicate action
  5111. (message "CONTENTS...")
  5112. (org-content)
  5113. (message "CONTENTS...done")
  5114. (setq org-cycle-global-status 'contents)
  5115. (run-hook-with-args 'org-cycle-hook 'contents))
  5116. ((and (eq last-command this-command)
  5117. (eq org-cycle-global-status 'contents))
  5118. ;; We just showed the table of contents - now show everything
  5119. (show-all)
  5120. (message "SHOW ALL")
  5121. (setq org-cycle-global-status 'all)
  5122. (run-hook-with-args 'org-cycle-hook 'all))
  5123. (t
  5124. ;; Default action: go to overview
  5125. (org-overview)
  5126. (message "OVERVIEW")
  5127. (setq org-cycle-global-status 'overview)
  5128. (run-hook-with-args 'org-cycle-hook 'overview))))
  5129. ((and org-drawers org-drawer-regexp
  5130. (save-excursion
  5131. (beginning-of-line 1)
  5132. (looking-at org-drawer-regexp)))
  5133. ;; Toggle block visibility
  5134. (org-flag-drawer
  5135. (not (get-char-property (match-end 0) 'invisible))))
  5136. ((integerp arg)
  5137. ;; Show-subtree, ARG levels up from here.
  5138. (save-excursion
  5139. (org-back-to-heading)
  5140. (outline-up-heading (if (< arg 0) (- arg)
  5141. (- (funcall outline-level) arg)))
  5142. (org-show-subtree)))
  5143. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5144. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5145. ;; At a heading: rotate between three different views
  5146. (org-back-to-heading)
  5147. (let ((goal-column 0) eoh eol eos)
  5148. ;; First, some boundaries
  5149. (save-excursion
  5150. (org-back-to-heading)
  5151. (save-excursion
  5152. (beginning-of-line 2)
  5153. (while (and (not (eobp)) ;; this is like `next-line'
  5154. (get-char-property (1- (point)) 'invisible))
  5155. (beginning-of-line 2)) (setq eol (point)))
  5156. (outline-end-of-heading) (setq eoh (point))
  5157. (org-end-of-subtree t)
  5158. (unless (eobp)
  5159. (skip-chars-forward " \t\n")
  5160. (beginning-of-line 1) ; in case this is an item
  5161. )
  5162. (setq eos (1- (point))))
  5163. ;; Find out what to do next and set `this-command'
  5164. (cond
  5165. ((= eos eoh)
  5166. ;; Nothing is hidden behind this heading
  5167. (message "EMPTY ENTRY")
  5168. (setq org-cycle-subtree-status nil)
  5169. (save-excursion
  5170. (goto-char eos)
  5171. (outline-next-heading)
  5172. (if (org-invisible-p) (org-flag-heading nil))))
  5173. ((or (>= eol eos)
  5174. (not (string-match "\\S-" (buffer-substring eol eos))))
  5175. ;; Entire subtree is hidden in one line: open it
  5176. (org-show-entry)
  5177. (show-children)
  5178. (message "CHILDREN")
  5179. (save-excursion
  5180. (goto-char eos)
  5181. (outline-next-heading)
  5182. (if (org-invisible-p) (org-flag-heading nil)))
  5183. (setq org-cycle-subtree-status 'children)
  5184. (run-hook-with-args 'org-cycle-hook 'children))
  5185. ((and (eq last-command this-command)
  5186. (eq org-cycle-subtree-status 'children))
  5187. ;; We just showed the children, now show everything.
  5188. (org-show-subtree)
  5189. (message "SUBTREE")
  5190. (setq org-cycle-subtree-status 'subtree)
  5191. (run-hook-with-args 'org-cycle-hook 'subtree))
  5192. (t
  5193. ;; Default action: hide the subtree.
  5194. (hide-subtree)
  5195. (message "FOLDED")
  5196. (setq org-cycle-subtree-status 'folded)
  5197. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5198. ;; TAB emulation
  5199. (buffer-read-only (org-back-to-heading))
  5200. ((org-try-cdlatex-tab))
  5201. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5202. (or (not (bolp))
  5203. (not (looking-at outline-regexp))))
  5204. (call-interactively (global-key-binding "\t")))
  5205. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5206. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5207. (or (and (eq org-cycle-emulate-tab 'white)
  5208. (= (match-end 0) (point-at-eol)))
  5209. (and (eq org-cycle-emulate-tab 'whitestart)
  5210. (>= (match-end 0) pos))))
  5211. t
  5212. (eq org-cycle-emulate-tab t))
  5213. ; (if (and (looking-at "[ \n\r\t]")
  5214. ; (string-match "^[ \t]*$" (buffer-substring
  5215. ; (point-at-bol) (point))))
  5216. ; (progn
  5217. ; (beginning-of-line 1)
  5218. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5219. (call-interactively (global-key-binding "\t")))
  5220. (t (save-excursion
  5221. (org-back-to-heading)
  5222. (org-cycle))))))
  5223. ;;;###autoload
  5224. (defun org-global-cycle (&optional arg)
  5225. "Cycle the global visibility. For details see `org-cycle'."
  5226. (interactive "P")
  5227. (let ((org-cycle-include-plain-lists
  5228. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5229. (if (integerp arg)
  5230. (progn
  5231. (show-all)
  5232. (hide-sublevels arg)
  5233. (setq org-cycle-global-status 'contents))
  5234. (org-cycle '(4)))))
  5235. (defun org-overview ()
  5236. "Switch to overview mode, shoing only top-level headlines.
  5237. Really, this shows all headlines with level equal or greater than the level
  5238. of the first headline in the buffer. This is important, because if the
  5239. first headline is not level one, then (hide-sublevels 1) gives confusing
  5240. results."
  5241. (interactive)
  5242. (let ((level (save-excursion
  5243. (goto-char (point-min))
  5244. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5245. (progn
  5246. (goto-char (match-beginning 0))
  5247. (funcall outline-level))))))
  5248. (and level (hide-sublevels level))))
  5249. (defun org-content (&optional arg)
  5250. "Show all headlines in the buffer, like a table of contents.
  5251. With numerical argument N, show content up to level N."
  5252. (interactive "P")
  5253. (save-excursion
  5254. ;; Visit all headings and show their offspring
  5255. (and (integerp arg) (org-overview))
  5256. (goto-char (point-max))
  5257. (catch 'exit
  5258. (while (and (progn (condition-case nil
  5259. (outline-previous-visible-heading 1)
  5260. (error (goto-char (point-min))))
  5261. t)
  5262. (looking-at outline-regexp))
  5263. (if (integerp arg)
  5264. (show-children (1- arg))
  5265. (show-branches))
  5266. (if (bobp) (throw 'exit nil))))))
  5267. (defun org-optimize-window-after-visibility-change (state)
  5268. "Adjust the window after a change in outline visibility.
  5269. This function is the default value of the hook `org-cycle-hook'."
  5270. (when (get-buffer-window (current-buffer))
  5271. (cond
  5272. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5273. ; ((eq state 'overview) (org-beginning-of-line))
  5274. ((eq state 'content) nil)
  5275. ((eq state 'all) nil)
  5276. ((eq state 'folded) nil)
  5277. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5278. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5279. (defun org-compact-display-after-subtree-move ()
  5280. (let (beg end)
  5281. (save-excursion
  5282. (if (org-up-heading-safe)
  5283. (progn
  5284. (hide-subtree)
  5285. (show-entry)
  5286. (show-children)
  5287. (org-cycle-show-empty-lines 'children)
  5288. (org-cycle-hide-drawers 'children))
  5289. (org-overview)))))
  5290. (defun org-cycle-show-empty-lines (state)
  5291. "Show empty lines above all visible headlines.
  5292. The region to be covered depends on STATE when called through
  5293. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5294. entire buffer covered. Note that an empty line is only shown if there
  5295. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5296. (when (> org-cycle-separator-lines 0)
  5297. (save-excursion
  5298. (let* ((n org-cycle-separator-lines)
  5299. (re (cond
  5300. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5301. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5302. (t (let ((ns (number-to-string (- n 2))))
  5303. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5304. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5305. beg end)
  5306. (cond
  5307. ((memq state '(overview contents t))
  5308. (setq beg (point-min) end (point-max)))
  5309. ((memq state '(children folded))
  5310. (setq beg (point) end (progn (org-end-of-subtree t t)
  5311. (beginning-of-line 2)
  5312. (point)))))
  5313. (when beg
  5314. (goto-char beg)
  5315. (while (re-search-forward re end t)
  5316. (if (not (get-char-property (match-end 1) 'invisible))
  5317. (outline-flag-region
  5318. (match-beginning 1) (match-end 1) nil)))))))
  5319. ;; Never hide empty lines at the end of the file.
  5320. (save-excursion
  5321. (goto-char (point-max))
  5322. (outline-previous-heading)
  5323. (outline-end-of-heading)
  5324. (if (and (looking-at "[ \t\n]+")
  5325. (= (match-end 0) (point-max)))
  5326. (outline-flag-region (point) (match-end 0) nil))))
  5327. (defun org-subtree-end-visible-p ()
  5328. "Is the end of the current subtree visible?"
  5329. (pos-visible-in-window-p
  5330. (save-excursion (org-end-of-subtree t) (point))))
  5331. (defun org-first-headline-recenter (&optional N)
  5332. "Move cursor to the first headline and recenter the headline.
  5333. Optional argument N means, put the headline into the Nth line of the window."
  5334. (goto-char (point-min))
  5335. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5336. (beginning-of-line)
  5337. (recenter (prefix-numeric-value N))))
  5338. ;;; Org-goto
  5339. (defvar org-goto-window-configuration nil)
  5340. (defvar org-goto-marker nil)
  5341. (defvar org-goto-map
  5342. (let ((map (make-sparse-keymap)))
  5343. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5344. (while (setq cmd (pop cmds))
  5345. (substitute-key-definition cmd cmd map global-map)))
  5346. (suppress-keymap map)
  5347. (org-defkey map "\C-m" 'org-goto-ret)
  5348. (org-defkey map [(return)] 'org-goto-ret)
  5349. (org-defkey map [(left)] 'org-goto-left)
  5350. (org-defkey map [(right)] 'org-goto-right)
  5351. (org-defkey map [(control ?g)] 'org-goto-quit)
  5352. (org-defkey map "\C-i" 'org-cycle)
  5353. (org-defkey map [(tab)] 'org-cycle)
  5354. (org-defkey map [(down)] 'outline-next-visible-heading)
  5355. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5356. (if org-goto-auto-isearch
  5357. (if (fboundp 'define-key-after)
  5358. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5359. nil)
  5360. (org-defkey map "q" 'org-goto-quit)
  5361. (org-defkey map "n" 'outline-next-visible-heading)
  5362. (org-defkey map "p" 'outline-previous-visible-heading)
  5363. (org-defkey map "f" 'outline-forward-same-level)
  5364. (org-defkey map "b" 'outline-backward-same-level)
  5365. (org-defkey map "u" 'outline-up-heading))
  5366. (org-defkey map "/" 'org-occur)
  5367. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5368. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5369. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5370. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5371. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5372. map))
  5373. (defconst org-goto-help
  5374. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5375. RET=jump to location [Q]uit and return to previous location
  5376. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5377. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5378. (defun org-goto (&optional alternative-interface)
  5379. "Look up a different location in the current file, keeping current visibility.
  5380. When you want look-up or go to a different location in a document, the
  5381. fastest way is often to fold the entire buffer and then dive into the tree.
  5382. This method has the disadvantage, that the previous location will be folded,
  5383. which may not be what you want.
  5384. This command works around this by showing a copy of the current buffer
  5385. in an indirect buffer, in overview mode. You can dive into the tree in
  5386. that copy, use org-occur and incremental search to find a location.
  5387. When pressing RET or `Q', the command returns to the original buffer in
  5388. which the visibility is still unchanged. After RET is will also jump to
  5389. the location selected in the indirect buffer and expose the
  5390. the headline hierarchy above."
  5391. (interactive "P")
  5392. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5393. (org-refile-use-outline-path t)
  5394. (interface
  5395. (if (not alternative-interface)
  5396. org-goto-interface
  5397. (if (eq org-goto-interface 'outline)
  5398. 'outline-path-completion
  5399. 'outline)))
  5400. (org-goto-start-pos (point))
  5401. (selected-point
  5402. (if (eq interface 'outline)
  5403. (car (org-get-location (current-buffer) org-goto-help))
  5404. (nth 3 (org-refile-get-location "Goto: ")))))
  5405. (if selected-point
  5406. (progn
  5407. (org-mark-ring-push org-goto-start-pos)
  5408. (goto-char selected-point)
  5409. (if (or (org-invisible-p) (org-invisible-p2))
  5410. (org-show-context 'org-goto)))
  5411. (message "Quit"))))
  5412. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5413. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5414. (defvar org-goto-local-auto-isearch-map) ; defined below
  5415. (defun org-get-location (buf help)
  5416. "Let the user select a location in the Org-mode buffer BUF.
  5417. This function uses a recursive edit. It returns the selected position
  5418. or nil."
  5419. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5420. (isearch-hide-immediately nil)
  5421. (isearch-search-fun-function
  5422. (lambda () 'org-goto-local-search-forward-headings))
  5423. (org-goto-selected-point org-goto-exit-command))
  5424. (save-excursion
  5425. (save-window-excursion
  5426. (delete-other-windows)
  5427. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5428. (switch-to-buffer
  5429. (condition-case nil
  5430. (make-indirect-buffer (current-buffer) "*org-goto*")
  5431. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5432. (with-output-to-temp-buffer "*Help*"
  5433. (princ help))
  5434. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5435. (setq buffer-read-only nil)
  5436. (let ((org-startup-truncated t)
  5437. (org-startup-folded nil)
  5438. (org-startup-align-all-tables nil))
  5439. (org-mode)
  5440. (org-overview))
  5441. (setq buffer-read-only t)
  5442. (if (and (boundp 'org-goto-start-pos)
  5443. (integer-or-marker-p org-goto-start-pos))
  5444. (let ((org-show-hierarchy-above t)
  5445. (org-show-siblings t)
  5446. (org-show-following-heading t))
  5447. (goto-char org-goto-start-pos)
  5448. (and (org-invisible-p) (org-show-context)))
  5449. (goto-char (point-min)))
  5450. (org-beginning-of-line)
  5451. (message "Select location and press RET")
  5452. (use-local-map org-goto-map)
  5453. (recursive-edit)
  5454. ))
  5455. (kill-buffer "*org-goto*")
  5456. (cons org-goto-selected-point org-goto-exit-command)))
  5457. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5458. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5459. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5460. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5461. (defun org-goto-local-search-forward-headings (string bound noerror)
  5462. "Search and make sure that anu matches are in headlines."
  5463. (catch 'return
  5464. (while (search-forward string bound noerror)
  5465. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5466. (and (member :headline context)
  5467. (not (member :tags context))))
  5468. (throw 'return (point))))))
  5469. (defun org-goto-local-auto-isearch ()
  5470. "Start isearch."
  5471. (interactive)
  5472. (goto-char (point-min))
  5473. (let ((keys (this-command-keys)))
  5474. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5475. (isearch-mode t)
  5476. (isearch-process-search-char (string-to-char keys)))))
  5477. (defun org-goto-ret (&optional arg)
  5478. "Finish `org-goto' by going to the new location."
  5479. (interactive "P")
  5480. (setq org-goto-selected-point (point)
  5481. org-goto-exit-command 'return)
  5482. (throw 'exit nil))
  5483. (defun org-goto-left ()
  5484. "Finish `org-goto' by going to the new location."
  5485. (interactive)
  5486. (if (org-on-heading-p)
  5487. (progn
  5488. (beginning-of-line 1)
  5489. (setq org-goto-selected-point (point)
  5490. org-goto-exit-command 'left)
  5491. (throw 'exit nil))
  5492. (error "Not on a heading")))
  5493. (defun org-goto-right ()
  5494. "Finish `org-goto' by going to the new location."
  5495. (interactive)
  5496. (if (org-on-heading-p)
  5497. (progn
  5498. (setq org-goto-selected-point (point)
  5499. org-goto-exit-command 'right)
  5500. (throw 'exit nil))
  5501. (error "Not on a heading")))
  5502. (defun org-goto-quit ()
  5503. "Finish `org-goto' without cursor motion."
  5504. (interactive)
  5505. (setq org-goto-selected-point nil)
  5506. (setq org-goto-exit-command 'quit)
  5507. (throw 'exit nil))
  5508. ;;; Indirect buffer display of subtrees
  5509. (defvar org-indirect-dedicated-frame nil
  5510. "This is the frame being used for indirect tree display.")
  5511. (defvar org-last-indirect-buffer nil)
  5512. (defun org-tree-to-indirect-buffer (&optional arg)
  5513. "Create indirect buffer and narrow it to current subtree.
  5514. With numerical prefix ARG, go up to this level and then take that tree.
  5515. If ARG is negative, go up that many levels.
  5516. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5517. indirect buffer previously made with this command, to avoid proliferation of
  5518. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5519. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5520. is kept so that you can work with several indirect buffers at the same time.
  5521. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5522. requests that a new frame be made for the new buffer, so that the dedicated
  5523. frame is not changed."
  5524. (interactive "P")
  5525. (let ((cbuf (current-buffer))
  5526. (cwin (selected-window))
  5527. (pos (point))
  5528. beg end level heading ibuf)
  5529. (save-excursion
  5530. (org-back-to-heading t)
  5531. (when (numberp arg)
  5532. (setq level (org-outline-level))
  5533. (if (< arg 0) (setq arg (+ level arg)))
  5534. (while (> (setq level (org-outline-level)) arg)
  5535. (outline-up-heading 1 t)))
  5536. (setq beg (point)
  5537. heading (org-get-heading))
  5538. (org-end-of-subtree t) (setq end (point)))
  5539. (if (and (buffer-live-p org-last-indirect-buffer)
  5540. (not (eq org-indirect-buffer-display 'new-frame))
  5541. (not arg))
  5542. (kill-buffer org-last-indirect-buffer))
  5543. (setq ibuf (org-get-indirect-buffer cbuf)
  5544. org-last-indirect-buffer ibuf)
  5545. (cond
  5546. ((or (eq org-indirect-buffer-display 'new-frame)
  5547. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5548. (select-frame (make-frame))
  5549. (delete-other-windows)
  5550. (switch-to-buffer ibuf)
  5551. (org-set-frame-title heading))
  5552. ((eq org-indirect-buffer-display 'dedicated-frame)
  5553. (raise-frame
  5554. (select-frame (or (and org-indirect-dedicated-frame
  5555. (frame-live-p org-indirect-dedicated-frame)
  5556. org-indirect-dedicated-frame)
  5557. (setq org-indirect-dedicated-frame (make-frame)))))
  5558. (delete-other-windows)
  5559. (switch-to-buffer ibuf)
  5560. (org-set-frame-title (concat "Indirect: " heading)))
  5561. ((eq org-indirect-buffer-display 'current-window)
  5562. (switch-to-buffer ibuf))
  5563. ((eq org-indirect-buffer-display 'other-window)
  5564. (pop-to-buffer ibuf))
  5565. (t (error "Invalid value.")))
  5566. (if (featurep 'xemacs)
  5567. (save-excursion (org-mode) (turn-on-font-lock)))
  5568. (narrow-to-region beg end)
  5569. (show-all)
  5570. (goto-char pos)
  5571. (and (window-live-p cwin) (select-window cwin))))
  5572. (defun org-get-indirect-buffer (&optional buffer)
  5573. (setq buffer (or buffer (current-buffer)))
  5574. (let ((n 1) (base (buffer-name buffer)) bname)
  5575. (while (buffer-live-p
  5576. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5577. (setq n (1+ n)))
  5578. (condition-case nil
  5579. (make-indirect-buffer buffer bname 'clone)
  5580. (error (make-indirect-buffer buffer bname)))))
  5581. (defun org-set-frame-title (title)
  5582. "Set the title of the current frame to the string TITLE."
  5583. ;; FIXME: how to name a single frame in XEmacs???
  5584. (unless (featurep 'xemacs)
  5585. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5586. ;;;; Structure editing
  5587. ;;; Inserting headlines
  5588. (defun org-insert-heading (&optional force-heading)
  5589. "Insert a new heading or item with same depth at point.
  5590. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5591. If point is at the beginning of a headline, insert a sibling before the
  5592. current headline. If point is not at the beginning, do not split the line,
  5593. but create the new hedline after the current line."
  5594. (interactive "P")
  5595. (if (= (buffer-size) 0)
  5596. (insert "\n* ")
  5597. (when (or force-heading (not (org-insert-item)))
  5598. (let* ((head (save-excursion
  5599. (condition-case nil
  5600. (progn
  5601. (org-back-to-heading)
  5602. (match-string 0))
  5603. (error "*"))))
  5604. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5605. pos)
  5606. (cond
  5607. ((and (org-on-heading-p) (bolp)
  5608. (or (bobp)
  5609. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5610. ;; insert before the current line
  5611. (open-line (if blank 2 1)))
  5612. ((and (bolp)
  5613. (or (bobp)
  5614. (save-excursion
  5615. (backward-char 1) (not (org-invisible-p)))))
  5616. ;; insert right here
  5617. nil)
  5618. (t
  5619. ; ;; in the middle of the line
  5620. ; (org-show-entry)
  5621. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5622. ; (if (and
  5623. ; (org-on-heading-p)
  5624. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5625. ; ;; protect the tags
  5626. ;; (let ((tags (match-string 2)) pos)
  5627. ; (delete-region (match-beginning 1) (match-end 1))
  5628. ; (setq pos (point-at-bol))
  5629. ; (newline (if blank 2 1))
  5630. ; (save-excursion
  5631. ; (goto-char pos)
  5632. ; (end-of-line 1)
  5633. ; (insert " " tags)
  5634. ; (org-set-tags nil 'align)))
  5635. ; (newline (if blank 2 1)))
  5636. ; (newline (if blank 2 1))))
  5637. ;; in the middle of the line
  5638. (org-show-entry)
  5639. (let ((split
  5640. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5641. tags pos)
  5642. (if (org-on-heading-p)
  5643. (progn
  5644. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5645. (setq tags (and (match-end 2) (match-string 2)))
  5646. (and (match-end 1)
  5647. (delete-region (match-beginning 1) (match-end 1)))
  5648. (setq pos (point-at-bol))
  5649. (or split (end-of-line 1))
  5650. (delete-horizontal-space)
  5651. (newline (if blank 2 1))
  5652. (when tags
  5653. (save-excursion
  5654. (goto-char pos)
  5655. (end-of-line 1)
  5656. (insert " " tags)
  5657. (org-set-tags nil 'align))))
  5658. (or split (end-of-line 1))
  5659. (newline (if blank 2 1))))))
  5660. (insert head) (just-one-space)
  5661. (setq pos (point))
  5662. (end-of-line 1)
  5663. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5664. (run-hooks 'org-insert-heading-hook)))))
  5665. (defun org-insert-heading-after-current ()
  5666. "Insert a new heading with same level as current, after current subtree."
  5667. (interactive)
  5668. (org-back-to-heading)
  5669. (org-insert-heading)
  5670. (org-move-subtree-down)
  5671. (end-of-line 1))
  5672. (defun org-insert-todo-heading (arg)
  5673. "Insert a new heading with the same level and TODO state as current heading.
  5674. If the heading has no TODO state, or if the state is DONE, use the first
  5675. state (TODO by default). Also with prefix arg, force first state."
  5676. (interactive "P")
  5677. (when (not (org-insert-item 'checkbox))
  5678. (org-insert-heading)
  5679. (save-excursion
  5680. (org-back-to-heading)
  5681. (outline-previous-heading)
  5682. (looking-at org-todo-line-regexp))
  5683. (if (or arg
  5684. (not (match-beginning 2))
  5685. (member (match-string 2) org-done-keywords))
  5686. (insert (car org-todo-keywords-1) " ")
  5687. (insert (match-string 2) " "))))
  5688. (defun org-insert-subheading (arg)
  5689. "Insert a new subheading and demote it.
  5690. Works for outline headings and for plain lists alike."
  5691. (interactive "P")
  5692. (org-insert-heading arg)
  5693. (cond
  5694. ((org-on-heading-p) (org-do-demote))
  5695. ((org-at-item-p) (org-indent-item 1))))
  5696. (defun org-insert-todo-subheading (arg)
  5697. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5698. Works for outline headings and for plain lists alike."
  5699. (interactive "P")
  5700. (org-insert-todo-heading arg)
  5701. (cond
  5702. ((org-on-heading-p) (org-do-demote))
  5703. ((org-at-item-p) (org-indent-item 1))))
  5704. ;;; Promotion and Demotion
  5705. (defun org-promote-subtree ()
  5706. "Promote the entire subtree.
  5707. See also `org-promote'."
  5708. (interactive)
  5709. (save-excursion
  5710. (org-map-tree 'org-promote))
  5711. (org-fix-position-after-promote))
  5712. (defun org-demote-subtree ()
  5713. "Demote the entire subtree. See `org-demote'.
  5714. See also `org-promote'."
  5715. (interactive)
  5716. (save-excursion
  5717. (org-map-tree 'org-demote))
  5718. (org-fix-position-after-promote))
  5719. (defun org-do-promote ()
  5720. "Promote the current heading higher up the tree.
  5721. If the region is active in `transient-mark-mode', promote all headings
  5722. in the region."
  5723. (interactive)
  5724. (save-excursion
  5725. (if (org-region-active-p)
  5726. (org-map-region 'org-promote (region-beginning) (region-end))
  5727. (org-promote)))
  5728. (org-fix-position-after-promote))
  5729. (defun org-do-demote ()
  5730. "Demote the current heading lower down the tree.
  5731. If the region is active in `transient-mark-mode', demote all headings
  5732. in the region."
  5733. (interactive)
  5734. (save-excursion
  5735. (if (org-region-active-p)
  5736. (org-map-region 'org-demote (region-beginning) (region-end))
  5737. (org-demote)))
  5738. (org-fix-position-after-promote))
  5739. (defun org-fix-position-after-promote ()
  5740. "Make sure that after pro/demotion cursor position is right."
  5741. (let ((pos (point)))
  5742. (when (save-excursion
  5743. (beginning-of-line 1)
  5744. (looking-at org-todo-line-regexp)
  5745. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5746. (cond ((eobp) (insert " "))
  5747. ((eolp) (insert " "))
  5748. ((equal (char-after) ?\ ) (forward-char 1))))))
  5749. (defun org-reduced-level (l)
  5750. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5751. (defun org-get-legal-level (level &optional change)
  5752. "Rectify a level change under the influence of `org-odd-levels-only'
  5753. LEVEL is a current level, CHANGE is by how much the level should be
  5754. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5755. even level numbers will become the next higher odd number."
  5756. (if org-odd-levels-only
  5757. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5758. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5759. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5760. (max 1 (+ level change))))
  5761. (defun org-promote ()
  5762. "Promote the current heading higher up the tree.
  5763. If the region is active in `transient-mark-mode', promote all headings
  5764. in the region."
  5765. (org-back-to-heading t)
  5766. (let* ((level (save-match-data (funcall outline-level)))
  5767. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5768. (diff (abs (- level (length up-head) -1))))
  5769. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5770. (replace-match up-head nil t)
  5771. ;; Fixup tag positioning
  5772. (and org-auto-align-tags (org-set-tags nil t))
  5773. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5774. (defun org-demote ()
  5775. "Demote the current heading lower down the tree.
  5776. If the region is active in `transient-mark-mode', demote all headings
  5777. in the region."
  5778. (org-back-to-heading t)
  5779. (let* ((level (save-match-data (funcall outline-level)))
  5780. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5781. (diff (abs (- level (length down-head) -1))))
  5782. (replace-match down-head nil t)
  5783. ;; Fixup tag positioning
  5784. (and org-auto-align-tags (org-set-tags nil t))
  5785. (if org-adapt-indentation (org-fixup-indentation diff))))
  5786. (defun org-map-tree (fun)
  5787. "Call FUN for every heading underneath the current one."
  5788. (org-back-to-heading)
  5789. (let ((level (funcall outline-level)))
  5790. (save-excursion
  5791. (funcall fun)
  5792. (while (and (progn
  5793. (outline-next-heading)
  5794. (> (funcall outline-level) level))
  5795. (not (eobp)))
  5796. (funcall fun)))))
  5797. (defun org-map-region (fun beg end)
  5798. "Call FUN for every heading between BEG and END."
  5799. (let ((org-ignore-region t))
  5800. (save-excursion
  5801. (setq end (copy-marker end))
  5802. (goto-char beg)
  5803. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5804. (< (point) end))
  5805. (funcall fun))
  5806. (while (and (progn
  5807. (outline-next-heading)
  5808. (< (point) end))
  5809. (not (eobp)))
  5810. (funcall fun)))))
  5811. (defun org-fixup-indentation (diff)
  5812. "Change the indentation in the current entry by DIFF
  5813. However, if any line in the current entry has no indentation, or if it
  5814. would end up with no indentation after the change, nothing at all is done."
  5815. (save-excursion
  5816. (let ((end (save-excursion (outline-next-heading)
  5817. (point-marker)))
  5818. (prohibit (if (> diff 0)
  5819. "^\\S-"
  5820. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5821. col)
  5822. (unless (save-excursion (end-of-line 1)
  5823. (re-search-forward prohibit end t))
  5824. (while (and (< (point) end)
  5825. (re-search-forward "^[ \t]+" end t))
  5826. (goto-char (match-end 0))
  5827. (setq col (current-column))
  5828. (if (< diff 0) (replace-match ""))
  5829. (indent-to (+ diff col))))
  5830. (move-marker end nil))))
  5831. (defun org-convert-to-odd-levels ()
  5832. "Convert an org-mode file with all levels allowed to one with odd levels.
  5833. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5834. level 5 etc."
  5835. (interactive)
  5836. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5837. (let ((org-odd-levels-only nil) n)
  5838. (save-excursion
  5839. (goto-char (point-min))
  5840. (while (re-search-forward "^\\*\\*+ " nil t)
  5841. (setq n (- (length (match-string 0)) 2))
  5842. (while (>= (setq n (1- n)) 0)
  5843. (org-demote))
  5844. (end-of-line 1))))))
  5845. (defun org-convert-to-oddeven-levels ()
  5846. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5847. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5848. section with an even level, conversion would destroy the structure of the file. An error
  5849. is signaled in this case."
  5850. (interactive)
  5851. (goto-char (point-min))
  5852. ;; First check if there are no even levels
  5853. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5854. (org-show-context t)
  5855. (error "Not all levels are odd in this file. Conversion not possible."))
  5856. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5857. (let ((org-odd-levels-only nil) n)
  5858. (save-excursion
  5859. (goto-char (point-min))
  5860. (while (re-search-forward "^\\*\\*+ " nil t)
  5861. (setq n (/ (1- (length (match-string 0))) 2))
  5862. (while (>= (setq n (1- n)) 0)
  5863. (org-promote))
  5864. (end-of-line 1))))))
  5865. (defun org-tr-level (n)
  5866. "Make N odd if required."
  5867. (if org-odd-levels-only (1+ (/ n 2)) n))
  5868. ;;; Vertical tree motion, cutting and pasting of subtrees
  5869. (defun org-move-subtree-up (&optional arg)
  5870. "Move the current subtree up past ARG headlines of the same level."
  5871. (interactive "p")
  5872. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5873. (defun org-move-subtree-down (&optional arg)
  5874. "Move the current subtree down past ARG headlines of the same level."
  5875. (interactive "p")
  5876. (setq arg (prefix-numeric-value arg))
  5877. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5878. 'outline-get-last-sibling))
  5879. (ins-point (make-marker))
  5880. (cnt (abs arg))
  5881. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5882. ;; Select the tree
  5883. (org-back-to-heading)
  5884. (setq beg0 (point))
  5885. (save-excursion
  5886. (setq ne-beg (org-back-over-empty-lines))
  5887. (setq beg (point)))
  5888. (save-match-data
  5889. (save-excursion (outline-end-of-heading)
  5890. (setq folded (org-invisible-p)))
  5891. (outline-end-of-subtree))
  5892. (outline-next-heading)
  5893. (setq ne-end (org-back-over-empty-lines))
  5894. (setq end (point))
  5895. (goto-char beg0)
  5896. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5897. ;; include less whitespace
  5898. (save-excursion
  5899. (goto-char beg)
  5900. (forward-line (- ne-beg ne-end))
  5901. (setq beg (point))))
  5902. ;; Find insertion point, with error handling
  5903. (while (> cnt 0)
  5904. (or (and (funcall movfunc) (looking-at outline-regexp))
  5905. (progn (goto-char beg0)
  5906. (error "Cannot move past superior level or buffer limit")))
  5907. (setq cnt (1- cnt)))
  5908. (if (> arg 0)
  5909. ;; Moving forward - still need to move over subtree
  5910. (progn (org-end-of-subtree t t)
  5911. (save-excursion
  5912. (org-back-over-empty-lines)
  5913. (or (bolp) (newline)))))
  5914. (setq ne-ins (org-back-over-empty-lines))
  5915. (move-marker ins-point (point))
  5916. (setq txt (buffer-substring beg end))
  5917. (delete-region beg end)
  5918. (outline-flag-region (1- beg) beg nil)
  5919. (outline-flag-region (1- (point)) (point) nil)
  5920. (insert txt)
  5921. (or (bolp) (insert "\n"))
  5922. (setq ins-end (point))
  5923. (goto-char ins-point)
  5924. (org-skip-whitespace)
  5925. (when (and (< arg 0)
  5926. (org-first-sibling-p)
  5927. (> ne-ins ne-beg))
  5928. ;; Move whitespace back to beginning
  5929. (save-excursion
  5930. (goto-char ins-end)
  5931. (let ((kill-whole-line t))
  5932. (kill-line (- ne-ins ne-beg)) (point)))
  5933. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5934. (move-marker ins-point nil)
  5935. (org-compact-display-after-subtree-move)
  5936. (unless folded
  5937. (org-show-entry)
  5938. (show-children)
  5939. (org-cycle-hide-drawers 'children))))
  5940. (defvar org-subtree-clip ""
  5941. "Clipboard for cut and paste of subtrees.
  5942. This is actually only a copy of the kill, because we use the normal kill
  5943. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5944. (defvar org-subtree-clip-folded nil
  5945. "Was the last copied subtree folded?
  5946. This is used to fold the tree back after pasting.")
  5947. (defun org-cut-subtree (&optional n)
  5948. "Cut the current subtree into the clipboard.
  5949. With prefix arg N, cut this many sequential subtrees.
  5950. This is a short-hand for marking the subtree and then cutting it."
  5951. (interactive "p")
  5952. (org-copy-subtree n 'cut))
  5953. (defun org-copy-subtree (&optional n cut)
  5954. "Cut the current subtree into the clipboard.
  5955. With prefix arg N, cut this many sequential subtrees.
  5956. This is a short-hand for marking the subtree and then copying it.
  5957. If CUT is non-nil, actually cut the subtree."
  5958. (interactive "p")
  5959. (let (beg end folded (beg0 (point)))
  5960. (if (interactive-p)
  5961. (org-back-to-heading nil) ; take what looks like a subtree
  5962. (org-back-to-heading t)) ; take what is really there
  5963. (org-back-over-empty-lines)
  5964. (setq beg (point))
  5965. (skip-chars-forward " \t\r\n")
  5966. (save-match-data
  5967. (save-excursion (outline-end-of-heading)
  5968. (setq folded (org-invisible-p)))
  5969. (condition-case nil
  5970. (outline-forward-same-level (1- n))
  5971. (error nil))
  5972. (org-end-of-subtree t t))
  5973. (org-back-over-empty-lines)
  5974. (setq end (point))
  5975. (goto-char beg0)
  5976. (when (> end beg)
  5977. (setq org-subtree-clip-folded folded)
  5978. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5979. (setq org-subtree-clip (current-kill 0))
  5980. (message "%s: Subtree(s) with %d characters"
  5981. (if cut "Cut" "Copied")
  5982. (length org-subtree-clip)))))
  5983. (defun org-paste-subtree (&optional level tree)
  5984. "Paste the clipboard as a subtree, with modification of headline level.
  5985. The entire subtree is promoted or demoted in order to match a new headline
  5986. level. By default, the new level is derived from the visible headings
  5987. before and after the insertion point, and taken to be the inferior headline
  5988. level of the two. So if the previous visible heading is level 3 and the
  5989. next is level 4 (or vice versa), level 4 will be used for insertion.
  5990. This makes sure that the subtree remains an independent subtree and does
  5991. not swallow low level entries.
  5992. You can also force a different level, either by using a numeric prefix
  5993. argument, or by inserting the heading marker by hand. For example, if the
  5994. cursor is after \"*****\", then the tree will be shifted to level 5.
  5995. If you want to insert the tree as is, just use \\[yank].
  5996. If optional TREE is given, use this text instead of the kill ring."
  5997. (interactive "P")
  5998. (unless (org-kill-is-subtree-p tree)
  5999. (error "%s"
  6000. (substitute-command-keys
  6001. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6002. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  6003. (^re (concat "^\\(" outline-regexp "\\)"))
  6004. (re (concat "\\(" outline-regexp "\\)"))
  6005. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6006. (old-level (if (string-match ^re txt)
  6007. (- (match-end 0) (match-beginning 0) 1)
  6008. -1))
  6009. (force-level (cond (level (prefix-numeric-value level))
  6010. ((string-match
  6011. ^re_ (buffer-substring (point-at-bol) (point)))
  6012. (- (match-end 1) (match-beginning 1)))
  6013. (t nil)))
  6014. (previous-level (save-excursion
  6015. (condition-case nil
  6016. (progn
  6017. (outline-previous-visible-heading 1)
  6018. (if (looking-at re)
  6019. (- (match-end 0) (match-beginning 0) 1)
  6020. 1))
  6021. (error 1))))
  6022. (next-level (save-excursion
  6023. (condition-case nil
  6024. (progn
  6025. (or (looking-at outline-regexp)
  6026. (outline-next-visible-heading 1))
  6027. (if (looking-at re)
  6028. (- (match-end 0) (match-beginning 0) 1)
  6029. 1))
  6030. (error 1))))
  6031. (new-level (or force-level (max previous-level next-level)))
  6032. (shift (if (or (= old-level -1)
  6033. (= new-level -1)
  6034. (= old-level new-level))
  6035. 0
  6036. (- new-level old-level)))
  6037. (delta (if (> shift 0) -1 1))
  6038. (func (if (> shift 0) 'org-demote 'org-promote))
  6039. (org-odd-levels-only nil)
  6040. beg end)
  6041. ;; Remove the forced level indicator
  6042. (if force-level
  6043. (delete-region (point-at-bol) (point)))
  6044. ;; Paste
  6045. (beginning-of-line 1)
  6046. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6047. (setq beg (point))
  6048. (insert-before-markers txt) ;; FIXME: correct fix????
  6049. (unless (string-match "\n\\'" txt) (insert "\n"))
  6050. (setq end (point))
  6051. (goto-char beg)
  6052. (skip-chars-forward " \t\n\r")
  6053. (setq beg (point))
  6054. ;; Shift if necessary
  6055. (unless (= shift 0)
  6056. (save-restriction
  6057. (narrow-to-region beg end)
  6058. (while (not (= shift 0))
  6059. (org-map-region func (point-min) (point-max))
  6060. (setq shift (+ delta shift)))
  6061. (goto-char (point-min))))
  6062. (when (interactive-p)
  6063. (message "Clipboard pasted as level %d subtree" new-level))
  6064. (if (and kill-ring
  6065. (eq org-subtree-clip (current-kill 0))
  6066. org-subtree-clip-folded)
  6067. ;; The tree was folded before it was killed/copied
  6068. (hide-subtree))))
  6069. (defun org-kill-is-subtree-p (&optional txt)
  6070. "Check if the current kill is an outline subtree, or a set of trees.
  6071. Returns nil if kill does not start with a headline, or if the first
  6072. headline level is not the largest headline level in the tree.
  6073. So this will actually accept several entries of equal levels as well,
  6074. which is OK for `org-paste-subtree'.
  6075. If optional TXT is given, check this string instead of the current kill."
  6076. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6077. (start-level (and kill
  6078. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6079. org-outline-regexp "\\)")
  6080. kill)
  6081. (- (match-end 2) (match-beginning 2) 1)))
  6082. (re (concat "^" org-outline-regexp))
  6083. (start (1+ (match-beginning 2))))
  6084. (if (not start-level)
  6085. (progn
  6086. nil) ;; does not even start with a heading
  6087. (catch 'exit
  6088. (while (setq start (string-match re kill (1+ start)))
  6089. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6090. (throw 'exit nil)))
  6091. t))))
  6092. (defun org-narrow-to-subtree ()
  6093. "Narrow buffer to the current subtree."
  6094. (interactive)
  6095. (save-excursion
  6096. (save-match-data
  6097. (narrow-to-region
  6098. (progn (org-back-to-heading) (point))
  6099. (progn (org-end-of-subtree t t) (point))))))
  6100. ;;; Outline Sorting
  6101. (defun org-sort (with-case)
  6102. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6103. Optional argument WITH-CASE means sort case-sensitively."
  6104. (interactive "P")
  6105. (if (org-at-table-p)
  6106. (org-call-with-arg 'org-table-sort-lines with-case)
  6107. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6108. (defvar org-priority-regexp) ; defined later in the file
  6109. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6110. "Sort entries on a certain level of an outline tree.
  6111. If there is an active region, the entries in the region are sorted.
  6112. Else, if the cursor is before the first entry, sort the top-level items.
  6113. Else, the children of the entry at point are sorted.
  6114. Sorting can be alphabetically, numerically, and by date/time as given by
  6115. the first time stamp in the entry. The command prompts for the sorting
  6116. type unless it has been given to the function through the SORTING-TYPE
  6117. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6118. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6119. called with point at the beginning of the record. It must return either
  6120. a string or a number that should serve as the sorting key for that record.
  6121. Comparing entries ignores case by default. However, with an optional argument
  6122. WITH-CASE, the sorting considers case as well."
  6123. (interactive "P")
  6124. (let ((case-func (if with-case 'identity 'downcase))
  6125. start beg end stars re re2
  6126. txt what tmp plain-list-p)
  6127. ;; Find beginning and end of region to sort
  6128. (cond
  6129. ((org-region-active-p)
  6130. ;; we will sort the region
  6131. (setq end (region-end)
  6132. what "region")
  6133. (goto-char (region-beginning))
  6134. (if (not (org-on-heading-p)) (outline-next-heading))
  6135. (setq start (point)))
  6136. ((org-at-item-p)
  6137. ;; we will sort this plain list
  6138. (org-beginning-of-item-list) (setq start (point))
  6139. (org-end-of-item-list) (setq end (point))
  6140. (goto-char start)
  6141. (setq plain-list-p t
  6142. what "plain list"))
  6143. ((or (org-on-heading-p)
  6144. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6145. ;; we will sort the children of the current headline
  6146. (org-back-to-heading)
  6147. (setq start (point)
  6148. end (progn (org-end-of-subtree t t)
  6149. (org-back-over-empty-lines)
  6150. (point))
  6151. what "children")
  6152. (goto-char start)
  6153. (show-subtree)
  6154. (outline-next-heading))
  6155. (t
  6156. ;; we will sort the top-level entries in this file
  6157. (goto-char (point-min))
  6158. (or (org-on-heading-p) (outline-next-heading))
  6159. (setq start (point) end (point-max) what "top-level")
  6160. (goto-char start)
  6161. (show-all)))
  6162. (setq beg (point))
  6163. (if (>= beg end) (error "Nothing to sort"))
  6164. (unless plain-list-p
  6165. (looking-at "\\(\\*+\\)")
  6166. (setq stars (match-string 1)
  6167. re (concat "^" (regexp-quote stars) " +")
  6168. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6169. txt (buffer-substring beg end))
  6170. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6171. (if (and (not (equal stars "*")) (string-match re2 txt))
  6172. (error "Region to sort contains a level above the first entry")))
  6173. (unless sorting-type
  6174. (message
  6175. (if plain-list-p
  6176. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6177. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6178. what)
  6179. (setq sorting-type (read-char-exclusive))
  6180. (and (= (downcase sorting-type) ?f)
  6181. (setq getkey-func
  6182. (completing-read "Sort using function: "
  6183. obarray 'fboundp t nil nil))
  6184. (setq getkey-func (intern getkey-func)))
  6185. (and (= (downcase sorting-type) ?r)
  6186. (setq property
  6187. (completing-read "Property: "
  6188. (mapcar 'list (org-buffer-property-keys t))
  6189. nil t))))
  6190. (message "Sorting entries...")
  6191. (save-restriction
  6192. (narrow-to-region start end)
  6193. (let ((dcst (downcase sorting-type))
  6194. (now (current-time)))
  6195. (sort-subr
  6196. (/= dcst sorting-type)
  6197. ;; This function moves to the beginning character of the "record" to
  6198. ;; be sorted.
  6199. (if plain-list-p
  6200. (lambda nil
  6201. (if (org-at-item-p) t (goto-char (point-max))))
  6202. (lambda nil
  6203. (if (re-search-forward re nil t)
  6204. (goto-char (match-beginning 0))
  6205. (goto-char (point-max)))))
  6206. ;; This function moves to the last character of the "record" being
  6207. ;; sorted.
  6208. (if plain-list-p
  6209. 'org-end-of-item
  6210. (lambda nil
  6211. (save-match-data
  6212. (condition-case nil
  6213. (outline-forward-same-level 1)
  6214. (error
  6215. (goto-char (point-max)))))))
  6216. ;; This function returns the value that gets sorted against.
  6217. (if plain-list-p
  6218. (lambda nil
  6219. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6220. (cond
  6221. ((= dcst ?n)
  6222. (string-to-number (buffer-substring (match-end 0)
  6223. (point-at-eol))))
  6224. ((= dcst ?a)
  6225. (buffer-substring (match-end 0) (point-at-eol)))
  6226. ((= dcst ?t)
  6227. (if (re-search-forward org-ts-regexp
  6228. (point-at-eol) t)
  6229. (org-time-string-to-time (match-string 0))
  6230. now))
  6231. ((= dcst ?f)
  6232. (if getkey-func
  6233. (progn
  6234. (setq tmp (funcall getkey-func))
  6235. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6236. tmp)
  6237. (error "Invalid key function `%s'" getkey-func)))
  6238. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6239. (lambda nil
  6240. (cond
  6241. ((= dcst ?n)
  6242. (if (looking-at outline-regexp)
  6243. (string-to-number (buffer-substring (match-end 0)
  6244. (point-at-eol)))
  6245. nil))
  6246. ((= dcst ?a)
  6247. (funcall case-func (buffer-substring (point-at-bol)
  6248. (point-at-eol))))
  6249. ((= dcst ?t)
  6250. (if (re-search-forward org-ts-regexp
  6251. (save-excursion
  6252. (forward-line 2)
  6253. (point)) t)
  6254. (org-time-string-to-time (match-string 0))
  6255. now))
  6256. ((= dcst ?p)
  6257. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6258. (string-to-char (match-string 2))
  6259. org-default-priority))
  6260. ((= dcst ?r)
  6261. (or (org-entry-get nil property) ""))
  6262. ((= dcst ?f)
  6263. (if getkey-func
  6264. (progn
  6265. (setq tmp (funcall getkey-func))
  6266. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6267. tmp)
  6268. (error "Invalid key function `%s'" getkey-func)))
  6269. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6270. nil
  6271. (cond
  6272. ((= dcst ?a) 'string<)
  6273. ((= dcst ?t) 'time-less-p)
  6274. (t nil)))))
  6275. (message "Sorting entries...done")))
  6276. (defun org-do-sort (table what &optional with-case sorting-type)
  6277. "Sort TABLE of WHAT according to SORTING-TYPE.
  6278. The user will be prompted for the SORTING-TYPE if the call to this
  6279. function does not specify it. WHAT is only for the prompt, to indicate
  6280. what is being sorted. The sorting key will be extracted from
  6281. the car of the elements of the table.
  6282. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6283. (unless sorting-type
  6284. (message
  6285. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6286. what)
  6287. (setq sorting-type (read-char-exclusive)))
  6288. (let ((dcst (downcase sorting-type))
  6289. extractfun comparefun)
  6290. ;; Define the appropriate functions
  6291. (cond
  6292. ((= dcst ?n)
  6293. (setq extractfun 'string-to-number
  6294. comparefun (if (= dcst sorting-type) '< '>)))
  6295. ((= dcst ?a)
  6296. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6297. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6298. comparefun (if (= dcst sorting-type)
  6299. 'string<
  6300. (lambda (a b) (and (not (string< a b))
  6301. (not (string= a b)))))))
  6302. ((= dcst ?t)
  6303. (setq extractfun
  6304. (lambda (x)
  6305. (if (string-match org-ts-regexp x)
  6306. (time-to-seconds
  6307. (org-time-string-to-time (match-string 0 x)))
  6308. 0))
  6309. comparefun (if (= dcst sorting-type) '< '>)))
  6310. (t (error "Invalid sorting type `%c'" sorting-type)))
  6311. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6312. table)
  6313. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6314. ;;;; Plain list items, including checkboxes
  6315. ;;; Plain list items
  6316. (defun org-at-item-p ()
  6317. "Is point in a line starting a hand-formatted item?"
  6318. (let ((llt org-plain-list-ordered-item-terminator))
  6319. (save-excursion
  6320. (goto-char (point-at-bol))
  6321. (looking-at
  6322. (cond
  6323. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6324. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6325. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6326. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6327. (defun org-in-item-p ()
  6328. "It the cursor inside a plain list item.
  6329. Does not have to be the first line."
  6330. (save-excursion
  6331. (condition-case nil
  6332. (progn
  6333. (org-beginning-of-item)
  6334. (org-at-item-p)
  6335. t)
  6336. (error nil))))
  6337. (defun org-insert-item (&optional checkbox)
  6338. "Insert a new item at the current level.
  6339. Return t when things worked, nil when we are not in an item."
  6340. (when (save-excursion
  6341. (condition-case nil
  6342. (progn
  6343. (org-beginning-of-item)
  6344. (org-at-item-p)
  6345. (if (org-invisible-p) (error "Invisible item"))
  6346. t)
  6347. (error nil)))
  6348. (let* ((bul (match-string 0))
  6349. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6350. (match-end 0)))
  6351. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6352. pos)
  6353. (cond
  6354. ((and (org-at-item-p) (<= (point) eow))
  6355. ;; before the bullet
  6356. (beginning-of-line 1)
  6357. (open-line (if blank 2 1)))
  6358. ((<= (point) eow)
  6359. (beginning-of-line 1))
  6360. (t
  6361. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6362. (end-of-line 1)
  6363. (delete-horizontal-space))
  6364. (newline (if blank 2 1))))
  6365. (insert bul (if checkbox "[ ]" ""))
  6366. (just-one-space)
  6367. (setq pos (point))
  6368. (end-of-line 1)
  6369. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6370. (org-maybe-renumber-ordered-list)
  6371. (and checkbox (org-update-checkbox-count-maybe))
  6372. t))
  6373. ;;; Checkboxes
  6374. (defun org-at-item-checkbox-p ()
  6375. "Is point at a line starting a plain-list item with a checklet?"
  6376. (and (org-at-item-p)
  6377. (save-excursion
  6378. (goto-char (match-end 0))
  6379. (skip-chars-forward " \t")
  6380. (looking-at "\\[[- X]\\]"))))
  6381. (defun org-toggle-checkbox (&optional arg)
  6382. "Toggle the checkbox in the current line."
  6383. (interactive "P")
  6384. (catch 'exit
  6385. (let (beg end status (firstnew 'unknown))
  6386. (cond
  6387. ((org-region-active-p)
  6388. (setq beg (region-beginning) end (region-end)))
  6389. ((org-on-heading-p)
  6390. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6391. ((org-at-item-checkbox-p)
  6392. (let ((pos (point)))
  6393. (replace-match
  6394. (cond (arg "[-]")
  6395. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6396. (t "[ ]"))
  6397. t t)
  6398. (goto-char pos))
  6399. (throw 'exit t))
  6400. (t (error "Not at a checkbox or heading, and no active region")))
  6401. (save-excursion
  6402. (goto-char beg)
  6403. (while (< (point) end)
  6404. (when (org-at-item-checkbox-p)
  6405. (setq status (equal (match-string 0) "[X]"))
  6406. (when (eq firstnew 'unknown)
  6407. (setq firstnew (not status)))
  6408. (replace-match
  6409. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6410. (beginning-of-line 2)))))
  6411. (org-update-checkbox-count-maybe))
  6412. (defun org-update-checkbox-count-maybe ()
  6413. "Update checkbox statistics unless turned off by user."
  6414. (when org-provide-checkbox-statistics
  6415. (org-update-checkbox-count)))
  6416. (defun org-update-checkbox-count (&optional all)
  6417. "Update the checkbox statistics in the current section.
  6418. This will find all statistic cookies like [57%] and [6/12] and update them
  6419. with the current numbers. With optional prefix argument ALL, do this for
  6420. the whole buffer."
  6421. (interactive "P")
  6422. (save-excursion
  6423. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6424. (beg (condition-case nil
  6425. (progn (outline-back-to-heading) (point))
  6426. (error (point-min))))
  6427. (end (move-marker (make-marker)
  6428. (progn (outline-next-heading) (point))))
  6429. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6430. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6431. (re-find (concat re "\\|" re-box))
  6432. beg-cookie end-cookie is-percent c-on c-off lim
  6433. eline curr-ind next-ind continue-from startsearch
  6434. (cstat 0)
  6435. )
  6436. (when all
  6437. (goto-char (point-min))
  6438. (outline-next-heading)
  6439. (setq beg (point) end (point-max)))
  6440. (goto-char end)
  6441. ;; find each statistic cookie
  6442. (while (re-search-backward re-find beg t)
  6443. (setq beg-cookie (match-beginning 1)
  6444. end-cookie (match-end 1)
  6445. cstat (+ cstat (if end-cookie 1 0))
  6446. startsearch (point-at-eol)
  6447. continue-from (point-at-bol)
  6448. is-percent (match-beginning 2)
  6449. lim (cond
  6450. ((org-on-heading-p) (outline-next-heading) (point))
  6451. ((org-at-item-p) (org-end-of-item) (point))
  6452. (t nil))
  6453. c-on 0
  6454. c-off 0)
  6455. (when lim
  6456. ;; find first checkbox for this cookie and gather
  6457. ;; statistics from all that are at this indentation level
  6458. (goto-char startsearch)
  6459. (if (re-search-forward re-box lim t)
  6460. (progn
  6461. (org-beginning-of-item)
  6462. (setq curr-ind (org-get-indentation))
  6463. (setq next-ind curr-ind)
  6464. (while (= curr-ind next-ind)
  6465. (save-excursion (end-of-line) (setq eline (point)))
  6466. (if (re-search-forward re-box eline t)
  6467. (if (member (match-string 2) '("[ ]" "[-]"))
  6468. (setq c-off (1+ c-off))
  6469. (setq c-on (1+ c-on))
  6470. )
  6471. )
  6472. (org-end-of-item)
  6473. (setq next-ind (org-get-indentation))
  6474. )))
  6475. (goto-char continue-from)
  6476. ;; update cookie
  6477. (when end-cookie
  6478. (delete-region beg-cookie end-cookie)
  6479. (goto-char beg-cookie)
  6480. (insert
  6481. (if is-percent
  6482. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6483. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6484. ;; update items checkbox if it has one
  6485. (when (org-at-item-p)
  6486. (org-beginning-of-item)
  6487. (when (and (> (+ c-on c-off) 0)
  6488. (re-search-forward re-box (point-at-eol) t))
  6489. (setq beg-cookie (match-beginning 2)
  6490. end-cookie (match-end 2))
  6491. (delete-region beg-cookie end-cookie)
  6492. (goto-char beg-cookie)
  6493. (cond ((= c-off 0) (insert "[X]"))
  6494. ((= c-on 0) (insert "[ ]"))
  6495. (t (insert "[-]")))
  6496. )))
  6497. (goto-char continue-from))
  6498. (when (interactive-p)
  6499. (message "Checkbox satistics updated %s (%d places)"
  6500. (if all "in entire file" "in current outline entry") cstat)))))
  6501. (defun org-get-checkbox-statistics-face ()
  6502. "Select the face for checkbox statistics.
  6503. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6504. it will be `org-todo'."
  6505. (if (match-end 1)
  6506. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6507. (if (and (> (match-end 2) (match-beginning 2))
  6508. (equal (match-string 2) (match-string 3)))
  6509. 'org-done
  6510. 'org-todo)))
  6511. (defun org-get-indentation (&optional line)
  6512. "Get the indentation of the current line, interpreting tabs.
  6513. When LINE is given, assume it represents a line and compute its indentation."
  6514. (if line
  6515. (if (string-match "^ *" (org-remove-tabs line))
  6516. (match-end 0))
  6517. (save-excursion
  6518. (beginning-of-line 1)
  6519. (skip-chars-forward " \t")
  6520. (current-column))))
  6521. (defun org-remove-tabs (s &optional width)
  6522. "Replace tabulators in S with spaces.
  6523. Assumes that s is a single line, starting in column 0."
  6524. (setq width (or width tab-width))
  6525. (while (string-match "\t" s)
  6526. (setq s (replace-match
  6527. (make-string
  6528. (- (* width (/ (+ (match-beginning 0) width) width))
  6529. (match-beginning 0)) ?\ )
  6530. t t s)))
  6531. s)
  6532. (defun org-fix-indentation (line ind)
  6533. "Fix indentation in LINE.
  6534. IND is a cons cell with target and minimum indentation.
  6535. If the current indenation in LINE is smaller than the minimum,
  6536. leave it alone. If it is larger than ind, set it to the target."
  6537. (let* ((l (org-remove-tabs line))
  6538. (i (org-get-indentation l))
  6539. (i1 (car ind)) (i2 (cdr ind)))
  6540. (if (>= i i2) (setq l (substring line i2)))
  6541. (if (> i1 0)
  6542. (concat (make-string i1 ?\ ) l)
  6543. l)))
  6544. (defcustom org-empty-line-terminates-plain-lists nil
  6545. "Non-nil means, an empty line ends all plain list levels.
  6546. When nil, empty lines are part of the preceeding item."
  6547. :group 'org-plain-lists
  6548. :type 'boolean)
  6549. (defun org-beginning-of-item ()
  6550. "Go to the beginning of the current hand-formatted item.
  6551. If the cursor is not in an item, throw an error."
  6552. (interactive)
  6553. (let ((pos (point))
  6554. (limit (save-excursion
  6555. (condition-case nil
  6556. (progn
  6557. (org-back-to-heading)
  6558. (beginning-of-line 2) (point))
  6559. (error (point-min)))))
  6560. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6561. ind ind1)
  6562. (if (org-at-item-p)
  6563. (beginning-of-line 1)
  6564. (beginning-of-line 1)
  6565. (skip-chars-forward " \t")
  6566. (setq ind (current-column))
  6567. (if (catch 'exit
  6568. (while t
  6569. (beginning-of-line 0)
  6570. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6571. (if (looking-at "[ \t]*$")
  6572. (setq ind1 ind-empty)
  6573. (skip-chars-forward " \t")
  6574. (setq ind1 (current-column)))
  6575. (if (< ind1 ind)
  6576. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6577. nil
  6578. (goto-char pos)
  6579. (error "Not in an item")))))
  6580. (defun org-end-of-item ()
  6581. "Go to the end of the current hand-formatted item.
  6582. If the cursor is not in an item, throw an error."
  6583. (interactive)
  6584. (let* ((pos (point))
  6585. ind1
  6586. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6587. (limit (save-excursion (outline-next-heading) (point)))
  6588. (ind (save-excursion
  6589. (org-beginning-of-item)
  6590. (skip-chars-forward " \t")
  6591. (current-column)))
  6592. (end (catch 'exit
  6593. (while t
  6594. (beginning-of-line 2)
  6595. (if (eobp) (throw 'exit (point)))
  6596. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6597. (if (looking-at "[ \t]*$")
  6598. (setq ind1 ind-empty)
  6599. (skip-chars-forward " \t")
  6600. (setq ind1 (current-column)))
  6601. (if (<= ind1 ind)
  6602. (throw 'exit (point-at-bol)))))))
  6603. (if end
  6604. (goto-char end)
  6605. (goto-char pos)
  6606. (error "Not in an item"))))
  6607. (defun org-next-item ()
  6608. "Move to the beginning of the next item in the current plain list.
  6609. Error if not at a plain list, or if this is the last item in the list."
  6610. (interactive)
  6611. (let (ind ind1 (pos (point)))
  6612. (org-beginning-of-item)
  6613. (setq ind (org-get-indentation))
  6614. (org-end-of-item)
  6615. (setq ind1 (org-get-indentation))
  6616. (unless (and (org-at-item-p) (= ind ind1))
  6617. (goto-char pos)
  6618. (error "On last item"))))
  6619. (defun org-previous-item ()
  6620. "Move to the beginning of the previous item in the current plain list.
  6621. Error if not at a plain list, or if this is the first item in the list."
  6622. (interactive)
  6623. (let (beg ind ind1 (pos (point)))
  6624. (org-beginning-of-item)
  6625. (setq beg (point))
  6626. (setq ind (org-get-indentation))
  6627. (goto-char beg)
  6628. (catch 'exit
  6629. (while t
  6630. (beginning-of-line 0)
  6631. (if (looking-at "[ \t]*$")
  6632. nil
  6633. (if (<= (setq ind1 (org-get-indentation)) ind)
  6634. (throw 'exit t)))))
  6635. (condition-case nil
  6636. (if (or (not (org-at-item-p))
  6637. (< ind1 (1- ind)))
  6638. (error "")
  6639. (org-beginning-of-item))
  6640. (error (goto-char pos)
  6641. (error "On first item")))))
  6642. (defun org-first-list-item-p ()
  6643. "Is this heading the item in a plain list?"
  6644. (unless (org-at-item-p)
  6645. (error "Not at a plain list item"))
  6646. (org-beginning-of-item)
  6647. (= (point) (save-excursion (org-beginning-of-item-list))))
  6648. (defun org-move-item-down ()
  6649. "Move the plain list item at point down, i.e. swap with following item.
  6650. Subitems (items with larger indentation) are considered part of the item,
  6651. so this really moves item trees."
  6652. (interactive)
  6653. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6654. (org-beginning-of-item)
  6655. (setq beg0 (point))
  6656. (save-excursion
  6657. (setq ne-beg (org-back-over-empty-lines))
  6658. (setq beg (point)))
  6659. (goto-char beg0)
  6660. (setq ind (org-get-indentation))
  6661. (org-end-of-item)
  6662. (setq end0 (point))
  6663. (setq ind1 (org-get-indentation))
  6664. (setq ne-end (org-back-over-empty-lines))
  6665. (setq end (point))
  6666. (goto-char beg0)
  6667. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6668. ;; include less whitespace
  6669. (save-excursion
  6670. (goto-char beg)
  6671. (forward-line (- ne-beg ne-end))
  6672. (setq beg (point))))
  6673. (goto-char end0)
  6674. (if (and (org-at-item-p) (= ind ind1))
  6675. (progn
  6676. (org-end-of-item)
  6677. (org-back-over-empty-lines)
  6678. (setq txt (buffer-substring beg end))
  6679. (save-excursion
  6680. (delete-region beg end))
  6681. (setq pos (point))
  6682. (insert txt)
  6683. (goto-char pos) (org-skip-whitespace)
  6684. (org-maybe-renumber-ordered-list))
  6685. (goto-char pos)
  6686. (error "Cannot move this item further down"))))
  6687. (defun org-move-item-up (arg)
  6688. "Move the plain list item at point up, i.e. swap with previous item.
  6689. Subitems (items with larger indentation) are considered part of the item,
  6690. so this really moves item trees."
  6691. (interactive "p")
  6692. (let (beg beg0 end ind ind1 (pos (point)) txt
  6693. ne-beg ne-ins ins-end)
  6694. (org-beginning-of-item)
  6695. (setq beg0 (point))
  6696. (setq ind (org-get-indentation))
  6697. (save-excursion
  6698. (setq ne-beg (org-back-over-empty-lines))
  6699. (setq beg (point)))
  6700. (goto-char beg0)
  6701. (org-end-of-item)
  6702. (setq end (point))
  6703. (goto-char beg0)
  6704. (catch 'exit
  6705. (while t
  6706. (beginning-of-line 0)
  6707. (if (looking-at "[ \t]*$")
  6708. (if org-empty-line-terminates-plain-lists
  6709. (progn
  6710. (goto-char pos)
  6711. (error "Cannot move this item further up"))
  6712. nil)
  6713. (if (<= (setq ind1 (org-get-indentation)) ind)
  6714. (throw 'exit t)))))
  6715. (condition-case nil
  6716. (org-beginning-of-item)
  6717. (error (goto-char beg)
  6718. (error "Cannot move this item further up")))
  6719. (setq ind1 (org-get-indentation))
  6720. (if (and (org-at-item-p) (= ind ind1))
  6721. (progn
  6722. (setq ne-ins (org-back-over-empty-lines))
  6723. (setq txt (buffer-substring beg end))
  6724. (save-excursion
  6725. (delete-region beg end))
  6726. (setq pos (point))
  6727. (insert txt)
  6728. (setq ins-end (point))
  6729. (goto-char pos) (org-skip-whitespace)
  6730. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6731. ;; Move whitespace back to beginning
  6732. (save-excursion
  6733. (goto-char ins-end)
  6734. (let ((kill-whole-line t))
  6735. (kill-line (- ne-ins ne-beg)) (point)))
  6736. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6737. (org-maybe-renumber-ordered-list))
  6738. (goto-char pos)
  6739. (error "Cannot move this item further up"))))
  6740. (defun org-maybe-renumber-ordered-list ()
  6741. "Renumber the ordered list at point if setup allows it.
  6742. This tests the user option `org-auto-renumber-ordered-lists' before
  6743. doing the renumbering."
  6744. (interactive)
  6745. (when (and org-auto-renumber-ordered-lists
  6746. (org-at-item-p))
  6747. (if (match-beginning 3)
  6748. (org-renumber-ordered-list 1)
  6749. (org-fix-bullet-type))))
  6750. (defun org-maybe-renumber-ordered-list-safe ()
  6751. (condition-case nil
  6752. (save-excursion
  6753. (org-maybe-renumber-ordered-list))
  6754. (error nil)))
  6755. (defun org-cycle-list-bullet (&optional which)
  6756. "Cycle through the different itemize/enumerate bullets.
  6757. This cycle the entire list level through the sequence:
  6758. `-' -> `+' -> `*' -> `1.' -> `1)'
  6759. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6760. 0 meand `-', 1 means `+' etc."
  6761. (interactive "P")
  6762. (org-preserve-lc
  6763. (org-beginning-of-item-list)
  6764. (org-at-item-p)
  6765. (beginning-of-line 1)
  6766. (let ((current (match-string 0))
  6767. (prevp (eq which 'previous))
  6768. new)
  6769. (setq new (cond
  6770. ((and (numberp which)
  6771. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6772. ((string-match "-" current) (if prevp "1)" "+"))
  6773. ((string-match "\\+" current)
  6774. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6775. ((string-match "\\*" current) (if prevp "+" "1."))
  6776. ((string-match "\\." current) (if prevp "*" "1)"))
  6777. ((string-match ")" current) (if prevp "1." "-"))
  6778. (t (error "This should not happen"))))
  6779. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6780. (org-fix-bullet-type)
  6781. (org-maybe-renumber-ordered-list))))
  6782. (defun org-get-string-indentation (s)
  6783. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6784. (let ((n -1) (i 0) (w tab-width) c)
  6785. (catch 'exit
  6786. (while (< (setq n (1+ n)) (length s))
  6787. (setq c (aref s n))
  6788. (cond ((= c ?\ ) (setq i (1+ i)))
  6789. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6790. (t (throw 'exit t)))))
  6791. i))
  6792. (defun org-renumber-ordered-list (arg)
  6793. "Renumber an ordered plain list.
  6794. Cursor needs to be in the first line of an item, the line that starts
  6795. with something like \"1.\" or \"2)\"."
  6796. (interactive "p")
  6797. (unless (and (org-at-item-p)
  6798. (match-beginning 3))
  6799. (error "This is not an ordered list"))
  6800. (let ((line (org-current-line))
  6801. (col (current-column))
  6802. (ind (org-get-string-indentation
  6803. (buffer-substring (point-at-bol) (match-beginning 3))))
  6804. ;; (term (substring (match-string 3) -1))
  6805. ind1 (n (1- arg))
  6806. fmt)
  6807. ;; find where this list begins
  6808. (org-beginning-of-item-list)
  6809. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6810. (setq fmt (concat "%d" (match-string 1)))
  6811. (beginning-of-line 0)
  6812. ;; walk forward and replace these numbers
  6813. (catch 'exit
  6814. (while t
  6815. (catch 'next
  6816. (beginning-of-line 2)
  6817. (if (eobp) (throw 'exit nil))
  6818. (if (looking-at "[ \t]*$") (throw 'next nil))
  6819. (skip-chars-forward " \t") (setq ind1 (current-column))
  6820. (if (> ind1 ind) (throw 'next t))
  6821. (if (< ind1 ind) (throw 'exit t))
  6822. (if (not (org-at-item-p)) (throw 'exit nil))
  6823. (delete-region (match-beginning 2) (match-end 2))
  6824. (goto-char (match-beginning 2))
  6825. (insert (format fmt (setq n (1+ n)))))))
  6826. (goto-line line)
  6827. (move-to-column col)))
  6828. (defun org-fix-bullet-type ()
  6829. "Make sure all items in this list have the same bullet as the firsst item."
  6830. (interactive)
  6831. (unless (org-at-item-p) (error "This is not a list"))
  6832. (let ((line (org-current-line))
  6833. (col (current-column))
  6834. (ind (current-indentation))
  6835. ind1 bullet)
  6836. ;; find where this list begins
  6837. (org-beginning-of-item-list)
  6838. (beginning-of-line 1)
  6839. ;; find out what the bullet type is
  6840. (looking-at "[ \t]*\\(\\S-+\\)")
  6841. (setq bullet (match-string 1))
  6842. ;; walk forward and replace these numbers
  6843. (beginning-of-line 0)
  6844. (catch 'exit
  6845. (while t
  6846. (catch 'next
  6847. (beginning-of-line 2)
  6848. (if (eobp) (throw 'exit nil))
  6849. (if (looking-at "[ \t]*$") (throw 'next nil))
  6850. (skip-chars-forward " \t") (setq ind1 (current-column))
  6851. (if (> ind1 ind) (throw 'next t))
  6852. (if (< ind1 ind) (throw 'exit t))
  6853. (if (not (org-at-item-p)) (throw 'exit nil))
  6854. (skip-chars-forward " \t")
  6855. (looking-at "\\S-+")
  6856. (replace-match bullet))))
  6857. (goto-line line)
  6858. (move-to-column col)
  6859. (if (string-match "[0-9]" bullet)
  6860. (org-renumber-ordered-list 1))))
  6861. (defun org-beginning-of-item-list ()
  6862. "Go to the beginning of the current item list.
  6863. I.e. to the first item in this list."
  6864. (interactive)
  6865. (org-beginning-of-item)
  6866. (let ((pos (point-at-bol))
  6867. (ind (org-get-indentation))
  6868. ind1)
  6869. ;; find where this list begins
  6870. (catch 'exit
  6871. (while t
  6872. (catch 'next
  6873. (beginning-of-line 0)
  6874. (if (looking-at "[ \t]*$")
  6875. (throw (if (bobp) 'exit 'next) t))
  6876. (skip-chars-forward " \t") (setq ind1 (current-column))
  6877. (if (or (< ind1 ind)
  6878. (and (= ind1 ind)
  6879. (not (org-at-item-p)))
  6880. (bobp))
  6881. (throw 'exit t)
  6882. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6883. (goto-char pos)))
  6884. (defun org-end-of-item-list ()
  6885. "Go to the end of the current item list.
  6886. I.e. to the text after the last item."
  6887. (interactive)
  6888. (org-beginning-of-item)
  6889. (let ((pos (point-at-bol))
  6890. (ind (org-get-indentation))
  6891. ind1)
  6892. ;; find where this list begins
  6893. (catch 'exit
  6894. (while t
  6895. (catch 'next
  6896. (beginning-of-line 2)
  6897. (if (looking-at "[ \t]*$")
  6898. (throw (if (eobp) 'exit 'next) t))
  6899. (skip-chars-forward " \t") (setq ind1 (current-column))
  6900. (if (or (< ind1 ind)
  6901. (and (= ind1 ind)
  6902. (not (org-at-item-p)))
  6903. (eobp))
  6904. (progn
  6905. (setq pos (point-at-bol))
  6906. (throw 'exit t))))))
  6907. (goto-char pos)))
  6908. (defvar org-last-indent-begin-marker (make-marker))
  6909. (defvar org-last-indent-end-marker (make-marker))
  6910. (defun org-outdent-item (arg)
  6911. "Outdent a local list item."
  6912. (interactive "p")
  6913. (org-indent-item (- arg)))
  6914. (defun org-indent-item (arg)
  6915. "Indent a local list item."
  6916. (interactive "p")
  6917. (unless (org-at-item-p)
  6918. (error "Not on an item"))
  6919. (save-excursion
  6920. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6921. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6922. (setq beg org-last-indent-begin-marker
  6923. end org-last-indent-end-marker)
  6924. (org-beginning-of-item)
  6925. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6926. (org-end-of-item)
  6927. (setq end (move-marker org-last-indent-end-marker (point))))
  6928. (goto-char beg)
  6929. (setq tmp (org-item-indent-positions)
  6930. ind (car tmp)
  6931. ind-down (nth 2 tmp)
  6932. ind-up (nth 1 tmp)
  6933. delta (if (> arg 0)
  6934. (if ind-down (- ind-down ind) 2)
  6935. (if ind-up (- ind-up ind) -2)))
  6936. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6937. (while (< (point) end)
  6938. (beginning-of-line 1)
  6939. (skip-chars-forward " \t") (setq ind1 (current-column))
  6940. (delete-region (point-at-bol) (point))
  6941. (or (eolp) (indent-to-column (+ ind1 delta)))
  6942. (beginning-of-line 2))))
  6943. (org-fix-bullet-type)
  6944. (org-maybe-renumber-ordered-list-safe)
  6945. (save-excursion
  6946. (beginning-of-line 0)
  6947. (condition-case nil (org-beginning-of-item) (error nil))
  6948. (org-maybe-renumber-ordered-list-safe)))
  6949. (defun org-item-indent-positions ()
  6950. "Return indentation for plain list items.
  6951. This returns a list with three values: The current indentation, the
  6952. parent indentation and the indentation a child should habe.
  6953. Assumes cursor in item line."
  6954. (let* ((bolpos (point-at-bol))
  6955. (ind (org-get-indentation))
  6956. ind-down ind-up pos)
  6957. (save-excursion
  6958. (org-beginning-of-item-list)
  6959. (skip-chars-backward "\n\r \t")
  6960. (when (org-in-item-p)
  6961. (org-beginning-of-item)
  6962. (setq ind-up (org-get-indentation))))
  6963. (setq pos (point))
  6964. (save-excursion
  6965. (cond
  6966. ((and (condition-case nil (progn (org-previous-item) t)
  6967. (error nil))
  6968. (or (forward-char 1) t)
  6969. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6970. (setq ind-down (org-get-indentation)))
  6971. ((and (goto-char pos)
  6972. (org-at-item-p))
  6973. (goto-char (match-end 0))
  6974. (skip-chars-forward " \t")
  6975. (setq ind-down (current-column)))))
  6976. (list ind ind-up ind-down)))
  6977. ;;; The orgstruct minor mode
  6978. ;; Define a minor mode which can be used in other modes in order to
  6979. ;; integrate the org-mode structure editing commands.
  6980. ;; This is really a hack, because the org-mode structure commands use
  6981. ;; keys which normally belong to the major mode. Here is how it
  6982. ;; works: The minor mode defines all the keys necessary to operate the
  6983. ;; structure commands, but wraps the commands into a function which
  6984. ;; tests if the cursor is currently at a headline or a plain list
  6985. ;; item. If that is the case, the structure command is used,
  6986. ;; temporarily setting many Org-mode variables like regular
  6987. ;; expressions for filling etc. However, when any of those keys is
  6988. ;; used at a different location, function uses `key-binding' to look
  6989. ;; up if the key has an associated command in another currently active
  6990. ;; keymap (minor modes, major mode, global), and executes that
  6991. ;; command. There might be problems if any of the keys is otherwise
  6992. ;; used as a prefix key.
  6993. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6994. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6995. ;; addresses this by checking explicitly for both bindings.
  6996. (defvar orgstruct-mode-map (make-sparse-keymap)
  6997. "Keymap for the minor `orgstruct-mode'.")
  6998. (defvar org-local-vars nil
  6999. "List of local variables, for use by `orgstruct-mode'")
  7000. ;;;###autoload
  7001. (define-minor-mode orgstruct-mode
  7002. "Toggle the minor more `orgstruct-mode'.
  7003. This mode is for using Org-mode structure commands in other modes.
  7004. The following key behave as if Org-mode was active, if the cursor
  7005. is on a headline, or on a plain list item (both in the definition
  7006. of Org-mode).
  7007. M-up Move entry/item up
  7008. M-down Move entry/item down
  7009. M-left Promote
  7010. M-right Demote
  7011. M-S-up Move entry/item up
  7012. M-S-down Move entry/item down
  7013. M-S-left Promote subtree
  7014. M-S-right Demote subtree
  7015. M-q Fill paragraph and items like in Org-mode
  7016. C-c ^ Sort entries
  7017. C-c - Cycle list bullet
  7018. TAB Cycle item visibility
  7019. M-RET Insert new heading/item
  7020. S-M-RET Insert new TODO heading / Chekbox item
  7021. C-c C-c Set tags / toggle checkbox"
  7022. nil " OrgStruct" nil
  7023. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7024. ;;;###autoload
  7025. (defun turn-on-orgstruct ()
  7026. "Unconditionally turn on `orgstruct-mode'."
  7027. (orgstruct-mode 1))
  7028. ;;;###autoload
  7029. (defun turn-on-orgstruct++ ()
  7030. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7031. In addition to setting orgstruct-mode, this also exports all indentation and
  7032. autofilling variables from org-mode into the buffer. Note that turning
  7033. off orgstruct-mode will *not* remove these additional settings."
  7034. (orgstruct-mode 1)
  7035. (let (var val)
  7036. (mapc
  7037. (lambda (x)
  7038. (when (string-match
  7039. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7040. (symbol-name (car x)))
  7041. (setq var (car x) val (nth 1 x))
  7042. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7043. org-local-vars)))
  7044. (defun orgstruct-error ()
  7045. "Error when there is no default binding for a structure key."
  7046. (interactive)
  7047. (error "This key has no function outside structure elements"))
  7048. (defun orgstruct-setup ()
  7049. "Setup orgstruct keymaps."
  7050. (let ((nfunc 0)
  7051. (bindings
  7052. (list
  7053. '([(meta up)] org-metaup)
  7054. '([(meta down)] org-metadown)
  7055. '([(meta left)] org-metaleft)
  7056. '([(meta right)] org-metaright)
  7057. '([(meta shift up)] org-shiftmetaup)
  7058. '([(meta shift down)] org-shiftmetadown)
  7059. '([(meta shift left)] org-shiftmetaleft)
  7060. '([(meta shift right)] org-shiftmetaright)
  7061. '([(shift up)] org-shiftup)
  7062. '([(shift down)] org-shiftdown)
  7063. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7064. '("\M-q" fill-paragraph)
  7065. '("\C-c^" org-sort)
  7066. '("\C-c-" org-cycle-list-bullet)))
  7067. elt key fun cmd)
  7068. (while (setq elt (pop bindings))
  7069. (setq nfunc (1+ nfunc))
  7070. (setq key (org-key (car elt))
  7071. fun (nth 1 elt)
  7072. cmd (orgstruct-make-binding fun nfunc key))
  7073. (org-defkey orgstruct-mode-map key cmd))
  7074. ;; Special treatment needed for TAB and RET
  7075. (org-defkey orgstruct-mode-map [(tab)]
  7076. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7077. (org-defkey orgstruct-mode-map "\C-i"
  7078. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7079. (org-defkey orgstruct-mode-map "\M-\C-m"
  7080. (orgstruct-make-binding 'org-insert-heading 105
  7081. "\M-\C-m" [(meta return)]))
  7082. (org-defkey orgstruct-mode-map [(meta return)]
  7083. (orgstruct-make-binding 'org-insert-heading 106
  7084. [(meta return)] "\M-\C-m"))
  7085. (org-defkey orgstruct-mode-map [(shift meta return)]
  7086. (orgstruct-make-binding 'org-insert-todo-heading 107
  7087. [(meta return)] "\M-\C-m"))
  7088. (unless org-local-vars
  7089. (setq org-local-vars (org-get-local-variables)))
  7090. t))
  7091. (defun orgstruct-make-binding (fun n &rest keys)
  7092. "Create a function for binding in the structure minor mode.
  7093. FUN is the command to call inside a table. N is used to create a unique
  7094. command name. KEYS are keys that should be checked in for a command
  7095. to execute outside of tables."
  7096. (eval
  7097. (list 'defun
  7098. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7099. '(arg)
  7100. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7101. "Outside of structure, run the binding of `"
  7102. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7103. "'.")
  7104. '(interactive "p")
  7105. (list 'if
  7106. '(org-context-p 'headline 'item)
  7107. (list 'org-run-like-in-org-mode (list 'quote fun))
  7108. (list 'let '(orgstruct-mode)
  7109. (list 'call-interactively
  7110. (append '(or)
  7111. (mapcar (lambda (k)
  7112. (list 'key-binding k))
  7113. keys)
  7114. '('orgstruct-error))))))))
  7115. (defun org-context-p (&rest contexts)
  7116. "Check if local context is and of CONTEXTS.
  7117. Possible values in the list of contexts are `table', `headline', and `item'."
  7118. (let ((pos (point)))
  7119. (goto-char (point-at-bol))
  7120. (prog1 (or (and (memq 'table contexts)
  7121. (looking-at "[ \t]*|"))
  7122. (and (memq 'headline contexts)
  7123. (looking-at "\\*+"))
  7124. (and (memq 'item contexts)
  7125. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7126. (goto-char pos))))
  7127. (defun org-get-local-variables ()
  7128. "Return a list of all local variables in an org-mode buffer."
  7129. (let (varlist)
  7130. (with-current-buffer (get-buffer-create "*Org tmp*")
  7131. (erase-buffer)
  7132. (org-mode)
  7133. (setq varlist (buffer-local-variables)))
  7134. (kill-buffer "*Org tmp*")
  7135. (delq nil
  7136. (mapcar
  7137. (lambda (x)
  7138. (setq x
  7139. (if (symbolp x)
  7140. (list x)
  7141. (list (car x) (list 'quote (cdr x)))))
  7142. (if (string-match
  7143. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7144. (symbol-name (car x)))
  7145. x nil))
  7146. varlist))))
  7147. ;;;###autoload
  7148. (defun org-run-like-in-org-mode (cmd)
  7149. (unless org-local-vars
  7150. (setq org-local-vars (org-get-local-variables)))
  7151. (eval (list 'let org-local-vars
  7152. (list 'call-interactively (list 'quote cmd)))))
  7153. ;;;; Archiving
  7154. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7155. (defun org-archive-subtree (&optional find-done)
  7156. "Move the current subtree to the archive.
  7157. The archive can be a certain top-level heading in the current file, or in
  7158. a different file. The tree will be moved to that location, the subtree
  7159. heading be marked DONE, and the current time will be added.
  7160. When called with prefix argument FIND-DONE, find whole trees without any
  7161. open TODO items and archive them (after getting confirmation from the user).
  7162. If the cursor is not at a headline when this comand is called, try all level
  7163. 1 trees. If the cursor is on a headline, only try the direct children of
  7164. this heading."
  7165. (interactive "P")
  7166. (if find-done
  7167. (org-archive-all-done)
  7168. ;; Save all relevant TODO keyword-relatex variables
  7169. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7170. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7171. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7172. (tr-org-done-keywords org-done-keywords)
  7173. (tr-org-todo-regexp org-todo-regexp)
  7174. (tr-org-todo-line-regexp org-todo-line-regexp)
  7175. (tr-org-odd-levels-only org-odd-levels-only)
  7176. (this-buffer (current-buffer))
  7177. (org-archive-location org-archive-location)
  7178. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7179. ;; start of variables that will be used for saving context
  7180. ;; The compiler complains about them - keep them anyway!
  7181. (file (abbreviate-file-name (buffer-file-name)))
  7182. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7183. (time (format-time-string
  7184. (substring (cdr org-time-stamp-formats) 1 -1)
  7185. (current-time)))
  7186. afile heading buffer level newfile-p
  7187. category todo priority
  7188. ;; start of variables that will be used for savind context
  7189. ltags itags prop)
  7190. ;; Try to find a local archive location
  7191. (save-excursion
  7192. (save-restriction
  7193. (widen)
  7194. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7195. (if (and prop (string-match "\\S-" prop))
  7196. (setq org-archive-location prop)
  7197. (if (or (re-search-backward re nil t)
  7198. (re-search-forward re nil t))
  7199. (setq org-archive-location (match-string 1))))))
  7200. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7201. (progn
  7202. (setq afile (format (match-string 1 org-archive-location)
  7203. (file-name-nondirectory buffer-file-name))
  7204. heading (match-string 2 org-archive-location)))
  7205. (error "Invalid `org-archive-location'"))
  7206. (if (> (length afile) 0)
  7207. (setq newfile-p (not (file-exists-p afile))
  7208. buffer (find-file-noselect afile))
  7209. (setq buffer (current-buffer)))
  7210. (unless buffer
  7211. (error "Cannot access file \"%s\"" afile))
  7212. (if (and (> (length heading) 0)
  7213. (string-match "^\\*+" heading))
  7214. (setq level (match-end 0))
  7215. (setq heading nil level 0))
  7216. (save-excursion
  7217. (org-back-to-heading t)
  7218. ;; Get context information that will be lost by moving the tree
  7219. (org-refresh-category-properties)
  7220. (setq category (org-get-category)
  7221. todo (and (looking-at org-todo-line-regexp)
  7222. (match-string 2))
  7223. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7224. ltags (org-get-tags)
  7225. itags (org-delete-all ltags (org-get-tags-at)))
  7226. (setq ltags (mapconcat 'identity ltags " ")
  7227. itags (mapconcat 'identity itags " "))
  7228. ;; We first only copy, in case something goes wrong
  7229. ;; we need to protect this-command, to avoid kill-region sets it,
  7230. ;; which would lead to duplication of subtrees
  7231. (let (this-command) (org-copy-subtree))
  7232. (set-buffer buffer)
  7233. ;; Enforce org-mode for the archive buffer
  7234. (if (not (org-mode-p))
  7235. ;; Force the mode for future visits.
  7236. (let ((org-insert-mode-line-in-empty-file t)
  7237. (org-inhibit-startup t))
  7238. (call-interactively 'org-mode)))
  7239. (when newfile-p
  7240. (goto-char (point-max))
  7241. (insert (format "\nArchived entries from file %s\n\n"
  7242. (buffer-file-name this-buffer))))
  7243. ;; Force the TODO keywords of the original buffer
  7244. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7245. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7246. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7247. (org-done-keywords tr-org-done-keywords)
  7248. (org-todo-regexp tr-org-todo-regexp)
  7249. (org-todo-line-regexp tr-org-todo-line-regexp)
  7250. (org-odd-levels-only
  7251. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7252. org-odd-levels-only
  7253. tr-org-odd-levels-only)))
  7254. (goto-char (point-min))
  7255. (show-all)
  7256. (if heading
  7257. (progn
  7258. (if (re-search-forward
  7259. (concat "^" (regexp-quote heading)
  7260. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7261. nil t)
  7262. (goto-char (match-end 0))
  7263. ;; Heading not found, just insert it at the end
  7264. (goto-char (point-max))
  7265. (or (bolp) (insert "\n"))
  7266. (insert "\n" heading "\n")
  7267. (end-of-line 0))
  7268. ;; Make the subtree visible
  7269. (show-subtree)
  7270. (org-end-of-subtree t)
  7271. (skip-chars-backward " \t\r\n")
  7272. (and (looking-at "[ \t\r\n]*")
  7273. (replace-match "\n\n")))
  7274. ;; No specific heading, just go to end of file.
  7275. (goto-char (point-max)) (insert "\n"))
  7276. ;; Paste
  7277. (org-paste-subtree (org-get-legal-level level 1))
  7278. ;; Mark the entry as done
  7279. (when (and org-archive-mark-done
  7280. (looking-at org-todo-line-regexp)
  7281. (or (not (match-end 2))
  7282. (not (member (match-string 2) org-done-keywords))))
  7283. (let (org-log-done org-todo-log-states)
  7284. (org-todo
  7285. (car (or (member org-archive-mark-done org-done-keywords)
  7286. org-done-keywords)))))
  7287. ;; Add the context info
  7288. (when org-archive-save-context-info
  7289. (let ((l org-archive-save-context-info) e n v)
  7290. (while (setq e (pop l))
  7291. (when (and (setq v (symbol-value e))
  7292. (stringp v) (string-match "\\S-" v))
  7293. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7294. (org-entry-put (point) n v)))))
  7295. ;; Save and kill the buffer, if it is not the same buffer.
  7296. (if (not (eq this-buffer buffer))
  7297. (progn (save-buffer) (kill-buffer buffer)))))
  7298. ;; Here we are back in the original buffer. Everything seems to have
  7299. ;; worked. So now cut the tree and finish up.
  7300. (let (this-command) (org-cut-subtree))
  7301. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7302. (message "Subtree archived %s"
  7303. (if (eq this-buffer buffer)
  7304. (concat "under heading: " heading)
  7305. (concat "in file: " (abbreviate-file-name afile)))))))
  7306. (defun org-refresh-category-properties ()
  7307. "Refresh category text properties in teh buffer."
  7308. (let ((def-cat (cond
  7309. ((null org-category)
  7310. (if buffer-file-name
  7311. (file-name-sans-extension
  7312. (file-name-nondirectory buffer-file-name))
  7313. "???"))
  7314. ((symbolp org-category) (symbol-name org-category))
  7315. (t org-category)))
  7316. beg end cat pos optionp)
  7317. (org-unmodified
  7318. (save-excursion
  7319. (save-restriction
  7320. (widen)
  7321. (goto-char (point-min))
  7322. (put-text-property (point) (point-max) 'org-category def-cat)
  7323. (while (re-search-forward
  7324. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7325. (setq pos (match-end 0)
  7326. optionp (equal (char-after (match-beginning 0)) ?#)
  7327. cat (org-trim (match-string 2)))
  7328. (if optionp
  7329. (setq beg (point-at-bol) end (point-max))
  7330. (org-back-to-heading t)
  7331. (setq beg (point) end (org-end-of-subtree t t)))
  7332. (put-text-property beg end 'org-category cat)
  7333. (goto-char pos)))))))
  7334. (defun org-archive-all-done (&optional tag)
  7335. "Archive sublevels of the current tree without open TODO items.
  7336. If the cursor is not on a headline, try all level 1 trees. If
  7337. it is on a headline, try all direct children.
  7338. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7339. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7340. (rea (concat ".*:" org-archive-tag ":"))
  7341. (begm (make-marker))
  7342. (endm (make-marker))
  7343. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7344. "Move subtree to archive (no open TODO items)? "))
  7345. beg end (cntarch 0))
  7346. (if (org-on-heading-p)
  7347. (progn
  7348. (setq re1 (concat "^" (regexp-quote
  7349. (make-string
  7350. (1+ (- (match-end 0) (match-beginning 0) 1))
  7351. ?*))
  7352. " "))
  7353. (move-marker begm (point))
  7354. (move-marker endm (org-end-of-subtree t)))
  7355. (setq re1 "^* ")
  7356. (move-marker begm (point-min))
  7357. (move-marker endm (point-max)))
  7358. (save-excursion
  7359. (goto-char begm)
  7360. (while (re-search-forward re1 endm t)
  7361. (setq beg (match-beginning 0)
  7362. end (save-excursion (org-end-of-subtree t) (point)))
  7363. (goto-char beg)
  7364. (if (re-search-forward re end t)
  7365. (goto-char end)
  7366. (goto-char beg)
  7367. (if (and (or (not tag) (not (looking-at rea)))
  7368. (y-or-n-p question))
  7369. (progn
  7370. (if tag
  7371. (org-toggle-tag org-archive-tag 'on)
  7372. (org-archive-subtree))
  7373. (setq cntarch (1+ cntarch)))
  7374. (goto-char end)))))
  7375. (message "%d trees archived" cntarch)))
  7376. (defun org-cycle-hide-drawers (state)
  7377. "Re-hide all drawers after a visibility state change."
  7378. (when (and (org-mode-p)
  7379. (not (memq state '(overview folded))))
  7380. (save-excursion
  7381. (let* ((globalp (memq state '(contents all)))
  7382. (beg (if globalp (point-min) (point)))
  7383. (end (if globalp (point-max) (org-end-of-subtree t))))
  7384. (goto-char beg)
  7385. (while (re-search-forward org-drawer-regexp end t)
  7386. (org-flag-drawer t))))))
  7387. (defun org-flag-drawer (flag)
  7388. (save-excursion
  7389. (beginning-of-line 1)
  7390. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7391. (let ((b (match-end 0))
  7392. (outline-regexp org-outline-regexp))
  7393. (if (re-search-forward
  7394. "^[ \t]*:END:"
  7395. (save-excursion (outline-next-heading) (point)) t)
  7396. (outline-flag-region b (point-at-eol) flag)
  7397. (error ":END: line missing"))))))
  7398. (defun org-cycle-hide-archived-subtrees (state)
  7399. "Re-hide all archived subtrees after a visibility state change."
  7400. (when (and (not org-cycle-open-archived-trees)
  7401. (not (memq state '(overview folded))))
  7402. (save-excursion
  7403. (let* ((globalp (memq state '(contents all)))
  7404. (beg (if globalp (point-min) (point)))
  7405. (end (if globalp (point-max) (org-end-of-subtree t))))
  7406. (org-hide-archived-subtrees beg end)
  7407. (goto-char beg)
  7408. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7409. (message "%s" (substitute-command-keys
  7410. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7411. (defun org-force-cycle-archived ()
  7412. "Cycle subtree even if it is archived."
  7413. (interactive)
  7414. (setq this-command 'org-cycle)
  7415. (let ((org-cycle-open-archived-trees t))
  7416. (call-interactively 'org-cycle)))
  7417. (defun org-hide-archived-subtrees (beg end)
  7418. "Re-hide all archived subtrees after a visibility state change."
  7419. (save-excursion
  7420. (let* ((re (concat ":" org-archive-tag ":")))
  7421. (goto-char beg)
  7422. (while (re-search-forward re end t)
  7423. (and (org-on-heading-p) (hide-subtree))
  7424. (org-end-of-subtree t)))))
  7425. (defun org-toggle-tag (tag &optional onoff)
  7426. "Toggle the tag TAG for the current line.
  7427. If ONOFF is `on' or `off', don't toggle but set to this state."
  7428. (unless (org-on-heading-p t) (error "Not on headling"))
  7429. (let (res current)
  7430. (save-excursion
  7431. (beginning-of-line)
  7432. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7433. (point-at-eol) t)
  7434. (progn
  7435. (setq current (match-string 1))
  7436. (replace-match ""))
  7437. (setq current ""))
  7438. (setq current (nreverse (org-split-string current ":")))
  7439. (cond
  7440. ((eq onoff 'on)
  7441. (setq res t)
  7442. (or (member tag current) (push tag current)))
  7443. ((eq onoff 'off)
  7444. (or (not (member tag current)) (setq current (delete tag current))))
  7445. (t (if (member tag current)
  7446. (setq current (delete tag current))
  7447. (setq res t)
  7448. (push tag current))))
  7449. (end-of-line 1)
  7450. (if current
  7451. (progn
  7452. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7453. (org-set-tags nil t))
  7454. (delete-horizontal-space))
  7455. (run-hooks 'org-after-tags-change-hook))
  7456. res))
  7457. (defun org-toggle-archive-tag (&optional arg)
  7458. "Toggle the archive tag for the current headline.
  7459. With prefix ARG, check all children of current headline and offer tagging
  7460. the children that do not contain any open TODO items."
  7461. (interactive "P")
  7462. (if arg
  7463. (org-archive-all-done 'tag)
  7464. (let (set)
  7465. (save-excursion
  7466. (org-back-to-heading t)
  7467. (setq set (org-toggle-tag org-archive-tag))
  7468. (when set (hide-subtree)))
  7469. (and set (beginning-of-line 1))
  7470. (message "Subtree %s" (if set "archived" "unarchived")))))
  7471. ;;;; Tables
  7472. ;;; The table editor
  7473. ;; Watch out: Here we are talking about two different kind of tables.
  7474. ;; Most of the code is for the tables created with the Org-mode table editor.
  7475. ;; Sometimes, we talk about tables created and edited with the table.el
  7476. ;; Emacs package. We call the former org-type tables, and the latter
  7477. ;; table.el-type tables.
  7478. (defun org-before-change-function (beg end)
  7479. "Every change indicates that a table might need an update."
  7480. (setq org-table-may-need-update t))
  7481. (defconst org-table-line-regexp "^[ \t]*|"
  7482. "Detects an org-type table line.")
  7483. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7484. "Detects an org-type table line.")
  7485. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7486. "Detects a table line marked for automatic recalculation.")
  7487. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7488. "Detects a table line marked for automatic recalculation.")
  7489. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7490. "Detects a table line marked for automatic recalculation.")
  7491. (defconst org-table-hline-regexp "^[ \t]*|-"
  7492. "Detects an org-type table hline.")
  7493. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7494. "Detects a table-type table hline.")
  7495. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7496. "Detects an org-type or table-type table.")
  7497. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7498. "Searching from within a table (any type) this finds the first line
  7499. outside the table.")
  7500. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7501. "Searching from within a table (any type) this finds the first line
  7502. outside the table.")
  7503. (defvar org-table-last-highlighted-reference nil)
  7504. (defvar org-table-formula-history nil)
  7505. (defvar org-table-column-names nil
  7506. "Alist with column names, derived from the `!' line.")
  7507. (defvar org-table-column-name-regexp nil
  7508. "Regular expression matching the current column names.")
  7509. (defvar org-table-local-parameters nil
  7510. "Alist with parameter names, derived from the `$' line.")
  7511. (defvar org-table-named-field-locations nil
  7512. "Alist with locations of named fields.")
  7513. (defvar org-table-current-line-types nil
  7514. "Table row types, non-nil only for the duration of a comand.")
  7515. (defvar org-table-current-begin-line nil
  7516. "Table begin line, non-nil only for the duration of a comand.")
  7517. (defvar org-table-current-begin-pos nil
  7518. "Table begin position, non-nil only for the duration of a comand.")
  7519. (defvar org-table-dlines nil
  7520. "Vector of data line line numbers in the current table.")
  7521. (defvar org-table-hlines nil
  7522. "Vector of hline line numbers in the current table.")
  7523. (defconst org-table-range-regexp
  7524. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7525. ;; 1 2 3 4 5
  7526. "Regular expression for matching ranges in formulas.")
  7527. (defconst org-table-range-regexp2
  7528. (concat
  7529. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7530. "\\.\\."
  7531. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7532. "Match a range for reference display.")
  7533. (defconst org-table-translate-regexp
  7534. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7535. "Match a reference that needs translation, for reference display.")
  7536. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7537. (defun org-table-create-with-table.el ()
  7538. "Use the table.el package to insert a new table.
  7539. If there is already a table at point, convert between Org-mode tables
  7540. and table.el tables."
  7541. (interactive)
  7542. (require 'table)
  7543. (cond
  7544. ((org-at-table.el-p)
  7545. (if (y-or-n-p "Convert table to Org-mode table? ")
  7546. (org-table-convert)))
  7547. ((org-at-table-p)
  7548. (if (y-or-n-p "Convert table to table.el table? ")
  7549. (org-table-convert)))
  7550. (t (call-interactively 'table-insert))))
  7551. (defun org-table-create-or-convert-from-region (arg)
  7552. "Convert region to table, or create an empty table.
  7553. If there is an active region, convert it to a table, using the function
  7554. `org-table-convert-region'. See the documentation of that function
  7555. to learn how the prefix argument is interpreted to determine the field
  7556. separator.
  7557. If there is no such region, create an empty table with `org-table-create'."
  7558. (interactive "P")
  7559. (if (org-region-active-p)
  7560. (org-table-convert-region (region-beginning) (region-end) arg)
  7561. (org-table-create arg)))
  7562. (defun org-table-create (&optional size)
  7563. "Query for a size and insert a table skeleton.
  7564. SIZE is a string Columns x Rows like for example \"3x2\"."
  7565. (interactive "P")
  7566. (unless size
  7567. (setq size (read-string
  7568. (concat "Table size Columns x Rows [e.g. "
  7569. org-table-default-size "]: ")
  7570. "" nil org-table-default-size)))
  7571. (let* ((pos (point))
  7572. (indent (make-string (current-column) ?\ ))
  7573. (split (org-split-string size " *x *"))
  7574. (rows (string-to-number (nth 1 split)))
  7575. (columns (string-to-number (car split)))
  7576. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7577. "\n")))
  7578. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7579. (point-at-bol) (point)))
  7580. (beginning-of-line 1)
  7581. (newline))
  7582. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7583. (dotimes (i rows) (insert line))
  7584. (goto-char pos)
  7585. (if (> rows 1)
  7586. ;; Insert a hline after the first row.
  7587. (progn
  7588. (end-of-line 1)
  7589. (insert "\n|-")
  7590. (goto-char pos)))
  7591. (org-table-align)))
  7592. (defun org-table-convert-region (beg0 end0 &optional separator)
  7593. "Convert region to a table.
  7594. The region goes from BEG0 to END0, but these borders will be moved
  7595. slightly, to make sure a beginning of line in the first line is included.
  7596. SEPARATOR specifies the field separator in the lines. It can have the
  7597. following values:
  7598. '(4) Use the comma as a field separator
  7599. '(16) Use a TAB as field separator
  7600. integer When a number, use that many spaces as field separator
  7601. nil When nil, the command tries to be smart and figure out the
  7602. separator in the following way:
  7603. - when each line contains a TAB, assume TAB-separated material
  7604. - when each line contains a comme, assume CSV material
  7605. - else, assume one or more SPACE charcters as separator."
  7606. (interactive "rP")
  7607. (let* ((beg (min beg0 end0))
  7608. (end (max beg0 end0))
  7609. re)
  7610. (goto-char beg)
  7611. (beginning-of-line 1)
  7612. (setq beg (move-marker (make-marker) (point)))
  7613. (goto-char end)
  7614. (if (bolp) (backward-char 1) (end-of-line 1))
  7615. (setq end (move-marker (make-marker) (point)))
  7616. ;; Get the right field separator
  7617. (unless separator
  7618. (goto-char beg)
  7619. (setq separator
  7620. (cond
  7621. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7622. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7623. (t 1))))
  7624. (setq re (cond
  7625. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7626. ((equal separator '(16)) "^\\|\t")
  7627. ((integerp separator)
  7628. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7629. (t (error "This should not happen"))))
  7630. (goto-char beg)
  7631. (while (re-search-forward re end t)
  7632. (replace-match "| " t t))
  7633. (goto-char beg)
  7634. (insert " ")
  7635. (org-table-align)))
  7636. (defun org-table-import (file arg)
  7637. "Import FILE as a table.
  7638. The file is assumed to be tab-separated. Such files can be produced by most
  7639. spreadsheet and database applications. If no tabs (at least one per line)
  7640. are found, lines will be split on whitespace into fields."
  7641. (interactive "f\nP")
  7642. (or (bolp) (newline))
  7643. (let ((beg (point))
  7644. (pm (point-max)))
  7645. (insert-file-contents file)
  7646. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7647. (defun org-table-export ()
  7648. "Export table as a tab-separated file.
  7649. Such a file can be imported into a spreadsheet program like Excel."
  7650. (interactive)
  7651. (let* ((beg (org-table-begin))
  7652. (end (org-table-end))
  7653. (table (buffer-substring beg end))
  7654. (file (read-file-name "Export table to: "))
  7655. buf)
  7656. (unless (or (not (file-exists-p file))
  7657. (y-or-n-p (format "Overwrite file %s? " file)))
  7658. (error "Abort"))
  7659. (with-current-buffer (find-file-noselect file)
  7660. (setq buf (current-buffer))
  7661. (erase-buffer)
  7662. (fundamental-mode)
  7663. (insert table)
  7664. (goto-char (point-min))
  7665. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7666. (replace-match "" t t)
  7667. (end-of-line 1))
  7668. (goto-char (point-min))
  7669. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7670. (replace-match "" t t)
  7671. (goto-char (min (1+ (point)) (point-max))))
  7672. (goto-char (point-min))
  7673. (while (re-search-forward "^-[-+]*$" nil t)
  7674. (replace-match "")
  7675. (if (looking-at "\n")
  7676. (delete-char 1)))
  7677. (goto-char (point-min))
  7678. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7679. (replace-match "\t" t t))
  7680. (save-buffer))
  7681. (kill-buffer buf)))
  7682. (defvar org-table-aligned-begin-marker (make-marker)
  7683. "Marker at the beginning of the table last aligned.
  7684. Used to check if cursor still is in that table, to minimize realignment.")
  7685. (defvar org-table-aligned-end-marker (make-marker)
  7686. "Marker at the end of the table last aligned.
  7687. Used to check if cursor still is in that table, to minimize realignment.")
  7688. (defvar org-table-last-alignment nil
  7689. "List of flags for flushright alignment, from the last re-alignment.
  7690. This is being used to correctly align a single field after TAB or RET.")
  7691. (defvar org-table-last-column-widths nil
  7692. "List of max width of fields in each column.
  7693. This is being used to correctly align a single field after TAB or RET.")
  7694. (defvar org-table-overlay-coordinates nil
  7695. "Overlay coordinates after each align of a table.")
  7696. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7697. (defvar org-last-recalc-line nil)
  7698. (defconst org-narrow-column-arrow "=>"
  7699. "Used as display property in narrowed table columns.")
  7700. (defun org-table-align ()
  7701. "Align the table at point by aligning all vertical bars."
  7702. (interactive)
  7703. (let* (
  7704. ;; Limits of table
  7705. (beg (org-table-begin))
  7706. (end (org-table-end))
  7707. ;; Current cursor position
  7708. (linepos (org-current-line))
  7709. (colpos (org-table-current-column))
  7710. (winstart (window-start))
  7711. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7712. lines (new "") lengths l typenums ty fields maxfields i
  7713. column
  7714. (indent "") cnt frac
  7715. rfmt hfmt
  7716. (spaces '(1 . 1))
  7717. (sp1 (car spaces))
  7718. (sp2 (cdr spaces))
  7719. (rfmt1 (concat
  7720. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7721. (hfmt1 (concat
  7722. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7723. emptystrings links dates emph narrow fmax f1 len c e)
  7724. (untabify beg end)
  7725. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7726. ;; Check if we have links or dates
  7727. (goto-char beg)
  7728. (setq links (re-search-forward org-bracket-link-regexp end t))
  7729. (goto-char beg)
  7730. (setq emph (and org-hide-emphasis-markers
  7731. (re-search-forward org-emph-re end t)))
  7732. (goto-char beg)
  7733. (setq dates (and org-display-custom-times
  7734. (re-search-forward org-ts-regexp-both end t)))
  7735. ;; Make sure the link properties are right
  7736. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7737. ;; Make sure the date properties are right
  7738. (when dates (goto-char beg) (while (org-activate-dates end)))
  7739. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7740. ;; Check if we are narrowing any columns
  7741. (goto-char beg)
  7742. (setq narrow (and org-format-transports-properties-p
  7743. (re-search-forward "<[0-9]+>" end t)))
  7744. ;; Get the rows
  7745. (setq lines (org-split-string
  7746. (buffer-substring beg end) "\n"))
  7747. ;; Store the indentation of the first line
  7748. (if (string-match "^ *" (car lines))
  7749. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7750. ;; Mark the hlines by setting the corresponding element to nil
  7751. ;; At the same time, we remove trailing space.
  7752. (setq lines (mapcar (lambda (l)
  7753. (if (string-match "^ *|-" l)
  7754. nil
  7755. (if (string-match "[ \t]+$" l)
  7756. (substring l 0 (match-beginning 0))
  7757. l)))
  7758. lines))
  7759. ;; Get the data fields by splitting the lines.
  7760. (setq fields (mapcar
  7761. (lambda (l)
  7762. (org-split-string l " *| *"))
  7763. (delq nil (copy-sequence lines))))
  7764. ;; How many fields in the longest line?
  7765. (condition-case nil
  7766. (setq maxfields (apply 'max (mapcar 'length fields)))
  7767. (error
  7768. (kill-region beg end)
  7769. (org-table-create org-table-default-size)
  7770. (error "Empty table - created default table")))
  7771. ;; A list of empty strings to fill any short rows on output
  7772. (setq emptystrings (make-list maxfields ""))
  7773. ;; Check for special formatting.
  7774. (setq i -1)
  7775. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7776. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7777. ;; Check if there is an explicit width specified
  7778. (when narrow
  7779. (setq c column fmax nil)
  7780. (while c
  7781. (setq e (pop c))
  7782. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7783. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7784. ;; Find fields that are wider than fmax, and shorten them
  7785. (when fmax
  7786. (loop for xx in column do
  7787. (when (and (stringp xx)
  7788. (> (org-string-width xx) fmax))
  7789. (org-add-props xx nil
  7790. 'help-echo
  7791. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7792. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7793. (unless (> f1 1)
  7794. (error "Cannot narrow field starting with wide link \"%s\""
  7795. (match-string 0 xx)))
  7796. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7797. (add-text-properties (- f1 2) f1
  7798. (list 'display org-narrow-column-arrow)
  7799. xx)))))
  7800. ;; Get the maximum width for each column
  7801. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7802. ;; Get the fraction of numbers, to decide about alignment of the column
  7803. (setq cnt 0 frac 0.0)
  7804. (loop for x in column do
  7805. (if (equal x "")
  7806. nil
  7807. (setq frac ( / (+ (* frac cnt)
  7808. (if (string-match org-table-number-regexp x) 1 0))
  7809. (setq cnt (1+ cnt))))))
  7810. (push (>= frac org-table-number-fraction) typenums))
  7811. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7812. ;; Store the alignment of this table, for later editing of single fields
  7813. (setq org-table-last-alignment typenums
  7814. org-table-last-column-widths lengths)
  7815. ;; With invisible characters, `format' does not get the field width right
  7816. ;; So we need to make these fields wide by hand.
  7817. (when (or links emph)
  7818. (loop for i from 0 upto (1- maxfields) do
  7819. (setq len (nth i lengths))
  7820. (loop for j from 0 upto (1- (length fields)) do
  7821. (setq c (nthcdr i (car (nthcdr j fields))))
  7822. (if (and (stringp (car c))
  7823. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7824. ; (string-match org-bracket-link-regexp (car c))
  7825. (< (org-string-width (car c)) len))
  7826. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7827. ;; Compute the formats needed for output of the table
  7828. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7829. (while (setq l (pop lengths))
  7830. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7831. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7832. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7833. (setq rfmt (concat rfmt "\n")
  7834. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7835. (setq new (mapconcat
  7836. (lambda (l)
  7837. (if l (apply 'format rfmt
  7838. (append (pop fields) emptystrings))
  7839. hfmt))
  7840. lines ""))
  7841. ;; Replace the old one
  7842. (delete-region beg end)
  7843. (move-marker end nil)
  7844. (move-marker org-table-aligned-begin-marker (point))
  7845. (insert new)
  7846. (move-marker org-table-aligned-end-marker (point))
  7847. (when (and orgtbl-mode (not (org-mode-p)))
  7848. (goto-char org-table-aligned-begin-marker)
  7849. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7850. ;; Try to move to the old location
  7851. (goto-line winstartline)
  7852. (setq winstart (point-at-bol))
  7853. (goto-line linepos)
  7854. (set-window-start (selected-window) winstart 'noforce)
  7855. (org-table-goto-column colpos)
  7856. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7857. (setq org-table-may-need-update nil)
  7858. ))
  7859. (defun org-string-width (s)
  7860. "Compute width of string, ignoring invisible characters.
  7861. This ignores character with invisibility property `org-link', and also
  7862. characters with property `org-cwidth', because these will become invisible
  7863. upon the next fontification round."
  7864. (let (b l)
  7865. (when (or (eq t buffer-invisibility-spec)
  7866. (assq 'org-link buffer-invisibility-spec))
  7867. (while (setq b (text-property-any 0 (length s)
  7868. 'invisible 'org-link s))
  7869. (setq s (concat (substring s 0 b)
  7870. (substring s (or (next-single-property-change
  7871. b 'invisible s) (length s)))))))
  7872. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7873. (setq s (concat (substring s 0 b)
  7874. (substring s (or (next-single-property-change
  7875. b 'org-cwidth s) (length s))))))
  7876. (setq l (string-width s) b -1)
  7877. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7878. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7879. l))
  7880. (defun org-table-begin (&optional table-type)
  7881. "Find the beginning of the table and return its position.
  7882. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7883. (save-excursion
  7884. (if (not (re-search-backward
  7885. (if table-type org-table-any-border-regexp
  7886. org-table-border-regexp)
  7887. nil t))
  7888. (progn (goto-char (point-min)) (point))
  7889. (goto-char (match-beginning 0))
  7890. (beginning-of-line 2)
  7891. (point))))
  7892. (defun org-table-end (&optional table-type)
  7893. "Find the end of the table and return its position.
  7894. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7895. (save-excursion
  7896. (if (not (re-search-forward
  7897. (if table-type org-table-any-border-regexp
  7898. org-table-border-regexp)
  7899. nil t))
  7900. (goto-char (point-max))
  7901. (goto-char (match-beginning 0)))
  7902. (point-marker)))
  7903. (defun org-table-justify-field-maybe (&optional new)
  7904. "Justify the current field, text to left, number to right.
  7905. Optional argument NEW may specify text to replace the current field content."
  7906. (cond
  7907. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7908. ((org-at-table-hline-p))
  7909. ((and (not new)
  7910. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7911. (current-buffer)))
  7912. (< (point) org-table-aligned-begin-marker)
  7913. (>= (point) org-table-aligned-end-marker)))
  7914. ;; This is not the same table, force a full re-align
  7915. (setq org-table-may-need-update t))
  7916. (t ;; realign the current field, based on previous full realign
  7917. (let* ((pos (point)) s
  7918. (col (org-table-current-column))
  7919. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7920. l f n o e)
  7921. (when (> col 0)
  7922. (skip-chars-backward "^|\n")
  7923. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7924. (progn
  7925. (setq s (match-string 1)
  7926. o (match-string 0)
  7927. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7928. e (not (= (match-beginning 2) (match-end 2))))
  7929. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7930. l (if e "|" (setq org-table-may-need-update t) ""))
  7931. n (format f s))
  7932. (if new
  7933. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7934. (setq n (format f new))
  7935. (setq n (concat new "|") org-table-may-need-update t)))
  7936. (or (equal n o)
  7937. (let (org-table-may-need-update)
  7938. (replace-match n t t))))
  7939. (setq org-table-may-need-update t))
  7940. (goto-char pos))))))
  7941. (defun org-table-next-field ()
  7942. "Go to the next field in the current table, creating new lines as needed.
  7943. Before doing so, re-align the table if necessary."
  7944. (interactive)
  7945. (org-table-maybe-eval-formula)
  7946. (org-table-maybe-recalculate-line)
  7947. (if (and org-table-automatic-realign
  7948. org-table-may-need-update)
  7949. (org-table-align))
  7950. (let ((end (org-table-end)))
  7951. (if (org-at-table-hline-p)
  7952. (end-of-line 1))
  7953. (condition-case nil
  7954. (progn
  7955. (re-search-forward "|" end)
  7956. (if (looking-at "[ \t]*$")
  7957. (re-search-forward "|" end))
  7958. (if (and (looking-at "-")
  7959. org-table-tab-jumps-over-hlines
  7960. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7961. (goto-char (match-beginning 1)))
  7962. (if (looking-at "-")
  7963. (progn
  7964. (beginning-of-line 0)
  7965. (org-table-insert-row 'below))
  7966. (if (looking-at " ") (forward-char 1))))
  7967. (error
  7968. (org-table-insert-row 'below)))))
  7969. (defun org-table-previous-field ()
  7970. "Go to the previous field in the table.
  7971. Before doing so, re-align the table if necessary."
  7972. (interactive)
  7973. (org-table-justify-field-maybe)
  7974. (org-table-maybe-recalculate-line)
  7975. (if (and org-table-automatic-realign
  7976. org-table-may-need-update)
  7977. (org-table-align))
  7978. (if (org-at-table-hline-p)
  7979. (end-of-line 1))
  7980. (re-search-backward "|" (org-table-begin))
  7981. (re-search-backward "|" (org-table-begin))
  7982. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7983. (re-search-backward "|" (org-table-begin)))
  7984. (if (looking-at "| ?")
  7985. (goto-char (match-end 0))))
  7986. (defun org-table-next-row ()
  7987. "Go to the next row (same column) in the current table.
  7988. Before doing so, re-align the table if necessary."
  7989. (interactive)
  7990. (org-table-maybe-eval-formula)
  7991. (org-table-maybe-recalculate-line)
  7992. (if (or (looking-at "[ \t]*$")
  7993. (save-excursion (skip-chars-backward " \t") (bolp)))
  7994. (newline)
  7995. (if (and org-table-automatic-realign
  7996. org-table-may-need-update)
  7997. (org-table-align))
  7998. (let ((col (org-table-current-column)))
  7999. (beginning-of-line 2)
  8000. (if (or (not (org-at-table-p))
  8001. (org-at-table-hline-p))
  8002. (progn
  8003. (beginning-of-line 0)
  8004. (org-table-insert-row 'below)))
  8005. (org-table-goto-column col)
  8006. (skip-chars-backward "^|\n\r")
  8007. (if (looking-at " ") (forward-char 1)))))
  8008. (defun org-table-copy-down (n)
  8009. "Copy a field down in the current column.
  8010. If the field at the cursor is empty, copy into it the content of the nearest
  8011. non-empty field above. With argument N, use the Nth non-empty field.
  8012. If the current field is not empty, it is copied down to the next row, and
  8013. the cursor is moved with it. Therefore, repeating this command causes the
  8014. column to be filled row-by-row.
  8015. If the variable `org-table-copy-increment' is non-nil and the field is an
  8016. integer or a timestamp, it will be incremented while copying. In the case of
  8017. a timestamp, if the cursor is on the year, change the year. If it is on the
  8018. month or the day, change that. Point will stay on the current date field
  8019. in order to easily repeat the interval."
  8020. (interactive "p")
  8021. (let* ((colpos (org-table-current-column))
  8022. (col (current-column))
  8023. (field (org-table-get-field))
  8024. (non-empty (string-match "[^ \t]" field))
  8025. (beg (org-table-begin))
  8026. txt)
  8027. (org-table-check-inside-data-field)
  8028. (if non-empty
  8029. (progn
  8030. (setq txt (org-trim field))
  8031. (org-table-next-row)
  8032. (org-table-blank-field))
  8033. (save-excursion
  8034. (setq txt
  8035. (catch 'exit
  8036. (while (progn (beginning-of-line 1)
  8037. (re-search-backward org-table-dataline-regexp
  8038. beg t))
  8039. (org-table-goto-column colpos t)
  8040. (if (and (looking-at
  8041. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8042. (= (setq n (1- n)) 0))
  8043. (throw 'exit (match-string 1))))))))
  8044. (if txt
  8045. (progn
  8046. (if (and org-table-copy-increment
  8047. (string-match "^[0-9]+$" txt))
  8048. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8049. (insert txt)
  8050. (move-to-column col)
  8051. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8052. (org-timestamp-up 1)
  8053. (org-table-maybe-recalculate-line))
  8054. (org-table-align)
  8055. (move-to-column col))
  8056. (error "No non-empty field found"))))
  8057. (defun org-table-check-inside-data-field ()
  8058. "Is point inside a table data field?
  8059. I.e. not on a hline or before the first or after the last column?
  8060. This actually throws an error, so it aborts the current command."
  8061. (if (or (not (org-at-table-p))
  8062. (= (org-table-current-column) 0)
  8063. (org-at-table-hline-p)
  8064. (looking-at "[ \t]*$"))
  8065. (error "Not in table data field")))
  8066. (defvar org-table-clip nil
  8067. "Clipboard for table regions.")
  8068. (defun org-table-blank-field ()
  8069. "Blank the current table field or active region."
  8070. (interactive)
  8071. (org-table-check-inside-data-field)
  8072. (if (and (interactive-p) (org-region-active-p))
  8073. (let (org-table-clip)
  8074. (org-table-cut-region (region-beginning) (region-end)))
  8075. (skip-chars-backward "^|")
  8076. (backward-char 1)
  8077. (if (looking-at "|[^|\n]+")
  8078. (let* ((pos (match-beginning 0))
  8079. (match (match-string 0))
  8080. (len (org-string-width match)))
  8081. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8082. (goto-char (+ 2 pos))
  8083. (substring match 1)))))
  8084. (defun org-table-get-field (&optional n replace)
  8085. "Return the value of the field in column N of current row.
  8086. N defaults to current field.
  8087. If REPLACE is a string, replace field with this value. The return value
  8088. is always the old value."
  8089. (and n (org-table-goto-column n))
  8090. (skip-chars-backward "^|\n")
  8091. (backward-char 1)
  8092. (if (looking-at "|[^|\r\n]*")
  8093. (let* ((pos (match-beginning 0))
  8094. (val (buffer-substring (1+ pos) (match-end 0))))
  8095. (if replace
  8096. (replace-match (concat "|" replace) t t))
  8097. (goto-char (min (point-at-eol) (+ 2 pos)))
  8098. val)
  8099. (forward-char 1) ""))
  8100. (defun org-table-field-info (arg)
  8101. "Show info about the current field, and highlight any reference at point."
  8102. (interactive "P")
  8103. (org-table-get-specials)
  8104. (save-excursion
  8105. (let* ((pos (point))
  8106. (col (org-table-current-column))
  8107. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8108. (name (car (rassoc (list (org-current-line) col)
  8109. org-table-named-field-locations)))
  8110. (eql (org-table-get-stored-formulas))
  8111. (dline (org-table-current-dline))
  8112. (ref (format "@%d$%d" dline col))
  8113. (ref1 (org-table-convert-refs-to-an ref))
  8114. (fequation (or (assoc name eql) (assoc ref eql)))
  8115. (cequation (assoc (int-to-string col) eql))
  8116. (eqn (or fequation cequation)))
  8117. (goto-char pos)
  8118. (condition-case nil
  8119. (org-table-show-reference 'local)
  8120. (error nil))
  8121. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8122. dline col
  8123. (if cname (concat " or $" cname) "")
  8124. dline col ref1
  8125. (if name (concat " or $" name) "")
  8126. ;; FIXME: formula info not correct if special table line
  8127. (if eqn
  8128. (concat ", formula: "
  8129. (org-table-formula-to-user
  8130. (concat
  8131. (if (string-match "^[$@]"(car eqn)) "" "$")
  8132. (car eqn) "=" (cdr eqn))))
  8133. "")))))
  8134. (defun org-table-current-column ()
  8135. "Find out which column we are in.
  8136. When called interactively, column is also displayed in echo area."
  8137. (interactive)
  8138. (if (interactive-p) (org-table-check-inside-data-field))
  8139. (save-excursion
  8140. (let ((cnt 0) (pos (point)))
  8141. (beginning-of-line 1)
  8142. (while (search-forward "|" pos t)
  8143. (setq cnt (1+ cnt)))
  8144. (if (interactive-p) (message "This is table column %d" cnt))
  8145. cnt)))
  8146. (defun org-table-current-dline ()
  8147. "Find out what table data line we are in.
  8148. Only datalins count for this."
  8149. (interactive)
  8150. (if (interactive-p) (org-table-check-inside-data-field))
  8151. (save-excursion
  8152. (let ((cnt 0) (pos (point)))
  8153. (goto-char (org-table-begin))
  8154. (while (<= (point) pos)
  8155. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8156. (beginning-of-line 2))
  8157. (if (interactive-p) (message "This is table line %d" cnt))
  8158. cnt)))
  8159. (defun org-table-goto-column (n &optional on-delim force)
  8160. "Move the cursor to the Nth column in the current table line.
  8161. With optional argument ON-DELIM, stop with point before the left delimiter
  8162. of the field.
  8163. If there are less than N fields, just go to after the last delimiter.
  8164. However, when FORCE is non-nil, create new columns if necessary."
  8165. (interactive "p")
  8166. (let ((pos (point-at-eol)))
  8167. (beginning-of-line 1)
  8168. (when (> n 0)
  8169. (while (and (> (setq n (1- n)) -1)
  8170. (or (search-forward "|" pos t)
  8171. (and force
  8172. (progn (end-of-line 1)
  8173. (skip-chars-backward "^|")
  8174. (insert " | "))))))
  8175. ; (backward-char 2) t)))))
  8176. (when (and force (not (looking-at ".*|")))
  8177. (save-excursion (end-of-line 1) (insert " | ")))
  8178. (if on-delim
  8179. (backward-char 1)
  8180. (if (looking-at " ") (forward-char 1))))))
  8181. (defun org-at-table-p (&optional table-type)
  8182. "Return t if the cursor is inside an org-type table.
  8183. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8184. (if org-enable-table-editor
  8185. (save-excursion
  8186. (beginning-of-line 1)
  8187. (looking-at (if table-type org-table-any-line-regexp
  8188. org-table-line-regexp)))
  8189. nil))
  8190. (defun org-at-table.el-p ()
  8191. "Return t if and only if we are at a table.el table."
  8192. (and (org-at-table-p 'any)
  8193. (save-excursion
  8194. (goto-char (org-table-begin 'any))
  8195. (looking-at org-table1-hline-regexp))))
  8196. (defun org-table-recognize-table.el ()
  8197. "If there is a table.el table nearby, recognize it and move into it."
  8198. (if org-table-tab-recognizes-table.el
  8199. (if (org-at-table.el-p)
  8200. (progn
  8201. (beginning-of-line 1)
  8202. (if (looking-at org-table-dataline-regexp)
  8203. nil
  8204. (if (looking-at org-table1-hline-regexp)
  8205. (progn
  8206. (beginning-of-line 2)
  8207. (if (looking-at org-table-any-border-regexp)
  8208. (beginning-of-line -1)))))
  8209. (if (re-search-forward "|" (org-table-end t) t)
  8210. (progn
  8211. (require 'table)
  8212. (if (table--at-cell-p (point))
  8213. t
  8214. (message "recognizing table.el table...")
  8215. (table-recognize-table)
  8216. (message "recognizing table.el table...done")))
  8217. (error "This should not happen..."))
  8218. t)
  8219. nil)
  8220. nil))
  8221. (defun org-at-table-hline-p ()
  8222. "Return t if the cursor is inside a hline in a table."
  8223. (if org-enable-table-editor
  8224. (save-excursion
  8225. (beginning-of-line 1)
  8226. (looking-at org-table-hline-regexp))
  8227. nil))
  8228. (defun org-table-insert-column ()
  8229. "Insert a new column into the table."
  8230. (interactive)
  8231. (if (not (org-at-table-p))
  8232. (error "Not at a table"))
  8233. (org-table-find-dataline)
  8234. (let* ((col (max 1 (org-table-current-column)))
  8235. (beg (org-table-begin))
  8236. (end (org-table-end))
  8237. ;; Current cursor position
  8238. (linepos (org-current-line))
  8239. (colpos col))
  8240. (goto-char beg)
  8241. (while (< (point) end)
  8242. (if (org-at-table-hline-p)
  8243. nil
  8244. (org-table-goto-column col t)
  8245. (insert "| "))
  8246. (beginning-of-line 2))
  8247. (move-marker end nil)
  8248. (goto-line linepos)
  8249. (org-table-goto-column colpos)
  8250. (org-table-align)
  8251. (org-table-fix-formulas "$" nil (1- col) 1)))
  8252. (defun org-table-find-dataline ()
  8253. "Find a dataline in the current table, which is needed for column commands."
  8254. (if (and (org-at-table-p)
  8255. (not (org-at-table-hline-p)))
  8256. t
  8257. (let ((col (current-column))
  8258. (end (org-table-end)))
  8259. (move-to-column col)
  8260. (while (and (< (point) end)
  8261. (or (not (= (current-column) col))
  8262. (org-at-table-hline-p)))
  8263. (beginning-of-line 2)
  8264. (move-to-column col))
  8265. (if (and (org-at-table-p)
  8266. (not (org-at-table-hline-p)))
  8267. t
  8268. (error
  8269. "Please position cursor in a data line for column operations")))))
  8270. (defun org-table-delete-column ()
  8271. "Delete a column from the table."
  8272. (interactive)
  8273. (if (not (org-at-table-p))
  8274. (error "Not at a table"))
  8275. (org-table-find-dataline)
  8276. (org-table-check-inside-data-field)
  8277. (let* ((col (org-table-current-column))
  8278. (beg (org-table-begin))
  8279. (end (org-table-end))
  8280. ;; Current cursor position
  8281. (linepos (org-current-line))
  8282. (colpos col))
  8283. (goto-char beg)
  8284. (while (< (point) end)
  8285. (if (org-at-table-hline-p)
  8286. nil
  8287. (org-table-goto-column col t)
  8288. (and (looking-at "|[^|\n]+|")
  8289. (replace-match "|")))
  8290. (beginning-of-line 2))
  8291. (move-marker end nil)
  8292. (goto-line linepos)
  8293. (org-table-goto-column colpos)
  8294. (org-table-align)
  8295. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8296. col -1 col)))
  8297. (defun org-table-move-column-right ()
  8298. "Move column to the right."
  8299. (interactive)
  8300. (org-table-move-column nil))
  8301. (defun org-table-move-column-left ()
  8302. "Move column to the left."
  8303. (interactive)
  8304. (org-table-move-column 'left))
  8305. (defun org-table-move-column (&optional left)
  8306. "Move the current column to the right. With arg LEFT, move to the left."
  8307. (interactive "P")
  8308. (if (not (org-at-table-p))
  8309. (error "Not at a table"))
  8310. (org-table-find-dataline)
  8311. (org-table-check-inside-data-field)
  8312. (let* ((col (org-table-current-column))
  8313. (col1 (if left (1- col) col))
  8314. (beg (org-table-begin))
  8315. (end (org-table-end))
  8316. ;; Current cursor position
  8317. (linepos (org-current-line))
  8318. (colpos (if left (1- col) (1+ col))))
  8319. (if (and left (= col 1))
  8320. (error "Cannot move column further left"))
  8321. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8322. (error "Cannot move column further right"))
  8323. (goto-char beg)
  8324. (while (< (point) end)
  8325. (if (org-at-table-hline-p)
  8326. nil
  8327. (org-table-goto-column col1 t)
  8328. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8329. (replace-match "|\\2|\\1|")))
  8330. (beginning-of-line 2))
  8331. (move-marker end nil)
  8332. (goto-line linepos)
  8333. (org-table-goto-column colpos)
  8334. (org-table-align)
  8335. (org-table-fix-formulas
  8336. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8337. (cons (number-to-string colpos) (number-to-string col))))))
  8338. (defun org-table-move-row-down ()
  8339. "Move table row down."
  8340. (interactive)
  8341. (org-table-move-row nil))
  8342. (defun org-table-move-row-up ()
  8343. "Move table row up."
  8344. (interactive)
  8345. (org-table-move-row 'up))
  8346. (defun org-table-move-row (&optional up)
  8347. "Move the current table line down. With arg UP, move it up."
  8348. (interactive "P")
  8349. (let* ((col (current-column))
  8350. (pos (point))
  8351. (hline1p (save-excursion (beginning-of-line 1)
  8352. (looking-at org-table-hline-regexp)))
  8353. (dline1 (org-table-current-dline))
  8354. (dline2 (+ dline1 (if up -1 1)))
  8355. (tonew (if up 0 2))
  8356. txt hline2p)
  8357. (beginning-of-line tonew)
  8358. (unless (org-at-table-p)
  8359. (goto-char pos)
  8360. (error "Cannot move row further"))
  8361. (setq hline2p (looking-at org-table-hline-regexp))
  8362. (goto-char pos)
  8363. (beginning-of-line 1)
  8364. (setq pos (point))
  8365. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8366. (delete-region (point) (1+ (point-at-eol)))
  8367. (beginning-of-line tonew)
  8368. (insert txt)
  8369. (beginning-of-line 0)
  8370. (move-to-column col)
  8371. (unless (or hline1p hline2p)
  8372. (org-table-fix-formulas
  8373. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8374. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8375. (defun org-table-insert-row (&optional arg)
  8376. "Insert a new row above the current line into the table.
  8377. With prefix ARG, insert below the current line."
  8378. (interactive "P")
  8379. (if (not (org-at-table-p))
  8380. (error "Not at a table"))
  8381. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8382. (new (org-table-clean-line line)))
  8383. ;; Fix the first field if necessary
  8384. (if (string-match "^[ \t]*| *[#$] *|" line)
  8385. (setq new (replace-match (match-string 0 line) t t new)))
  8386. (beginning-of-line (if arg 2 1))
  8387. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8388. (beginning-of-line 0)
  8389. (re-search-forward "| ?" (point-at-eol) t)
  8390. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8391. (org-table-align))
  8392. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8393. (defun org-table-insert-hline (&optional above)
  8394. "Insert a horizontal-line below the current line into the table.
  8395. With prefix ABOVE, insert above the current line."
  8396. (interactive "P")
  8397. (if (not (org-at-table-p))
  8398. (error "Not at a table"))
  8399. (let ((line (org-table-clean-line
  8400. (buffer-substring (point-at-bol) (point-at-eol))))
  8401. (col (current-column)))
  8402. (while (string-match "|\\( +\\)|" line)
  8403. (setq line (replace-match
  8404. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8405. ?-) "|") t t line)))
  8406. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8407. (beginning-of-line (if above 1 2))
  8408. (insert line "\n")
  8409. (beginning-of-line (if above 1 -1))
  8410. (move-to-column col)
  8411. (and org-table-overlay-coordinates (org-table-align))))
  8412. (defun org-table-hline-and-move (&optional same-column)
  8413. "Insert a hline and move to the row below that line."
  8414. (interactive "P")
  8415. (let ((col (org-table-current-column)))
  8416. (org-table-maybe-eval-formula)
  8417. (org-table-maybe-recalculate-line)
  8418. (org-table-insert-hline)
  8419. (end-of-line 2)
  8420. (if (looking-at "\n[ \t]*|-")
  8421. (progn (insert "\n|") (org-table-align))
  8422. (org-table-next-field))
  8423. (if same-column (org-table-goto-column col))))
  8424. (defun org-table-clean-line (s)
  8425. "Convert a table line S into a string with only \"|\" and space.
  8426. In particular, this does handle wide and invisible characters."
  8427. (if (string-match "^[ \t]*|-" s)
  8428. ;; It's a hline, just map the characters
  8429. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8430. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8431. (setq s (replace-match
  8432. (concat "|" (make-string (org-string-width (match-string 1 s))
  8433. ?\ ) "|")
  8434. t t s)))
  8435. s))
  8436. (defun org-table-kill-row ()
  8437. "Delete the current row or horizontal line from the table."
  8438. (interactive)
  8439. (if (not (org-at-table-p))
  8440. (error "Not at a table"))
  8441. (let ((col (current-column))
  8442. (dline (org-table-current-dline)))
  8443. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8444. (if (not (org-at-table-p)) (beginning-of-line 0))
  8445. (move-to-column col)
  8446. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8447. dline -1 dline)))
  8448. (defun org-table-sort-lines (with-case &optional sorting-type)
  8449. "Sort table lines according to the column at point.
  8450. The position of point indicates the column to be used for
  8451. sorting, and the range of lines is the range between the nearest
  8452. horizontal separator lines, or the entire table of no such lines
  8453. exist. If point is before the first column, you will be prompted
  8454. for the sorting column. If there is an active region, the mark
  8455. specifies the first line and the sorting column, while point
  8456. should be in the last line to be included into the sorting.
  8457. The command then prompts for the sorting type which can be
  8458. alphabetically, numerically, or by time (as given in a time stamp
  8459. in the field). Sorting in reverse order is also possible.
  8460. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8461. If SORTING-TYPE is specified when this function is called from a Lisp
  8462. program, no prompting will take place. SORTING-TYPE must be a character,
  8463. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8464. should be done in reverse order."
  8465. (interactive "P")
  8466. (let* ((thisline (org-current-line))
  8467. (thiscol (org-table-current-column))
  8468. beg end bcol ecol tend tbeg column lns pos)
  8469. (when (equal thiscol 0)
  8470. (if (interactive-p)
  8471. (setq thiscol
  8472. (string-to-number
  8473. (read-string "Use column N for sorting: ")))
  8474. (setq thiscol 1))
  8475. (org-table-goto-column thiscol))
  8476. (org-table-check-inside-data-field)
  8477. (if (org-region-active-p)
  8478. (progn
  8479. (setq beg (region-beginning) end (region-end))
  8480. (goto-char beg)
  8481. (setq column (org-table-current-column)
  8482. beg (point-at-bol))
  8483. (goto-char end)
  8484. (setq end (point-at-bol 2)))
  8485. (setq column (org-table-current-column)
  8486. pos (point)
  8487. tbeg (org-table-begin)
  8488. tend (org-table-end))
  8489. (if (re-search-backward org-table-hline-regexp tbeg t)
  8490. (setq beg (point-at-bol 2))
  8491. (goto-char tbeg)
  8492. (setq beg (point-at-bol 1)))
  8493. (goto-char pos)
  8494. (if (re-search-forward org-table-hline-regexp tend t)
  8495. (setq end (point-at-bol 1))
  8496. (goto-char tend)
  8497. (setq end (point-at-bol))))
  8498. (setq beg (move-marker (make-marker) beg)
  8499. end (move-marker (make-marker) end))
  8500. (untabify beg end)
  8501. (goto-char beg)
  8502. (org-table-goto-column column)
  8503. (skip-chars-backward "^|")
  8504. (setq bcol (current-column))
  8505. (org-table-goto-column (1+ column))
  8506. (skip-chars-backward "^|")
  8507. (setq ecol (1- (current-column)))
  8508. (org-table-goto-column column)
  8509. (setq lns (mapcar (lambda(x) (cons
  8510. (org-sort-remove-invisible
  8511. (nth (1- column)
  8512. (org-split-string x "[ \t]*|[ \t]*")))
  8513. x))
  8514. (org-split-string (buffer-substring beg end) "\n")))
  8515. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8516. (delete-region beg end)
  8517. (move-marker beg nil)
  8518. (move-marker end nil)
  8519. (insert (mapconcat 'cdr lns "\n") "\n")
  8520. (goto-line thisline)
  8521. (org-table-goto-column thiscol)
  8522. (message "%d lines sorted, based on column %d" (length lns) column)))
  8523. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8524. (defun org-sort-remove-invisible (s)
  8525. (remove-text-properties 0 (length s) org-rm-props s)
  8526. (while (string-match org-bracket-link-regexp s)
  8527. (setq s (replace-match (if (match-end 2)
  8528. (match-string 3 s)
  8529. (match-string 1 s)) t t s)))
  8530. s)
  8531. (defun org-table-cut-region (beg end)
  8532. "Copy region in table to the clipboard and blank all relevant fields."
  8533. (interactive "r")
  8534. (org-table-copy-region beg end 'cut))
  8535. (defun org-table-copy-region (beg end &optional cut)
  8536. "Copy rectangular region in table to clipboard.
  8537. A special clipboard is used which can only be accessed
  8538. with `org-table-paste-rectangle'."
  8539. (interactive "rP")
  8540. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8541. region cols
  8542. (rpl (if cut " " nil)))
  8543. (goto-char beg)
  8544. (org-table-check-inside-data-field)
  8545. (setq l01 (org-current-line)
  8546. c01 (org-table-current-column))
  8547. (goto-char end)
  8548. (org-table-check-inside-data-field)
  8549. (setq l02 (org-current-line)
  8550. c02 (org-table-current-column))
  8551. (setq l1 (min l01 l02) l2 (max l01 l02)
  8552. c1 (min c01 c02) c2 (max c01 c02))
  8553. (catch 'exit
  8554. (while t
  8555. (catch 'nextline
  8556. (if (> l1 l2) (throw 'exit t))
  8557. (goto-line l1)
  8558. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8559. (setq cols nil ic1 c1 ic2 c2)
  8560. (while (< ic1 (1+ ic2))
  8561. (push (org-table-get-field ic1 rpl) cols)
  8562. (setq ic1 (1+ ic1)))
  8563. (push (nreverse cols) region)
  8564. (setq l1 (1+ l1)))))
  8565. (setq org-table-clip (nreverse region))
  8566. (if cut (org-table-align))
  8567. org-table-clip))
  8568. (defun org-table-paste-rectangle ()
  8569. "Paste a rectangular region into a table.
  8570. The upper right corner ends up in the current field. All involved fields
  8571. will be overwritten. If the rectangle does not fit into the present table,
  8572. the table is enlarged as needed. The process ignores horizontal separator
  8573. lines."
  8574. (interactive)
  8575. (unless (and org-table-clip (listp org-table-clip))
  8576. (error "First cut/copy a region to paste!"))
  8577. (org-table-check-inside-data-field)
  8578. (let* ((clip org-table-clip)
  8579. (line (org-current-line))
  8580. (col (org-table-current-column))
  8581. (org-enable-table-editor t)
  8582. (org-table-automatic-realign nil)
  8583. c cols field)
  8584. (while (setq cols (pop clip))
  8585. (while (org-at-table-hline-p) (beginning-of-line 2))
  8586. (if (not (org-at-table-p))
  8587. (progn (end-of-line 0) (org-table-next-field)))
  8588. (setq c col)
  8589. (while (setq field (pop cols))
  8590. (org-table-goto-column c nil 'force)
  8591. (org-table-get-field nil field)
  8592. (setq c (1+ c)))
  8593. (beginning-of-line 2))
  8594. (goto-line line)
  8595. (org-table-goto-column col)
  8596. (org-table-align)))
  8597. (defun org-table-convert ()
  8598. "Convert from `org-mode' table to table.el and back.
  8599. Obviously, this only works within limits. When an Org-mode table is
  8600. converted to table.el, all horizontal separator lines get lost, because
  8601. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8602. A table.el table can be converted to an Org-mode table only if it does not
  8603. do row or column spanning. Multiline cells will become multiple cells.
  8604. Beware, Org-mode does not test if the table can be successfully converted - it
  8605. blindly applies a recipe that works for simple tables."
  8606. (interactive)
  8607. (require 'table)
  8608. (if (org-at-table.el-p)
  8609. ;; convert to Org-mode table
  8610. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8611. (end (move-marker (make-marker) (org-table-end t))))
  8612. (table-unrecognize-region beg end)
  8613. (goto-char beg)
  8614. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8615. (replace-match ""))
  8616. (goto-char beg))
  8617. (if (org-at-table-p)
  8618. ;; convert to table.el table
  8619. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8620. (end (move-marker (make-marker) (org-table-end))))
  8621. ;; first, get rid of all horizontal lines
  8622. (goto-char beg)
  8623. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8624. (replace-match ""))
  8625. ;; insert a hline before first
  8626. (goto-char beg)
  8627. (org-table-insert-hline 'above)
  8628. (beginning-of-line -1)
  8629. ;; insert a hline after each line
  8630. (while (progn (beginning-of-line 3) (< (point) end))
  8631. (org-table-insert-hline))
  8632. (goto-char beg)
  8633. (setq end (move-marker end (org-table-end)))
  8634. ;; replace "+" at beginning and ending of hlines
  8635. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8636. (replace-match "\\1+-"))
  8637. (goto-char beg)
  8638. (while (re-search-forward "-|[ \t]*$" end t)
  8639. (replace-match "-+"))
  8640. (goto-char beg)))))
  8641. (defun org-table-wrap-region (arg)
  8642. "Wrap several fields in a column like a paragraph.
  8643. This is useful if you'd like to spread the contents of a field over several
  8644. lines, in order to keep the table compact.
  8645. If there is an active region, and both point and mark are in the same column,
  8646. the text in the column is wrapped to minimum width for the given number of
  8647. lines. Generally, this makes the table more compact. A prefix ARG may be
  8648. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8649. formats the selected text to two lines. If the region was longer than two
  8650. lines, the remaining lines remain empty. A negative prefix argument reduces
  8651. the current number of lines by that amount. The wrapped text is pasted back
  8652. into the table. If you formatted it to more lines than it was before, fields
  8653. further down in the table get overwritten - so you might need to make space in
  8654. the table first.
  8655. If there is no region, the current field is split at the cursor position and
  8656. the text fragment to the right of the cursor is prepended to the field one
  8657. line down.
  8658. If there is no region, but you specify a prefix ARG, the current field gets
  8659. blank, and the content is appended to the field above."
  8660. (interactive "P")
  8661. (org-table-check-inside-data-field)
  8662. (if (org-region-active-p)
  8663. ;; There is a region: fill as a paragraph
  8664. (let* ((beg (region-beginning))
  8665. (cline (save-excursion (goto-char beg) (org-current-line)))
  8666. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8667. nlines)
  8668. (org-table-cut-region (region-beginning) (region-end))
  8669. (if (> (length (car org-table-clip)) 1)
  8670. (error "Region must be limited to single column"))
  8671. (setq nlines (if arg
  8672. (if (< arg 1)
  8673. (+ (length org-table-clip) arg)
  8674. arg)
  8675. (length org-table-clip)))
  8676. (setq org-table-clip
  8677. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8678. nil nlines)))
  8679. (goto-line cline)
  8680. (org-table-goto-column ccol)
  8681. (org-table-paste-rectangle))
  8682. ;; No region, split the current field at point
  8683. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8684. (skip-chars-forward "^\r\n|"))
  8685. (if arg
  8686. ;; combine with field above
  8687. (let ((s (org-table-blank-field))
  8688. (col (org-table-current-column)))
  8689. (beginning-of-line 0)
  8690. (while (org-at-table-hline-p) (beginning-of-line 0))
  8691. (org-table-goto-column col)
  8692. (skip-chars-forward "^|")
  8693. (skip-chars-backward " ")
  8694. (insert " " (org-trim s))
  8695. (org-table-align))
  8696. ;; split field
  8697. (if (looking-at "\\([^|]+\\)+|")
  8698. (let ((s (match-string 1)))
  8699. (replace-match " |")
  8700. (goto-char (match-beginning 0))
  8701. (org-table-next-row)
  8702. (insert (org-trim s) " ")
  8703. (org-table-align))
  8704. (org-table-next-row)))))
  8705. (defvar org-field-marker nil)
  8706. (defun org-table-edit-field (arg)
  8707. "Edit table field in a different window.
  8708. This is mainly useful for fields that contain hidden parts.
  8709. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8710. it can be edited in place."
  8711. (interactive "P")
  8712. (if arg
  8713. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8714. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8715. (remove-text-properties b e '(org-cwidth t invisible t
  8716. display t intangible t))
  8717. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8718. (font-lock-fontify-block)))
  8719. (let ((pos (move-marker (make-marker) (point)))
  8720. (field (org-table-get-field))
  8721. (cw (current-window-configuration))
  8722. p)
  8723. (org-switch-to-buffer-other-window "*Org tmp*")
  8724. (erase-buffer)
  8725. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8726. (let ((org-inhibit-startup t)) (org-mode))
  8727. (goto-char (setq p (point-max)))
  8728. (insert (org-trim field))
  8729. (remove-text-properties p (point-max)
  8730. '(invisible t org-cwidth t display t
  8731. intangible t))
  8732. (goto-char p)
  8733. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8734. (org-set-local 'org-window-configuration cw)
  8735. (org-set-local 'org-field-marker pos)
  8736. (message "Edit and finish with C-c C-c"))))
  8737. (defun org-table-finish-edit-field ()
  8738. "Finish editing a table data field.
  8739. Remove all newline characters, insert the result into the table, realign
  8740. the table and kill the editing buffer."
  8741. (let ((pos org-field-marker)
  8742. (cw org-window-configuration)
  8743. (cb (current-buffer))
  8744. text)
  8745. (goto-char (point-min))
  8746. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8747. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8748. (replace-match " "))
  8749. (setq text (org-trim (buffer-string)))
  8750. (set-window-configuration cw)
  8751. (kill-buffer cb)
  8752. (select-window (get-buffer-window (marker-buffer pos)))
  8753. (goto-char pos)
  8754. (move-marker pos nil)
  8755. (org-table-check-inside-data-field)
  8756. (org-table-get-field nil text)
  8757. (org-table-align)
  8758. (message "New field value inserted")))
  8759. (defun org-trim (s)
  8760. "Remove whitespace at beginning and end of string."
  8761. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8762. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8763. s)
  8764. (defun org-wrap (string &optional width lines)
  8765. "Wrap string to either a number of lines, or a width in characters.
  8766. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8767. that costs. If there is a word longer than WIDTH, the text is actually
  8768. wrapped to the length of that word.
  8769. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8770. many lines, whatever width that takes.
  8771. The return value is a list of lines, without newlines at the end."
  8772. (let* ((words (org-split-string string "[ \t\n]+"))
  8773. (maxword (apply 'max (mapcar 'org-string-width words)))
  8774. w ll)
  8775. (cond (width
  8776. (org-do-wrap words (max maxword width)))
  8777. (lines
  8778. (setq w maxword)
  8779. (setq ll (org-do-wrap words maxword))
  8780. (if (<= (length ll) lines)
  8781. ll
  8782. (setq ll words)
  8783. (while (> (length ll) lines)
  8784. (setq w (1+ w))
  8785. (setq ll (org-do-wrap words w)))
  8786. ll))
  8787. (t (error "Cannot wrap this")))))
  8788. (defun org-do-wrap (words width)
  8789. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8790. (let (lines line)
  8791. (while words
  8792. (setq line (pop words))
  8793. (while (and words (< (+ (length line) (length (car words))) width))
  8794. (setq line (concat line " " (pop words))))
  8795. (setq lines (push line lines)))
  8796. (nreverse lines)))
  8797. (defun org-split-string (string &optional separators)
  8798. "Splits STRING into substrings at SEPARATORS.
  8799. No empty strings are returned if there are matches at the beginning
  8800. and end of string."
  8801. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8802. (start 0)
  8803. notfirst
  8804. (list nil))
  8805. (while (and (string-match rexp string
  8806. (if (and notfirst
  8807. (= start (match-beginning 0))
  8808. (< start (length string)))
  8809. (1+ start) start))
  8810. (< (match-beginning 0) (length string)))
  8811. (setq notfirst t)
  8812. (or (eq (match-beginning 0) 0)
  8813. (and (eq (match-beginning 0) (match-end 0))
  8814. (eq (match-beginning 0) start))
  8815. (setq list
  8816. (cons (substring string start (match-beginning 0))
  8817. list)))
  8818. (setq start (match-end 0)))
  8819. (or (eq start (length string))
  8820. (setq list
  8821. (cons (substring string start)
  8822. list)))
  8823. (nreverse list)))
  8824. (defun org-table-map-tables (function)
  8825. "Apply FUNCTION to the start of all tables in the buffer."
  8826. (save-excursion
  8827. (save-restriction
  8828. (widen)
  8829. (goto-char (point-min))
  8830. (while (re-search-forward org-table-any-line-regexp nil t)
  8831. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8832. (beginning-of-line 1)
  8833. (if (looking-at org-table-line-regexp)
  8834. (save-excursion (funcall function)))
  8835. (re-search-forward org-table-any-border-regexp nil 1))))
  8836. (message "Mapping tables: done"))
  8837. (defvar org-timecnt) ; dynamically scoped parameter
  8838. (defun org-table-sum (&optional beg end nlast)
  8839. "Sum numbers in region of current table column.
  8840. The result will be displayed in the echo area, and will be available
  8841. as kill to be inserted with \\[yank].
  8842. If there is an active region, it is interpreted as a rectangle and all
  8843. numbers in that rectangle will be summed. If there is no active
  8844. region and point is located in a table column, sum all numbers in that
  8845. column.
  8846. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8847. numbers are assumed to be times as well (in decimal hours) and the
  8848. numbers are added as such.
  8849. If NLAST is a number, only the NLAST fields will actually be summed."
  8850. (interactive)
  8851. (save-excursion
  8852. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8853. (cond
  8854. ((and beg end)) ; beg and end given explicitly
  8855. ((org-region-active-p)
  8856. (setq beg (region-beginning) end (region-end)))
  8857. (t
  8858. (setq col (org-table-current-column))
  8859. (goto-char (org-table-begin))
  8860. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8861. (error "No table data"))
  8862. (org-table-goto-column col)
  8863. (setq beg (point))
  8864. (goto-char (org-table-end))
  8865. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8866. (error "No table data"))
  8867. (org-table-goto-column col)
  8868. (setq end (point))))
  8869. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8870. (items1 (cond ((not nlast) items)
  8871. ((>= nlast (length items)) items)
  8872. (t (setq items (reverse items))
  8873. (setcdr (nthcdr (1- nlast) items) nil)
  8874. (nreverse items))))
  8875. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8876. items1)))
  8877. (res (apply '+ numbers))
  8878. (sres (if (= org-timecnt 0)
  8879. (format "%g" res)
  8880. (setq diff (* 3600 res)
  8881. h (floor (/ diff 3600)) diff (mod diff 3600)
  8882. m (floor (/ diff 60)) diff (mod diff 60)
  8883. s diff)
  8884. (format "%d:%02d:%02d" h m s))))
  8885. (kill-new sres)
  8886. (if (interactive-p)
  8887. (message "%s"
  8888. (substitute-command-keys
  8889. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8890. (length numbers) sres))))
  8891. sres))))
  8892. (defun org-table-get-number-for-summing (s)
  8893. (let (n)
  8894. (if (string-match "^ *|? *" s)
  8895. (setq s (replace-match "" nil nil s)))
  8896. (if (string-match " *|? *$" s)
  8897. (setq s (replace-match "" nil nil s)))
  8898. (setq n (string-to-number s))
  8899. (cond
  8900. ((and (string-match "0" s)
  8901. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8902. ((string-match "\\`[ \t]+\\'" s) nil)
  8903. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8904. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8905. (m (string-to-number (or (match-string 2 s) "0")))
  8906. (s (string-to-number (or (match-string 4 s) "0"))))
  8907. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8908. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8909. ((equal n 0) nil)
  8910. (t n))))
  8911. (defun org-table-current-field-formula (&optional key noerror)
  8912. "Return the formula active for the current field.
  8913. Assumes that specials are in place.
  8914. If KEY is given, return the key to this formula.
  8915. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8916. (let* ((name (car (rassoc (list (org-current-line)
  8917. (org-table-current-column))
  8918. org-table-named-field-locations)))
  8919. (col (org-table-current-column))
  8920. (scol (int-to-string col))
  8921. (ref (format "@%d$%d" (org-table-current-dline) col))
  8922. (stored-list (org-table-get-stored-formulas noerror))
  8923. (ass (or (assoc name stored-list)
  8924. (assoc ref stored-list)
  8925. (assoc scol stored-list))))
  8926. (if key
  8927. (car ass)
  8928. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8929. (cdr ass))))))
  8930. (defun org-table-get-formula (&optional equation named)
  8931. "Read a formula from the minibuffer, offer stored formula as default.
  8932. When NAMED is non-nil, look for a named equation."
  8933. (let* ((stored-list (org-table-get-stored-formulas))
  8934. (name (car (rassoc (list (org-current-line)
  8935. (org-table-current-column))
  8936. org-table-named-field-locations)))
  8937. (ref (format "@%d$%d" (org-table-current-dline)
  8938. (org-table-current-column)))
  8939. (refass (assoc ref stored-list))
  8940. (scol (if named
  8941. (if name name ref)
  8942. (int-to-string (org-table-current-column))))
  8943. (dummy (and (or name refass) (not named)
  8944. (not (y-or-n-p "Replace field formula with column formula? " ))
  8945. (error "Abort")))
  8946. (name (or name ref))
  8947. (org-table-may-need-update nil)
  8948. (stored (cdr (assoc scol stored-list)))
  8949. (eq (cond
  8950. ((and stored equation (string-match "^ *=? *$" equation))
  8951. stored)
  8952. ((stringp equation)
  8953. equation)
  8954. (t (org-table-formula-from-user
  8955. (read-string
  8956. (org-table-formula-to-user
  8957. (format "%s formula %s%s="
  8958. (if named "Field" "Column")
  8959. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8960. scol))
  8961. (if stored (org-table-formula-to-user stored) "")
  8962. 'org-table-formula-history
  8963. )))))
  8964. mustsave)
  8965. (when (not (string-match "\\S-" eq))
  8966. ;; remove formula
  8967. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8968. (org-table-store-formulas stored-list)
  8969. (error "Formula removed"))
  8970. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8971. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8972. (if (and name (not named))
  8973. ;; We set the column equation, delete the named one.
  8974. (setq stored-list (delq (assoc name stored-list) stored-list)
  8975. mustsave t))
  8976. (if stored
  8977. (setcdr (assoc scol stored-list) eq)
  8978. (setq stored-list (cons (cons scol eq) stored-list)))
  8979. (if (or mustsave (not (equal stored eq)))
  8980. (org-table-store-formulas stored-list))
  8981. eq))
  8982. (defun org-table-store-formulas (alist)
  8983. "Store the list of formulas below the current table."
  8984. (setq alist (sort alist 'org-table-formula-less-p))
  8985. (save-excursion
  8986. (goto-char (org-table-end))
  8987. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8988. (progn
  8989. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8990. (goto-char (match-beginning 2))
  8991. (delete-region (match-beginning 2) (match-end 0)))
  8992. (insert "#+TBLFM:"))
  8993. (insert " "
  8994. (mapconcat (lambda (x)
  8995. (concat
  8996. (if (equal (string-to-char (car x)) ?@) "" "$")
  8997. (car x) "=" (cdr x)))
  8998. alist "::")
  8999. "\n")))
  9000. (defsubst org-table-formula-make-cmp-string (a)
  9001. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  9002. (concat
  9003. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  9004. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  9005. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  9006. (defun org-table-formula-less-p (a b)
  9007. "Compare two formulas for sorting."
  9008. (let ((as (org-table-formula-make-cmp-string (car a)))
  9009. (bs (org-table-formula-make-cmp-string (car b))))
  9010. (and as bs (string< as bs))))
  9011. (defun org-table-get-stored-formulas (&optional noerror)
  9012. "Return an alist with the stored formulas directly after current table."
  9013. (interactive)
  9014. (let (scol eq eq-alist strings string seen)
  9015. (save-excursion
  9016. (goto-char (org-table-end))
  9017. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9018. (setq strings (org-split-string (match-string 2) " *:: *"))
  9019. (while (setq string (pop strings))
  9020. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9021. (setq scol (if (match-end 2)
  9022. (match-string 2 string)
  9023. (match-string 1 string))
  9024. eq (match-string 3 string)
  9025. eq-alist (cons (cons scol eq) eq-alist))
  9026. (if (member scol seen)
  9027. (if noerror
  9028. (progn
  9029. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9030. (ding)
  9031. (sit-for 2))
  9032. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9033. (push scol seen))))))
  9034. (nreverse eq-alist)))
  9035. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9036. "Modify the equations after the table structure has been edited.
  9037. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9038. For all numbers larger than LIMIT, shift them by DELTA."
  9039. (save-excursion
  9040. (goto-char (org-table-end))
  9041. (when (looking-at "#\\+TBLFM:")
  9042. (let ((re (concat key "\\([0-9]+\\)"))
  9043. (re2
  9044. (when remove
  9045. (if (equal key "$")
  9046. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9047. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9048. s n a)
  9049. (when remove
  9050. (while (re-search-forward re2 (point-at-eol) t)
  9051. (replace-match "")))
  9052. (while (re-search-forward re (point-at-eol) t)
  9053. (setq s (match-string 1) n (string-to-number s))
  9054. (cond
  9055. ((setq a (assoc s replace))
  9056. (replace-match (concat key (cdr a)) t t))
  9057. ((and limit (> n limit))
  9058. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9059. (defun org-table-get-specials ()
  9060. "Get the column names and local parameters for this table."
  9061. (save-excursion
  9062. (let ((beg (org-table-begin)) (end (org-table-end))
  9063. names name fields fields1 field cnt
  9064. c v l line col types dlines hlines)
  9065. (setq org-table-column-names nil
  9066. org-table-local-parameters nil
  9067. org-table-named-field-locations nil
  9068. org-table-current-begin-line nil
  9069. org-table-current-begin-pos nil
  9070. org-table-current-line-types nil)
  9071. (goto-char beg)
  9072. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9073. (setq names (org-split-string (match-string 1) " *| *")
  9074. cnt 1)
  9075. (while (setq name (pop names))
  9076. (setq cnt (1+ cnt))
  9077. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9078. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9079. (setq org-table-column-names (nreverse org-table-column-names))
  9080. (setq org-table-column-name-regexp
  9081. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9082. (goto-char beg)
  9083. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9084. (setq fields (org-split-string (match-string 1) " *| *"))
  9085. (while (setq field (pop fields))
  9086. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9087. (push (cons (match-string 1 field) (match-string 2 field))
  9088. org-table-local-parameters))))
  9089. (goto-char beg)
  9090. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9091. (setq c (match-string 1)
  9092. fields (org-split-string (match-string 2) " *| *"))
  9093. (save-excursion
  9094. (beginning-of-line (if (equal c "_") 2 0))
  9095. (setq line (org-current-line) col 1)
  9096. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9097. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9098. (while (and fields1 (setq field (pop fields)))
  9099. (setq v (pop fields1) col (1+ col))
  9100. (when (and (stringp field) (stringp v)
  9101. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9102. (push (cons field v) org-table-local-parameters)
  9103. (push (list field line col) org-table-named-field-locations))))
  9104. ;; Analyse the line types
  9105. (goto-char beg)
  9106. (setq org-table-current-begin-line (org-current-line)
  9107. org-table-current-begin-pos (point)
  9108. l org-table-current-begin-line)
  9109. (while (looking-at "[ \t]*|\\(-\\)?")
  9110. (push (if (match-end 1) 'hline 'dline) types)
  9111. (if (match-end 1) (push l hlines) (push l dlines))
  9112. (beginning-of-line 2)
  9113. (setq l (1+ l)))
  9114. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9115. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9116. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9117. (defun org-table-maybe-eval-formula ()
  9118. "Check if the current field starts with \"=\" or \":=\".
  9119. If yes, store the formula and apply it."
  9120. ;; We already know we are in a table. Get field will only return a formula
  9121. ;; when appropriate. It might return a separator line, but no problem.
  9122. (when org-table-formula-evaluate-inline
  9123. (let* ((field (org-trim (or (org-table-get-field) "")))
  9124. named eq)
  9125. (when (string-match "^:?=\\(.*\\)" field)
  9126. (setq named (equal (string-to-char field) ?:)
  9127. eq (match-string 1 field))
  9128. (if (or (fboundp 'calc-eval)
  9129. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9130. (org-table-eval-formula (if named '(4) nil)
  9131. (org-table-formula-from-user eq))
  9132. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9133. (defvar org-recalc-commands nil
  9134. "List of commands triggering the recalculation of a line.
  9135. Will be filled automatically during use.")
  9136. (defvar org-recalc-marks
  9137. '((" " . "Unmarked: no special line, no automatic recalculation")
  9138. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9139. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9140. ("!" . "Column name definition line. Reference in formula as $name.")
  9141. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9142. ("_" . "Names for values in row below this one.")
  9143. ("^" . "Names for values in row above this one.")))
  9144. (defun org-table-rotate-recalc-marks (&optional newchar)
  9145. "Rotate the recalculation mark in the first column.
  9146. If in any row, the first field is not consistent with a mark,
  9147. insert a new column for the markers.
  9148. When there is an active region, change all the lines in the region,
  9149. after prompting for the marking character.
  9150. After each change, a message will be displayed indicating the meaning
  9151. of the new mark."
  9152. (interactive)
  9153. (unless (org-at-table-p) (error "Not at a table"))
  9154. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9155. (beg (org-table-begin))
  9156. (end (org-table-end))
  9157. (l (org-current-line))
  9158. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9159. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9160. (have-col
  9161. (save-excursion
  9162. (goto-char beg)
  9163. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9164. (col (org-table-current-column))
  9165. (forcenew (car (assoc newchar org-recalc-marks)))
  9166. epos new)
  9167. (when l1
  9168. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9169. (setq newchar (char-to-string (read-char-exclusive))
  9170. forcenew (car (assoc newchar org-recalc-marks))))
  9171. (if (and newchar (not forcenew))
  9172. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9173. newchar))
  9174. (if l1 (goto-line l1))
  9175. (save-excursion
  9176. (beginning-of-line 1)
  9177. (unless (looking-at org-table-dataline-regexp)
  9178. (error "Not at a table data line")))
  9179. (unless have-col
  9180. (org-table-goto-column 1)
  9181. (org-table-insert-column)
  9182. (org-table-goto-column (1+ col)))
  9183. (setq epos (point-at-eol))
  9184. (save-excursion
  9185. (beginning-of-line 1)
  9186. (org-table-get-field
  9187. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9188. (concat " "
  9189. (setq new (or forcenew
  9190. (cadr (member (match-string 1) marks))))
  9191. " ")
  9192. " # ")))
  9193. (if (and l1 l2)
  9194. (progn
  9195. (goto-line l1)
  9196. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9197. (and (looking-at org-table-dataline-regexp)
  9198. (org-table-get-field 1 (concat " " new " "))))
  9199. (goto-line l1)))
  9200. (if (not (= epos (point-at-eol))) (org-table-align))
  9201. (goto-line l)
  9202. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9203. (defun org-table-maybe-recalculate-line ()
  9204. "Recompute the current line if marked for it, and if we haven't just done it."
  9205. (interactive)
  9206. (and org-table-allow-automatic-line-recalculation
  9207. (not (and (memq last-command org-recalc-commands)
  9208. (equal org-last-recalc-line (org-current-line))))
  9209. (save-excursion (beginning-of-line 1)
  9210. (looking-at org-table-auto-recalculate-regexp))
  9211. (org-table-recalculate) t))
  9212. (defvar org-table-formula-debug nil
  9213. "Non-nil means, debug table formulas.
  9214. When nil, simply write \"#ERROR\" in corrupted fields.")
  9215. (make-variable-buffer-local 'org-table-formula-debug)
  9216. (defvar modes)
  9217. (defsubst org-set-calc-mode (var &optional value)
  9218. (if (stringp var)
  9219. (setq var (assoc var '(("D" calc-angle-mode deg)
  9220. ("R" calc-angle-mode rad)
  9221. ("F" calc-prefer-frac t)
  9222. ("S" calc-symbolic-mode t)))
  9223. value (nth 2 var) var (nth 1 var)))
  9224. (if (memq var modes)
  9225. (setcar (cdr (memq var modes)) value)
  9226. (cons var (cons value modes)))
  9227. modes)
  9228. (defun org-table-eval-formula (&optional arg equation
  9229. suppress-align suppress-const
  9230. suppress-store suppress-analysis)
  9231. "Replace the table field value at the cursor by the result of a calculation.
  9232. This function makes use of Dave Gillespie's Calc package, in my view the
  9233. most exciting program ever written for GNU Emacs. So you need to have Calc
  9234. installed in order to use this function.
  9235. In a table, this command replaces the value in the current field with the
  9236. result of a formula. It also installs the formula as the \"current\" column
  9237. formula, by storing it in a special line below the table. When called
  9238. with a `C-u' prefix, the current field must ba a named field, and the
  9239. formula is installed as valid in only this specific field.
  9240. When called with two `C-u' prefixes, insert the active equation
  9241. for the field back into the current field, so that it can be
  9242. edited there. This is useful in order to use \\[org-table-show-reference]
  9243. to check the referenced fields.
  9244. When called, the command first prompts for a formula, which is read in
  9245. the minibuffer. Previously entered formulas are available through the
  9246. history list, and the last used formula is offered as a default.
  9247. These stored formulas are adapted correctly when moving, inserting, or
  9248. deleting columns with the corresponding commands.
  9249. The formula can be any algebraic expression understood by the Calc package.
  9250. For details, see the Org-mode manual.
  9251. This function can also be called from Lisp programs and offers
  9252. additional arguments: EQUATION can be the formula to apply. If this
  9253. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9254. used to speed-up recursive calls by by-passing unnecessary aligns.
  9255. SUPPRESS-CONST suppresses the interpretation of constants in the
  9256. formula, assuming that this has been done already outside the function.
  9257. SUPPRESS-STORE means the formula should not be stored, either because
  9258. it is already stored, or because it is a modified equation that should
  9259. not overwrite the stored one."
  9260. (interactive "P")
  9261. (org-table-check-inside-data-field)
  9262. (or suppress-analysis (org-table-get-specials))
  9263. (if (equal arg '(16))
  9264. (let ((eq (org-table-current-field-formula)))
  9265. (or eq (error "No equation active for current field"))
  9266. (org-table-get-field nil eq)
  9267. (org-table-align)
  9268. (setq org-table-may-need-update t))
  9269. (let* (fields
  9270. (ndown (if (integerp arg) arg 1))
  9271. (org-table-automatic-realign nil)
  9272. (case-fold-search nil)
  9273. (down (> ndown 1))
  9274. (formula (if (and equation suppress-store)
  9275. equation
  9276. (org-table-get-formula equation (equal arg '(4)))))
  9277. (n0 (org-table-current-column))
  9278. (modes (copy-sequence org-calc-default-modes))
  9279. (numbers nil) ; was a variable, now fixed default
  9280. (keep-empty nil)
  9281. n form form0 bw fmt x ev orig c lispp literal)
  9282. ;; Parse the format string. Since we have a lot of modes, this is
  9283. ;; a lot of work. However, I think calc still uses most of the time.
  9284. (if (string-match ";" formula)
  9285. (let ((tmp (org-split-string formula ";")))
  9286. (setq formula (car tmp)
  9287. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9288. (nth 1 tmp)))
  9289. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9290. (setq c (string-to-char (match-string 1 fmt))
  9291. n (string-to-number (match-string 2 fmt)))
  9292. (if (= c ?p)
  9293. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9294. (setq modes (org-set-calc-mode
  9295. 'calc-float-format
  9296. (list (cdr (assoc c '((?n . float) (?f . fix)
  9297. (?s . sci) (?e . eng))))
  9298. n))))
  9299. (setq fmt (replace-match "" t t fmt)))
  9300. (if (string-match "[NT]" fmt)
  9301. (setq numbers (equal (match-string 0 fmt) "N")
  9302. fmt (replace-match "" t t fmt)))
  9303. (if (string-match "L" fmt)
  9304. (setq literal t
  9305. fmt (replace-match "" t t fmt)))
  9306. (if (string-match "E" fmt)
  9307. (setq keep-empty t
  9308. fmt (replace-match "" t t fmt)))
  9309. (while (string-match "[DRFS]" fmt)
  9310. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9311. (setq fmt (replace-match "" t t fmt)))
  9312. (unless (string-match "\\S-" fmt)
  9313. (setq fmt nil))))
  9314. (if (and (not suppress-const) org-table-formula-use-constants)
  9315. (setq formula (org-table-formula-substitute-names formula)))
  9316. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9317. (while (> ndown 0)
  9318. (setq fields (org-split-string
  9319. (org-no-properties
  9320. (buffer-substring (point-at-bol) (point-at-eol)))
  9321. " *| *"))
  9322. (if (eq numbers t)
  9323. (setq fields (mapcar
  9324. (lambda (x) (number-to-string (string-to-number x)))
  9325. fields)))
  9326. (setq ndown (1- ndown))
  9327. (setq form (copy-sequence formula)
  9328. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9329. (if (and lispp literal) (setq lispp 'literal))
  9330. ;; Check for old vertical references
  9331. (setq form (org-rewrite-old-row-references form))
  9332. ;; Insert complex ranges
  9333. (while (string-match org-table-range-regexp form)
  9334. (setq form
  9335. (replace-match
  9336. (save-match-data
  9337. (org-table-make-reference
  9338. (org-table-get-range (match-string 0 form) nil n0)
  9339. keep-empty numbers lispp))
  9340. t t form)))
  9341. ;; Insert simple ranges
  9342. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9343. (setq form
  9344. (replace-match
  9345. (save-match-data
  9346. (org-table-make-reference
  9347. (org-sublist
  9348. fields (string-to-number (match-string 1 form))
  9349. (string-to-number (match-string 2 form)))
  9350. keep-empty numbers lispp))
  9351. t t form)))
  9352. (setq form0 form)
  9353. ;; Insert the references to fields in same row
  9354. (while (string-match "\\$\\([0-9]+\\)" form)
  9355. (setq n (string-to-number (match-string 1 form))
  9356. x (nth (1- (if (= n 0) n0 n)) fields))
  9357. (unless x (error "Invalid field specifier \"%s\""
  9358. (match-string 0 form)))
  9359. (setq form (replace-match
  9360. (save-match-data
  9361. (org-table-make-reference x nil numbers lispp))
  9362. t t form)))
  9363. (if lispp
  9364. (setq ev (condition-case nil
  9365. (eval (eval (read form)))
  9366. (error "#ERROR"))
  9367. ev (if (numberp ev) (number-to-string ev) ev))
  9368. (or (fboundp 'calc-eval)
  9369. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9370. (setq ev (calc-eval (cons form modes)
  9371. (if numbers 'num))))
  9372. (when org-table-formula-debug
  9373. (with-output-to-temp-buffer "*Substitution History*"
  9374. (princ (format "Substitution history of formula
  9375. Orig: %s
  9376. $xyz-> %s
  9377. @r$c-> %s
  9378. $1-> %s\n" orig formula form0 form))
  9379. (if (listp ev)
  9380. (princ (format " %s^\nError: %s"
  9381. (make-string (car ev) ?\-) (nth 1 ev)))
  9382. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9383. ev (or fmt "NONE")
  9384. (if fmt (format fmt (string-to-number ev)) ev)))))
  9385. (setq bw (get-buffer-window "*Substitution History*"))
  9386. (shrink-window-if-larger-than-buffer bw)
  9387. (unless (and (interactive-p) (not ndown))
  9388. (unless (let (inhibit-redisplay)
  9389. (y-or-n-p "Debugging Formula. Continue to next? "))
  9390. (org-table-align)
  9391. (error "Abort"))
  9392. (delete-window bw)
  9393. (message "")))
  9394. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9395. (org-table-justify-field-maybe
  9396. (if fmt (format fmt (string-to-number ev)) ev))
  9397. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9398. (call-interactively 'org-return)
  9399. (setq ndown 0)))
  9400. (and down (org-table-maybe-recalculate-line))
  9401. (or suppress-align (and org-table-may-need-update
  9402. (org-table-align))))))
  9403. (defun org-table-put-field-property (prop value)
  9404. (save-excursion
  9405. (put-text-property (progn (skip-chars-backward "^|") (point))
  9406. (progn (skip-chars-forward "^|") (point))
  9407. prop value)))
  9408. (defun org-table-get-range (desc &optional tbeg col highlight)
  9409. "Get a calc vector from a column, accorting to descriptor DESC.
  9410. Optional arguments TBEG and COL can give the beginning of the table and
  9411. the current column, to avoid unnecessary parsing.
  9412. HIGHLIGHT means, just highlight the range."
  9413. (if (not (equal (string-to-char desc) ?@))
  9414. (setq desc (concat "@" desc)))
  9415. (save-excursion
  9416. (or tbeg (setq tbeg (org-table-begin)))
  9417. (or col (setq col (org-table-current-column)))
  9418. (let ((thisline (org-current-line))
  9419. beg end c1 c2 r1 r2 rangep tmp)
  9420. (unless (string-match org-table-range-regexp desc)
  9421. (error "Invalid table range specifier `%s'" desc))
  9422. (setq rangep (match-end 3)
  9423. r1 (and (match-end 1) (match-string 1 desc))
  9424. r2 (and (match-end 4) (match-string 4 desc))
  9425. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9426. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9427. (and c1 (setq c1 (+ (string-to-number c1)
  9428. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9429. (and c2 (setq c2 (+ (string-to-number c2)
  9430. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9431. (if (equal r1 "") (setq r1 nil))
  9432. (if (equal r2 "") (setq r2 nil))
  9433. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9434. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9435. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9436. (if (not r1) (setq r1 thisline))
  9437. (if (not r2) (setq r2 thisline))
  9438. (if (not c1) (setq c1 col))
  9439. (if (not c2) (setq c2 col))
  9440. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9441. ;; just one field
  9442. (progn
  9443. (goto-line r1)
  9444. (while (not (looking-at org-table-dataline-regexp))
  9445. (beginning-of-line 2))
  9446. (prog1 (org-trim (org-table-get-field c1))
  9447. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9448. ;; A range, return a vector
  9449. ;; First sort the numbers to get a regular ractangle
  9450. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9451. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9452. (goto-line r1)
  9453. (while (not (looking-at org-table-dataline-regexp))
  9454. (beginning-of-line 2))
  9455. (org-table-goto-column c1)
  9456. (setq beg (point))
  9457. (goto-line r2)
  9458. (while (not (looking-at org-table-dataline-regexp))
  9459. (beginning-of-line 0))
  9460. (org-table-goto-column c2)
  9461. (setq end (point))
  9462. (if highlight
  9463. (org-table-highlight-rectangle
  9464. beg (progn (skip-chars-forward "^|\n") (point))))
  9465. ;; return string representation of calc vector
  9466. (mapcar 'org-trim
  9467. (apply 'append (org-table-copy-region beg end)))))))
  9468. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9469. "Analyze descriptor DESC and retrieve the corresponding line number.
  9470. The cursor is currently in line CLINE, the table begins in line BLINE,
  9471. and TABLE is a vector with line types."
  9472. (if (string-match "^[0-9]+$" desc)
  9473. (aref org-table-dlines (string-to-number desc))
  9474. (setq cline (or cline (org-current-line))
  9475. bline (or bline org-table-current-begin-line)
  9476. table (or table org-table-current-line-types))
  9477. (if (or
  9478. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9479. ;; 1 2 3 4 5 6
  9480. (and (not (match-end 3)) (not (match-end 6)))
  9481. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9482. (error "invalid row descriptor `%s'" desc))
  9483. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9484. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9485. (odir (and (match-end 5) (match-string 5 desc)))
  9486. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9487. (i (- cline bline))
  9488. (rel (and (match-end 6)
  9489. (or (and (match-end 1) (not (match-end 3)))
  9490. (match-end 5)))))
  9491. (if (and hn (not hdir))
  9492. (progn
  9493. (setq i 0 hdir "+")
  9494. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9495. (if (and (not hn) on (not odir))
  9496. (error "should never happen");;(aref org-table-dlines on)
  9497. (if (and hn (> hn 0))
  9498. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9499. (if on
  9500. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9501. (+ bline i)))))
  9502. (defun org-find-row-type (table i type backwards relative n)
  9503. (let ((l (length table)))
  9504. (while (> n 0)
  9505. (while (and (setq i (+ i (if backwards -1 1)))
  9506. (>= i 0) (< i l)
  9507. (not (eq (aref table i) type))
  9508. (if (and relative (eq (aref table i) 'hline))
  9509. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9510. t)))
  9511. (setq n (1- n)))
  9512. (if (or (< i 0) (>= i l))
  9513. (error "Row descriptior leads outside table")
  9514. i)))
  9515. (defun org-rewrite-old-row-references (s)
  9516. (if (string-match "&[-+0-9I]" s)
  9517. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9518. s))
  9519. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9520. "Convert list ELEMENTS to something appropriate to insert into formula.
  9521. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9522. NUMBERS indicates that everything should be converted to numbers.
  9523. LISPP means to return something appropriate for a Lisp list."
  9524. (if (stringp elements) ; just a single val
  9525. (if lispp
  9526. (if (eq lispp 'literal)
  9527. elements
  9528. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9529. (if (equal elements "") (setq elements "0"))
  9530. (if numbers (number-to-string (string-to-number elements)) elements))
  9531. (unless keep-empty
  9532. (setq elements
  9533. (delq nil
  9534. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9535. elements))))
  9536. (setq elements (or elements '("0")))
  9537. (if lispp
  9538. (mapconcat
  9539. (lambda (x)
  9540. (if (eq lispp 'literal)
  9541. x
  9542. (prin1-to-string (if numbers (string-to-number x) x))))
  9543. elements " ")
  9544. (concat "[" (mapconcat
  9545. (lambda (x)
  9546. (if numbers (number-to-string (string-to-number x)) x))
  9547. elements
  9548. ",") "]"))))
  9549. (defun org-table-recalculate (&optional all noalign)
  9550. "Recalculate the current table line by applying all stored formulas.
  9551. With prefix arg ALL, do this for all lines in the table."
  9552. (interactive "P")
  9553. (or (memq this-command org-recalc-commands)
  9554. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9555. (unless (org-at-table-p) (error "Not at a table"))
  9556. (if (equal all '(16))
  9557. (org-table-iterate)
  9558. (org-table-get-specials)
  9559. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9560. (lambda (a b) (string< (car a) (car b)))))
  9561. (inhibit-redisplay (not debug-on-error))
  9562. (line-re org-table-dataline-regexp)
  9563. (thisline (org-current-line))
  9564. (thiscol (org-table-current-column))
  9565. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9566. ;; Insert constants in all formulas
  9567. (setq eqlist
  9568. (mapcar (lambda (x)
  9569. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9570. x)
  9571. eqlist))
  9572. ;; Split the equation list
  9573. (while (setq eq (pop eqlist))
  9574. (if (<= (string-to-char (car eq)) ?9)
  9575. (push eq eqlnum)
  9576. (push eq eqlname)))
  9577. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9578. (if all
  9579. (progn
  9580. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9581. (goto-char (setq beg (org-table-begin)))
  9582. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9583. ;; This is a table with marked lines, compute selected lines
  9584. (setq line-re org-table-recalculate-regexp)
  9585. ;; Move forward to the first non-header line
  9586. (if (and (re-search-forward org-table-dataline-regexp end t)
  9587. (re-search-forward org-table-hline-regexp end t)
  9588. (re-search-forward org-table-dataline-regexp end t))
  9589. (setq beg (match-beginning 0))
  9590. nil))) ;; just leave beg where it is
  9591. (setq beg (point-at-bol)
  9592. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9593. (goto-char beg)
  9594. (and all (message "Re-applying formulas to full table..."))
  9595. ;; First find the named fields, and mark them untouchanble
  9596. (remove-text-properties beg end '(org-untouchable t))
  9597. (while (setq eq (pop eqlname))
  9598. (setq name (car eq)
  9599. a (assoc name org-table-named-field-locations))
  9600. (and (not a)
  9601. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9602. (setq a (list name
  9603. (aref org-table-dlines
  9604. (string-to-number (match-string 1 name)))
  9605. (string-to-number (match-string 2 name)))))
  9606. (when (and a (or all (equal (nth 1 a) thisline)))
  9607. (message "Re-applying formula to field: %s" name)
  9608. (goto-line (nth 1 a))
  9609. (org-table-goto-column (nth 2 a))
  9610. (push (append a (list (cdr eq))) eqlname1)
  9611. (org-table-put-field-property :org-untouchable t)))
  9612. ;; Now evauluate the column formulas, but skip fields covered by
  9613. ;; field formulas
  9614. (goto-char beg)
  9615. (while (re-search-forward line-re end t)
  9616. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9617. ;; Unprotected line, recalculate
  9618. (and all (message "Re-applying formulas to full table...(line %d)"
  9619. (setq cnt (1+ cnt))))
  9620. (setq org-last-recalc-line (org-current-line))
  9621. (setq eql eqlnum)
  9622. (while (setq entry (pop eql))
  9623. (goto-line org-last-recalc-line)
  9624. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9625. (unless (get-text-property (point) :org-untouchable)
  9626. (org-table-eval-formula nil (cdr entry)
  9627. 'noalign 'nocst 'nostore 'noanalysis)))))
  9628. ;; Now evaluate the field formulas
  9629. (while (setq eq (pop eqlname1))
  9630. (message "Re-applying formula to field: %s" (car eq))
  9631. (goto-line (nth 1 eq))
  9632. (org-table-goto-column (nth 2 eq))
  9633. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9634. 'nostore 'noanalysis))
  9635. (goto-line thisline)
  9636. (org-table-goto-column thiscol)
  9637. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9638. (or noalign (and org-table-may-need-update (org-table-align))
  9639. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9640. ;; back to initial position
  9641. (message "Re-applying formulas...done")
  9642. (goto-line thisline)
  9643. (org-table-goto-column thiscol)
  9644. (or noalign (and org-table-may-need-update (org-table-align))
  9645. (and all (message "Re-applying formulas...done"))))))
  9646. (defun org-table-iterate (&optional arg)
  9647. "Recalculate the table until it does not change anymore."
  9648. (interactive "P")
  9649. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9650. (i 0)
  9651. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9652. thistbl)
  9653. (catch 'exit
  9654. (while (< i imax)
  9655. (setq i (1+ i))
  9656. (org-table-recalculate 'all)
  9657. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9658. (if (not (string= lasttbl thistbl))
  9659. (setq lasttbl thistbl)
  9660. (if (> i 1)
  9661. (message "Convergence after %d iterations" i)
  9662. (message "Table was already stable"))
  9663. (throw 'exit t)))
  9664. (error "No convergence after %d iterations" i))))
  9665. (defun org-table-formula-substitute-names (f)
  9666. "Replace $const with values in string F."
  9667. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9668. ;; First, check for column names
  9669. (while (setq start (string-match org-table-column-name-regexp f start))
  9670. (setq start (1+ start))
  9671. (setq a (assoc (match-string 1 f) org-table-column-names))
  9672. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9673. ;; Parameters and constants
  9674. (setq start 0)
  9675. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9676. (setq start (1+ start))
  9677. (if (setq a (save-match-data
  9678. (org-table-get-constant (match-string 1 f))))
  9679. (setq f (replace-match
  9680. (concat (if pp "(") a (if pp ")")) t t f))))
  9681. (if org-table-formula-debug
  9682. (put-text-property 0 (length f) :orig-formula f1 f))
  9683. f))
  9684. (defun org-table-get-constant (const)
  9685. "Find the value for a parameter or constant in a formula.
  9686. Parameters get priority."
  9687. (or (cdr (assoc const org-table-local-parameters))
  9688. (cdr (assoc const org-table-formula-constants-local))
  9689. (cdr (assoc const org-table-formula-constants))
  9690. (and (fboundp 'constants-get) (constants-get const))
  9691. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9692. (org-entry-get nil (substring const 5) 'inherit))
  9693. "#UNDEFINED_NAME"))
  9694. (defvar org-table-fedit-map
  9695. (let ((map (make-sparse-keymap)))
  9696. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9697. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9698. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9699. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9700. (org-defkey map "\C-c?" 'org-table-show-reference)
  9701. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9702. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9703. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9704. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9705. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9706. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9707. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9708. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9709. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9710. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9711. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9712. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9713. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9714. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9715. map))
  9716. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9717. '("Edit-Formulas"
  9718. ["Finish and Install" org-table-fedit-finish t]
  9719. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9720. ["Abort" org-table-fedit-abort t]
  9721. "--"
  9722. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9723. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9724. "--"
  9725. "Shift Reference at Point"
  9726. ["Up" org-table-fedit-ref-up t]
  9727. ["Down" org-table-fedit-ref-down t]
  9728. ["Left" org-table-fedit-ref-left t]
  9729. ["Right" org-table-fedit-ref-right t]
  9730. "-"
  9731. "Change Test Row for Column Formulas"
  9732. ["Up" org-table-fedit-line-up t]
  9733. ["Down" org-table-fedit-line-down t]
  9734. "--"
  9735. ["Scroll Table Window" org-table-fedit-scroll t]
  9736. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9737. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9738. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9739. org-table-overlay-coordinates)]
  9740. "--"
  9741. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9742. :style toggle :selected org-table-buffer-is-an]))
  9743. (defvar org-pos)
  9744. (defun org-table-edit-formulas ()
  9745. "Edit the formulas of the current table in a separate buffer."
  9746. (interactive)
  9747. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9748. (beginning-of-line 0))
  9749. (unless (org-at-table-p) (error "Not at a table"))
  9750. (org-table-get-specials)
  9751. (let ((key (org-table-current-field-formula 'key 'noerror))
  9752. (eql (sort (org-table-get-stored-formulas 'noerror)
  9753. 'org-table-formula-less-p))
  9754. (pos (move-marker (make-marker) (point)))
  9755. (startline 1)
  9756. (wc (current-window-configuration))
  9757. (titles '((column . "# Column Formulas\n")
  9758. (field . "# Field Formulas\n")
  9759. (named . "# Named Field Formulas\n")))
  9760. entry s type title)
  9761. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9762. (erase-buffer)
  9763. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9764. (let ((font-lock-global-modes '(not fundamental-mode)))
  9765. (fundamental-mode))
  9766. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9767. (org-set-local 'org-pos pos)
  9768. (org-set-local 'org-window-configuration wc)
  9769. (use-local-map org-table-fedit-map)
  9770. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9771. (easy-menu-add org-table-fedit-menu)
  9772. (setq startline (org-current-line))
  9773. (while (setq entry (pop eql))
  9774. (setq type (cond
  9775. ((equal (string-to-char (car entry)) ?@) 'field)
  9776. ((string-match "^[0-9]" (car entry)) 'column)
  9777. (t 'named)))
  9778. (when (setq title (assq type titles))
  9779. (or (bobp) (insert "\n"))
  9780. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9781. (setq titles (delq title titles)))
  9782. (if (equal key (car entry)) (setq startline (org-current-line)))
  9783. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9784. (car entry) " = " (cdr entry) "\n"))
  9785. (remove-text-properties 0 (length s) '(face nil) s)
  9786. (insert s))
  9787. (if (eq org-table-use-standard-references t)
  9788. (org-table-fedit-toggle-ref-type))
  9789. (goto-line startline)
  9790. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9791. (defun org-table-fedit-post-command ()
  9792. (when (not (memq this-command '(lisp-complete-symbol)))
  9793. (let ((win (selected-window)))
  9794. (save-excursion
  9795. (condition-case nil
  9796. (org-table-show-reference)
  9797. (error nil))
  9798. (select-window win)))))
  9799. (defun org-table-formula-to-user (s)
  9800. "Convert a formula from internal to user representation."
  9801. (if (eq org-table-use-standard-references t)
  9802. (org-table-convert-refs-to-an s)
  9803. s))
  9804. (defun org-table-formula-from-user (s)
  9805. "Convert a formula from user to internal representation."
  9806. (if org-table-use-standard-references
  9807. (org-table-convert-refs-to-rc s)
  9808. s))
  9809. (defun org-table-convert-refs-to-rc (s)
  9810. "Convert spreadsheet references from AB7 to @7$28.
  9811. Works for single references, but also for entire formulas and even the
  9812. full TBLFM line."
  9813. (let ((start 0))
  9814. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9815. (cond
  9816. ((match-end 3)
  9817. ;; format match, just advance
  9818. (setq start (match-end 0)))
  9819. ((and (> (match-beginning 0) 0)
  9820. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9821. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9822. ;; 3.e5 or something like this.
  9823. (setq start (match-end 0)))
  9824. (t
  9825. (setq start (match-beginning 0)
  9826. s (replace-match
  9827. (if (equal (match-string 2 s) "&")
  9828. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9829. (format "@%d$%d"
  9830. (string-to-number (match-string 2 s))
  9831. (org-letters-to-number (match-string 1 s))))
  9832. t t s)))))
  9833. s))
  9834. (defun org-table-convert-refs-to-an (s)
  9835. "Convert spreadsheet references from to @7$28 to AB7.
  9836. Works for single references, but also for entire formulas and even the
  9837. full TBLFM line."
  9838. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9839. (setq s (replace-match
  9840. (format "%s%d"
  9841. (org-number-to-letters
  9842. (string-to-number (match-string 2 s)))
  9843. (string-to-number (match-string 1 s)))
  9844. t t s)))
  9845. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9846. (setq s (replace-match (concat "\\1"
  9847. (org-number-to-letters
  9848. (string-to-number (match-string 2 s))) "&")
  9849. t nil s)))
  9850. s)
  9851. (defun org-letters-to-number (s)
  9852. "Convert a base 26 number represented by letters into an integer.
  9853. For example: AB -> 28."
  9854. (let ((n 0))
  9855. (setq s (upcase s))
  9856. (while (> (length s) 0)
  9857. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9858. s (substring s 1)))
  9859. n))
  9860. (defun org-number-to-letters (n)
  9861. "Convert an integer into a base 26 number represented by letters.
  9862. For example: 28 -> AB."
  9863. (let ((s ""))
  9864. (while (> n 0)
  9865. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9866. n (/ (1- n) 26)))
  9867. s))
  9868. (defun org-table-fedit-convert-buffer (function)
  9869. "Convert all references in this buffer, using FUNTION."
  9870. (let ((line (org-current-line)))
  9871. (goto-char (point-min))
  9872. (while (not (eobp))
  9873. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9874. (delete-region (point) (point-at-eol))
  9875. (or (eobp) (forward-char 1)))
  9876. (goto-line line)))
  9877. (defun org-table-fedit-toggle-ref-type ()
  9878. "Convert all references in the buffer from B3 to @3$2 and back."
  9879. (interactive)
  9880. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9881. (org-table-fedit-convert-buffer
  9882. (if org-table-buffer-is-an
  9883. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9884. (message "Reference type switched to %s"
  9885. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9886. (defun org-table-fedit-ref-up ()
  9887. "Shift the reference at point one row/hline up."
  9888. (interactive)
  9889. (org-table-fedit-shift-reference 'up))
  9890. (defun org-table-fedit-ref-down ()
  9891. "Shift the reference at point one row/hline down."
  9892. (interactive)
  9893. (org-table-fedit-shift-reference 'down))
  9894. (defun org-table-fedit-ref-left ()
  9895. "Shift the reference at point one field to the left."
  9896. (interactive)
  9897. (org-table-fedit-shift-reference 'left))
  9898. (defun org-table-fedit-ref-right ()
  9899. "Shift the reference at point one field to the right."
  9900. (interactive)
  9901. (org-table-fedit-shift-reference 'right))
  9902. (defun org-table-fedit-shift-reference (dir)
  9903. (cond
  9904. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9905. (if (memq dir '(left right))
  9906. (org-rematch-and-replace 1 (eq dir 'left))
  9907. (error "Cannot shift reference in this direction")))
  9908. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9909. ;; A B3-like reference
  9910. (if (memq dir '(up down))
  9911. (org-rematch-and-replace 2 (eq dir 'up))
  9912. (org-rematch-and-replace 1 (eq dir 'left))))
  9913. ((org-at-regexp-p
  9914. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9915. ;; An internal reference
  9916. (if (memq dir '(up down))
  9917. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9918. (org-rematch-and-replace 5 (eq dir 'left))))))
  9919. (defun org-rematch-and-replace (n &optional decr hline)
  9920. "Re-match the group N, and replace it with the shifted refrence."
  9921. (or (match-end n) (error "Cannot shift reference in this direction"))
  9922. (goto-char (match-beginning n))
  9923. (and (looking-at (regexp-quote (match-string n)))
  9924. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9925. t t)))
  9926. (defun org-shift-refpart (ref &optional decr hline)
  9927. "Shift a refrence part REF.
  9928. If DECR is set, decrease the references row/column, else increase.
  9929. If HLINE is set, this may be a hline reference, it certainly is not
  9930. a translation reference."
  9931. (save-match-data
  9932. (let* ((sign (string-match "^[-+]" ref)) n)
  9933. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9934. (cond
  9935. ((and hline (string-match "^I+" ref))
  9936. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9937. (setq n (+ n (if decr -1 1)))
  9938. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9939. (if sign
  9940. (setq sign (if (< n 0) "-" "+") n (abs n))
  9941. (setq n (max 1 n)))
  9942. (concat sign (make-string n ?I)))
  9943. ((string-match "^[0-9]+" ref)
  9944. (setq n (string-to-number (concat sign ref)))
  9945. (setq n (+ n (if decr -1 1)))
  9946. (if sign
  9947. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9948. (number-to-string (max 1 n))))
  9949. ((string-match "^[a-zA-Z]+" ref)
  9950. (org-number-to-letters
  9951. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9952. (t (error "Cannot shift reference"))))))
  9953. (defun org-table-fedit-toggle-coordinates ()
  9954. "Toggle the display of coordinates in the refrenced table."
  9955. (interactive)
  9956. (let ((pos (marker-position org-pos)))
  9957. (with-current-buffer (marker-buffer org-pos)
  9958. (save-excursion
  9959. (goto-char pos)
  9960. (org-table-toggle-coordinate-overlays)))))
  9961. (defun org-table-fedit-finish (&optional arg)
  9962. "Parse the buffer for formula definitions and install them.
  9963. With prefix ARG, apply the new formulas to the table."
  9964. (interactive "P")
  9965. (org-table-remove-rectangle-highlight)
  9966. (if org-table-use-standard-references
  9967. (progn
  9968. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9969. (setq org-table-buffer-is-an nil)))
  9970. (let ((pos org-pos) eql var form)
  9971. (goto-char (point-min))
  9972. (while (re-search-forward
  9973. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9974. nil t)
  9975. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9976. form (match-string 3))
  9977. (setq form (org-trim form))
  9978. (when (not (equal form ""))
  9979. (while (string-match "[ \t]*\n[ \t]*" form)
  9980. (setq form (replace-match " " t t form)))
  9981. (when (assoc var eql)
  9982. (error "Double formulas for %s" var))
  9983. (push (cons var form) eql)))
  9984. (setq org-pos nil)
  9985. (set-window-configuration org-window-configuration)
  9986. (select-window (get-buffer-window (marker-buffer pos)))
  9987. (goto-char pos)
  9988. (unless (org-at-table-p)
  9989. (error "Lost table position - cannot install formulae"))
  9990. (org-table-store-formulas eql)
  9991. (move-marker pos nil)
  9992. (kill-buffer "*Edit Formulas*")
  9993. (if arg
  9994. (org-table-recalculate 'all)
  9995. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9996. (defun org-table-fedit-abort ()
  9997. "Abort editing formulas, without installing the changes."
  9998. (interactive)
  9999. (org-table-remove-rectangle-highlight)
  10000. (let ((pos org-pos))
  10001. (set-window-configuration org-window-configuration)
  10002. (select-window (get-buffer-window (marker-buffer pos)))
  10003. (goto-char pos)
  10004. (move-marker pos nil)
  10005. (message "Formula editing aborted without installing changes")))
  10006. (defun org-table-fedit-lisp-indent ()
  10007. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  10008. (interactive)
  10009. (let ((pos (point)) beg end ind)
  10010. (beginning-of-line 1)
  10011. (cond
  10012. ((looking-at "[ \t]")
  10013. (goto-char pos)
  10014. (call-interactively 'lisp-indent-line))
  10015. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10016. ((not (fboundp 'pp-buffer))
  10017. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10018. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10019. (goto-char (- (match-end 0) 2))
  10020. (setq beg (point))
  10021. (setq ind (make-string (current-column) ?\ ))
  10022. (condition-case nil (forward-sexp 1)
  10023. (error
  10024. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10025. (setq end (point))
  10026. (save-restriction
  10027. (narrow-to-region beg end)
  10028. (if (eq last-command this-command)
  10029. (progn
  10030. (goto-char (point-min))
  10031. (setq this-command nil)
  10032. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10033. (replace-match " ")))
  10034. (pp-buffer)
  10035. (untabify (point-min) (point-max))
  10036. (goto-char (1+ (point-min)))
  10037. (while (re-search-forward "^." nil t)
  10038. (beginning-of-line 1)
  10039. (insert ind))
  10040. (goto-char (point-max))
  10041. (backward-delete-char 1)))
  10042. (goto-char beg))
  10043. (t nil))))
  10044. (defvar org-show-positions nil)
  10045. (defun org-table-show-reference (&optional local)
  10046. "Show the location/value of the $ expression at point."
  10047. (interactive)
  10048. (org-table-remove-rectangle-highlight)
  10049. (catch 'exit
  10050. (let ((pos (if local (point) org-pos))
  10051. (face2 'highlight)
  10052. (org-inhibit-highlight-removal t)
  10053. (win (selected-window))
  10054. (org-show-positions nil)
  10055. var name e what match dest)
  10056. (if local (org-table-get-specials))
  10057. (setq what (cond
  10058. ((or (org-at-regexp-p org-table-range-regexp2)
  10059. (org-at-regexp-p org-table-translate-regexp)
  10060. (org-at-regexp-p org-table-range-regexp))
  10061. (setq match
  10062. (save-match-data
  10063. (org-table-convert-refs-to-rc (match-string 0))))
  10064. 'range)
  10065. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10066. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10067. ((not local) nil)
  10068. (t (error "No reference at point")))
  10069. match (and what (or match (match-string 0))))
  10070. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10071. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10072. 'secondary-selection))
  10073. (org-add-hook 'before-change-functions
  10074. 'org-table-remove-rectangle-highlight)
  10075. (if (eq what 'name) (setq var (substring match 1)))
  10076. (when (eq what 'range)
  10077. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10078. (setq match (org-table-formula-substitute-names match)))
  10079. (unless local
  10080. (save-excursion
  10081. (end-of-line 1)
  10082. (re-search-backward "^\\S-" nil t)
  10083. (beginning-of-line 1)
  10084. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10085. (setq dest
  10086. (save-match-data
  10087. (org-table-convert-refs-to-rc (match-string 1))))
  10088. (org-table-add-rectangle-overlay
  10089. (match-beginning 1) (match-end 1) face2))))
  10090. (if (and (markerp pos) (marker-buffer pos))
  10091. (if (get-buffer-window (marker-buffer pos))
  10092. (select-window (get-buffer-window (marker-buffer pos)))
  10093. (org-switch-to-buffer-other-window (get-buffer-window
  10094. (marker-buffer pos)))))
  10095. (goto-char pos)
  10096. (org-table-force-dataline)
  10097. (when dest
  10098. (setq name (substring dest 1))
  10099. (cond
  10100. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10101. (setq e (assoc name org-table-named-field-locations))
  10102. (goto-line (nth 1 e))
  10103. (org-table-goto-column (nth 2 e)))
  10104. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10105. (let ((l (string-to-number (match-string 1 dest)))
  10106. (c (string-to-number (match-string 2 dest))))
  10107. (goto-line (aref org-table-dlines l))
  10108. (org-table-goto-column c)))
  10109. (t (org-table-goto-column (string-to-number name))))
  10110. (move-marker pos (point))
  10111. (org-table-highlight-rectangle nil nil face2))
  10112. (cond
  10113. ((equal dest match))
  10114. ((not match))
  10115. ((eq what 'range)
  10116. (condition-case nil
  10117. (save-excursion
  10118. (org-table-get-range match nil nil 'highlight))
  10119. (error nil)))
  10120. ((setq e (assoc var org-table-named-field-locations))
  10121. (goto-line (nth 1 e))
  10122. (org-table-goto-column (nth 2 e))
  10123. (org-table-highlight-rectangle (point) (point))
  10124. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10125. ((setq e (assoc var org-table-column-names))
  10126. (org-table-goto-column (string-to-number (cdr e)))
  10127. (org-table-highlight-rectangle (point) (point))
  10128. (goto-char (org-table-begin))
  10129. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10130. (org-table-end) t)
  10131. (progn
  10132. (goto-char (match-beginning 1))
  10133. (org-table-highlight-rectangle)
  10134. (message "Named column (column %s)" (cdr e)))
  10135. (error "Column name not found")))
  10136. ((eq what 'column)
  10137. ;; column number
  10138. (org-table-goto-column (string-to-number (substring match 1)))
  10139. (org-table-highlight-rectangle (point) (point))
  10140. (message "Column %s" (substring match 1)))
  10141. ((setq e (assoc var org-table-local-parameters))
  10142. (goto-char (org-table-begin))
  10143. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10144. (progn
  10145. (goto-char (match-beginning 1))
  10146. (org-table-highlight-rectangle)
  10147. (message "Local parameter."))
  10148. (error "Parameter not found")))
  10149. (t
  10150. (cond
  10151. ((not var) (error "No reference at point"))
  10152. ((setq e (assoc var org-table-formula-constants-local))
  10153. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10154. var (cdr e)))
  10155. ((setq e (assoc var org-table-formula-constants))
  10156. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10157. var (cdr e)))
  10158. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10159. (message "Constant: $%s=%s, from `constants.el'%s."
  10160. var e (format " (%s units)" constants-unit-system)))
  10161. (t (error "Undefined name $%s" var)))))
  10162. (goto-char pos)
  10163. (when (and org-show-positions
  10164. (not (memq this-command '(org-table-fedit-scroll
  10165. org-table-fedit-scroll-down))))
  10166. (push pos org-show-positions)
  10167. (push org-table-current-begin-pos org-show-positions)
  10168. (let ((min (apply 'min org-show-positions))
  10169. (max (apply 'max org-show-positions)))
  10170. (goto-char min) (recenter 0)
  10171. (goto-char max)
  10172. (or (pos-visible-in-window-p max) (recenter -1))))
  10173. (select-window win))))
  10174. (defun org-table-force-dataline ()
  10175. "Make sure the cursor is in a dataline in a table."
  10176. (unless (save-excursion
  10177. (beginning-of-line 1)
  10178. (looking-at org-table-dataline-regexp))
  10179. (let* ((re org-table-dataline-regexp)
  10180. (p1 (save-excursion (re-search-forward re nil 'move)))
  10181. (p2 (save-excursion (re-search-backward re nil 'move))))
  10182. (cond ((and p1 p2)
  10183. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10184. p1 p2)))
  10185. ((or p1 p2) (goto-char (or p1 p2)))
  10186. (t (error "No table dataline around here"))))))
  10187. (defun org-table-fedit-line-up ()
  10188. "Move cursor one line up in the window showing the table."
  10189. (interactive)
  10190. (org-table-fedit-move 'previous-line))
  10191. (defun org-table-fedit-line-down ()
  10192. "Move cursor one line down in the window showing the table."
  10193. (interactive)
  10194. (org-table-fedit-move 'next-line))
  10195. (defun org-table-fedit-move (command)
  10196. "Move the cursor in the window shoinw the table.
  10197. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10198. (let ((org-table-allow-automatic-line-recalculation nil)
  10199. (pos org-pos) (win (selected-window)) p)
  10200. (select-window (get-buffer-window (marker-buffer org-pos)))
  10201. (setq p (point))
  10202. (call-interactively command)
  10203. (while (and (org-at-table-p)
  10204. (org-at-table-hline-p))
  10205. (call-interactively command))
  10206. (or (org-at-table-p) (goto-char p))
  10207. (move-marker pos (point))
  10208. (select-window win)))
  10209. (defun org-table-fedit-scroll (N)
  10210. (interactive "p")
  10211. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10212. (scroll-other-window N)))
  10213. (defun org-table-fedit-scroll-down (N)
  10214. (interactive "p")
  10215. (org-table-fedit-scroll (- N)))
  10216. (defvar org-table-rectangle-overlays nil)
  10217. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10218. "Add a new overlay."
  10219. (let ((ov (org-make-overlay beg end)))
  10220. (org-overlay-put ov 'face (or face 'secondary-selection))
  10221. (push ov org-table-rectangle-overlays)))
  10222. (defun org-table-highlight-rectangle (&optional beg end face)
  10223. "Highlight rectangular region in a table."
  10224. (setq beg (or beg (point)) end (or end (point)))
  10225. (let ((b (min beg end))
  10226. (e (max beg end))
  10227. l1 c1 l2 c2 tmp)
  10228. (and (boundp 'org-show-positions)
  10229. (setq org-show-positions (cons b (cons e org-show-positions))))
  10230. (goto-char (min beg end))
  10231. (setq l1 (org-current-line)
  10232. c1 (org-table-current-column))
  10233. (goto-char (max beg end))
  10234. (setq l2 (org-current-line)
  10235. c2 (org-table-current-column))
  10236. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10237. (goto-line l1)
  10238. (beginning-of-line 1)
  10239. (loop for line from l1 to l2 do
  10240. (when (looking-at org-table-dataline-regexp)
  10241. (org-table-goto-column c1)
  10242. (skip-chars-backward "^|\n") (setq beg (point))
  10243. (org-table-goto-column c2)
  10244. (skip-chars-forward "^|\n") (setq end (point))
  10245. (org-table-add-rectangle-overlay beg end face))
  10246. (beginning-of-line 2))
  10247. (goto-char b))
  10248. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10249. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10250. "Remove the rectangle overlays."
  10251. (unless org-inhibit-highlight-removal
  10252. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10253. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10254. (setq org-table-rectangle-overlays nil)))
  10255. (defvar org-table-coordinate-overlays nil
  10256. "Collects the cooordinate grid overlays, so that they can be removed.")
  10257. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10258. (defun org-table-overlay-coordinates ()
  10259. "Add overlays to the table at point, to show row/column coordinates."
  10260. (interactive)
  10261. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10262. (setq org-table-coordinate-overlays nil)
  10263. (save-excursion
  10264. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10265. (goto-char (org-table-begin))
  10266. (while (org-at-table-p)
  10267. (setq eol (point-at-eol))
  10268. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10269. (push ov org-table-coordinate-overlays)
  10270. (setq hline (looking-at org-table-hline-regexp))
  10271. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10272. (format "%4d" (setq id (1+ id)))))
  10273. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10274. (when hline
  10275. (setq ic 0)
  10276. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10277. (setq beg (1+ (match-beginning 0))
  10278. ic (1+ ic)
  10279. s1 (concat "$" (int-to-string ic))
  10280. s2 (org-number-to-letters ic)
  10281. str (if (eq org-table-use-standard-references t) s2 s1))
  10282. (setq ov (org-make-overlay beg (+ beg (length str))))
  10283. (push ov org-table-coordinate-overlays)
  10284. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10285. (beginning-of-line 2)))))
  10286. (defun org-table-toggle-coordinate-overlays ()
  10287. "Toggle the display of Row/Column numbers in tables."
  10288. (interactive)
  10289. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10290. (message "Row/Column number display turned %s"
  10291. (if org-table-overlay-coordinates "on" "off"))
  10292. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10293. (org-table-align))
  10294. (unless org-table-overlay-coordinates
  10295. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10296. (setq org-table-coordinate-overlays nil)))
  10297. (defun org-table-toggle-formula-debugger ()
  10298. "Toggle the formula debugger in tables."
  10299. (interactive)
  10300. (setq org-table-formula-debug (not org-table-formula-debug))
  10301. (message "Formula debugging has been turned %s"
  10302. (if org-table-formula-debug "on" "off")))
  10303. ;;; The orgtbl minor mode
  10304. ;; Define a minor mode which can be used in other modes in order to
  10305. ;; integrate the org-mode table editor.
  10306. ;; This is really a hack, because the org-mode table editor uses several
  10307. ;; keys which normally belong to the major mode, for example the TAB and
  10308. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10309. ;; necessary to operate the table editor, but wraps the commands into a
  10310. ;; function which tests if the cursor is currently inside a table. If that
  10311. ;; is the case, the table editor command is executed. However, when any of
  10312. ;; those keys is used outside a table, the function uses `key-binding' to
  10313. ;; look up if the key has an associated command in another currently active
  10314. ;; keymap (minor modes, major mode, global), and executes that command.
  10315. ;; There might be problems if any of the keys used by the table editor is
  10316. ;; otherwise used as a prefix key.
  10317. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10318. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10319. ;; addresses this by checking explicitly for both bindings.
  10320. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10321. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10322. ;; Some modes bind other commands to simple characters, for example
  10323. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10324. ;; active, this binding is ignored inside tables and replaced with a
  10325. ;; modified self-insert.
  10326. (defvar orgtbl-mode nil
  10327. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10328. table editor in arbitrary modes.")
  10329. (make-variable-buffer-local 'orgtbl-mode)
  10330. (defvar orgtbl-mode-map (make-keymap)
  10331. "Keymap for `orgtbl-mode'.")
  10332. ;;;###autoload
  10333. (defun turn-on-orgtbl ()
  10334. "Unconditionally turn on `orgtbl-mode'."
  10335. (orgtbl-mode 1))
  10336. (defvar org-old-auto-fill-inhibit-regexp nil
  10337. "Local variable used by `orgtbl-mode'")
  10338. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10339. "Matches a line belonging to an orgtbl.")
  10340. (defconst orgtbl-extra-font-lock-keywords
  10341. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10342. 0 (quote 'org-table) 'prepend))
  10343. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10344. ;;;###autoload
  10345. (defun orgtbl-mode (&optional arg)
  10346. "The `org-mode' table editor as a minor mode for use in other modes."
  10347. (interactive)
  10348. (if (org-mode-p)
  10349. ;; Exit without error, in case some hook functions calls this
  10350. ;; by accident in org-mode.
  10351. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10352. (setq orgtbl-mode
  10353. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10354. (if orgtbl-mode
  10355. (progn
  10356. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10357. ;; Make sure we are first in minor-mode-map-alist
  10358. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10359. (and c (setq minor-mode-map-alist
  10360. (cons c (delq c minor-mode-map-alist)))))
  10361. (org-set-local (quote org-table-may-need-update) t)
  10362. (org-add-hook 'before-change-functions 'org-before-change-function
  10363. nil 'local)
  10364. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10365. auto-fill-inhibit-regexp)
  10366. (org-set-local 'auto-fill-inhibit-regexp
  10367. (if auto-fill-inhibit-regexp
  10368. (concat orgtbl-line-start-regexp "\\|"
  10369. auto-fill-inhibit-regexp)
  10370. orgtbl-line-start-regexp))
  10371. (org-add-to-invisibility-spec '(org-cwidth))
  10372. (when (fboundp 'font-lock-add-keywords)
  10373. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10374. (org-restart-font-lock))
  10375. (easy-menu-add orgtbl-mode-menu)
  10376. (run-hooks 'orgtbl-mode-hook))
  10377. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10378. (org-cleanup-narrow-column-properties)
  10379. (org-remove-from-invisibility-spec '(org-cwidth))
  10380. (remove-hook 'before-change-functions 'org-before-change-function t)
  10381. (when (fboundp 'font-lock-remove-keywords)
  10382. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10383. (org-restart-font-lock))
  10384. (easy-menu-remove orgtbl-mode-menu)
  10385. (force-mode-line-update 'all))))
  10386. (defun org-cleanup-narrow-column-properties ()
  10387. "Remove all properties related to narrow-column invisibility."
  10388. (let ((s 1))
  10389. (while (setq s (text-property-any s (point-max)
  10390. 'display org-narrow-column-arrow))
  10391. (remove-text-properties s (1+ s) '(display t)))
  10392. (setq s 1)
  10393. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10394. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10395. (setq s 1)
  10396. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10397. (remove-text-properties s (1+ s) '(invisible t)))))
  10398. ;; Install it as a minor mode.
  10399. (put 'orgtbl-mode :included t)
  10400. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10401. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10402. (defun orgtbl-make-binding (fun n &rest keys)
  10403. "Create a function for binding in the table minor mode.
  10404. FUN is the command to call inside a table. N is used to create a unique
  10405. command name. KEYS are keys that should be checked in for a command
  10406. to execute outside of tables."
  10407. (eval
  10408. (list 'defun
  10409. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10410. '(arg)
  10411. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10412. "Outside of tables, run the binding of `"
  10413. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10414. "'.")
  10415. '(interactive "p")
  10416. (list 'if
  10417. '(org-at-table-p)
  10418. (list 'call-interactively (list 'quote fun))
  10419. (list 'let '(orgtbl-mode)
  10420. (list 'call-interactively
  10421. (append '(or)
  10422. (mapcar (lambda (k)
  10423. (list 'key-binding k))
  10424. keys)
  10425. '('orgtbl-error))))))))
  10426. (defun orgtbl-error ()
  10427. "Error when there is no default binding for a table key."
  10428. (interactive)
  10429. (error "This key has no function outside tables"))
  10430. (defun orgtbl-setup ()
  10431. "Setup orgtbl keymaps."
  10432. (let ((nfunc 0)
  10433. (bindings
  10434. (list
  10435. '([(meta shift left)] org-table-delete-column)
  10436. '([(meta left)] org-table-move-column-left)
  10437. '([(meta right)] org-table-move-column-right)
  10438. '([(meta shift right)] org-table-insert-column)
  10439. '([(meta shift up)] org-table-kill-row)
  10440. '([(meta shift down)] org-table-insert-row)
  10441. '([(meta up)] org-table-move-row-up)
  10442. '([(meta down)] org-table-move-row-down)
  10443. '("\C-c\C-w" org-table-cut-region)
  10444. '("\C-c\M-w" org-table-copy-region)
  10445. '("\C-c\C-y" org-table-paste-rectangle)
  10446. '("\C-c-" org-table-insert-hline)
  10447. '("\C-c}" org-table-toggle-coordinate-overlays)
  10448. '("\C-c{" org-table-toggle-formula-debugger)
  10449. '("\C-m" org-table-next-row)
  10450. '([(shift return)] org-table-copy-down)
  10451. '("\C-c\C-q" org-table-wrap-region)
  10452. '("\C-c?" org-table-field-info)
  10453. '("\C-c " org-table-blank-field)
  10454. '("\C-c+" org-table-sum)
  10455. '("\C-c=" org-table-eval-formula)
  10456. '("\C-c'" org-table-edit-formulas)
  10457. '("\C-c`" org-table-edit-field)
  10458. '("\C-c*" org-table-recalculate)
  10459. '("\C-c|" org-table-create-or-convert-from-region)
  10460. '("\C-c^" org-table-sort-lines)
  10461. '([(control ?#)] org-table-rotate-recalc-marks)))
  10462. elt key fun cmd)
  10463. (while (setq elt (pop bindings))
  10464. (setq nfunc (1+ nfunc))
  10465. (setq key (org-key (car elt))
  10466. fun (nth 1 elt)
  10467. cmd (orgtbl-make-binding fun nfunc key))
  10468. (org-defkey orgtbl-mode-map key cmd))
  10469. ;; Special treatment needed for TAB and RET
  10470. (org-defkey orgtbl-mode-map [(return)]
  10471. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10472. (org-defkey orgtbl-mode-map "\C-m"
  10473. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10474. (org-defkey orgtbl-mode-map [(tab)]
  10475. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10476. (org-defkey orgtbl-mode-map "\C-i"
  10477. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10478. (org-defkey orgtbl-mode-map [(shift tab)]
  10479. (orgtbl-make-binding 'org-table-previous-field 104
  10480. [(shift tab)] [(tab)] "\C-i"))
  10481. (org-defkey orgtbl-mode-map "\M-\C-m"
  10482. (orgtbl-make-binding 'org-table-wrap-region 105
  10483. "\M-\C-m" [(meta return)]))
  10484. (org-defkey orgtbl-mode-map [(meta return)]
  10485. (orgtbl-make-binding 'org-table-wrap-region 106
  10486. [(meta return)] "\M-\C-m"))
  10487. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10488. (when orgtbl-optimized
  10489. ;; If the user wants maximum table support, we need to hijack
  10490. ;; some standard editing functions
  10491. (org-remap orgtbl-mode-map
  10492. 'self-insert-command 'orgtbl-self-insert-command
  10493. 'delete-char 'org-delete-char
  10494. 'delete-backward-char 'org-delete-backward-char)
  10495. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10496. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10497. '("OrgTbl"
  10498. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10499. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10500. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10501. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10502. "--"
  10503. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10504. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10505. ["Copy Field from Above"
  10506. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10507. "--"
  10508. ("Column"
  10509. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10510. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10511. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10512. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10513. ("Row"
  10514. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10515. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10516. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10517. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10518. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10519. "--"
  10520. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10521. ("Rectangle"
  10522. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10523. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10524. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10525. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10526. "--"
  10527. ("Radio tables"
  10528. ["Insert table template" orgtbl-insert-radio-table
  10529. (assq major-mode orgtbl-radio-table-templates)]
  10530. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10531. "--"
  10532. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10533. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10534. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10535. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10536. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10537. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10538. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10539. ["Sum Column/Rectangle" org-table-sum
  10540. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10541. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10542. ["Debug Formulas"
  10543. org-table-toggle-formula-debugger :active (org-at-table-p)
  10544. :keys "C-c {"
  10545. :style toggle :selected org-table-formula-debug]
  10546. ["Show Col/Row Numbers"
  10547. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10548. :keys "C-c }"
  10549. :style toggle :selected org-table-overlay-coordinates]
  10550. ))
  10551. t))
  10552. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10553. "If the cursor is inside a table, realign the table.
  10554. It it is a table to be sent away to a receiver, do it.
  10555. With prefix arg, also recompute table."
  10556. (interactive "P")
  10557. (let ((pos (point)) action)
  10558. (save-excursion
  10559. (beginning-of-line 1)
  10560. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10561. ((looking-at "[ \t]*|") pos)
  10562. ((looking-at "#\\+TBLFM:") 'recalc))))
  10563. (cond
  10564. ((integerp action)
  10565. (goto-char action)
  10566. (org-table-maybe-eval-formula)
  10567. (if arg
  10568. (call-interactively 'org-table-recalculate)
  10569. (org-table-maybe-recalculate-line))
  10570. (call-interactively 'org-table-align)
  10571. (orgtbl-send-table 'maybe))
  10572. ((eq action 'recalc)
  10573. (save-excursion
  10574. (beginning-of-line 1)
  10575. (skip-chars-backward " \r\n\t")
  10576. (if (org-at-table-p)
  10577. (org-call-with-arg 'org-table-recalculate t))))
  10578. (t (let (orgtbl-mode)
  10579. (call-interactively (key-binding "\C-c\C-c")))))))
  10580. (defun orgtbl-tab (arg)
  10581. "Justification and field motion for `orgtbl-mode'."
  10582. (interactive "P")
  10583. (if arg (org-table-edit-field t)
  10584. (org-table-justify-field-maybe)
  10585. (org-table-next-field)))
  10586. (defun orgtbl-ret ()
  10587. "Justification and field motion for `orgtbl-mode'."
  10588. (interactive)
  10589. (org-table-justify-field-maybe)
  10590. (org-table-next-row))
  10591. (defun orgtbl-self-insert-command (N)
  10592. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10593. If the cursor is in a table looking at whitespace, the whitespace is
  10594. overwritten, and the table is not marked as requiring realignment."
  10595. (interactive "p")
  10596. (if (and (org-at-table-p)
  10597. (or
  10598. (and org-table-auto-blank-field
  10599. (member last-command
  10600. '(orgtbl-hijacker-command-100
  10601. orgtbl-hijacker-command-101
  10602. orgtbl-hijacker-command-102
  10603. orgtbl-hijacker-command-103
  10604. orgtbl-hijacker-command-104
  10605. orgtbl-hijacker-command-105))
  10606. (org-table-blank-field))
  10607. t)
  10608. (eq N 1)
  10609. (looking-at "[^|\n]* +|"))
  10610. (let (org-table-may-need-update)
  10611. (goto-char (1- (match-end 0)))
  10612. (delete-backward-char 1)
  10613. (goto-char (match-beginning 0))
  10614. (self-insert-command N))
  10615. (setq org-table-may-need-update t)
  10616. (let (orgtbl-mode)
  10617. (call-interactively (key-binding (vector last-input-event))))))
  10618. (defun org-force-self-insert (N)
  10619. "Needed to enforce self-insert under remapping."
  10620. (interactive "p")
  10621. (self-insert-command N))
  10622. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10623. "Regula expression matching exponentials as produced by calc.")
  10624. (defvar org-table-clean-did-remove-column nil)
  10625. (defun orgtbl-export (table target)
  10626. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10627. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10628. org-table-last-alignment org-table-last-column-widths
  10629. maxcol column)
  10630. (if (not (fboundp func))
  10631. (error "Cannot export orgtbl table to %s" target))
  10632. (setq lines (org-table-clean-before-export lines))
  10633. (setq table
  10634. (mapcar
  10635. (lambda (x)
  10636. (if (string-match org-table-hline-regexp x)
  10637. 'hline
  10638. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10639. lines))
  10640. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10641. table)))
  10642. (loop for i from (1- maxcol) downto 0 do
  10643. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10644. (setq column (delq nil column))
  10645. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10646. (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))
  10647. (funcall func table nil)))
  10648. (defun orgtbl-send-table (&optional maybe)
  10649. "Send a tranformed version of this table to the receiver position.
  10650. With argument MAYBE, fail quietly if no transformation is defined for
  10651. this table."
  10652. (interactive)
  10653. (catch 'exit
  10654. (unless (org-at-table-p) (error "Not at a table"))
  10655. ;; when non-interactive, we assume align has just happened.
  10656. (when (interactive-p) (org-table-align))
  10657. (save-excursion
  10658. (goto-char (org-table-begin))
  10659. (beginning-of-line 0)
  10660. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10661. (if maybe
  10662. (throw 'exit nil)
  10663. (error "Don't know how to transform this table."))))
  10664. (let* ((name (match-string 1))
  10665. beg
  10666. (transform (intern (match-string 2)))
  10667. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10668. (skip (plist-get params :skip))
  10669. (skipcols (plist-get params :skipcols))
  10670. (txt (buffer-substring-no-properties
  10671. (org-table-begin) (org-table-end)))
  10672. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10673. (lines (org-table-clean-before-export lines))
  10674. (i0 (if org-table-clean-did-remove-column 2 1))
  10675. (table (mapcar
  10676. (lambda (x)
  10677. (if (string-match org-table-hline-regexp x)
  10678. 'hline
  10679. (org-remove-by-index
  10680. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10681. skipcols i0)))
  10682. lines))
  10683. (fun (if (= i0 2) 'cdr 'identity))
  10684. (org-table-last-alignment
  10685. (org-remove-by-index (funcall fun org-table-last-alignment)
  10686. skipcols i0))
  10687. (org-table-last-column-widths
  10688. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10689. skipcols i0)))
  10690. (unless (fboundp transform)
  10691. (error "No such transformation function %s" transform))
  10692. (setq txt (funcall transform table params))
  10693. ;; Find the insertion place
  10694. (save-excursion
  10695. (goto-char (point-min))
  10696. (unless (re-search-forward
  10697. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10698. (error "Don't know where to insert translated table"))
  10699. (goto-char (match-beginning 0))
  10700. (beginning-of-line 2)
  10701. (setq beg (point))
  10702. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10703. (error "Cannot find end of insertion region"))
  10704. (beginning-of-line 1)
  10705. (delete-region beg (point))
  10706. (goto-char beg)
  10707. (insert txt "\n"))
  10708. (message "Table converted and installed at receiver location"))))
  10709. (defun org-remove-by-index (list indices &optional i0)
  10710. "Remove the elements in LIST with indices in INDICES.
  10711. First element has index 0, or I0 if given."
  10712. (if (not indices)
  10713. list
  10714. (if (integerp indices) (setq indices (list indices)))
  10715. (setq i0 (1- (or i0 0)))
  10716. (delq :rm (mapcar (lambda (x)
  10717. (setq i0 (1+ i0))
  10718. (if (memq i0 indices) :rm x))
  10719. list))))
  10720. (defun orgtbl-toggle-comment ()
  10721. "Comment or uncomment the orgtbl at point."
  10722. (interactive)
  10723. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10724. (re2 (concat "^" orgtbl-line-start-regexp))
  10725. (commented (save-excursion (beginning-of-line 1)
  10726. (cond ((looking-at re1) t)
  10727. ((looking-at re2) nil)
  10728. (t (error "Not at an org table")))))
  10729. (re (if commented re1 re2))
  10730. beg end)
  10731. (save-excursion
  10732. (beginning-of-line 1)
  10733. (while (looking-at re) (beginning-of-line 0))
  10734. (beginning-of-line 2)
  10735. (setq beg (point))
  10736. (while (looking-at re) (beginning-of-line 2))
  10737. (setq end (point)))
  10738. (comment-region beg end (if commented '(4) nil))))
  10739. (defun orgtbl-insert-radio-table ()
  10740. "Insert a radio table template appropriate for this major mode."
  10741. (interactive)
  10742. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10743. (txt (nth 1 e))
  10744. name pos)
  10745. (unless e (error "No radio table setup defined for %s" major-mode))
  10746. (setq name (read-string "Table name: "))
  10747. (while (string-match "%n" txt)
  10748. (setq txt (replace-match name t t txt)))
  10749. (or (bolp) (insert "\n"))
  10750. (setq pos (point))
  10751. (insert txt)
  10752. (goto-char pos)))
  10753. (defun org-get-param (params header i sym &optional hsym)
  10754. "Get parameter value for symbol SYM.
  10755. If this is a header line, actually get the value for the symbol with an
  10756. additional \"h\" inserted after the colon.
  10757. If the value is a protperty list, get the element for the current column.
  10758. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10759. (let ((val (plist-get params sym)))
  10760. (and hsym header (setq val (or (plist-get params hsym) val)))
  10761. (if (consp val) (plist-get val i) val)))
  10762. (defun orgtbl-to-generic (table params)
  10763. "Convert the orgtbl-mode TABLE to some other format.
  10764. This generic routine can be used for many standard cases.
  10765. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10766. separator line, or a list of fields for that line.
  10767. PARAMS is a property list of parameters that can influence the conversion.
  10768. For the generic converter, some parameters are obligatory: You need to
  10769. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10770. :splice, you must have :tstart and :tend.
  10771. Valid parameters are
  10772. :tstart String to start the table. Ignored when :splice is t.
  10773. :tend String to end the table. Ignored when :splice is t.
  10774. :splice When set to t, return only table body lines, don't wrap
  10775. them into :tstart and :tend. Default is nil.
  10776. :hline String to be inserted on horizontal separation lines.
  10777. May be nil to ignore hlines.
  10778. :lstart String to start a new table line.
  10779. :lend String to end a table line
  10780. :sep Separator between two fields
  10781. :lfmt Format for entire line, with enough %s to capture all fields.
  10782. If this is present, :lstart, :lend, and :sep are ignored.
  10783. :fmt A format to be used to wrap the field, should contain
  10784. %s for the original field value. For example, to wrap
  10785. everything in dollars, you could use :fmt \"$%s$\".
  10786. This may also be a property list with column numbers and
  10787. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10788. :hlstart :hlend :hlsep :hlfmt :hfmt
  10789. Same as above, specific for the header lines in the table.
  10790. All lines before the first hline are treated as header.
  10791. If any of these is not present, the data line value is used.
  10792. :efmt Use this format to print numbers with exponentials.
  10793. The format should have %s twice for inserting mantissa
  10794. and exponent, for example \"%s\\\\times10^{%s}\". This
  10795. may also be a property list with column numbers and
  10796. formats. :fmt will still be applied after :efmt.
  10797. In addition to this, the parameters :skip and :skipcols are always handled
  10798. directly by `orgtbl-send-table'. See manual."
  10799. (interactive)
  10800. (let* ((p params)
  10801. (splicep (plist-get p :splice))
  10802. (hline (plist-get p :hline))
  10803. rtn line i fm efm lfmt h)
  10804. ;; Do we have a header?
  10805. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10806. (setq h t))
  10807. ;; Put header
  10808. (unless splicep
  10809. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10810. ;; Now loop over all lines
  10811. (while (setq line (pop table))
  10812. (if (eq line 'hline)
  10813. ;; A horizontal separator line
  10814. (progn (if hline (push hline rtn))
  10815. (setq h nil)) ; no longer in header
  10816. ;; A normal line. Convert the fields, push line onto the result list
  10817. (setq i 0)
  10818. (setq line
  10819. (mapcar
  10820. (lambda (f)
  10821. (setq i (1+ i)
  10822. fm (org-get-param p h i :fmt :hfmt)
  10823. efm (org-get-param p h i :efmt))
  10824. (if (and efm (string-match orgtbl-exp-regexp f))
  10825. (setq f (format
  10826. efm (match-string 1 f) (match-string 2 f))))
  10827. (if fm (setq f (format fm f)))
  10828. f)
  10829. line))
  10830. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10831. (push (apply 'format lfmt line) rtn)
  10832. (push (concat
  10833. (org-get-param p h i :lstart :hlstart)
  10834. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10835. (org-get-param p h i :lend :hlend))
  10836. rtn))))
  10837. (unless splicep
  10838. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10839. (mapconcat 'identity (nreverse rtn) "\n")))
  10840. (defun orgtbl-to-latex (table params)
  10841. "Convert the orgtbl-mode TABLE to LaTeX.
  10842. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10843. separator line, or a list of fields for that line.
  10844. PARAMS is a property list of parameters that can influence the conversion.
  10845. Supports all parameters from `orgtbl-to-generic'. Most important for
  10846. LaTeX are:
  10847. :splice When set to t, return only table body lines, don't wrap
  10848. them into a tabular environment. Default is nil.
  10849. :fmt A format to be used to wrap the field, should contain %s for the
  10850. original field value. For example, to wrap everything in dollars,
  10851. use :fmt \"$%s$\". This may also be a property list with column
  10852. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10853. :efmt Format for transforming numbers with exponentials. The format
  10854. should have %s twice for inserting mantissa and exponent, for
  10855. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10856. This may also be a property list with column numbers and formats.
  10857. The general parameters :skip and :skipcols have already been applied when
  10858. this function is called."
  10859. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10860. org-table-last-alignment ""))
  10861. (params2
  10862. (list
  10863. :tstart (concat "\\begin{tabular}{" alignment "}")
  10864. :tend "\\end{tabular}"
  10865. :lstart "" :lend " \\\\" :sep " & "
  10866. :efmt "%s\\,(%s)" :hline "\\hline")))
  10867. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10868. (defun orgtbl-to-html (table params)
  10869. "Convert the orgtbl-mode TABLE to LaTeX.
  10870. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10871. separator line, or a list of fields for that line.
  10872. PARAMS is a property list of parameters that can influence the conversion.
  10873. Currently this function recognizes the following parameters:
  10874. :splice When set to t, return only table body lines, don't wrap
  10875. them into a <table> environment. Default is nil.
  10876. The general parameters :skip and :skipcols have already been applied when
  10877. this function is called. The function does *not* use `orgtbl-to-generic',
  10878. so you cannot specify parameters for it."
  10879. (let* ((splicep (plist-get params :splice))
  10880. html)
  10881. ;; Just call the formatter we already have
  10882. ;; We need to make text lines for it, so put the fields back together.
  10883. (setq html (org-format-org-table-html
  10884. (mapcar
  10885. (lambda (x)
  10886. (if (eq x 'hline)
  10887. "|----+----|"
  10888. (concat "| " (mapconcat 'identity x " | ") " |")))
  10889. table)
  10890. splicep))
  10891. (if (string-match "\n+\\'" html)
  10892. (setq html (replace-match "" t t html)))
  10893. html))
  10894. (defun orgtbl-to-texinfo (table params)
  10895. "Convert the orgtbl-mode TABLE to TeXInfo.
  10896. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10897. separator line, or a list of fields for that line.
  10898. PARAMS is a property list of parameters that can influence the conversion.
  10899. Supports all parameters from `orgtbl-to-generic'. Most important for
  10900. TeXInfo are:
  10901. :splice nil/t When set to t, return only table body lines, don't wrap
  10902. them into a multitable environment. Default is nil.
  10903. :fmt fmt A format to be used to wrap the field, should contain
  10904. %s for the original field value. For example, to wrap
  10905. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10906. This may also be a property list with column numbers and
  10907. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10908. :cf \"f1 f2..\" The column fractions for the table. By default these
  10909. are computed automatically from the width of the columns
  10910. under org-mode.
  10911. The general parameters :skip and :skipcols have already been applied when
  10912. this function is called."
  10913. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10914. (colfrac (or (plist-get params :cf)
  10915. (mapconcat
  10916. (lambda (x) (format "%.3f" (/ (float x) total)))
  10917. org-table-last-column-widths " ")))
  10918. (params2
  10919. (list
  10920. :tstart (concat "@multitable @columnfractions " colfrac)
  10921. :tend "@end multitable"
  10922. :lstart "@item " :lend "" :sep " @tab "
  10923. :hlstart "@headitem ")))
  10924. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10925. ;;;; Link Stuff
  10926. ;;; Link abbreviations
  10927. (defun org-link-expand-abbrev (link)
  10928. "Apply replacements as defined in `org-link-abbrev-alist."
  10929. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10930. (let* ((key (match-string 1 link))
  10931. (as (or (assoc key org-link-abbrev-alist-local)
  10932. (assoc key org-link-abbrev-alist)))
  10933. (tag (and (match-end 2) (match-string 3 link)))
  10934. rpl)
  10935. (if (not as)
  10936. link
  10937. (setq rpl (cdr as))
  10938. (cond
  10939. ((symbolp rpl) (funcall rpl tag))
  10940. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10941. (t (concat rpl tag)))))
  10942. link))
  10943. ;;; Storing and inserting links
  10944. (defvar org-insert-link-history nil
  10945. "Minibuffer history for links inserted with `org-insert-link'.")
  10946. (defvar org-stored-links nil
  10947. "Contains the links stored with `org-store-link'.")
  10948. (defvar org-store-link-plist nil
  10949. "Plist with info about the most recently link created with `org-store-link'.")
  10950. (defvar org-link-protocols nil
  10951. "Link protocols added to Org-mode using `org-add-link-type'.")
  10952. (defvar org-store-link-functions nil
  10953. "List of functions that are called to create and store a link.
  10954. Each function will be called in turn until one returns a non-nil
  10955. value. Each function should check if it is responsible for creating
  10956. this link (for example by looking at the major mode).
  10957. If not, it must exit and return nil.
  10958. If yes, it should return a non-nil value after a calling
  10959. `org-store-link-props' with a list of properties and values.
  10960. Special properties are:
  10961. :type The link prefix. like \"http\". This must be given.
  10962. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10963. This is obligatory as well.
  10964. :description Optional default description for the second pair
  10965. of brackets in an Org-mode link. The user can still change
  10966. this when inserting this link into an Org-mode buffer.
  10967. In addition to these, any additional properties can be specified
  10968. and then used in remember templates.")
  10969. (defun org-add-link-type (type &optional follow publish)
  10970. "Add TYPE to the list of `org-link-types'.
  10971. Re-compute all regular expressions depending on `org-link-types'
  10972. FOLLOW and PUBLISH are two functions. Both take the link path as
  10973. an argument.
  10974. FOLLOW should do whatever is necessary to follow the link, for example
  10975. to find a file or display a mail message.
  10976. PUBLISH takes the path and retuns the string that should be used when
  10977. this document is published. FIMXE: This is actually not yet implemented."
  10978. (add-to-list 'org-link-types type t)
  10979. (org-make-link-regexps)
  10980. (add-to-list 'org-link-protocols
  10981. (list type follow publish)))
  10982. (defun org-add-agenda-custom-command (entry)
  10983. "Replace or add a command in `org-agenda-custom-commands'.
  10984. This is mostly for hacking and trying a new command - once the command
  10985. works you probably want to add it to `org-agenda-custom-commands' for good."
  10986. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10987. (if ass
  10988. (setcdr ass (cdr entry))
  10989. (push entry org-agenda-custom-commands))))
  10990. ;;;###autoload
  10991. (defun org-store-link (arg)
  10992. "\\<org-mode-map>Store an org-link to the current location.
  10993. This link is added to `org-stored-links' and can later be inserted
  10994. into an org-buffer with \\[org-insert-link].
  10995. For some link types, a prefix arg is interpreted:
  10996. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10997. For file links, arg negates `org-context-in-file-links'."
  10998. (interactive "P")
  10999. (setq org-store-link-plist nil) ; reset
  11000. (let (link cpltxt desc description search txt)
  11001. (cond
  11002. ((run-hook-with-args-until-success 'org-store-link-functions)
  11003. (setq link (plist-get org-store-link-plist :link)
  11004. desc (or (plist-get org-store-link-plist :description) link)))
  11005. ((eq major-mode 'bbdb-mode)
  11006. (let ((name (bbdb-record-name (bbdb-current-record)))
  11007. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  11008. (setq cpltxt (concat "bbdb:" (or name company))
  11009. link (org-make-link cpltxt))
  11010. (org-store-link-props :type "bbdb" :name name :company company)))
  11011. ((eq major-mode 'Info-mode)
  11012. (setq link (org-make-link "info:"
  11013. (file-name-nondirectory Info-current-file)
  11014. ":" Info-current-node))
  11015. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  11016. ":" Info-current-node))
  11017. (org-store-link-props :type "info" :file Info-current-file
  11018. :node Info-current-node))
  11019. ((eq major-mode 'calendar-mode)
  11020. (let ((cd (calendar-cursor-to-date)))
  11021. (setq link
  11022. (format-time-string
  11023. (car org-time-stamp-formats)
  11024. (apply 'encode-time
  11025. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11026. nil nil nil))))
  11027. (org-store-link-props :type "calendar" :date cd)))
  11028. ((or (eq major-mode 'vm-summary-mode)
  11029. (eq major-mode 'vm-presentation-mode))
  11030. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11031. (vm-follow-summary-cursor)
  11032. (save-excursion
  11033. (vm-select-folder-buffer)
  11034. (let* ((message (car vm-message-pointer))
  11035. (folder buffer-file-name)
  11036. (subject (vm-su-subject message))
  11037. (to (vm-get-header-contents message "To"))
  11038. (from (vm-get-header-contents message "From"))
  11039. (message-id (vm-su-message-id message)))
  11040. (org-store-link-props :type "vm" :from from :to to :subject subject
  11041. :message-id message-id)
  11042. (setq message-id (org-remove-angle-brackets message-id))
  11043. (setq folder (abbreviate-file-name folder))
  11044. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11045. folder)
  11046. (setq folder (replace-match "" t t folder)))
  11047. (setq cpltxt (org-email-link-description))
  11048. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11049. ((eq major-mode 'wl-summary-mode)
  11050. (let* ((msgnum (wl-summary-message-number))
  11051. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11052. msgnum 'message-id))
  11053. (wl-message-entity
  11054. (if (fboundp 'elmo-message-entity)
  11055. (elmo-message-entity
  11056. wl-summary-buffer-elmo-folder msgnum)
  11057. (elmo-msgdb-overview-get-entity
  11058. msgnum (wl-summary-buffer-msgdb))))
  11059. (from (wl-summary-line-from))
  11060. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11061. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11062. (wl-summary-line-subject))))
  11063. (org-store-link-props :type "wl" :from from :to to
  11064. :subject subject :message-id message-id)
  11065. (setq message-id (org-remove-angle-brackets message-id))
  11066. (setq cpltxt (org-email-link-description))
  11067. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11068. "#" message-id))))
  11069. ((or (equal major-mode 'mh-folder-mode)
  11070. (equal major-mode 'mh-show-mode))
  11071. (let ((from (org-mhe-get-header "From:"))
  11072. (to (org-mhe-get-header "To:"))
  11073. (message-id (org-mhe-get-header "Message-Id:"))
  11074. (subject (org-mhe-get-header "Subject:")))
  11075. (org-store-link-props :type "mh" :from from :to to
  11076. :subject subject :message-id message-id)
  11077. (setq cpltxt (org-email-link-description))
  11078. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11079. (org-remove-angle-brackets message-id)))))
  11080. ((or (eq major-mode 'rmail-mode)
  11081. (eq major-mode 'rmail-summary-mode))
  11082. (save-window-excursion
  11083. (save-restriction
  11084. (when (eq major-mode 'rmail-summary-mode)
  11085. (rmail-show-message rmail-current-message))
  11086. (rmail-narrow-to-non-pruned-header)
  11087. (let ((folder buffer-file-name)
  11088. (message-id (mail-fetch-field "message-id"))
  11089. (from (mail-fetch-field "from"))
  11090. (to (mail-fetch-field "to"))
  11091. (subject (mail-fetch-field "subject")))
  11092. (org-store-link-props
  11093. :type "rmail" :from from :to to
  11094. :subject subject :message-id message-id)
  11095. (setq message-id (org-remove-angle-brackets message-id))
  11096. (setq cpltxt (org-email-link-description))
  11097. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11098. (rmail-show-message rmail-current-message))))
  11099. ((eq major-mode 'gnus-group-mode)
  11100. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11101. (gnus-group-group-name)) ; version
  11102. ((fboundp 'gnus-group-name)
  11103. (gnus-group-name))
  11104. (t "???"))))
  11105. (unless group (error "Not on a group"))
  11106. (org-store-link-props :type "gnus" :group group)
  11107. (setq cpltxt (concat
  11108. (if (org-xor arg org-usenet-links-prefer-google)
  11109. "http://groups.google.com/groups?group="
  11110. "gnus:")
  11111. group)
  11112. link (org-make-link cpltxt))))
  11113. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11114. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11115. (let* ((group gnus-newsgroup-name)
  11116. (article (gnus-summary-article-number))
  11117. (header (gnus-summary-article-header article))
  11118. (from (mail-header-from header))
  11119. (message-id (mail-header-id header))
  11120. (date (mail-header-date header))
  11121. (subject (gnus-summary-subject-string)))
  11122. (org-store-link-props :type "gnus" :from from :subject subject
  11123. :message-id message-id :group group)
  11124. (setq cpltxt (org-email-link-description))
  11125. (if (org-xor arg org-usenet-links-prefer-google)
  11126. (setq link
  11127. (concat
  11128. cpltxt "\n "
  11129. (format "http://groups.google.com/groups?as_umsgid=%s"
  11130. (org-fixup-message-id-for-http message-id))))
  11131. (setq link (org-make-link "gnus:" group
  11132. "#" (number-to-string article))))))
  11133. ((eq major-mode 'w3-mode)
  11134. (setq cpltxt (url-view-url t)
  11135. link (org-make-link cpltxt))
  11136. (org-store-link-props :type "w3" :url (url-view-url t)))
  11137. ((eq major-mode 'w3m-mode)
  11138. (setq cpltxt (or w3m-current-title w3m-current-url)
  11139. link (org-make-link w3m-current-url))
  11140. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11141. ((setq search (run-hook-with-args-until-success
  11142. 'org-create-file-search-functions))
  11143. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11144. "::" search))
  11145. (setq cpltxt (or description link)))
  11146. ((eq major-mode 'image-mode)
  11147. (setq cpltxt (concat "file:"
  11148. (abbreviate-file-name buffer-file-name))
  11149. link (org-make-link cpltxt))
  11150. (org-store-link-props :type "image" :file buffer-file-name))
  11151. ((eq major-mode 'dired-mode)
  11152. ;; link to the file in the current line
  11153. (setq cpltxt (concat "file:"
  11154. (abbreviate-file-name
  11155. (expand-file-name
  11156. (dired-get-filename nil t))))
  11157. link (org-make-link cpltxt)))
  11158. ((and buffer-file-name (org-mode-p))
  11159. ;; Just link to current headline
  11160. (setq cpltxt (concat "file:"
  11161. (abbreviate-file-name buffer-file-name)))
  11162. ;; Add a context search string
  11163. (when (org-xor org-context-in-file-links arg)
  11164. ;; Check if we are on a target
  11165. (if (org-in-regexp "<<\\(.*?\\)>>")
  11166. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11167. (setq txt (cond
  11168. ((org-on-heading-p) nil)
  11169. ((org-region-active-p)
  11170. (buffer-substring (region-beginning) (region-end)))
  11171. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11172. (when (or (null txt) (string-match "\\S-" txt))
  11173. (setq cpltxt
  11174. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11175. desc "NONE"))))
  11176. (if (string-match "::\\'" cpltxt)
  11177. (setq cpltxt (substring cpltxt 0 -2)))
  11178. (setq link (org-make-link cpltxt)))
  11179. ((buffer-file-name (buffer-base-buffer))
  11180. ;; Just link to this file here.
  11181. (setq cpltxt (concat "file:"
  11182. (abbreviate-file-name
  11183. (buffer-file-name (buffer-base-buffer)))))
  11184. ;; Add a context string
  11185. (when (org-xor org-context-in-file-links arg)
  11186. (setq txt (if (org-region-active-p)
  11187. (buffer-substring (region-beginning) (region-end))
  11188. (buffer-substring (point-at-bol) (point-at-eol))))
  11189. ;; Only use search option if there is some text.
  11190. (when (string-match "\\S-" txt)
  11191. (setq cpltxt
  11192. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11193. desc "NONE")))
  11194. (setq link (org-make-link cpltxt)))
  11195. ((interactive-p)
  11196. (error "Cannot link to a buffer which is not visiting a file"))
  11197. (t (setq link nil)))
  11198. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11199. (setq link (or link cpltxt)
  11200. desc (or desc cpltxt))
  11201. (if (equal desc "NONE") (setq desc nil))
  11202. (if (and (interactive-p) link)
  11203. (progn
  11204. (setq org-stored-links
  11205. (cons (list link desc) org-stored-links))
  11206. (message "Stored: %s" (or desc link)))
  11207. (and link (org-make-link-string link desc)))))
  11208. (defun org-store-link-props (&rest plist)
  11209. "Store link properties, extract names and addresses."
  11210. (let (x adr)
  11211. (when (setq x (plist-get plist :from))
  11212. (setq adr (mail-extract-address-components x))
  11213. (plist-put plist :fromname (car adr))
  11214. (plist-put plist :fromaddress (nth 1 adr)))
  11215. (when (setq x (plist-get plist :to))
  11216. (setq adr (mail-extract-address-components x))
  11217. (plist-put plist :toname (car adr))
  11218. (plist-put plist :toaddress (nth 1 adr))))
  11219. (let ((from (plist-get plist :from))
  11220. (to (plist-get plist :to)))
  11221. (when (and from to org-from-is-user-regexp)
  11222. (plist-put plist :fromto
  11223. (if (string-match org-from-is-user-regexp from)
  11224. (concat "to %t")
  11225. (concat "from %f")))))
  11226. (setq org-store-link-plist plist))
  11227. (defun org-email-link-description (&optional fmt)
  11228. "Return the description part of an email link.
  11229. This takes information from `org-store-link-plist' and formats it
  11230. according to FMT (default from `org-email-link-description-format')."
  11231. (setq fmt (or fmt org-email-link-description-format))
  11232. (let* ((p org-store-link-plist)
  11233. (to (plist-get p :toaddress))
  11234. (from (plist-get p :fromaddress))
  11235. (table
  11236. (list
  11237. (cons "%c" (plist-get p :fromto))
  11238. (cons "%F" (plist-get p :from))
  11239. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11240. (cons "%T" (plist-get p :to))
  11241. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11242. (cons "%s" (plist-get p :subject))
  11243. (cons "%m" (plist-get p :message-id)))))
  11244. (when (string-match "%c" fmt)
  11245. ;; Check if the user wrote this message
  11246. (if (and org-from-is-user-regexp from to
  11247. (save-match-data (string-match org-from-is-user-regexp from)))
  11248. (setq fmt (replace-match "to %t" t t fmt))
  11249. (setq fmt (replace-match "from %f" t t fmt))))
  11250. (org-replace-escapes fmt table)))
  11251. (defun org-make-org-heading-search-string (&optional string heading)
  11252. "Make search string for STRING or current headline."
  11253. (interactive)
  11254. (let ((s (or string (org-get-heading))))
  11255. (unless (and string (not heading))
  11256. ;; We are using a headline, clean up garbage in there.
  11257. (if (string-match org-todo-regexp s)
  11258. (setq s (replace-match "" t t s)))
  11259. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11260. (setq s (replace-match "" t t s)))
  11261. (setq s (org-trim s))
  11262. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11263. org-comment-string "\\)") s)
  11264. (setq s (replace-match "" t t s)))
  11265. (while (string-match org-ts-regexp s)
  11266. (setq s (replace-match "" t t s))))
  11267. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11268. (setq s (replace-match " " t t s)))
  11269. (or string (setq s (concat "*" s))) ; Add * for headlines
  11270. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11271. (defun org-make-link (&rest strings)
  11272. "Concatenate STRINGS."
  11273. (apply 'concat strings))
  11274. (defun org-make-link-string (link &optional description)
  11275. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11276. (unless (string-match "\\S-" link)
  11277. (error "Empty link"))
  11278. (when (stringp description)
  11279. ;; Remove brackets from the description, they are fatal.
  11280. (while (string-match "\\[" description)
  11281. (setq description (replace-match "{" t t description)))
  11282. (while (string-match "\\]" description)
  11283. (setq description (replace-match "}" t t description))))
  11284. (when (equal (org-link-escape link) description)
  11285. ;; No description needed, it is identical
  11286. (setq description nil))
  11287. (when (and (not description)
  11288. (not (equal link (org-link-escape link))))
  11289. (setq description link))
  11290. (concat "[[" (org-link-escape link) "]"
  11291. (if description (concat "[" description "]") "")
  11292. "]"))
  11293. (defconst org-link-escape-chars
  11294. '((?\ . "%20")
  11295. (?\[ . "%5B")
  11296. (?\] . "%5D")
  11297. (?\340 . "%E0") ; `a
  11298. (?\342 . "%E2") ; ^a
  11299. (?\347 . "%E7") ; ,c
  11300. (?\350 . "%E8") ; `e
  11301. (?\351 . "%E9") ; 'e
  11302. (?\352 . "%EA") ; ^e
  11303. (?\356 . "%EE") ; ^i
  11304. (?\364 . "%F4") ; ^o
  11305. (?\371 . "%F9") ; `u
  11306. (?\373 . "%FB") ; ^u
  11307. (?\; . "%3B")
  11308. (?? . "%3F")
  11309. (?= . "%3D")
  11310. (?+ . "%2B")
  11311. )
  11312. "Association list of escapes for some characters problematic in links.
  11313. This is the list that is used for internal purposes.")
  11314. (defconst org-link-escape-chars-browser
  11315. '((?\ . "%20")) ; 32 for the SPC char
  11316. "Association list of escapes for some characters problematic in links.
  11317. This is the list that is used before handing over to the browser.")
  11318. (defun org-link-escape (text &optional table)
  11319. "Escape charaters in TEXT that are problematic for links."
  11320. (setq table (or table org-link-escape-chars))
  11321. (when text
  11322. (let ((re (mapconcat (lambda (x) (regexp-quote
  11323. (char-to-string (car x))))
  11324. table "\\|")))
  11325. (while (string-match re text)
  11326. (setq text
  11327. (replace-match
  11328. (cdr (assoc (string-to-char (match-string 0 text))
  11329. table))
  11330. t t text)))
  11331. text)))
  11332. (defun org-link-unescape (text &optional table)
  11333. "Reverse the action of `org-link-escape'."
  11334. (setq table (or table org-link-escape-chars))
  11335. (when text
  11336. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11337. table "\\|")))
  11338. (while (string-match re text)
  11339. (setq text
  11340. (replace-match
  11341. (char-to-string (car (rassoc (match-string 0 text) table)))
  11342. t t text)))
  11343. text)))
  11344. (defun org-xor (a b)
  11345. "Exclusive or."
  11346. (if a (not b) b))
  11347. (defun org-get-header (header)
  11348. "Find a header field in the current buffer."
  11349. (save-excursion
  11350. (goto-char (point-min))
  11351. (let ((case-fold-search t) s)
  11352. (cond
  11353. ((eq header 'from)
  11354. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11355. (setq s (match-string 1)))
  11356. (while (string-match "\"" s)
  11357. (setq s (replace-match "" t t s)))
  11358. (if (string-match "[<(].*" s)
  11359. (setq s (replace-match "" t t s))))
  11360. ((eq header 'message-id)
  11361. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11362. (setq s (match-string 1))))
  11363. ((eq header 'subject)
  11364. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11365. (setq s (match-string 1)))))
  11366. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11367. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11368. s)))
  11369. (defun org-fixup-message-id-for-http (s)
  11370. "Replace special characters in a message id, so it can be used in an http query."
  11371. (while (string-match "<" s)
  11372. (setq s (replace-match "%3C" t t s)))
  11373. (while (string-match ">" s)
  11374. (setq s (replace-match "%3E" t t s)))
  11375. (while (string-match "@" s)
  11376. (setq s (replace-match "%40" t t s)))
  11377. s)
  11378. ;;;###autoload
  11379. (defun org-insert-link-global ()
  11380. "Insert a link like Org-mode does.
  11381. This command can be called in any mode to insert a link in Org-mode syntax."
  11382. (interactive)
  11383. (org-run-like-in-org-mode 'org-insert-link))
  11384. (defun org-insert-link (&optional complete-file)
  11385. "Insert a link. At the prompt, enter the link.
  11386. Completion can be used to select a link previously stored with
  11387. `org-store-link'. When the empty string is entered (i.e. if you just
  11388. press RET at the prompt), the link defaults to the most recently
  11389. stored link. As SPC triggers completion in the minibuffer, you need to
  11390. use M-SPC or C-q SPC to force the insertion of a space character.
  11391. You will also be prompted for a description, and if one is given, it will
  11392. be displayed in the buffer instead of the link.
  11393. If there is already a link at point, this command will allow you to edit link
  11394. and description parts.
  11395. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11396. selected using completion. The path to the file will be relative to
  11397. the current directory if the file is in the current directory or a
  11398. subdirectory. Otherwise, the link will be the absolute path as
  11399. completed in the minibuffer (i.e. normally ~/path/to/file).
  11400. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11401. is in the current directory or below.
  11402. With three \\[universal-argument] prefixes, negate the meaning of
  11403. `org-keep-stored-link-after-insertion'."
  11404. (interactive "P")
  11405. (let* ((wcf (current-window-configuration))
  11406. (region (if (org-region-active-p)
  11407. (buffer-substring (region-beginning) (region-end))))
  11408. (remove (and region (list (region-beginning) (region-end))))
  11409. (desc region)
  11410. tmphist ; byte-compile incorrectly complains about this
  11411. link entry file)
  11412. (cond
  11413. ((org-in-regexp org-bracket-link-regexp 1)
  11414. ;; We do have a link at point, and we are going to edit it.
  11415. (setq remove (list (match-beginning 0) (match-end 0)))
  11416. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11417. (setq link (read-string "Link: "
  11418. (org-link-unescape
  11419. (org-match-string-no-properties 1)))))
  11420. ((or (org-in-regexp org-angle-link-re)
  11421. (org-in-regexp org-plain-link-re))
  11422. ;; Convert to bracket link
  11423. (setq remove (list (match-beginning 0) (match-end 0))
  11424. link (read-string "Link: "
  11425. (org-remove-angle-brackets (match-string 0)))))
  11426. ((equal complete-file '(4))
  11427. ;; Completing read for file names.
  11428. (setq file (read-file-name "File: "))
  11429. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11430. (pwd1 (file-name-as-directory (abbreviate-file-name
  11431. (expand-file-name ".")))))
  11432. (cond
  11433. ((equal complete-file '(16))
  11434. (setq link (org-make-link
  11435. "file:"
  11436. (abbreviate-file-name (expand-file-name file)))))
  11437. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11438. (setq link (org-make-link "file:" (match-string 1 file))))
  11439. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11440. (expand-file-name file))
  11441. (setq link (org-make-link
  11442. "file:" (match-string 1 (expand-file-name file)))))
  11443. (t (setq link (org-make-link "file:" file))))))
  11444. (t
  11445. ;; Read link, with completion for stored links.
  11446. (with-output-to-temp-buffer "*Org Links*"
  11447. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11448. (when org-stored-links
  11449. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11450. (princ (mapconcat
  11451. (lambda (x)
  11452. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11453. (reverse org-stored-links) "\n"))))
  11454. (let ((cw (selected-window)))
  11455. (select-window (get-buffer-window "*Org Links*"))
  11456. (shrink-window-if-larger-than-buffer)
  11457. (setq truncate-lines t)
  11458. (select-window cw))
  11459. ;; Fake a link history, containing the stored links.
  11460. (setq tmphist (append (mapcar 'car org-stored-links)
  11461. org-insert-link-history))
  11462. (unwind-protect
  11463. (setq link (org-completing-read
  11464. "Link: "
  11465. (append
  11466. (mapcar (lambda (x) (list (concat (car x) ":")))
  11467. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11468. (mapcar (lambda (x) (list (concat x ":")))
  11469. org-link-types))
  11470. nil nil nil
  11471. 'tmphist
  11472. (or (car (car org-stored-links)))))
  11473. (set-window-configuration wcf)
  11474. (kill-buffer "*Org Links*"))
  11475. (setq entry (assoc link org-stored-links))
  11476. (or entry (push link org-insert-link-history))
  11477. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11478. (not org-keep-stored-link-after-insertion))
  11479. (setq org-stored-links (delq (assoc link org-stored-links)
  11480. org-stored-links)))
  11481. (setq desc (or desc (nth 1 entry)))))
  11482. (if (string-match org-plain-link-re link)
  11483. ;; URL-like link, normalize the use of angular brackets.
  11484. (setq link (org-make-link (org-remove-angle-brackets link))))
  11485. ;; Check if we are linking to the current file with a search option
  11486. ;; If yes, simplify the link by using only the search option.
  11487. (when (and buffer-file-name
  11488. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11489. (let* ((path (match-string 1 link))
  11490. (case-fold-search nil)
  11491. (search (match-string 2 link)))
  11492. (save-match-data
  11493. (if (equal (file-truename buffer-file-name) (file-truename path))
  11494. ;; We are linking to this same file, with a search option
  11495. (setq link search)))))
  11496. ;; Check if we can/should use a relative path. If yes, simplify the link
  11497. (when (string-match "\\<file:\\(.*\\)" link)
  11498. (let* ((path (match-string 1 link))
  11499. (origpath path)
  11500. (case-fold-search nil))
  11501. (cond
  11502. ((eq org-link-file-path-type 'absolute)
  11503. (setq path (abbreviate-file-name (expand-file-name path))))
  11504. ((eq org-link-file-path-type 'noabbrev)
  11505. (setq path (expand-file-name path)))
  11506. ((eq org-link-file-path-type 'relative)
  11507. (setq path (file-relative-name path)))
  11508. (t
  11509. (save-match-data
  11510. (if (string-match (concat "^" (regexp-quote
  11511. (file-name-as-directory
  11512. (expand-file-name "."))))
  11513. (expand-file-name path))
  11514. ;; We are linking a file with relative path name.
  11515. (setq path (substring (expand-file-name path)
  11516. (match-end 0)))))))
  11517. (setq link (concat "file:" path))
  11518. (if (equal desc origpath)
  11519. (setq desc path))))
  11520. (setq desc (read-string "Description: " desc))
  11521. (unless (string-match "\\S-" desc) (setq desc nil))
  11522. (if remove (apply 'delete-region remove))
  11523. (insert (org-make-link-string link desc))))
  11524. (defun org-completing-read (&rest args)
  11525. (let ((minibuffer-local-completion-map
  11526. (copy-keymap minibuffer-local-completion-map)))
  11527. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11528. (apply 'completing-read args)))
  11529. ;;; Opening/following a link
  11530. (defvar org-link-search-failed nil)
  11531. (defun org-next-link ()
  11532. "Move forward to the next link.
  11533. If the link is in hidden text, expose it."
  11534. (interactive)
  11535. (when (and org-link-search-failed (eq this-command last-command))
  11536. (goto-char (point-min))
  11537. (message "Link search wrapped back to beginning of buffer"))
  11538. (setq org-link-search-failed nil)
  11539. (let* ((pos (point))
  11540. (ct (org-context))
  11541. (a (assoc :link ct)))
  11542. (if a (goto-char (nth 2 a)))
  11543. (if (re-search-forward org-any-link-re nil t)
  11544. (progn
  11545. (goto-char (match-beginning 0))
  11546. (if (org-invisible-p) (org-show-context)))
  11547. (goto-char pos)
  11548. (setq org-link-search-failed t)
  11549. (error "No further link found"))))
  11550. (defun org-previous-link ()
  11551. "Move backward to the previous link.
  11552. If the link is in hidden text, expose it."
  11553. (interactive)
  11554. (when (and org-link-search-failed (eq this-command last-command))
  11555. (goto-char (point-max))
  11556. (message "Link search wrapped back to end of buffer"))
  11557. (setq org-link-search-failed nil)
  11558. (let* ((pos (point))
  11559. (ct (org-context))
  11560. (a (assoc :link ct)))
  11561. (if a (goto-char (nth 1 a)))
  11562. (if (re-search-backward org-any-link-re nil t)
  11563. (progn
  11564. (goto-char (match-beginning 0))
  11565. (if (org-invisible-p) (org-show-context)))
  11566. (goto-char pos)
  11567. (setq org-link-search-failed t)
  11568. (error "No further link found"))))
  11569. (defun org-find-file-at-mouse (ev)
  11570. "Open file link or URL at mouse."
  11571. (interactive "e")
  11572. (mouse-set-point ev)
  11573. (org-open-at-point 'in-emacs))
  11574. (defun org-open-at-mouse (ev)
  11575. "Open file link or URL at mouse."
  11576. (interactive "e")
  11577. (mouse-set-point ev)
  11578. (org-open-at-point))
  11579. (defvar org-window-config-before-follow-link nil
  11580. "The window configuration before following a link.
  11581. This is saved in case the need arises to restore it.")
  11582. (defvar org-open-link-marker (make-marker)
  11583. "Marker pointing to the location where `org-open-at-point; was called.")
  11584. ;;;###autoload
  11585. (defun org-open-at-point-global ()
  11586. "Follow a link like Org-mode does.
  11587. This command can be called in any mode to follow a link that has
  11588. Org-mode syntax."
  11589. (interactive)
  11590. (org-run-like-in-org-mode 'org-open-at-point))
  11591. (defun org-open-at-point (&optional in-emacs)
  11592. "Open link at or after point.
  11593. If there is no link at point, this function will search forward up to
  11594. the end of the current subtree.
  11595. Normally, files will be opened by an appropriate application. If the
  11596. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11597. (interactive "P")
  11598. (move-marker org-open-link-marker (point))
  11599. (setq org-window-config-before-follow-link (current-window-configuration))
  11600. (org-remove-occur-highlights nil nil t)
  11601. (if (org-at-timestamp-p t)
  11602. (org-follow-timestamp-link)
  11603. (let (type path link line search (pos (point)))
  11604. (catch 'match
  11605. (save-excursion
  11606. (skip-chars-forward "^]\n\r")
  11607. (when (org-in-regexp org-bracket-link-regexp)
  11608. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11609. (while (string-match " *\n *" link)
  11610. (setq link (replace-match " " t t link)))
  11611. (setq link (org-link-expand-abbrev link))
  11612. (if (string-match org-link-re-with-space2 link)
  11613. (setq type (match-string 1 link) path (match-string 2 link))
  11614. (setq type "thisfile" path link))
  11615. (throw 'match t)))
  11616. (when (get-text-property (point) 'org-linked-text)
  11617. (setq type "thisfile"
  11618. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11619. (1+ (point)) (point))
  11620. path (buffer-substring
  11621. (previous-single-property-change pos 'org-linked-text)
  11622. (next-single-property-change pos 'org-linked-text)))
  11623. (throw 'match t))
  11624. (save-excursion
  11625. (when (or (org-in-regexp org-angle-link-re)
  11626. (org-in-regexp org-plain-link-re))
  11627. (setq type (match-string 1) path (match-string 2))
  11628. (throw 'match t)))
  11629. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11630. (setq type "tree-match"
  11631. path (match-string 1))
  11632. (throw 'match t))
  11633. (save-excursion
  11634. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11635. (setq type "tags"
  11636. path (match-string 1))
  11637. (while (string-match ":" path)
  11638. (setq path (replace-match "+" t t path)))
  11639. (throw 'match t))))
  11640. (unless path
  11641. (error "No link found"))
  11642. ;; Remove any trailing spaces in path
  11643. (if (string-match " +\\'" path)
  11644. (setq path (replace-match "" t t path)))
  11645. (cond
  11646. ((assoc type org-link-protocols)
  11647. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11648. ((equal type "mailto")
  11649. (let ((cmd (car org-link-mailto-program))
  11650. (args (cdr org-link-mailto-program)) args1
  11651. (address path) (subject "") a)
  11652. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11653. (setq address (match-string 1 path)
  11654. subject (org-link-escape (match-string 2 path))))
  11655. (while args
  11656. (cond
  11657. ((not (stringp (car args))) (push (pop args) args1))
  11658. (t (setq a (pop args))
  11659. (if (string-match "%a" a)
  11660. (setq a (replace-match address t t a)))
  11661. (if (string-match "%s" a)
  11662. (setq a (replace-match subject t t a)))
  11663. (push a args1))))
  11664. (apply cmd (nreverse args1))))
  11665. ((member type '("http" "https" "ftp" "news"))
  11666. (browse-url (concat type ":" (org-link-escape
  11667. path org-link-escape-chars-browser))))
  11668. ((member type '("message"))
  11669. (browse-url (concat type ":" path)))
  11670. ((string= type "tags")
  11671. (org-tags-view in-emacs path))
  11672. ((string= type "thisfile")
  11673. (if in-emacs
  11674. (switch-to-buffer-other-window
  11675. (org-get-buffer-for-internal-link (current-buffer)))
  11676. (org-mark-ring-push))
  11677. (let ((cmd `(org-link-search
  11678. ,path
  11679. ,(cond ((equal in-emacs '(4)) 'occur)
  11680. ((equal in-emacs '(16)) 'org-occur)
  11681. (t nil))
  11682. ,pos)))
  11683. (condition-case nil (eval cmd)
  11684. (error (progn (widen) (eval cmd))))))
  11685. ((string= type "tree-match")
  11686. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11687. ((string= type "file")
  11688. (if (string-match "::\\([0-9]+\\)\\'" path)
  11689. (setq line (string-to-number (match-string 1 path))
  11690. path (substring path 0 (match-beginning 0)))
  11691. (if (string-match "::\\(.+\\)\\'" path)
  11692. (setq search (match-string 1 path)
  11693. path (substring path 0 (match-beginning 0)))))
  11694. (if (string-match "[*?{]" (file-name-nondirectory path))
  11695. (dired path)
  11696. (org-open-file path in-emacs line search)))
  11697. ((string= type "news")
  11698. (org-follow-gnus-link path))
  11699. ((string= type "bbdb")
  11700. (org-follow-bbdb-link path))
  11701. ((string= type "info")
  11702. (org-follow-info-link path))
  11703. ((string= type "gnus")
  11704. (let (group article)
  11705. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11706. (error "Error in Gnus link"))
  11707. (setq group (match-string 1 path)
  11708. article (match-string 3 path))
  11709. (org-follow-gnus-link group article)))
  11710. ((string= type "vm")
  11711. (let (folder article)
  11712. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11713. (error "Error in VM link"))
  11714. (setq folder (match-string 1 path)
  11715. article (match-string 3 path))
  11716. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11717. (org-follow-vm-link folder article in-emacs)))
  11718. ((string= type "wl")
  11719. (let (folder article)
  11720. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11721. (error "Error in Wanderlust link"))
  11722. (setq folder (match-string 1 path)
  11723. article (match-string 3 path))
  11724. (org-follow-wl-link folder article)))
  11725. ((string= type "mhe")
  11726. (let (folder article)
  11727. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11728. (error "Error in MHE link"))
  11729. (setq folder (match-string 1 path)
  11730. article (match-string 3 path))
  11731. (org-follow-mhe-link folder article)))
  11732. ((string= type "rmail")
  11733. (let (folder article)
  11734. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11735. (error "Error in RMAIL link"))
  11736. (setq folder (match-string 1 path)
  11737. article (match-string 3 path))
  11738. (org-follow-rmail-link folder article)))
  11739. ((string= type "shell")
  11740. (let ((cmd path))
  11741. (if (or (not org-confirm-shell-link-function)
  11742. (funcall org-confirm-shell-link-function
  11743. (format "Execute \"%s\" in shell? "
  11744. (org-add-props cmd nil
  11745. 'face 'org-warning))))
  11746. (progn
  11747. (message "Executing %s" cmd)
  11748. (shell-command cmd))
  11749. (error "Abort"))))
  11750. ((string= type "elisp")
  11751. (let ((cmd path))
  11752. (if (or (not org-confirm-elisp-link-function)
  11753. (funcall org-confirm-elisp-link-function
  11754. (format "Execute \"%s\" as elisp? "
  11755. (org-add-props cmd nil
  11756. 'face 'org-warning))))
  11757. (message "%s => %s" cmd (eval (read cmd)))
  11758. (error "Abort"))))
  11759. (t
  11760. (browse-url-at-point)))))
  11761. (move-marker org-open-link-marker nil))
  11762. ;;; File search
  11763. (defvar org-create-file-search-functions nil
  11764. "List of functions to construct the right search string for a file link.
  11765. These functions are called in turn with point at the location to
  11766. which the link should point.
  11767. A function in the hook should first test if it would like to
  11768. handle this file type, for example by checking the major-mode or
  11769. the file extension. If it decides not to handle this file, it
  11770. should just return nil to give other functions a chance. If it
  11771. does handle the file, it must return the search string to be used
  11772. when following the link. The search string will be part of the
  11773. file link, given after a double colon, and `org-open-at-point'
  11774. will automatically search for it. If special measures must be
  11775. taken to make the search successful, another function should be
  11776. added to the companion hook `org-execute-file-search-functions',
  11777. which see.
  11778. A function in this hook may also use `setq' to set the variable
  11779. `description' to provide a suggestion for the descriptive text to
  11780. be used for this link when it gets inserted into an Org-mode
  11781. buffer with \\[org-insert-link].")
  11782. (defvar org-execute-file-search-functions nil
  11783. "List of functions to execute a file search triggered by a link.
  11784. Functions added to this hook must accept a single argument, the
  11785. search string that was part of the file link, the part after the
  11786. double colon. The function must first check if it would like to
  11787. handle this search, for example by checking the major-mode or the
  11788. file extension. If it decides not to handle this search, it
  11789. should just return nil to give other functions a chance. If it
  11790. does handle the search, it must return a non-nil value to keep
  11791. other functions from trying.
  11792. Each function can access the current prefix argument through the
  11793. variable `current-prefix-argument'. Note that a single prefix is
  11794. used to force opening a link in Emacs, so it may be good to only
  11795. use a numeric or double prefix to guide the search function.
  11796. In case this is needed, a function in this hook can also restore
  11797. the window configuration before `org-open-at-point' was called using:
  11798. (set-window-configuration org-window-config-before-follow-link)")
  11799. (defun org-link-search (s &optional type avoid-pos)
  11800. "Search for a link search option.
  11801. If S is surrounded by forward slashes, it is interpreted as a
  11802. regular expression. In org-mode files, this will create an `org-occur'
  11803. sparse tree. In ordinary files, `occur' will be used to list matches.
  11804. If the current buffer is in `dired-mode', grep will be used to search
  11805. in all files. If AVOID-POS is given, ignore matches near that position."
  11806. (let ((case-fold-search t)
  11807. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11808. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11809. (append '(("") (" ") ("\t") ("\n"))
  11810. org-emphasis-alist)
  11811. "\\|") "\\)"))
  11812. (pos (point))
  11813. (pre "") (post "")
  11814. words re0 re1 re2 re3 re4 re5 re2a reall)
  11815. (cond
  11816. ;; First check if there are any special
  11817. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11818. ;; Now try the builtin stuff
  11819. ((save-excursion
  11820. (goto-char (point-min))
  11821. (and
  11822. (re-search-forward
  11823. (concat "<<" (regexp-quote s0) ">>") nil t)
  11824. (setq pos (match-beginning 0))))
  11825. ;; There is an exact target for this
  11826. (goto-char pos))
  11827. ((string-match "^/\\(.*\\)/$" s)
  11828. ;; A regular expression
  11829. (cond
  11830. ((org-mode-p)
  11831. (org-occur (match-string 1 s)))
  11832. ;;((eq major-mode 'dired-mode)
  11833. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11834. (t (org-do-occur (match-string 1 s)))))
  11835. (t
  11836. ;; A normal search strings
  11837. (when (equal (string-to-char s) ?*)
  11838. ;; Anchor on headlines, post may include tags.
  11839. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11840. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11841. s (substring s 1)))
  11842. (remove-text-properties
  11843. 0 (length s)
  11844. '(face nil mouse-face nil keymap nil fontified nil) s)
  11845. ;; Make a series of regular expressions to find a match
  11846. (setq words (org-split-string s "[ \n\r\t]+")
  11847. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11848. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11849. "\\)" markers)
  11850. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11851. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11852. re1 (concat pre re2 post)
  11853. re3 (concat pre re4 post)
  11854. re5 (concat pre ".*" re4)
  11855. re2 (concat pre re2)
  11856. re2a (concat pre re2a)
  11857. re4 (concat pre re4)
  11858. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11859. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11860. re5 "\\)"
  11861. ))
  11862. (cond
  11863. ((eq type 'org-occur) (org-occur reall))
  11864. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11865. (t (goto-char (point-min))
  11866. (if (or (org-search-not-self 1 re0 nil t)
  11867. (org-search-not-self 1 re1 nil t)
  11868. (org-search-not-self 1 re2 nil t)
  11869. (org-search-not-self 1 re2a nil t)
  11870. (org-search-not-self 1 re3 nil t)
  11871. (org-search-not-self 1 re4 nil t)
  11872. (org-search-not-self 1 re5 nil t)
  11873. )
  11874. (goto-char (match-beginning 1))
  11875. (goto-char pos)
  11876. (error "No match")))))
  11877. (t
  11878. ;; Normal string-search
  11879. (goto-char (point-min))
  11880. (if (search-forward s nil t)
  11881. (goto-char (match-beginning 0))
  11882. (error "No match"))))
  11883. (and (org-mode-p) (org-show-context 'link-search))))
  11884. (defun org-search-not-self (group &rest args)
  11885. "Execute `re-search-forward', but only accept matches that do not
  11886. enclose the position of `org-open-link-marker'."
  11887. (let ((m org-open-link-marker))
  11888. (catch 'exit
  11889. (while (apply 're-search-forward args)
  11890. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11891. (goto-char (match-end group))
  11892. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11893. (> (match-beginning 0) (marker-position m))
  11894. (< (match-end 0) (marker-position m)))
  11895. (save-match-data
  11896. (or (not (org-in-regexp
  11897. org-bracket-link-analytic-regexp 1))
  11898. (not (match-end 4)) ; no description
  11899. (and (<= (match-beginning 4) (point))
  11900. (>= (match-end 4) (point))))))
  11901. (throw 'exit (point))))))))
  11902. (defun org-get-buffer-for-internal-link (buffer)
  11903. "Return a buffer to be used for displaying the link target of internal links."
  11904. (cond
  11905. ((not org-display-internal-link-with-indirect-buffer)
  11906. buffer)
  11907. ((string-match "(Clone)$" (buffer-name buffer))
  11908. (message "Buffer is already a clone, not making another one")
  11909. ;; we also do not modify visibility in this case
  11910. buffer)
  11911. (t ; make a new indirect buffer for displaying the link
  11912. (let* ((bn (buffer-name buffer))
  11913. (ibn (concat bn "(Clone)"))
  11914. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11915. (with-current-buffer ib (org-overview))
  11916. ib))))
  11917. (defun org-do-occur (regexp &optional cleanup)
  11918. "Call the Emacs command `occur'.
  11919. If CLEANUP is non-nil, remove the printout of the regular expression
  11920. in the *Occur* buffer. This is useful if the regex is long and not useful
  11921. to read."
  11922. (occur regexp)
  11923. (when cleanup
  11924. (let ((cwin (selected-window)) win beg end)
  11925. (when (setq win (get-buffer-window "*Occur*"))
  11926. (select-window win))
  11927. (goto-char (point-min))
  11928. (when (re-search-forward "match[a-z]+" nil t)
  11929. (setq beg (match-end 0))
  11930. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11931. (setq end (1- (match-beginning 0)))))
  11932. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11933. (goto-char (point-min))
  11934. (select-window cwin))))
  11935. ;;; The mark ring for links jumps
  11936. (defvar org-mark-ring nil
  11937. "Mark ring for positions before jumps in Org-mode.")
  11938. (defvar org-mark-ring-last-goto nil
  11939. "Last position in the mark ring used to go back.")
  11940. ;; Fill and close the ring
  11941. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11942. (loop for i from 1 to org-mark-ring-length do
  11943. (push (make-marker) org-mark-ring))
  11944. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11945. org-mark-ring)
  11946. (defun org-mark-ring-push (&optional pos buffer)
  11947. "Put the current position or POS into the mark ring and rotate it."
  11948. (interactive)
  11949. (setq pos (or pos (point)))
  11950. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11951. (move-marker (car org-mark-ring)
  11952. (or pos (point))
  11953. (or buffer (current-buffer)))
  11954. (message "%s"
  11955. (substitute-command-keys
  11956. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11957. (defun org-mark-ring-goto (&optional n)
  11958. "Jump to the previous position in the mark ring.
  11959. With prefix arg N, jump back that many stored positions. When
  11960. called several times in succession, walk through the entire ring.
  11961. Org-mode commands jumping to a different position in the current file,
  11962. or to another Org-mode file, automatically push the old position
  11963. onto the ring."
  11964. (interactive "p")
  11965. (let (p m)
  11966. (if (eq last-command this-command)
  11967. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11968. (setq p org-mark-ring))
  11969. (setq org-mark-ring-last-goto p)
  11970. (setq m (car p))
  11971. (switch-to-buffer (marker-buffer m))
  11972. (goto-char m)
  11973. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11974. (defun org-remove-angle-brackets (s)
  11975. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11976. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11977. s)
  11978. (defun org-add-angle-brackets (s)
  11979. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11980. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11981. s)
  11982. ;;; Following specific links
  11983. (defun org-follow-timestamp-link ()
  11984. (cond
  11985. ((org-at-date-range-p t)
  11986. (let ((org-agenda-start-on-weekday)
  11987. (t1 (match-string 1))
  11988. (t2 (match-string 2)))
  11989. (setq t1 (time-to-days (org-time-string-to-time t1))
  11990. t2 (time-to-days (org-time-string-to-time t2)))
  11991. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11992. ((org-at-timestamp-p t)
  11993. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11994. (substring (match-string 1) 0 10)))
  11995. 1))
  11996. (t (error "This should not happen"))))
  11997. (defun org-follow-bbdb-link (name)
  11998. "Follow a BBDB link to NAME."
  11999. (require 'bbdb)
  12000. (let ((inhibit-redisplay (not debug-on-error))
  12001. (bbdb-electric-p nil))
  12002. (catch 'exit
  12003. ;; Exact match on name
  12004. (bbdb-name (concat "\\`" name "\\'") nil)
  12005. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12006. ;; Exact match on name
  12007. (bbdb-company (concat "\\`" name "\\'") nil)
  12008. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12009. ;; Partial match on name
  12010. (bbdb-name name nil)
  12011. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12012. ;; Partial match on company
  12013. (bbdb-company name nil)
  12014. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12015. ;; General match including network address and notes
  12016. (bbdb name nil)
  12017. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  12018. (delete-window (get-buffer-window "*BBDB*"))
  12019. (error "No matching BBDB record")))))
  12020. (defun org-follow-info-link (name)
  12021. "Follow an info file & node link to NAME."
  12022. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12023. (string-match "\\(.*\\)" name))
  12024. (progn
  12025. (require 'info)
  12026. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12027. (Info-find-node (match-string 1 name) (match-string 2 name))
  12028. (Info-find-node (match-string 1 name) "Top")))
  12029. (message "Could not open: %s" name)))
  12030. (defun org-follow-gnus-link (&optional group article)
  12031. "Follow a Gnus link to GROUP and ARTICLE."
  12032. (require 'gnus)
  12033. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12034. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12035. (cond ((and group article)
  12036. (gnus-group-read-group 1 nil group)
  12037. (gnus-summary-goto-article (string-to-number article) nil t))
  12038. (group (gnus-group-jump-to-group group))))
  12039. (defun org-follow-vm-link (&optional folder article readonly)
  12040. "Follow a VM link to FOLDER and ARTICLE."
  12041. (require 'vm)
  12042. (setq article (org-add-angle-brackets article))
  12043. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12044. ;; ange-ftp or efs or tramp access
  12045. (let ((user (or (match-string 1 folder) (user-login-name)))
  12046. (host (match-string 2 folder))
  12047. (file (match-string 3 folder)))
  12048. (cond
  12049. ((featurep 'tramp)
  12050. ;; use tramp to access the file
  12051. (if (featurep 'xemacs)
  12052. (setq folder (format "[%s@%s]%s" user host file))
  12053. (setq folder (format "/%s@%s:%s" user host file))))
  12054. (t
  12055. ;; use ange-ftp or efs
  12056. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12057. (setq folder (format "/%s@%s:%s" user host file))))))
  12058. (when folder
  12059. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12060. (sit-for 0.1)
  12061. (when article
  12062. (vm-select-folder-buffer)
  12063. (widen)
  12064. (let ((case-fold-search t))
  12065. (goto-char (point-min))
  12066. (if (not (re-search-forward
  12067. (concat "^" "message-id: *" (regexp-quote article))))
  12068. (error "Could not find the specified message in this folder"))
  12069. (vm-isearch-update)
  12070. (vm-isearch-narrow)
  12071. (vm-beginning-of-message)
  12072. (vm-summarize)))))
  12073. (defun org-follow-wl-link (folder article)
  12074. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12075. (if (and (string= folder "%")
  12076. article
  12077. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12078. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12079. ;; Thus, we recompose folder and article ids.
  12080. (setq folder (format "%s#%s" folder (match-string 1 article))
  12081. article (match-string 3 article)))
  12082. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12083. (error "No such folder: %s" folder))
  12084. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12085. (and article
  12086. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12087. (wl-summary-redisplay)))
  12088. (defun org-follow-rmail-link (folder article)
  12089. "Follow an RMAIL link to FOLDER and ARTICLE."
  12090. (setq article (org-add-angle-brackets article))
  12091. (let (message-number)
  12092. (save-excursion
  12093. (save-window-excursion
  12094. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12095. (setq message-number
  12096. (save-restriction
  12097. (widen)
  12098. (goto-char (point-max))
  12099. (if (re-search-backward
  12100. (concat "^Message-ID:\\s-+" (regexp-quote
  12101. (or article "")))
  12102. nil t)
  12103. (rmail-what-message))))))
  12104. (if message-number
  12105. (progn
  12106. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12107. (rmail-show-message message-number)
  12108. message-number)
  12109. (error "Message not found"))))
  12110. ;;; mh-e integration based on planner-mode
  12111. (defun org-mhe-get-message-real-folder ()
  12112. "Return the name of the current message real folder, so if you use
  12113. sequences, it will now work."
  12114. (save-excursion
  12115. (let* ((folder
  12116. (if (equal major-mode 'mh-folder-mode)
  12117. mh-current-folder
  12118. ;; Refer to the show buffer
  12119. mh-show-folder-buffer))
  12120. (end-index
  12121. (if (boundp 'mh-index-folder)
  12122. (min (length mh-index-folder) (length folder))))
  12123. )
  12124. ;; a simple test on mh-index-data does not work, because
  12125. ;; mh-index-data is always nil in a show buffer.
  12126. (if (and (boundp 'mh-index-folder)
  12127. (string= mh-index-folder (substring folder 0 end-index)))
  12128. (if (equal major-mode 'mh-show-mode)
  12129. (save-window-excursion
  12130. (let (pop-up-frames)
  12131. (when (buffer-live-p (get-buffer folder))
  12132. (progn
  12133. (pop-to-buffer folder)
  12134. (org-mhe-get-message-folder-from-index)
  12135. )
  12136. )))
  12137. (org-mhe-get-message-folder-from-index)
  12138. )
  12139. folder
  12140. )
  12141. )))
  12142. (defun org-mhe-get-message-folder-from-index ()
  12143. "Returns the name of the message folder in a index folder buffer."
  12144. (save-excursion
  12145. (mh-index-previous-folder)
  12146. (re-search-forward "^\\(+.*\\)$" nil t)
  12147. (message "%s" (match-string 1))))
  12148. (defun org-mhe-get-message-folder ()
  12149. "Return the name of the current message folder. Be careful if you
  12150. use sequences."
  12151. (save-excursion
  12152. (if (equal major-mode 'mh-folder-mode)
  12153. mh-current-folder
  12154. ;; Refer to the show buffer
  12155. mh-show-folder-buffer)))
  12156. (defun org-mhe-get-message-num ()
  12157. "Return the number of the current message. Be careful if you
  12158. use sequences."
  12159. (save-excursion
  12160. (if (equal major-mode 'mh-folder-mode)
  12161. (mh-get-msg-num nil)
  12162. ;; Refer to the show buffer
  12163. (mh-show-buffer-message-number))))
  12164. (defun org-mhe-get-header (header)
  12165. "Return a header of the message in folder mode. This will create a
  12166. show buffer for the corresponding message. If you have a more clever
  12167. idea..."
  12168. (let* ((folder (org-mhe-get-message-folder))
  12169. (num (org-mhe-get-message-num))
  12170. (buffer (get-buffer-create (concat "show-" folder)))
  12171. (header-field))
  12172. (with-current-buffer buffer
  12173. (mh-display-msg num folder)
  12174. (if (equal major-mode 'mh-folder-mode)
  12175. (mh-header-display)
  12176. (mh-show-header-display))
  12177. (set-buffer buffer)
  12178. (setq header-field (mh-get-header-field header))
  12179. (if (equal major-mode 'mh-folder-mode)
  12180. (mh-show)
  12181. (mh-show-show))
  12182. header-field)))
  12183. (defun org-follow-mhe-link (folder article)
  12184. "Follow an MHE link to FOLDER and ARTICLE.
  12185. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12186. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12187. ARTICLE is searched in all folders. Indexed searches (swish++,
  12188. namazu, and others supported by MH-E) will always search in all
  12189. folders."
  12190. (require 'mh-e)
  12191. (require 'mh-search)
  12192. (require 'mh-utils)
  12193. (mh-find-path)
  12194. (if (not article)
  12195. (mh-visit-folder (mh-normalize-folder-name folder))
  12196. (setq article (org-add-angle-brackets article))
  12197. (mh-search-choose)
  12198. (if (equal mh-searcher 'pick)
  12199. (progn
  12200. (mh-search folder (list "--message-id" article))
  12201. (when (and org-mhe-search-all-folders
  12202. (not (org-mhe-get-message-real-folder)))
  12203. (kill-this-buffer)
  12204. (mh-search "+" (list "--message-id" article))))
  12205. (mh-search "+" article))
  12206. (if (org-mhe-get-message-real-folder)
  12207. (mh-show-msg 1)
  12208. (kill-this-buffer)
  12209. (error "Message not found"))))
  12210. ;;; BibTeX links
  12211. ;; Use the custom search meachnism to construct and use search strings for
  12212. ;; file links to BibTeX database entries.
  12213. (defun org-create-file-search-in-bibtex ()
  12214. "Create the search string and description for a BibTeX database entry."
  12215. (when (eq major-mode 'bibtex-mode)
  12216. ;; yes, we want to construct this search string.
  12217. ;; Make a good description for this entry, using names, year and the title
  12218. ;; Put it into the `description' variable which is dynamically scoped.
  12219. (let ((bibtex-autokey-names 1)
  12220. (bibtex-autokey-names-stretch 1)
  12221. (bibtex-autokey-name-case-convert-function 'identity)
  12222. (bibtex-autokey-name-separator " & ")
  12223. (bibtex-autokey-additional-names " et al.")
  12224. (bibtex-autokey-year-length 4)
  12225. (bibtex-autokey-name-year-separator " ")
  12226. (bibtex-autokey-titlewords 3)
  12227. (bibtex-autokey-titleword-separator " ")
  12228. (bibtex-autokey-titleword-case-convert-function 'identity)
  12229. (bibtex-autokey-titleword-length 'infty)
  12230. (bibtex-autokey-year-title-separator ": "))
  12231. (setq description (bibtex-generate-autokey)))
  12232. ;; Now parse the entry, get the key and return it.
  12233. (save-excursion
  12234. (bibtex-beginning-of-entry)
  12235. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12236. (defun org-execute-file-search-in-bibtex (s)
  12237. "Find the link search string S as a key for a database entry."
  12238. (when (eq major-mode 'bibtex-mode)
  12239. ;; Yes, we want to do the search in this file.
  12240. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12241. (goto-char (point-min))
  12242. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12243. (regexp-quote s) "[ \t\n]*,") nil t)
  12244. (goto-char (match-beginning 0)))
  12245. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12246. ;; Use double prefix to indicate that any web link should be browsed
  12247. (let ((b (current-buffer)) (p (point)))
  12248. ;; Restore the window configuration because we just use the web link
  12249. (set-window-configuration org-window-config-before-follow-link)
  12250. (save-excursion (set-buffer b) (goto-char p)
  12251. (bibtex-url)))
  12252. (recenter 0)) ; Move entry start to beginning of window
  12253. ;; return t to indicate that the search is done.
  12254. t))
  12255. ;; Finally add the functions to the right hooks.
  12256. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12257. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12258. ;; end of Bibtex link setup
  12259. ;;; Following file links
  12260. (defun org-open-file (path &optional in-emacs line search)
  12261. "Open the file at PATH.
  12262. First, this expands any special file name abbreviations. Then the
  12263. configuration variable `org-file-apps' is checked if it contains an
  12264. entry for this file type, and if yes, the corresponding command is launched.
  12265. If no application is found, Emacs simply visits the file.
  12266. With optional argument IN-EMACS, Emacs will visit the file.
  12267. Optional LINE specifies a line to go to, optional SEARCH a string to
  12268. search for. If LINE or SEARCH is given, the file will always be
  12269. opened in Emacs.
  12270. If the file does not exist, an error is thrown."
  12271. (setq in-emacs (or in-emacs line search))
  12272. (let* ((file (if (equal path "")
  12273. buffer-file-name
  12274. (substitute-in-file-name (expand-file-name path))))
  12275. (apps (append org-file-apps (org-default-apps)))
  12276. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12277. (dirp (if remp nil (file-directory-p file)))
  12278. (dfile (downcase file))
  12279. (old-buffer (current-buffer))
  12280. (old-pos (point))
  12281. (old-mode major-mode)
  12282. ext cmd)
  12283. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12284. (setq ext (match-string 1 dfile))
  12285. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12286. (setq ext (match-string 1 dfile))))
  12287. (if in-emacs
  12288. (setq cmd 'emacs)
  12289. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12290. (and dirp (cdr (assoc 'directory apps)))
  12291. (cdr (assoc ext apps))
  12292. (cdr (assoc t apps)))))
  12293. (when (eq cmd 'mailcap)
  12294. (require 'mailcap)
  12295. (mailcap-parse-mailcaps)
  12296. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12297. (command (mailcap-mime-info mime-type)))
  12298. (if (stringp command)
  12299. (setq cmd command)
  12300. (setq cmd 'emacs))))
  12301. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12302. (not (file-exists-p file))
  12303. (not org-open-non-existing-files))
  12304. (error "No such file: %s" file))
  12305. (cond
  12306. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12307. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12308. (while (string-match "['\"]%s['\"]" cmd)
  12309. (setq cmd (replace-match "%s" t t cmd)))
  12310. (while (string-match "%s" cmd)
  12311. (setq cmd (replace-match
  12312. (save-match-data (shell-quote-argument file))
  12313. t t cmd)))
  12314. (save-window-excursion
  12315. (start-process-shell-command cmd nil cmd)))
  12316. ((or (stringp cmd)
  12317. (eq cmd 'emacs))
  12318. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12319. (widen)
  12320. (if line (goto-line line)
  12321. (if search (org-link-search search))))
  12322. ((consp cmd)
  12323. (eval cmd))
  12324. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12325. (and (org-mode-p) (eq old-mode 'org-mode)
  12326. (or (not (equal old-buffer (current-buffer)))
  12327. (not (equal old-pos (point))))
  12328. (org-mark-ring-push old-pos old-buffer))))
  12329. (defun org-default-apps ()
  12330. "Return the default applications for this operating system."
  12331. (cond
  12332. ((eq system-type 'darwin)
  12333. org-file-apps-defaults-macosx)
  12334. ((eq system-type 'windows-nt)
  12335. org-file-apps-defaults-windowsnt)
  12336. (t org-file-apps-defaults-gnu)))
  12337. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12338. (defun org-file-remote-p (file)
  12339. "Test whether FILE specifies a location on a remote system.
  12340. Return non-nil if the location is indeed remote.
  12341. For example, the filename \"/user@host:/foo\" specifies a location
  12342. on the system \"/user@host:\"."
  12343. (cond ((fboundp 'file-remote-p)
  12344. (file-remote-p file))
  12345. ((fboundp 'tramp-handle-file-remote-p)
  12346. (tramp-handle-file-remote-p file))
  12347. ((and (boundp 'ange-ftp-name-format)
  12348. (string-match (car ange-ftp-name-format) file))
  12349. t)
  12350. (t nil)))
  12351. ;;;; Hooks for remember.el, and refiling
  12352. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12353. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12354. ;;;###autoload
  12355. (defun org-remember-insinuate ()
  12356. "Setup remember.el for use wiht Org-mode."
  12357. (require 'remember)
  12358. (setq remember-annotation-functions '(org-remember-annotation))
  12359. (setq remember-handler-functions '(org-remember-handler))
  12360. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12361. ;;;###autoload
  12362. (defun org-remember-annotation ()
  12363. "Return a link to the current location as an annotation for remember.el.
  12364. If you are using Org-mode files as target for data storage with
  12365. remember.el, then the annotations should include a link compatible with the
  12366. conventions in Org-mode. This function returns such a link."
  12367. (org-store-link nil))
  12368. (defconst org-remember-help
  12369. "Select a destination location for the note.
  12370. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12371. RET on headline -> Store as sublevel entry to current headline
  12372. RET at beg-of-buf -> Append to file as level 2 headline
  12373. <left>/<right> -> before/after current headline, same headings level")
  12374. (defvar org-remember-previous-location nil)
  12375. (defvar org-force-remember-template-char) ;; dynamically scoped
  12376. (defun org-select-remember-template (&optional use-char)
  12377. (when org-remember-templates
  12378. (let* ((templates (mapcar (lambda (x)
  12379. (if (stringp (car x))
  12380. (append (list (nth 1 x) (car x)) (cddr x))
  12381. (append (list (car x) "") (cdr x))))
  12382. org-remember-templates))
  12383. (char (or use-char
  12384. (cond
  12385. ((= (length templates) 1)
  12386. (caar templates))
  12387. ((and (boundp 'org-force-remember-template-char)
  12388. org-force-remember-template-char)
  12389. (if (stringp org-force-remember-template-char)
  12390. (string-to-char org-force-remember-template-char)
  12391. org-force-remember-template-char))
  12392. (t
  12393. (message "Select template: %s"
  12394. (mapconcat
  12395. (lambda (x)
  12396. (cond
  12397. ((not (string-match "\\S-" (nth 1 x)))
  12398. (format "[%c]" (car x)))
  12399. ((equal (downcase (car x))
  12400. (downcase (aref (nth 1 x) 0)))
  12401. (format "[%c]%s" (car x)
  12402. (substring (nth 1 x) 1)))
  12403. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12404. templates " "))
  12405. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12406. (when (equal char0 ?\C-g)
  12407. (jump-to-register remember-register)
  12408. (kill-buffer remember-buffer))
  12409. char0))))))
  12410. (cddr (assoc char templates)))))
  12411. (defvar x-last-selected-text)
  12412. (defvar x-last-selected-text-primary)
  12413. ;;;###autoload
  12414. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12415. "Initialize *remember* buffer with template, invoke `org-mode'.
  12416. This function should be placed into `remember-mode-hook' and in fact requires
  12417. to be run from that hook to function properly."
  12418. (if org-remember-templates
  12419. (let* ((entry (org-select-remember-template use-char))
  12420. (tpl (car entry))
  12421. (plist-p (if org-store-link-plist t nil))
  12422. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12423. (string-match "\\S-" (nth 1 entry)))
  12424. (nth 1 entry)
  12425. org-default-notes-file))
  12426. (headline (nth 2 entry))
  12427. (v-c (or (and (eq window-system 'x)
  12428. (fboundp 'x-cut-buffer-or-selection-value)
  12429. (x-cut-buffer-or-selection-value))
  12430. (org-bound-and-true-p x-last-selected-text)
  12431. (org-bound-and-true-p x-last-selected-text-primary)
  12432. (and (> (length kill-ring) 0) (current-kill 0))))
  12433. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12434. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12435. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12436. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12437. ;; `initial' and `annotation' are bound in `remember'
  12438. (v-i (if (boundp 'initial) initial))
  12439. (v-a (if (and (boundp 'annotation) annotation)
  12440. (if (equal annotation "[[]]") "" annotation)
  12441. ""))
  12442. (v-A (if (and v-a
  12443. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12444. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12445. v-a))
  12446. (v-n user-full-name)
  12447. (org-startup-folded nil)
  12448. org-time-was-given org-end-time-was-given x
  12449. prompt completions char time pos default histvar)
  12450. (setq org-store-link-plist
  12451. (append (list :annotation v-a :initial v-i)
  12452. org-store-link-plist))
  12453. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12454. (erase-buffer)
  12455. (insert (substitute-command-keys
  12456. (format
  12457. "## Filing location: Select interactively, default, or last used:
  12458. ## %s to select file and header location interactively.
  12459. ## %s \"%s\" -> \"* %s\"
  12460. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12461. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12462. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12463. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12464. (abbreviate-file-name (or file org-default-notes-file))
  12465. (or headline "")
  12466. (or (car org-remember-previous-location) "???")
  12467. (or (cdr org-remember-previous-location) "???"))))
  12468. (insert tpl) (goto-char (point-min))
  12469. ;; Simple %-escapes
  12470. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12471. (when (and initial (equal (match-string 0) "%i"))
  12472. (save-match-data
  12473. (let* ((lead (buffer-substring
  12474. (point-at-bol) (match-beginning 0))))
  12475. (setq v-i (mapconcat 'identity
  12476. (org-split-string initial "\n")
  12477. (concat "\n" lead))))))
  12478. (replace-match
  12479. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12480. t t))
  12481. ;; %[] Insert contents of a file.
  12482. (goto-char (point-min))
  12483. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12484. (let ((start (match-beginning 0))
  12485. (end (match-end 0))
  12486. (filename (expand-file-name (match-string 1))))
  12487. (goto-char start)
  12488. (delete-region start end)
  12489. (condition-case error
  12490. (insert-file-contents filename)
  12491. (error (insert (format "%%![Couldn't insert %s: %s]"
  12492. filename error))))))
  12493. ;; %() embedded elisp
  12494. (goto-char (point-min))
  12495. (while (re-search-forward "%\\((.+)\\)" nil t)
  12496. (goto-char (match-beginning 0))
  12497. (let ((template-start (point)))
  12498. (forward-char 1)
  12499. (let ((result
  12500. (condition-case error
  12501. (eval (read (current-buffer)))
  12502. (error (format "%%![Error: %s]" error)))))
  12503. (delete-region template-start (point))
  12504. (insert result))))
  12505. ;; From the property list
  12506. (when plist-p
  12507. (goto-char (point-min))
  12508. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12509. (and (setq x (or (plist-get org-store-link-plist
  12510. (intern (match-string 1))) ""))
  12511. (replace-match x t t))))
  12512. ;; Turn on org-mode in the remember buffer, set local variables
  12513. (org-mode)
  12514. (org-set-local 'org-finish-function 'org-remember-finalize)
  12515. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12516. (org-set-local 'org-default-notes-file file))
  12517. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12518. (org-set-local 'org-remember-default-headline headline))
  12519. ;; Interactive template entries
  12520. (goto-char (point-min))
  12521. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12522. (setq char (if (match-end 3) (match-string 3))
  12523. prompt (if (match-end 2) (match-string 2)))
  12524. (goto-char (match-beginning 0))
  12525. (replace-match "")
  12526. (setq completions nil default nil)
  12527. (when prompt
  12528. (setq completions (org-split-string prompt "|")
  12529. prompt (pop completions)
  12530. default (car completions)
  12531. histvar (intern (concat
  12532. "org-remember-template-prompt-history::"
  12533. (or prompt "")))
  12534. completions (mapcar 'list completions)))
  12535. (cond
  12536. ((member char '("G" "g"))
  12537. (let* ((org-last-tags-completion-table
  12538. (org-global-tags-completion-table
  12539. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12540. (org-add-colon-after-tag-completion t)
  12541. (ins (completing-read
  12542. (if prompt (concat prompt ": ") "Tags: ")
  12543. 'org-tags-completion-function nil nil nil
  12544. 'org-tags-history)))
  12545. (setq ins (mapconcat 'identity
  12546. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12547. ":"))
  12548. (when (string-match "\\S-" ins)
  12549. (or (equal (char-before) ?:) (insert ":"))
  12550. (insert ins)
  12551. (or (equal (char-after) ?:) (insert ":")))))
  12552. (char
  12553. (setq org-time-was-given (equal (upcase char) char))
  12554. (setq time (org-read-date (equal (upcase char) "U") t nil
  12555. prompt))
  12556. (org-insert-time-stamp time org-time-was-given
  12557. (member char '("u" "U"))
  12558. nil nil (list org-end-time-was-given)))
  12559. (t
  12560. (insert (org-completing-read
  12561. (concat (if prompt prompt "Enter string")
  12562. (if default (concat " [" default "]"))
  12563. ": ")
  12564. completions nil nil nil histvar default)))))
  12565. (goto-char (point-min))
  12566. (if (re-search-forward "%\\?" nil t)
  12567. (replace-match "")
  12568. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12569. (org-mode)
  12570. (org-set-local 'org-finish-function 'org-remember-finalize))
  12571. (when (save-excursion
  12572. (goto-char (point-min))
  12573. (re-search-forward "%!" nil t))
  12574. (replace-match "")
  12575. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12576. (defun org-remember-finish-immediately ()
  12577. "File remember note immediately.
  12578. This should be run in `post-command-hook' and will remove itself
  12579. from that hook."
  12580. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12581. (when org-finish-function
  12582. (funcall org-finish-function)))
  12583. (defvar org-clock-marker) ; Defined below
  12584. (defun org-remember-finalize ()
  12585. "Finalize the remember process."
  12586. (unless (fboundp 'remember-finalize)
  12587. (defalias 'remember-finalize 'remember-buffer))
  12588. (when (and org-clock-marker
  12589. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12590. ;; FIXME: test this, this is w/o notetaking!
  12591. (let (org-log-note-clock-out) (org-clock-out)))
  12592. (when buffer-file-name
  12593. (save-buffer)
  12594. (setq buffer-file-name nil))
  12595. (remember-finalize))
  12596. ;;;###autoload
  12597. (defun org-remember (&optional goto org-force-remember-template-char)
  12598. "Call `remember'. If this is already a remember buffer, re-apply template.
  12599. If there is an active region, make sure remember uses it as initial content
  12600. of the remember buffer.
  12601. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12602. anything, just go to the file/headline where the selected template usually
  12603. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12604. note stored by remember.
  12605. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12606. associated with a template in `org-remember-templates'."
  12607. (interactive "P")
  12608. (cond
  12609. ((equal goto '(4)) (org-go-to-remember-target))
  12610. ((equal goto '(16)) (org-remember-goto-last-stored))
  12611. (t
  12612. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12613. (progn
  12614. (when (< (length org-remember-templates) 2)
  12615. (error "No other template available"))
  12616. (erase-buffer)
  12617. (let ((annotation (plist-get org-store-link-plist :annotation))
  12618. (initial (plist-get org-store-link-plist :initial)))
  12619. (org-remember-apply-template))
  12620. (message "Press C-c C-c to remember data"))
  12621. (if (org-region-active-p)
  12622. (remember (buffer-substring (point) (mark)))
  12623. (call-interactively 'remember))))))
  12624. (defun org-remember-goto-last-stored ()
  12625. "Go to the location where the last remember note was stored."
  12626. (interactive)
  12627. (bookmark-jump "org-remember-last-stored")
  12628. (message "This is the last note stored by remember"))
  12629. (defun org-go-to-remember-target (&optional template-key)
  12630. "Go to the target location of a remember template.
  12631. The user is queried for the template."
  12632. (interactive)
  12633. (let* ((entry (org-select-remember-template template-key))
  12634. (file (nth 1 entry))
  12635. (heading (nth 2 entry))
  12636. visiting)
  12637. (unless (and file (stringp file) (string-match "\\S-" file))
  12638. (setq file org-default-notes-file))
  12639. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12640. (setq heading org-remember-default-headline))
  12641. (setq visiting (org-find-base-buffer-visiting file))
  12642. (if (not visiting) (find-file-noselect file))
  12643. (switch-to-buffer (or visiting (get-file-buffer file)))
  12644. (widen)
  12645. (goto-char (point-min))
  12646. (if (re-search-forward
  12647. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12648. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12649. nil t)
  12650. (goto-char (match-beginning 0))
  12651. (error "Target headline not found: %s" heading))))
  12652. (defvar org-note-abort nil) ; dynamically scoped
  12653. ;;;###autoload
  12654. (defun org-remember-handler ()
  12655. "Store stuff from remember.el into an org file.
  12656. First prompts for an org file. If the user just presses return, the value
  12657. of `org-default-notes-file' is used.
  12658. Then the command offers the headings tree of the selected file in order to
  12659. file the text at a specific location.
  12660. You can either immediately press RET to get the note appended to the
  12661. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12662. find a better place. Then press RET or <left> or <right> in insert the note.
  12663. Key Cursor position Note gets inserted
  12664. -----------------------------------------------------------------------------
  12665. RET buffer-start as level 1 heading at end of file
  12666. RET on headline as sublevel of the heading at cursor
  12667. RET no heading at cursor position, level taken from context.
  12668. Or use prefix arg to specify level manually.
  12669. <left> on headline as same level, before current heading
  12670. <right> on headline as same level, after current heading
  12671. So the fastest way to store the note is to press RET RET to append it to
  12672. the default file. This way your current train of thought is not
  12673. interrupted, in accordance with the principles of remember.el.
  12674. You can also get the fast execution without prompting by using
  12675. C-u C-c C-c to exit the remember buffer. See also the variable
  12676. `org-remember-store-without-prompt'.
  12677. Before being stored away, the function ensures that the text has a
  12678. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12679. is constructed from the current date and some additional data.
  12680. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12681. also indented so that it starts in the same column as the headline
  12682. \(i.e. after the stars).
  12683. See also the variable `org-reverse-note-order'."
  12684. (goto-char (point-min))
  12685. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12686. (replace-match ""))
  12687. (goto-char (point-max))
  12688. (beginning-of-line 1)
  12689. (while (looking-at "[ \t]*$\\|##.*")
  12690. (delete-region (1- (point)) (point-max))
  12691. (beginning-of-line 1))
  12692. (catch 'quit
  12693. (if org-note-abort (throw 'quit nil))
  12694. (let* ((txt (buffer-substring (point-min) (point-max)))
  12695. (fastp (org-xor (equal current-prefix-arg '(4))
  12696. org-remember-store-without-prompt))
  12697. (file (cond
  12698. (fastp org-default-notes-file)
  12699. ((and (eq org-remember-interactive-interface 'refile)
  12700. org-refile-targets)
  12701. org-default-notes-file)
  12702. ((not (and (equal current-prefix-arg '(16))
  12703. org-remember-previous-location))
  12704. (org-get-org-file))))
  12705. (heading org-remember-default-headline)
  12706. (visiting (and file (org-find-base-buffer-visiting file)))
  12707. (org-startup-folded nil)
  12708. (org-startup-align-all-tables nil)
  12709. (org-goto-start-pos 1)
  12710. spos exitcmd level indent reversed)
  12711. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12712. (setq file (car org-remember-previous-location)
  12713. heading (cdr org-remember-previous-location)
  12714. fastp t))
  12715. (setq current-prefix-arg nil)
  12716. (if (string-match "[ \t\n]+\\'" txt)
  12717. (setq txt (replace-match "" t t txt)))
  12718. ;; Modify text so that it becomes a nice subtree which can be inserted
  12719. ;; into an org tree.
  12720. (let* ((lines (split-string txt "\n"))
  12721. first)
  12722. (setq first (car lines) lines (cdr lines))
  12723. (if (string-match "^\\*+ " first)
  12724. ;; Is already a headline
  12725. (setq indent nil)
  12726. ;; We need to add a headline: Use time and first buffer line
  12727. (setq lines (cons first lines)
  12728. first (concat "* " (current-time-string)
  12729. " (" (remember-buffer-desc) ")")
  12730. indent " "))
  12731. (if (and org-adapt-indentation indent)
  12732. (setq lines (mapcar
  12733. (lambda (x)
  12734. (if (string-match "\\S-" x)
  12735. (concat indent x) x))
  12736. lines)))
  12737. (setq txt (concat first "\n"
  12738. (mapconcat 'identity lines "\n"))))
  12739. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12740. (setq txt (replace-match "\n\n" t t txt))
  12741. (if (string-match "[ \t\n]*\\'" txt)
  12742. (setq txt (replace-match "\n" t t txt))))
  12743. ;; Put the modified text back into the remember buffer, for refile.
  12744. (erase-buffer)
  12745. (insert txt)
  12746. (goto-char (point-min))
  12747. (when (and (eq org-remember-interactive-interface 'refile)
  12748. (not fastp))
  12749. (org-refile nil (or visiting (find-file-noselect file)))
  12750. (throw 'quit t))
  12751. ;; Find the file
  12752. (if (not visiting) (find-file-noselect file))
  12753. (with-current-buffer (or visiting (get-file-buffer file))
  12754. (unless (org-mode-p)
  12755. (error "Target files for remember notes must be in Org-mode"))
  12756. (save-excursion
  12757. (save-restriction
  12758. (widen)
  12759. (and (goto-char (point-min))
  12760. (not (re-search-forward "^\\* " nil t))
  12761. (insert "\n* " (or heading "Notes") "\n"))
  12762. (setq reversed (org-notes-order-reversed-p))
  12763. ;; Find the default location
  12764. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12765. (goto-char (point-min))
  12766. (if (re-search-forward
  12767. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12768. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12769. nil t)
  12770. (setq org-goto-start-pos (match-beginning 0))
  12771. (when fastp
  12772. (goto-char (point-max))
  12773. (unless (bolp) (newline))
  12774. (insert "* " heading "\n")
  12775. (setq org-goto-start-pos (point-at-bol 0)))))
  12776. ;; Ask the User for a location, using the appropriate interface
  12777. (cond
  12778. (fastp (setq spos org-goto-start-pos
  12779. exitcmd 'return))
  12780. ((eq org-remember-interactive-interface 'outline)
  12781. (setq spos (org-get-location (current-buffer)
  12782. org-remember-help)
  12783. exitcmd (cdr spos)
  12784. spos (car spos)))
  12785. ((eq org-remember-interactive-interface 'outline-path-completion)
  12786. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12787. (org-refile-use-outline-path t))
  12788. (setq spos (org-refile-get-location "Heading: ")
  12789. exitcmd 'return
  12790. spos (nth 3 spos))))
  12791. (t (error "this should not hapen")))
  12792. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12793. ; not handle this note
  12794. (goto-char spos)
  12795. (cond ((org-on-heading-p t)
  12796. (org-back-to-heading t)
  12797. (setq level (funcall outline-level))
  12798. (cond
  12799. ((eq exitcmd 'return)
  12800. ;; sublevel of current
  12801. (setq org-remember-previous-location
  12802. (cons (abbreviate-file-name file)
  12803. (org-get-heading 'notags)))
  12804. (if reversed
  12805. (outline-next-heading)
  12806. (org-end-of-subtree t)
  12807. (if (not (bolp))
  12808. (if (looking-at "[ \t]*\n")
  12809. (beginning-of-line 2)
  12810. (end-of-line 1)
  12811. (insert "\n"))))
  12812. (bookmark-set "org-remember-last-stored")
  12813. (org-paste-subtree (org-get-legal-level level 1) txt))
  12814. ((eq exitcmd 'left)
  12815. ;; before current
  12816. (bookmark-set "org-remember-last-stored")
  12817. (org-paste-subtree level txt))
  12818. ((eq exitcmd 'right)
  12819. ;; after current
  12820. (org-end-of-subtree t)
  12821. (bookmark-set "org-remember-last-stored")
  12822. (org-paste-subtree level txt))
  12823. (t (error "This should not happen"))))
  12824. ((and (bobp) (not reversed))
  12825. ;; Put it at the end, one level below level 1
  12826. (save-restriction
  12827. (widen)
  12828. (goto-char (point-max))
  12829. (if (not (bolp)) (newline))
  12830. (bookmark-set "org-remember-last-stored")
  12831. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12832. ((and (bobp) reversed)
  12833. ;; Put it at the start, as level 1
  12834. (save-restriction
  12835. (widen)
  12836. (goto-char (point-min))
  12837. (re-search-forward "^\\*+ " nil t)
  12838. (beginning-of-line 1)
  12839. (bookmark-set "org-remember-last-stored")
  12840. (org-paste-subtree 1 txt)))
  12841. (t
  12842. ;; Put it right there, with automatic level determined by
  12843. ;; org-paste-subtree or from prefix arg
  12844. (bookmark-set "org-remember-last-stored")
  12845. (org-paste-subtree
  12846. (if (numberp current-prefix-arg) current-prefix-arg)
  12847. txt)))
  12848. (when remember-save-after-remembering
  12849. (save-buffer)
  12850. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12851. t) ;; return t to indicate that we took care of this note.
  12852. (defun org-get-org-file ()
  12853. "Read a filename, with default directory `org-directory'."
  12854. (let ((default (or org-default-notes-file remember-data-file)))
  12855. (read-file-name (format "File name [%s]: " default)
  12856. (file-name-as-directory org-directory)
  12857. default)))
  12858. (defun org-notes-order-reversed-p ()
  12859. "Check if the current file should receive notes in reversed order."
  12860. (cond
  12861. ((not org-reverse-note-order) nil)
  12862. ((eq t org-reverse-note-order) t)
  12863. ((not (listp org-reverse-note-order)) nil)
  12864. (t (catch 'exit
  12865. (let ((all org-reverse-note-order)
  12866. entry)
  12867. (while (setq entry (pop all))
  12868. (if (string-match (car entry) buffer-file-name)
  12869. (throw 'exit (cdr entry))))
  12870. nil)))))
  12871. ;;; Refiling
  12872. (defvar org-refile-target-table nil
  12873. "The list of refile targets, created by `org-refile'.")
  12874. (defvar org-agenda-new-buffers nil
  12875. "Buffers created to visit agenda files.")
  12876. (defun org-get-refile-targets (&optional default-buffer)
  12877. "Produce a table with refile targets."
  12878. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12879. targets txt re files f desc descre)
  12880. (with-current-buffer (or default-buffer (current-buffer))
  12881. (while (setq entry (pop entries))
  12882. (setq files (car entry) desc (cdr entry))
  12883. (cond
  12884. ((null files) (setq files (list (current-buffer))))
  12885. ((eq files 'org-agenda-files)
  12886. (setq files (org-agenda-files 'unrestricted)))
  12887. ((and (symbolp files) (fboundp files))
  12888. (setq files (funcall files)))
  12889. ((and (symbolp files) (boundp files))
  12890. (setq files (symbol-value files))))
  12891. (if (stringp files) (setq files (list files)))
  12892. (cond
  12893. ((eq (car desc) :tag)
  12894. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12895. ((eq (car desc) :todo)
  12896. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12897. ((eq (car desc) :regexp)
  12898. (setq descre (cdr desc)))
  12899. ((eq (car desc) :level)
  12900. (setq descre (concat "^\\*\\{" (number-to-string
  12901. (if org-odd-levels-only
  12902. (1- (* 2 (cdr desc)))
  12903. (cdr desc)))
  12904. "\\}[ \t]")))
  12905. ((eq (car desc) :maxlevel)
  12906. (setq descre (concat "^\\*\\{1," (number-to-string
  12907. (if org-odd-levels-only
  12908. (1- (* 2 (cdr desc)))
  12909. (cdr desc)))
  12910. "\\}[ \t]")))
  12911. (t (error "Bad refiling target description %s" desc)))
  12912. (while (setq f (pop files))
  12913. (save-excursion
  12914. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12915. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12916. (save-excursion
  12917. (save-restriction
  12918. (widen)
  12919. (goto-char (point-min))
  12920. (while (re-search-forward descre nil t)
  12921. (goto-char (point-at-bol))
  12922. (when (looking-at org-complex-heading-regexp)
  12923. (setq txt (match-string 4)
  12924. re (concat "^" (regexp-quote
  12925. (buffer-substring (match-beginning 1)
  12926. (match-end 4)))))
  12927. (if (match-end 5) (setq re (concat re "[ \t]+"
  12928. (regexp-quote
  12929. (match-string 5)))))
  12930. (setq re (concat re "[ \t]*$"))
  12931. (when org-refile-use-outline-path
  12932. (setq txt (mapconcat 'identity
  12933. (append
  12934. (if (eq org-refile-use-outline-path 'file)
  12935. (list (file-name-nondirectory
  12936. (buffer-file-name (buffer-base-buffer))))
  12937. (if (eq org-refile-use-outline-path 'full-file-path)
  12938. (list (buffer-file-name (buffer-base-buffer)))))
  12939. (org-get-outline-path)
  12940. (list txt))
  12941. "/")))
  12942. (push (list txt f re (point)) targets))
  12943. (goto-char (point-at-eol))))))))
  12944. (nreverse targets))))
  12945. (defun org-get-outline-path ()
  12946. "Return the outline path to the current entry, as a list."
  12947. (let (rtn)
  12948. (save-excursion
  12949. (while (org-up-heading-safe)
  12950. (when (looking-at org-complex-heading-regexp)
  12951. (push (org-match-string-no-properties 4) rtn)))
  12952. rtn)))
  12953. (defvar org-refile-history nil
  12954. "History for refiling operations.")
  12955. (defun org-refile (&optional goto default-buffer)
  12956. "Move the entry at point to another heading.
  12957. The list of target headings is compiled using the information in
  12958. `org-refile-targets', which see. This list is created upon first use, and
  12959. you can update it by calling this command with a double prefix (`C-u C-u').
  12960. FIXME: Can we find a better way of updating?
  12961. At the target location, the entry is filed as a subitem of the target heading.
  12962. Depending on `org-reverse-note-order', the new subitem will either be the
  12963. first of the last subitem.
  12964. With prefix arg GOTO, the command will only visit the target location,
  12965. not actually move anything.
  12966. With a double prefix `C-c C-c', go to the location where the last refiling
  12967. operation has put the subtree.
  12968. With a double prefix argument, the command can be used to jump to any
  12969. heading in the current buffer."
  12970. (interactive "P")
  12971. (let* ((cbuf (current-buffer))
  12972. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12973. pos it nbuf file re level reversed)
  12974. (if (equal goto '(16))
  12975. (org-refile-goto-last-stored)
  12976. (when (setq it (org-refile-get-location
  12977. (if goto "Goto: " "Refile to: ") default-buffer))
  12978. (setq file (nth 1 it)
  12979. re (nth 2 it)
  12980. pos (nth 3 it))
  12981. (setq nbuf (or (find-buffer-visiting file)
  12982. (find-file-noselect file)))
  12983. (if goto
  12984. (progn
  12985. (switch-to-buffer nbuf)
  12986. (goto-char pos)
  12987. (org-show-context 'org-goto))
  12988. (org-copy-special)
  12989. (save-excursion
  12990. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12991. (find-file-noselect file))))
  12992. (setq reversed (org-notes-order-reversed-p))
  12993. (save-excursion
  12994. (save-restriction
  12995. (widen)
  12996. (goto-char pos)
  12997. (looking-at outline-regexp)
  12998. (setq level (org-get-legal-level (funcall outline-level) 1))
  12999. (goto-char
  13000. (if reversed
  13001. (outline-next-heading)
  13002. (or (save-excursion (outline-get-next-sibling))
  13003. (org-end-of-subtree t t)
  13004. (point-max))))
  13005. (bookmark-set "org-refile-last-stored")
  13006. (org-paste-subtree level))))
  13007. (org-cut-special)
  13008. (message "Entry refiled to \"%s\"" (car it)))))))
  13009. (defun org-refile-goto-last-stored ()
  13010. "Go to the location where the last refile was stored."
  13011. (interactive)
  13012. (bookmark-jump "org-refile-last-stored")
  13013. (message "This is the location of the last refile"))
  13014. (defun org-refile-get-location (&optional prompt default-buffer)
  13015. "Prompt the user for a refile location, using PROMPT."
  13016. (let ((org-refile-targets org-refile-targets)
  13017. (org-refile-use-outline-path org-refile-use-outline-path))
  13018. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  13019. (unless org-refile-target-table
  13020. (error "No refile targets"))
  13021. (let* ((cbuf (current-buffer))
  13022. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13023. (fname (and filename (file-truename filename)))
  13024. (tbl (mapcar
  13025. (lambda (x)
  13026. (if (not (equal fname (file-truename (nth 1 x))))
  13027. (cons (concat (car x) " (" (file-name-nondirectory
  13028. (nth 1 x)) ")")
  13029. (cdr x))
  13030. x))
  13031. org-refile-target-table))
  13032. (completion-ignore-case t))
  13033. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13034. tbl)))
  13035. ;;;; Dynamic blocks
  13036. (defun org-find-dblock (name)
  13037. "Find the first dynamic block with name NAME in the buffer.
  13038. If not found, stay at current position and return nil."
  13039. (let (pos)
  13040. (save-excursion
  13041. (goto-char (point-min))
  13042. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13043. nil t)
  13044. (match-beginning 0))))
  13045. (if pos (goto-char pos))
  13046. pos))
  13047. (defconst org-dblock-start-re
  13048. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13049. "Matches the startline of a dynamic block, with parameters.")
  13050. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13051. "Matches the end of a dyhamic block.")
  13052. (defun org-create-dblock (plist)
  13053. "Create a dynamic block section, with parameters taken from PLIST.
  13054. PLIST must containe a :name entry which is used as name of the block."
  13055. (unless (bolp) (newline))
  13056. (let ((name (plist-get plist :name)))
  13057. (insert "#+BEGIN: " name)
  13058. (while plist
  13059. (if (eq (car plist) :name)
  13060. (setq plist (cddr plist))
  13061. (insert " " (prin1-to-string (pop plist)))))
  13062. (insert "\n\n#+END:\n")
  13063. (beginning-of-line -2)))
  13064. (defun org-prepare-dblock ()
  13065. "Prepare dynamic block for refresh.
  13066. This empties the block, puts the cursor at the insert position and returns
  13067. the property list including an extra property :name with the block name."
  13068. (unless (looking-at org-dblock-start-re)
  13069. (error "Not at a dynamic block"))
  13070. (let* ((begdel (1+ (match-end 0)))
  13071. (name (org-no-properties (match-string 1)))
  13072. (params (append (list :name name)
  13073. (read (concat "(" (match-string 3) ")")))))
  13074. (unless (re-search-forward org-dblock-end-re nil t)
  13075. (error "Dynamic block not terminated"))
  13076. (delete-region begdel (match-beginning 0))
  13077. (goto-char begdel)
  13078. (open-line 1)
  13079. params))
  13080. (defun org-map-dblocks (&optional command)
  13081. "Apply COMMAND to all dynamic blocks in the current buffer.
  13082. If COMMAND is not given, use `org-update-dblock'."
  13083. (let ((cmd (or command 'org-update-dblock))
  13084. pos)
  13085. (save-excursion
  13086. (goto-char (point-min))
  13087. (while (re-search-forward org-dblock-start-re nil t)
  13088. (goto-char (setq pos (match-beginning 0)))
  13089. (condition-case nil
  13090. (funcall cmd)
  13091. (error (message "Error during update of dynamic block")))
  13092. (goto-char pos)
  13093. (unless (re-search-forward org-dblock-end-re nil t)
  13094. (error "Dynamic block not terminated"))))))
  13095. (defun org-dblock-update (&optional arg)
  13096. "User command for updating dynamic blocks.
  13097. Update the dynamic block at point. With prefix ARG, update all dynamic
  13098. blocks in the buffer."
  13099. (interactive "P")
  13100. (if arg
  13101. (org-update-all-dblocks)
  13102. (or (looking-at org-dblock-start-re)
  13103. (org-beginning-of-dblock))
  13104. (org-update-dblock)))
  13105. (defun org-update-dblock ()
  13106. "Update the dynamic block at point
  13107. This means to empty the block, parse for parameters and then call
  13108. the correct writing function."
  13109. (save-window-excursion
  13110. (let* ((pos (point))
  13111. (line (org-current-line))
  13112. (params (org-prepare-dblock))
  13113. (name (plist-get params :name))
  13114. (cmd (intern (concat "org-dblock-write:" name))))
  13115. (message "Updating dynamic block `%s' at line %d..." name line)
  13116. (funcall cmd params)
  13117. (message "Updating dynamic block `%s' at line %d...done" name line)
  13118. (goto-char pos))))
  13119. (defun org-beginning-of-dblock ()
  13120. "Find the beginning of the dynamic block at point.
  13121. Error if there is no scuh block at point."
  13122. (let ((pos (point))
  13123. beg)
  13124. (end-of-line 1)
  13125. (if (and (re-search-backward org-dblock-start-re nil t)
  13126. (setq beg (match-beginning 0))
  13127. (re-search-forward org-dblock-end-re nil t)
  13128. (> (match-end 0) pos))
  13129. (goto-char beg)
  13130. (goto-char pos)
  13131. (error "Not in a dynamic block"))))
  13132. (defun org-update-all-dblocks ()
  13133. "Update all dynamic blocks in the buffer.
  13134. This function can be used in a hook."
  13135. (when (org-mode-p)
  13136. (org-map-dblocks 'org-update-dblock)))
  13137. ;;;; Completion
  13138. (defconst org-additional-option-like-keywords
  13139. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13140. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13141. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13142. (defun org-complete (&optional arg)
  13143. "Perform completion on word at point.
  13144. At the beginning of a headline, this completes TODO keywords as given in
  13145. `org-todo-keywords'.
  13146. If the current word is preceded by a backslash, completes the TeX symbols
  13147. that are supported for HTML support.
  13148. If the current word is preceded by \"#+\", completes special words for
  13149. setting file options.
  13150. In the line after \"#+STARTUP:, complete valid keywords.\"
  13151. At all other locations, this simply calls the value of
  13152. `org-completion-fallback-command'."
  13153. (interactive "P")
  13154. (org-without-partial-completion
  13155. (catch 'exit
  13156. (let* ((end (point))
  13157. (beg1 (save-excursion
  13158. (skip-chars-backward (org-re "[:alnum:]_@"))
  13159. (point)))
  13160. (beg (save-excursion
  13161. (skip-chars-backward "a-zA-Z0-9_:$")
  13162. (point)))
  13163. (confirm (lambda (x) (stringp (car x))))
  13164. (searchhead (equal (char-before beg) ?*))
  13165. (tag (and (equal (char-before beg1) ?:)
  13166. (equal (char-after (point-at-bol)) ?*)))
  13167. (prop (and (equal (char-before beg1) ?:)
  13168. (not (equal (char-after (point-at-bol)) ?*))))
  13169. (texp (equal (char-before beg) ?\\))
  13170. (link (equal (char-before beg) ?\[))
  13171. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13172. beg)
  13173. "#+"))
  13174. (startup (string-match "^#\\+STARTUP:.*"
  13175. (buffer-substring (point-at-bol) (point))))
  13176. (completion-ignore-case opt)
  13177. (type nil)
  13178. (tbl nil)
  13179. (table (cond
  13180. (opt
  13181. (setq type :opt)
  13182. (append
  13183. (mapcar
  13184. (lambda (x)
  13185. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13186. (cons (match-string 2 x) (match-string 1 x)))
  13187. (org-split-string (org-get-current-options) "\n"))
  13188. (mapcar 'list org-additional-option-like-keywords)))
  13189. (startup
  13190. (setq type :startup)
  13191. org-startup-options)
  13192. (link (append org-link-abbrev-alist-local
  13193. org-link-abbrev-alist))
  13194. (texp
  13195. (setq type :tex)
  13196. org-html-entities)
  13197. ((string-match "\\`\\*+[ \t]+\\'"
  13198. (buffer-substring (point-at-bol) beg))
  13199. (setq type :todo)
  13200. (mapcar 'list org-todo-keywords-1))
  13201. (searchhead
  13202. (setq type :searchhead)
  13203. (save-excursion
  13204. (goto-char (point-min))
  13205. (while (re-search-forward org-todo-line-regexp nil t)
  13206. (push (list
  13207. (org-make-org-heading-search-string
  13208. (match-string 3) t))
  13209. tbl)))
  13210. tbl)
  13211. (tag (setq type :tag beg beg1)
  13212. (or org-tag-alist (org-get-buffer-tags)))
  13213. (prop (setq type :prop beg beg1)
  13214. (mapcar 'list (org-buffer-property-keys nil t t)))
  13215. (t (progn
  13216. (call-interactively org-completion-fallback-command)
  13217. (throw 'exit nil)))))
  13218. (pattern (buffer-substring-no-properties beg end))
  13219. (completion (try-completion pattern table confirm)))
  13220. (cond ((eq completion t)
  13221. (if (not (assoc (upcase pattern) table))
  13222. (message "Already complete")
  13223. (if (equal type :opt)
  13224. (insert (substring (cdr (assoc (upcase pattern) table))
  13225. (length pattern)))
  13226. (if (memq type '(:tag :prop)) (insert ":")))))
  13227. ((null completion)
  13228. (message "Can't find completion for \"%s\"" pattern)
  13229. (ding))
  13230. ((not (string= pattern completion))
  13231. (delete-region beg end)
  13232. (if (string-match " +$" completion)
  13233. (setq completion (replace-match "" t t completion)))
  13234. (insert completion)
  13235. (if (get-buffer-window "*Completions*")
  13236. (delete-window (get-buffer-window "*Completions*")))
  13237. (if (assoc completion table)
  13238. (if (eq type :todo) (insert " ")
  13239. (if (memq type '(:tag :prop)) (insert ":"))))
  13240. (if (and (equal type :opt) (assoc completion table))
  13241. (message "%s" (substitute-command-keys
  13242. "Press \\[org-complete] again to insert example settings"))))
  13243. (t
  13244. (message "Making completion list...")
  13245. (let ((list (sort (all-completions pattern table confirm)
  13246. 'string<)))
  13247. (with-output-to-temp-buffer "*Completions*"
  13248. (condition-case nil
  13249. ;; Protection needed for XEmacs and emacs 21
  13250. (display-completion-list list pattern)
  13251. (error (display-completion-list list)))))
  13252. (message "Making completion list...%s" "done")))))))
  13253. ;;;; TODO, DEADLINE, Comments
  13254. (defun org-toggle-comment ()
  13255. "Change the COMMENT state of an entry."
  13256. (interactive)
  13257. (save-excursion
  13258. (org-back-to-heading)
  13259. (let (case-fold-search)
  13260. (if (looking-at (concat outline-regexp
  13261. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13262. (replace-match "" t t nil 1)
  13263. (if (looking-at outline-regexp)
  13264. (progn
  13265. (goto-char (match-end 0))
  13266. (insert org-comment-string " ")))))))
  13267. (defvar org-last-todo-state-is-todo nil
  13268. "This is non-nil when the last TODO state change led to a TODO state.
  13269. If the last change removed the TODO tag or switched to DONE, then
  13270. this is nil.")
  13271. (defvar org-setting-tags nil) ; dynamically skiped
  13272. ;; FIXME: better place
  13273. (defun org-property-or-variable-value (var &optional inherit)
  13274. "Check if there is a property fixing the value of VAR.
  13275. If yes, return this value. If not, return the current value of the variable."
  13276. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13277. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13278. (read prop)
  13279. (symbol-value var))))
  13280. (defun org-parse-local-options (string var)
  13281. "Parse STRING for startup setting relevant for variable VAR."
  13282. (let ((rtn (symbol-value var))
  13283. e opts)
  13284. (save-match-data
  13285. (if (or (not string) (not (string-match "\\S-" string)))
  13286. rtn
  13287. (setq opts (delq nil (mapcar (lambda (x)
  13288. (setq e (assoc x org-startup-options))
  13289. (if (eq (nth 1 e) var) e nil))
  13290. (org-split-string string "[ \t]+"))))
  13291. (if (not opts)
  13292. rtn
  13293. (setq rtn nil)
  13294. (while (setq e (pop opts))
  13295. (if (not (nth 3 e))
  13296. (setq rtn (nth 2 e))
  13297. (if (not (listp rtn)) (setq rtn nil))
  13298. (push (nth 2 e) rtn)))
  13299. rtn)))))
  13300. (defvar org-blocker-hook nil
  13301. "Hook for functions that are allowed to block a state change.
  13302. Each function gets as its single argument a property list, see
  13303. `org-trigger-hook' for more information about this list.
  13304. If any of the functions in this hook returns nil, the state change
  13305. is blocked.")
  13306. (defvar org-trigger-hook nil
  13307. "Hook for functions that are triggered by a state change.
  13308. Each function gets as its single argument a property list with at least
  13309. the following elements:
  13310. (:type type-of-change :position pos-at-entry-start
  13311. :from old-state :to new-state)
  13312. Depending on the type, more properties may be present.
  13313. This mechanism is currently implemented for:
  13314. TODO state changes
  13315. ------------------
  13316. :type todo-state-change
  13317. :from previous state (keyword as a string), or nil
  13318. :to new state (keyword as a string), or nil")
  13319. (defun org-todo (&optional arg)
  13320. "Change the TODO state of an item.
  13321. The state of an item is given by a keyword at the start of the heading,
  13322. like
  13323. *** TODO Write paper
  13324. *** DONE Call mom
  13325. The different keywords are specified in the variable `org-todo-keywords'.
  13326. By default the available states are \"TODO\" and \"DONE\".
  13327. So for this example: when the item starts with TODO, it is changed to DONE.
  13328. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13329. DONE are present, add TODO at the beginning of the heading.
  13330. With C-u prefix arg, use completion to determine the new state.
  13331. With numeric prefix arg, switch to that state.
  13332. For calling through lisp, arg is also interpreted in the following way:
  13333. 'none -> empty state
  13334. \"\"(empty string) -> switch to empty state
  13335. 'done -> switch to DONE
  13336. 'nextset -> switch to the next set of keywords
  13337. 'previousset -> switch to the previous set of keywords
  13338. \"WAITING\" -> switch to the specified keyword, but only if it
  13339. really is a member of `org-todo-keywords'."
  13340. (interactive "P")
  13341. (save-excursion
  13342. (catch 'exit
  13343. (org-back-to-heading)
  13344. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13345. (or (looking-at (concat " +" org-todo-regexp " *"))
  13346. (looking-at " *"))
  13347. (let* ((match-data (match-data))
  13348. (startpos (point-at-bol))
  13349. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13350. (org-log-done org-log-done)
  13351. (org-log-repeat org-log-repeat)
  13352. (org-todo-log-states org-todo-log-states)
  13353. (this (match-string 1))
  13354. (hl-pos (match-beginning 0))
  13355. (head (org-get-todo-sequence-head this))
  13356. (ass (assoc head org-todo-kwd-alist))
  13357. (interpret (nth 1 ass))
  13358. (done-word (nth 3 ass))
  13359. (final-done-word (nth 4 ass))
  13360. (last-state (or this ""))
  13361. (completion-ignore-case t)
  13362. (member (member this org-todo-keywords-1))
  13363. (tail (cdr member))
  13364. (state (cond
  13365. ((and org-todo-key-trigger
  13366. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13367. (and (not arg) org-use-fast-todo-selection
  13368. (not (eq org-use-fast-todo-selection 'prefix)))))
  13369. ;; Use fast selection
  13370. (org-fast-todo-selection))
  13371. ((and (equal arg '(4))
  13372. (or (not org-use-fast-todo-selection)
  13373. (not org-todo-key-trigger)))
  13374. ;; Read a state with completion
  13375. (completing-read "State: " (mapcar (lambda(x) (list x))
  13376. org-todo-keywords-1)
  13377. nil t))
  13378. ((eq arg 'right)
  13379. (if this
  13380. (if tail (car tail) nil)
  13381. (car org-todo-keywords-1)))
  13382. ((eq arg 'left)
  13383. (if (equal member org-todo-keywords-1)
  13384. nil
  13385. (if this
  13386. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13387. org-todo-keywords-1)
  13388. (org-last org-todo-keywords-1))))
  13389. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13390. (setq arg nil))) ; hack to fall back to cycling
  13391. (arg
  13392. ;; user or caller requests a specific state
  13393. (cond
  13394. ((equal arg "") nil)
  13395. ((eq arg 'none) nil)
  13396. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13397. ((eq arg 'nextset)
  13398. (or (car (cdr (member head org-todo-heads)))
  13399. (car org-todo-heads)))
  13400. ((eq arg 'previousset)
  13401. (let ((org-todo-heads (reverse org-todo-heads)))
  13402. (or (car (cdr (member head org-todo-heads)))
  13403. (car org-todo-heads))))
  13404. ((car (member arg org-todo-keywords-1)))
  13405. ((nth (1- (prefix-numeric-value arg))
  13406. org-todo-keywords-1))))
  13407. ((null member) (or head (car org-todo-keywords-1)))
  13408. ((equal this final-done-word) nil) ;; -> make empty
  13409. ((null tail) nil) ;; -> first entry
  13410. ((eq interpret 'sequence)
  13411. (car tail))
  13412. ((memq interpret '(type priority))
  13413. (if (eq this-command last-command)
  13414. (car tail)
  13415. (if (> (length tail) 0)
  13416. (or done-word (car org-done-keywords))
  13417. nil)))
  13418. (t nil)))
  13419. (next (if state (concat " " state " ") " "))
  13420. (change-plist (list :type 'todo-state-change :from this :to state
  13421. :position startpos))
  13422. dolog now-done-p)
  13423. (when org-blocker-hook
  13424. (unless (save-excursion
  13425. (save-match-data
  13426. (run-hook-with-args-until-failure
  13427. 'org-blocker-hook change-plist)))
  13428. (if (interactive-p)
  13429. (error "TODO state change from %s to %s blocked" this state)
  13430. ;; fail silently
  13431. (message "TODO state change from %s to %s blocked" this state)
  13432. (throw 'exit nil))))
  13433. (store-match-data match-data)
  13434. (replace-match next t t)
  13435. (unless (pos-visible-in-window-p hl-pos)
  13436. (message "TODO state changed to %s" (org-trim next)))
  13437. (unless head
  13438. (setq head (org-get-todo-sequence-head state)
  13439. ass (assoc head org-todo-kwd-alist)
  13440. interpret (nth 1 ass)
  13441. done-word (nth 3 ass)
  13442. final-done-word (nth 4 ass)))
  13443. (when (memq arg '(nextset previousset))
  13444. (message "Keyword-Set %d/%d: %s"
  13445. (- (length org-todo-sets) -1
  13446. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13447. (length org-todo-sets)
  13448. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13449. (setq org-last-todo-state-is-todo
  13450. (not (member state org-done-keywords)))
  13451. (setq now-done-p (and (member state org-done-keywords)
  13452. (not (member this org-done-keywords))))
  13453. (and logging (org-local-logging logging))
  13454. (when (and (or org-todo-log-states org-log-done)
  13455. (not (memq arg '(nextset previousset))))
  13456. ;; we need to look at recording a time and note
  13457. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13458. (nth 2 (assoc this org-todo-log-states))))
  13459. (when (and state
  13460. (member state org-not-done-keywords)
  13461. (not (member this org-not-done-keywords)))
  13462. ;; This is now a todo state and was not one before
  13463. ;; If there was a CLOSED time stamp, get rid of it.
  13464. (org-add-planning-info nil nil 'closed))
  13465. (when (and now-done-p org-log-done)
  13466. ;; It is now done, and it was not done before
  13467. (org-add-planning-info 'closed (org-current-time))
  13468. (if (and (not dolog) (eq 'note org-log-done))
  13469. (org-add-log-maybe 'done state 'findpos 'note)))
  13470. (when (and state dolog)
  13471. ;; This is a non-nil state, and we need to log it
  13472. (org-add-log-maybe 'state state 'findpos dolog)))
  13473. ;; Fixup tag positioning
  13474. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13475. (run-hooks 'org-after-todo-state-change-hook)
  13476. (if (and arg (not (member state org-done-keywords)))
  13477. (setq head (org-get-todo-sequence-head state)))
  13478. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13479. ;; Do we need to trigger a repeat?
  13480. (when now-done-p (org-auto-repeat-maybe state))
  13481. ;; Fixup cursor location if close to the keyword
  13482. (if (and (outline-on-heading-p)
  13483. (not (bolp))
  13484. (save-excursion (beginning-of-line 1)
  13485. (looking-at org-todo-line-regexp))
  13486. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13487. (progn
  13488. (goto-char (or (match-end 2) (match-end 1)))
  13489. (just-one-space)))
  13490. (when org-trigger-hook
  13491. (save-excursion
  13492. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13493. (defun org-local-logging (value)
  13494. "Get logging settings from a property VALUE."
  13495. (let* (words w a)
  13496. ;; directly set the variables, they are already local.
  13497. (setq org-log-done nil
  13498. org-log-repeat nil
  13499. org-todo-log-states nil)
  13500. (setq words (org-split-string value))
  13501. (while (setq w (pop words))
  13502. (cond
  13503. ((setq a (assoc w org-startup-options))
  13504. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13505. (set (nth 1 a) (nth 2 a))))
  13506. ((setq a (org-extract-log-state-settings w))
  13507. (and (member (car a) org-todo-keywords-1)
  13508. (push a org-todo-log-states)))))))
  13509. (defun org-get-todo-sequence-head (kwd)
  13510. "Return the head of the TODO sequence to which KWD belongs.
  13511. If KWD is not set, check if there is a text property remembering the
  13512. right sequence."
  13513. (let (p)
  13514. (cond
  13515. ((not kwd)
  13516. (or (get-text-property (point-at-bol) 'org-todo-head)
  13517. (progn
  13518. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13519. nil (point-at-eol)))
  13520. (get-text-property p 'org-todo-head))))
  13521. ((not (member kwd org-todo-keywords-1))
  13522. (car org-todo-keywords-1))
  13523. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13524. (defun org-fast-todo-selection ()
  13525. "Fast TODO keyword selection with single keys.
  13526. Returns the new TODO keyword, or nil if no state change should occur."
  13527. (let* ((fulltable org-todo-key-alist)
  13528. (done-keywords org-done-keywords) ;; needed for the faces.
  13529. (maxlen (apply 'max (mapcar
  13530. (lambda (x)
  13531. (if (stringp (car x)) (string-width (car x)) 0))
  13532. fulltable)))
  13533. (expert nil)
  13534. (fwidth (+ maxlen 3 1 3))
  13535. (ncol (/ (- (window-width) 4) fwidth))
  13536. tg cnt e c tbl
  13537. groups ingroup)
  13538. (save-window-excursion
  13539. (if expert
  13540. (set-buffer (get-buffer-create " *Org todo*"))
  13541. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13542. (erase-buffer)
  13543. (org-set-local 'org-done-keywords done-keywords)
  13544. (setq tbl fulltable cnt 0)
  13545. (while (setq e (pop tbl))
  13546. (cond
  13547. ((equal e '(:startgroup))
  13548. (push '() groups) (setq ingroup t)
  13549. (when (not (= cnt 0))
  13550. (setq cnt 0)
  13551. (insert "\n"))
  13552. (insert "{ "))
  13553. ((equal e '(:endgroup))
  13554. (setq ingroup nil cnt 0)
  13555. (insert "}\n"))
  13556. (t
  13557. (setq tg (car e) c (cdr e))
  13558. (if ingroup (push tg (car groups)))
  13559. (setq tg (org-add-props tg nil 'face
  13560. (org-get-todo-face tg)))
  13561. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13562. (insert "[" c "] " tg (make-string
  13563. (- fwidth 4 (length tg)) ?\ ))
  13564. (when (= (setq cnt (1+ cnt)) ncol)
  13565. (insert "\n")
  13566. (if ingroup (insert " "))
  13567. (setq cnt 0)))))
  13568. (insert "\n")
  13569. (goto-char (point-min))
  13570. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13571. (fit-window-to-buffer))
  13572. (message "[a-z..]:Set [SPC]:clear")
  13573. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13574. (cond
  13575. ((or (= c ?\C-g)
  13576. (and (= c ?q) (not (rassoc c fulltable))))
  13577. (setq quit-flag t))
  13578. ((= c ?\ ) nil)
  13579. ((setq e (rassoc c fulltable) tg (car e))
  13580. tg)
  13581. (t (setq quit-flag t))))))
  13582. (defun org-get-repeat ()
  13583. "Check if tere is a deadline/schedule with repeater in this entry."
  13584. (save-match-data
  13585. (save-excursion
  13586. (org-back-to-heading t)
  13587. (if (re-search-forward
  13588. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13589. (match-string 1)))))
  13590. (defvar org-last-changed-timestamp)
  13591. (defvar org-log-post-message)
  13592. (defvar org-log-note-purpose)
  13593. (defun org-auto-repeat-maybe (done-word)
  13594. "Check if the current headline contains a repeated deadline/schedule.
  13595. If yes, set TODO state back to what it was and change the base date
  13596. of repeating deadline/scheduled time stamps to new date.
  13597. This function is run automatically after each state change to a DONE state."
  13598. ;; last-state is dynamically scoped into this function
  13599. (let* ((repeat (org-get-repeat))
  13600. (aa (assoc last-state org-todo-kwd-alist))
  13601. (interpret (nth 1 aa))
  13602. (head (nth 2 aa))
  13603. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13604. (msg "Entry repeats: ")
  13605. (org-log-done nil)
  13606. (org-todo-log-states nil)
  13607. re type n what ts)
  13608. (when repeat
  13609. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13610. (org-todo (if (eq interpret 'type) last-state head))
  13611. (when (and org-log-repeat
  13612. (or (not (memq 'org-add-log-note
  13613. (default-value 'post-command-hook)))
  13614. (eq org-log-note-purpose 'done)))
  13615. ;; Make sure a note is taken;
  13616. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13617. 'findpos org-log-repeat))
  13618. (org-back-to-heading t)
  13619. (org-add-planning-info nil nil 'closed)
  13620. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13621. org-deadline-time-regexp "\\)\\|\\("
  13622. org-ts-regexp "\\)"))
  13623. (while (re-search-forward
  13624. re (save-excursion (outline-next-heading) (point)) t)
  13625. (setq type (if (match-end 1) org-scheduled-string
  13626. (if (match-end 3) org-deadline-string "Plain:"))
  13627. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13628. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13629. (setq n (string-to-number (match-string 1 ts))
  13630. what (match-string 2 ts))
  13631. (if (equal what "w") (setq n (* n 7) what "d"))
  13632. (org-timestamp-change n (cdr (assoc what whata)))
  13633. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13634. (setq org-log-post-message msg)
  13635. (message "%s" msg))))
  13636. (defun org-show-todo-tree (arg)
  13637. "Make a compact tree which shows all headlines marked with TODO.
  13638. The tree will show the lines where the regexp matches, and all higher
  13639. headlines above the match.
  13640. With a \\[universal-argument] prefix, also show the DONE entries.
  13641. With a numeric prefix N, construct a sparse tree for the Nth element
  13642. of `org-todo-keywords-1'."
  13643. (interactive "P")
  13644. (let ((case-fold-search nil)
  13645. (kwd-re
  13646. (cond ((null arg) org-not-done-regexp)
  13647. ((equal arg '(4))
  13648. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13649. (mapcar 'list org-todo-keywords-1))))
  13650. (concat "\\("
  13651. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13652. "\\)\\>")))
  13653. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13654. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13655. org-todo-keywords-1)))
  13656. (t (error "Invalid prefix argument: %s" arg)))))
  13657. (message "%d TODO entries found"
  13658. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13659. (defun org-deadline (&optional remove)
  13660. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13661. With argument REMOVE, remove any deadline from the item."
  13662. (interactive "P")
  13663. (if remove
  13664. (progn
  13665. (org-remove-timestamp-with-keyword org-deadline-string)
  13666. (message "Item no longer has a deadline."))
  13667. (org-add-planning-info 'deadline nil 'closed)))
  13668. (defun org-schedule (&optional remove)
  13669. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13670. With argument REMOVE, remove any scheduling date from the item."
  13671. (interactive "P")
  13672. (if remove
  13673. (progn
  13674. (org-remove-timestamp-with-keyword org-scheduled-string)
  13675. (message "Item is no longer scheduled."))
  13676. (org-add-planning-info 'scheduled nil 'closed)))
  13677. (defun org-remove-timestamp-with-keyword (keyword)
  13678. "Remove all time stamps with KEYWORD in the current entry."
  13679. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13680. beg)
  13681. (save-excursion
  13682. (org-back-to-heading t)
  13683. (setq beg (point))
  13684. (org-end-of-subtree t t)
  13685. (while (re-search-backward re beg t)
  13686. (replace-match "")
  13687. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13688. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13689. (defun org-add-planning-info (what &optional time &rest remove)
  13690. "Insert new timestamp with keyword in the line directly after the headline.
  13691. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13692. If non is given, the user is prompted for a date.
  13693. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13694. be removed."
  13695. (interactive)
  13696. (let (org-time-was-given org-end-time-was-given)
  13697. (when what (setq time (or time (org-read-date nil 'to-time))))
  13698. (when (and org-insert-labeled-timestamps-at-point
  13699. (member what '(scheduled deadline)))
  13700. (insert
  13701. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13702. (org-insert-time-stamp time org-time-was-given
  13703. nil nil nil (list org-end-time-was-given))
  13704. (setq what nil))
  13705. (save-excursion
  13706. (save-restriction
  13707. (let (col list elt ts buffer-invisibility-spec)
  13708. (org-back-to-heading t)
  13709. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13710. (goto-char (match-end 1))
  13711. (setq col (current-column))
  13712. (goto-char (match-end 0))
  13713. (if (eobp) (insert "\n") (forward-char 1))
  13714. (if (and (not (looking-at outline-regexp))
  13715. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13716. "[^\r\n]*"))
  13717. (not (equal (match-string 1) org-clock-string)))
  13718. (narrow-to-region (match-beginning 0) (match-end 0))
  13719. (insert-before-markers "\n")
  13720. (backward-char 1)
  13721. (narrow-to-region (point) (point))
  13722. (indent-to-column col))
  13723. ;; Check if we have to remove something.
  13724. (setq list (cons what remove))
  13725. (while list
  13726. (setq elt (pop list))
  13727. (goto-char (point-min))
  13728. (when (or (and (eq elt 'scheduled)
  13729. (re-search-forward org-scheduled-time-regexp nil t))
  13730. (and (eq elt 'deadline)
  13731. (re-search-forward org-deadline-time-regexp nil t))
  13732. (and (eq elt 'closed)
  13733. (re-search-forward org-closed-time-regexp nil t)))
  13734. (replace-match "")
  13735. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13736. (if (looking-at " +") (replace-match ""))))
  13737. (goto-char (point-max))
  13738. (when what
  13739. (insert
  13740. (if (not (equal (char-before) ?\ )) " " "")
  13741. (cond ((eq what 'scheduled) org-scheduled-string)
  13742. ((eq what 'deadline) org-deadline-string)
  13743. ((eq what 'closed) org-closed-string))
  13744. " ")
  13745. (setq ts (org-insert-time-stamp
  13746. time
  13747. (or org-time-was-given
  13748. (and (eq what 'closed) org-log-done-with-time))
  13749. (eq what 'closed)
  13750. nil nil (list org-end-time-was-given)))
  13751. (end-of-line 1))
  13752. (goto-char (point-min))
  13753. (widen)
  13754. (if (looking-at "[ \t]+\r?\n")
  13755. (replace-match ""))
  13756. ts)))))
  13757. (defvar org-log-note-marker (make-marker))
  13758. (defvar org-log-note-purpose nil)
  13759. (defvar org-log-note-state nil)
  13760. (defvar org-log-note-how nil)
  13761. (defvar org-log-note-window-configuration nil)
  13762. (defvar org-log-note-return-to (make-marker))
  13763. (defvar org-log-post-message nil
  13764. "Message to be displayed after a log note has been stored.
  13765. The auto-repeater uses this.")
  13766. (defun org-add-log-maybe (&optional purpose state findpos how)
  13767. "Set up the post command hook to take a note.
  13768. If this is about to TODO state change, the new state is expected in STATE.
  13769. When FINDPOS is non-nil, find the correct position for the note in
  13770. the current entry. If not, assume that it can be inserted at point."
  13771. (save-excursion
  13772. (when findpos
  13773. (org-back-to-heading t)
  13774. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13775. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13776. "[^\r\n]*\\)?"))
  13777. (goto-char (match-end 0))
  13778. (unless org-log-states-order-reversed
  13779. (and (= (char-after) ?\n) (forward-char 1))
  13780. (org-skip-over-state-notes)
  13781. (skip-chars-backward " \t\n\r")))
  13782. (move-marker org-log-note-marker (point))
  13783. (setq org-log-note-purpose purpose
  13784. org-log-note-state state
  13785. org-log-note-how how)
  13786. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13787. (defun org-skip-over-state-notes ()
  13788. "Skip past the list of State notes in an entry."
  13789. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13790. (while (looking-at "[ \t]*- State")
  13791. (condition-case nil
  13792. (org-next-item)
  13793. (error (org-end-of-item)))))
  13794. (defun org-add-log-note (&optional purpose)
  13795. "Pop up a window for taking a note, and add this note later at point."
  13796. (remove-hook 'post-command-hook 'org-add-log-note)
  13797. (setq org-log-note-window-configuration (current-window-configuration))
  13798. (delete-other-windows)
  13799. (move-marker org-log-note-return-to (point))
  13800. (switch-to-buffer (marker-buffer org-log-note-marker))
  13801. (goto-char org-log-note-marker)
  13802. (org-switch-to-buffer-other-window "*Org Note*")
  13803. (erase-buffer)
  13804. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13805. (org-store-log-note)
  13806. (let ((org-inhibit-startup t)) (org-mode))
  13807. (insert (format "# Insert note for %s.
  13808. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13809. (cond
  13810. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13811. ((eq org-log-note-purpose 'done) "closed todo item")
  13812. ((eq org-log-note-purpose 'state)
  13813. (format "state change to \"%s\"" org-log-note-state))
  13814. (t (error "This should not happen")))))
  13815. (org-set-local 'org-finish-function 'org-store-log-note)))
  13816. (defun org-store-log-note ()
  13817. "Finish taking a log note, and insert it to where it belongs."
  13818. (let ((txt (buffer-string))
  13819. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13820. lines ind)
  13821. (kill-buffer (current-buffer))
  13822. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13823. (setq txt (replace-match "" t t txt)))
  13824. (if (string-match "\\s-+\\'" txt)
  13825. (setq txt (replace-match "" t t txt)))
  13826. (setq lines (org-split-string txt "\n"))
  13827. (when (and note (string-match "\\S-" note))
  13828. (setq note
  13829. (org-replace-escapes
  13830. note
  13831. (list (cons "%u" (user-login-name))
  13832. (cons "%U" user-full-name)
  13833. (cons "%t" (format-time-string
  13834. (org-time-stamp-format 'long 'inactive)
  13835. (current-time)))
  13836. (cons "%s" (if org-log-note-state
  13837. (concat "\"" org-log-note-state "\"")
  13838. "")))))
  13839. (if lines (setq note (concat note " \\\\")))
  13840. (push note lines))
  13841. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13842. (when lines
  13843. (save-excursion
  13844. (set-buffer (marker-buffer org-log-note-marker))
  13845. (save-excursion
  13846. (goto-char org-log-note-marker)
  13847. (move-marker org-log-note-marker nil)
  13848. (end-of-line 1)
  13849. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13850. (indent-relative nil)
  13851. (insert "- " (pop lines))
  13852. (org-indent-line-function)
  13853. (beginning-of-line 1)
  13854. (looking-at "[ \t]*")
  13855. (setq ind (concat (match-string 0) " "))
  13856. (end-of-line 1)
  13857. (while lines (insert "\n" ind (pop lines)))))))
  13858. (set-window-configuration org-log-note-window-configuration)
  13859. (with-current-buffer (marker-buffer org-log-note-return-to)
  13860. (goto-char org-log-note-return-to))
  13861. (move-marker org-log-note-return-to nil)
  13862. (and org-log-post-message (message "%s" org-log-post-message)))
  13863. ;; FIXME: what else would be useful?
  13864. ;; - priority
  13865. ;; - date
  13866. (defun org-sparse-tree (&optional arg)
  13867. "Create a sparse tree, prompt for the details.
  13868. This command can create sparse trees. You first need to select the type
  13869. of match used to create the tree:
  13870. t Show entries with a specific TODO keyword.
  13871. T Show entries selected by a tags match.
  13872. p Enter a property name and its value (both with completion on existing
  13873. names/values) and show entries with that property.
  13874. r Show entries matching a regular expression
  13875. d Show deadlines due within `org-deadline-warning-days'."
  13876. (interactive "P")
  13877. (let (ans kwd value)
  13878. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13879. (setq ans (read-char-exclusive))
  13880. (cond
  13881. ((equal ans ?d)
  13882. (call-interactively 'org-check-deadlines))
  13883. ((equal ans ?b)
  13884. (call-interactively 'org-check-before-date))
  13885. ((equal ans ?t)
  13886. (org-show-todo-tree '(4)))
  13887. ((equal ans ?T)
  13888. (call-interactively 'org-tags-sparse-tree))
  13889. ((member ans '(?p ?P))
  13890. (setq kwd (completing-read "Property: "
  13891. (mapcar 'list (org-buffer-property-keys))))
  13892. (setq value (completing-read "Value: "
  13893. (mapcar 'list (org-property-values kwd))))
  13894. (unless (string-match "\\`{.*}\\'" value)
  13895. (setq value (concat "\"" value "\"")))
  13896. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13897. ((member ans '(?r ?R ?/))
  13898. (call-interactively 'org-occur))
  13899. (t (error "No such sparse tree command \"%c\"" ans)))))
  13900. (defvar org-occur-highlights nil)
  13901. (make-variable-buffer-local 'org-occur-highlights)
  13902. (defun org-occur (regexp &optional keep-previous callback)
  13903. "Make a compact tree which shows all matches of REGEXP.
  13904. The tree will show the lines where the regexp matches, and all higher
  13905. headlines above the match. It will also show the heading after the match,
  13906. to make sure editing the matching entry is easy.
  13907. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13908. call to `org-occur' will be kept, to allow stacking of calls to this
  13909. command.
  13910. If CALLBACK is non-nil, it is a function which is called to confirm
  13911. that the match should indeed be shown."
  13912. (interactive "sRegexp: \nP")
  13913. (or keep-previous (org-remove-occur-highlights nil nil t))
  13914. (let ((cnt 0))
  13915. (save-excursion
  13916. (goto-char (point-min))
  13917. (if (or (not keep-previous) ; do not want to keep
  13918. (not org-occur-highlights)) ; no previous matches
  13919. ;; hide everything
  13920. (org-overview))
  13921. (while (re-search-forward regexp nil t)
  13922. (when (or (not callback)
  13923. (save-match-data (funcall callback)))
  13924. (setq cnt (1+ cnt))
  13925. (when org-highlight-sparse-tree-matches
  13926. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13927. (org-show-context 'occur-tree))))
  13928. (when org-remove-highlights-with-change
  13929. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13930. nil 'local))
  13931. (unless org-sparse-tree-open-archived-trees
  13932. (org-hide-archived-subtrees (point-min) (point-max)))
  13933. (run-hooks 'org-occur-hook)
  13934. (if (interactive-p)
  13935. (message "%d match(es) for regexp %s" cnt regexp))
  13936. cnt))
  13937. (defun org-show-context (&optional key)
  13938. "Make sure point and context and visible.
  13939. How much context is shown depends upon the variables
  13940. `org-show-hierarchy-above', `org-show-following-heading'. and
  13941. `org-show-siblings'."
  13942. (let ((heading-p (org-on-heading-p t))
  13943. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13944. (following-p (org-get-alist-option org-show-following-heading key))
  13945. (entry-p (org-get-alist-option org-show-entry-below key))
  13946. (siblings-p (org-get-alist-option org-show-siblings key)))
  13947. (catch 'exit
  13948. ;; Show heading or entry text
  13949. (if (and heading-p (not entry-p))
  13950. (org-flag-heading nil) ; only show the heading
  13951. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13952. (org-show-hidden-entry))) ; show entire entry
  13953. (when following-p
  13954. ;; Show next sibling, or heading below text
  13955. (save-excursion
  13956. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13957. (org-flag-heading nil))))
  13958. (when siblings-p (org-show-siblings))
  13959. (when hierarchy-p
  13960. ;; show all higher headings, possibly with siblings
  13961. (save-excursion
  13962. (while (and (condition-case nil
  13963. (progn (org-up-heading-all 1) t)
  13964. (error nil))
  13965. (not (bobp)))
  13966. (org-flag-heading nil)
  13967. (when siblings-p (org-show-siblings))))))))
  13968. (defun org-reveal (&optional siblings)
  13969. "Show current entry, hierarchy above it, and the following headline.
  13970. This can be used to show a consistent set of context around locations
  13971. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13972. not t for the search context.
  13973. With optional argument SIBLINGS, on each level of the hierarchy all
  13974. siblings are shown. This repairs the tree structure to what it would
  13975. look like when opened with hierarchical calls to `org-cycle'."
  13976. (interactive "P")
  13977. (let ((org-show-hierarchy-above t)
  13978. (org-show-following-heading t)
  13979. (org-show-siblings (if siblings t org-show-siblings)))
  13980. (org-show-context nil)))
  13981. (defun org-highlight-new-match (beg end)
  13982. "Highlight from BEG to END and mark the highlight is an occur headline."
  13983. (let ((ov (org-make-overlay beg end)))
  13984. (org-overlay-put ov 'face 'secondary-selection)
  13985. (push ov org-occur-highlights)))
  13986. (defun org-remove-occur-highlights (&optional beg end noremove)
  13987. "Remove the occur highlights from the buffer.
  13988. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13989. from the `before-change-functions' in the current buffer."
  13990. (interactive)
  13991. (unless org-inhibit-highlight-removal
  13992. (mapc 'org-delete-overlay org-occur-highlights)
  13993. (setq org-occur-highlights nil)
  13994. (unless noremove
  13995. (remove-hook 'before-change-functions
  13996. 'org-remove-occur-highlights 'local))))
  13997. ;;;; Priorities
  13998. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13999. "Regular expression matching the priority indicator.")
  14000. (defvar org-remove-priority-next-time nil)
  14001. (defun org-priority-up ()
  14002. "Increase the priority of the current item."
  14003. (interactive)
  14004. (org-priority 'up))
  14005. (defun org-priority-down ()
  14006. "Decrease the priority of the current item."
  14007. (interactive)
  14008. (org-priority 'down))
  14009. (defun org-priority (&optional action)
  14010. "Change the priority of an item by ARG.
  14011. ACTION can be `set', `up', `down', or a character."
  14012. (interactive)
  14013. (setq action (or action 'set))
  14014. (let (current new news have remove)
  14015. (save-excursion
  14016. (org-back-to-heading)
  14017. (if (looking-at org-priority-regexp)
  14018. (setq current (string-to-char (match-string 2))
  14019. have t)
  14020. (setq current org-default-priority))
  14021. (cond
  14022. ((or (eq action 'set) (integerp action))
  14023. (if (integerp action)
  14024. (setq new action)
  14025. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14026. (setq new (read-char-exclusive)))
  14027. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14028. (= (upcase org-lowest-priority) org-lowest-priority))
  14029. (setq new (upcase new)))
  14030. (cond ((equal new ?\ ) (setq remove t))
  14031. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14032. (error "Priority must be between `%c' and `%c'"
  14033. org-highest-priority org-lowest-priority))))
  14034. ((eq action 'up)
  14035. (if (and (not have) (eq last-command this-command))
  14036. (setq new org-lowest-priority)
  14037. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14038. org-default-priority (1- current)))))
  14039. ((eq action 'down)
  14040. (if (and (not have) (eq last-command this-command))
  14041. (setq new org-highest-priority)
  14042. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14043. org-default-priority (1+ current)))))
  14044. (t (error "Invalid action")))
  14045. (if (or (< (upcase new) org-highest-priority)
  14046. (> (upcase new) org-lowest-priority))
  14047. (setq remove t))
  14048. (setq news (format "%c" new))
  14049. (if have
  14050. (if remove
  14051. (replace-match "" t t nil 1)
  14052. (replace-match news t t nil 2))
  14053. (if remove
  14054. (error "No priority cookie found in line")
  14055. (looking-at org-todo-line-regexp)
  14056. (if (match-end 2)
  14057. (progn
  14058. (goto-char (match-end 2))
  14059. (insert " [#" news "]"))
  14060. (goto-char (match-beginning 3))
  14061. (insert "[#" news "] ")))))
  14062. (org-preserve-lc (org-set-tags nil 'align))
  14063. (if remove
  14064. (message "Priority removed")
  14065. (message "Priority of current item set to %s" news))))
  14066. (defun org-get-priority (s)
  14067. "Find priority cookie and return priority."
  14068. (save-match-data
  14069. (if (not (string-match org-priority-regexp s))
  14070. (* 1000 (- org-lowest-priority org-default-priority))
  14071. (* 1000 (- org-lowest-priority
  14072. (string-to-char (match-string 2 s)))))))
  14073. ;;;; Tags
  14074. (defun org-scan-tags (action matcher &optional todo-only)
  14075. "Scan headline tags with inheritance and produce output ACTION.
  14076. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14077. evaluated, testing if a given set of tags qualifies a headline for
  14078. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14079. are included in the output."
  14080. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14081. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14082. (org-re
  14083. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14084. (props (list 'face nil
  14085. 'done-face 'org-done
  14086. 'undone-face nil
  14087. 'mouse-face 'highlight
  14088. 'org-not-done-regexp org-not-done-regexp
  14089. 'org-todo-regexp org-todo-regexp
  14090. 'keymap org-agenda-keymap
  14091. 'help-echo
  14092. (format "mouse-2 or RET jump to org file %s"
  14093. (abbreviate-file-name
  14094. (or (buffer-file-name (buffer-base-buffer))
  14095. (buffer-name (buffer-base-buffer)))))))
  14096. (case-fold-search nil)
  14097. lspos
  14098. tags tags-list tags-alist (llast 0) rtn level category i txt
  14099. todo marker entry priority)
  14100. (save-excursion
  14101. (goto-char (point-min))
  14102. (when (eq action 'sparse-tree)
  14103. (org-overview)
  14104. (org-remove-occur-highlights))
  14105. (while (re-search-forward re nil t)
  14106. (catch :skip
  14107. (setq todo (if (match-end 1) (match-string 2))
  14108. tags (if (match-end 4) (match-string 4)))
  14109. (goto-char (setq lspos (1+ (match-beginning 0))))
  14110. (setq level (org-reduced-level (funcall outline-level))
  14111. category (org-get-category))
  14112. (setq i llast llast level)
  14113. ;; remove tag lists from same and sublevels
  14114. (while (>= i level)
  14115. (when (setq entry (assoc i tags-alist))
  14116. (setq tags-alist (delete entry tags-alist)))
  14117. (setq i (1- i)))
  14118. ;; add the nex tags
  14119. (when tags
  14120. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14121. tags-alist
  14122. (cons (cons level tags) tags-alist)))
  14123. ;; compile tags for current headline
  14124. (setq tags-list
  14125. (if org-use-tag-inheritance
  14126. (apply 'append (mapcar 'cdr tags-alist))
  14127. tags))
  14128. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14129. (eval matcher)
  14130. (or (not org-agenda-skip-archived-trees)
  14131. (not (member org-archive-tag tags-list))))
  14132. (and (eq action 'agenda) (org-agenda-skip))
  14133. ;; list this headline
  14134. (if (eq action 'sparse-tree)
  14135. (progn
  14136. (and org-highlight-sparse-tree-matches
  14137. (org-get-heading) (match-end 0)
  14138. (org-highlight-new-match
  14139. (match-beginning 0) (match-beginning 1)))
  14140. (org-show-context 'tags-tree))
  14141. (setq txt (org-format-agenda-item
  14142. ""
  14143. (concat
  14144. (if org-tags-match-list-sublevels
  14145. (make-string (1- level) ?.) "")
  14146. (org-get-heading))
  14147. category tags-list)
  14148. priority (org-get-priority txt))
  14149. (goto-char lspos)
  14150. (setq marker (org-agenda-new-marker))
  14151. (org-add-props txt props
  14152. 'org-marker marker 'org-hd-marker marker 'org-category category
  14153. 'priority priority 'type "tagsmatch")
  14154. (push txt rtn))
  14155. ;; if we are to skip sublevels, jump to end of subtree
  14156. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14157. (when (and (eq action 'sparse-tree)
  14158. (not org-sparse-tree-open-archived-trees))
  14159. (org-hide-archived-subtrees (point-min) (point-max)))
  14160. (nreverse rtn)))
  14161. (defvar todo-only) ;; dynamically scoped
  14162. (defun org-tags-sparse-tree (&optional todo-only match)
  14163. "Create a sparse tree according to tags string MATCH.
  14164. MATCH can contain positive and negative selection of tags, like
  14165. \"+WORK+URGENT-WITHBOSS\".
  14166. If optional argument TODO_ONLY is non-nil, only select lines that are
  14167. also TODO lines."
  14168. (interactive "P")
  14169. (org-prepare-agenda-buffers (list (current-buffer)))
  14170. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14171. (defvar org-cached-props nil)
  14172. (defun org-cached-entry-get (pom property)
  14173. (if (or (eq t org-use-property-inheritance)
  14174. (member property org-use-property-inheritance))
  14175. ;; Caching is not possible, check it directly
  14176. (org-entry-get pom property 'inherit)
  14177. ;; Get all properties, so that we can do complicated checks easily
  14178. (cdr (assoc property (or org-cached-props
  14179. (setq org-cached-props
  14180. (org-entry-properties pom)))))))
  14181. (defun org-global-tags-completion-table (&optional files)
  14182. "Return the list of all tags in all agenda buffer/files."
  14183. (save-excursion
  14184. (org-uniquify
  14185. (delq nil
  14186. (apply 'append
  14187. (mapcar
  14188. (lambda (file)
  14189. (set-buffer (find-file-noselect file))
  14190. (append (org-get-buffer-tags)
  14191. (mapcar (lambda (x) (if (stringp (car-safe x))
  14192. (list (car-safe x)) nil))
  14193. org-tag-alist)))
  14194. (if (and files (car files))
  14195. files
  14196. (org-agenda-files))))))))
  14197. (defun org-make-tags-matcher (match)
  14198. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14199. ;; todo-only is scoped dynamically into this function, and the function
  14200. ;; may change it it the matcher asksk for it.
  14201. (unless match
  14202. ;; Get a new match request, with completion
  14203. (let ((org-last-tags-completion-table
  14204. (org-global-tags-completion-table)))
  14205. (setq match (completing-read
  14206. "Match: " 'org-tags-completion-function nil nil nil
  14207. 'org-tags-history))))
  14208. ;; Parse the string and create a lisp form
  14209. (let ((match0 match)
  14210. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14211. minus tag mm
  14212. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14213. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14214. (if (string-match "/+" match)
  14215. ;; match contains also a todo-matching request
  14216. (progn
  14217. (setq tagsmatch (substring match 0 (match-beginning 0))
  14218. todomatch (substring match (match-end 0)))
  14219. (if (string-match "^!" todomatch)
  14220. (setq todo-only t todomatch (substring todomatch 1)))
  14221. (if (string-match "^\\s-*$" todomatch)
  14222. (setq todomatch nil)))
  14223. ;; only matching tags
  14224. (setq tagsmatch match todomatch nil))
  14225. ;; Make the tags matcher
  14226. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14227. (setq tagsmatcher t)
  14228. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14229. (while (setq term (pop orterms))
  14230. (while (and (equal (substring term -1) "\\") orterms)
  14231. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14232. (while (string-match re term)
  14233. (setq minus (and (match-end 1)
  14234. (equal (match-string 1 term) "-"))
  14235. tag (match-string 2 term)
  14236. re-p (equal (string-to-char tag) ?{)
  14237. level-p (match-end 3)
  14238. prop-p (match-end 4)
  14239. mm (cond
  14240. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14241. (level-p `(= level ,(string-to-number
  14242. (match-string 3 term))))
  14243. (prop-p
  14244. (setq pn (match-string 4 term)
  14245. pv (match-string 5 term)
  14246. cat-p (equal pn "CATEGORY")
  14247. re-p (equal (string-to-char pv) ?{)
  14248. pv (substring pv 1 -1))
  14249. (if (equal pn "CATEGORY")
  14250. (setq gv '(get-text-property (point) 'org-category))
  14251. (setq gv `(org-cached-entry-get nil ,pn)))
  14252. (if re-p
  14253. `(string-match ,pv (or ,gv ""))
  14254. `(equal ,pv (or ,gv ""))))
  14255. (t `(member ,(downcase tag) tags-list)))
  14256. mm (if minus (list 'not mm) mm)
  14257. term (substring term (match-end 0)))
  14258. (push mm tagsmatcher))
  14259. (push (if (> (length tagsmatcher) 1)
  14260. (cons 'and tagsmatcher)
  14261. (car tagsmatcher))
  14262. orlist)
  14263. (setq tagsmatcher nil))
  14264. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14265. (setq tagsmatcher
  14266. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14267. ;; Make the todo matcher
  14268. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14269. (setq todomatcher t)
  14270. (setq orterms (org-split-string todomatch "|") orlist nil)
  14271. (while (setq term (pop orterms))
  14272. (while (string-match re term)
  14273. (setq minus (and (match-end 1)
  14274. (equal (match-string 1 term) "-"))
  14275. kwd (match-string 2 term)
  14276. re-p (equal (string-to-char kwd) ?{)
  14277. term (substring term (match-end 0))
  14278. mm (if re-p
  14279. `(string-match ,(substring kwd 1 -1) todo)
  14280. (list 'equal 'todo kwd))
  14281. mm (if minus (list 'not mm) mm))
  14282. (push mm todomatcher))
  14283. (push (if (> (length todomatcher) 1)
  14284. (cons 'and todomatcher)
  14285. (car todomatcher))
  14286. orlist)
  14287. (setq todomatcher nil))
  14288. (setq todomatcher (if (> (length orlist) 1)
  14289. (cons 'or orlist) (car orlist))))
  14290. ;; Return the string and lisp forms of the matcher
  14291. (setq matcher (if todomatcher
  14292. (list 'and tagsmatcher todomatcher)
  14293. tagsmatcher))
  14294. (cons match0 matcher)))
  14295. (defun org-match-any-p (re list)
  14296. "Does re match any element of list?"
  14297. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14298. (delq nil list))
  14299. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14300. (defvar org-tags-overlay (org-make-overlay 1 1))
  14301. (org-detach-overlay org-tags-overlay)
  14302. (defun org-align-tags-here (to-col)
  14303. ;; Assumes that this is a headline
  14304. (let ((pos (point)) (col (current-column)) tags)
  14305. (beginning-of-line 1)
  14306. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14307. (< pos (match-beginning 2)))
  14308. (progn
  14309. (setq tags (match-string 2))
  14310. (goto-char (match-beginning 1))
  14311. (insert " ")
  14312. (delete-region (point) (1+ (match-end 0)))
  14313. (backward-char 1)
  14314. (move-to-column
  14315. (max (1+ (current-column))
  14316. (1+ col)
  14317. (if (> to-col 0)
  14318. to-col
  14319. (- (abs to-col) (length tags))))
  14320. t)
  14321. (insert tags)
  14322. (move-to-column (min (current-column) col) t))
  14323. (goto-char pos))))
  14324. (defun org-set-tags (&optional arg just-align)
  14325. "Set the tags for the current headline.
  14326. With prefix ARG, realign all tags in headings in the current buffer."
  14327. (interactive "P")
  14328. (let* ((re (concat "^" outline-regexp))
  14329. (current (org-get-tags-string))
  14330. (col (current-column))
  14331. (org-setting-tags t)
  14332. table current-tags inherited-tags ; computed below when needed
  14333. tags p0 c0 c1 rpl)
  14334. (if arg
  14335. (save-excursion
  14336. (goto-char (point-min))
  14337. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14338. (while (re-search-forward re nil t)
  14339. (org-set-tags nil t)
  14340. (end-of-line 1)))
  14341. (message "All tags realigned to column %d" org-tags-column))
  14342. (if just-align
  14343. (setq tags current)
  14344. ;; Get a new set of tags from the user
  14345. (save-excursion
  14346. (setq table (or org-tag-alist (org-get-buffer-tags))
  14347. org-last-tags-completion-table table
  14348. current-tags (org-split-string current ":")
  14349. inherited-tags (nreverse
  14350. (nthcdr (length current-tags)
  14351. (nreverse (org-get-tags-at))))
  14352. tags
  14353. (if (or (eq t org-use-fast-tag-selection)
  14354. (and org-use-fast-tag-selection
  14355. (delq nil (mapcar 'cdr table))))
  14356. (org-fast-tag-selection
  14357. current-tags inherited-tags table
  14358. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14359. (let ((org-add-colon-after-tag-completion t))
  14360. (org-trim
  14361. (org-without-partial-completion
  14362. (completing-read "Tags: " 'org-tags-completion-function
  14363. nil nil current 'org-tags-history)))))))
  14364. (while (string-match "[-+&]+" tags)
  14365. ;; No boolean logic, just a list
  14366. (setq tags (replace-match ":" t t tags))))
  14367. (if (string-match "\\`[\t ]*\\'" tags)
  14368. (setq tags "")
  14369. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14370. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14371. ;; Insert new tags at the correct column
  14372. (beginning-of-line 1)
  14373. (cond
  14374. ((and (equal current "") (equal tags "")))
  14375. ((re-search-forward
  14376. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14377. (point-at-eol) t)
  14378. (if (equal tags "")
  14379. (setq rpl "")
  14380. (goto-char (match-beginning 0))
  14381. (setq c0 (current-column) p0 (point)
  14382. c1 (max (1+ c0) (if (> org-tags-column 0)
  14383. org-tags-column
  14384. (- (- org-tags-column) (length tags))))
  14385. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14386. (replace-match rpl t t)
  14387. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14388. tags)
  14389. (t (error "Tags alignment failed")))
  14390. (move-to-column col)
  14391. (unless just-align
  14392. (run-hooks 'org-after-tags-change-hook)))))
  14393. (defun org-change-tag-in-region (beg end tag off)
  14394. "Add or remove TAG for each entry in the region.
  14395. This works in the agenda, and also in an org-mode buffer."
  14396. (interactive
  14397. (list (region-beginning) (region-end)
  14398. (let ((org-last-tags-completion-table
  14399. (if (org-mode-p)
  14400. (org-get-buffer-tags)
  14401. (org-global-tags-completion-table))))
  14402. (completing-read
  14403. "Tag: " 'org-tags-completion-function nil nil nil
  14404. 'org-tags-history))
  14405. (progn
  14406. (message "[s]et or [r]emove? ")
  14407. (equal (read-char-exclusive) ?r))))
  14408. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14409. (let ((agendap (equal major-mode 'org-agenda-mode))
  14410. l1 l2 m buf pos newhead (cnt 0))
  14411. (goto-char end)
  14412. (setq l2 (1- (org-current-line)))
  14413. (goto-char beg)
  14414. (setq l1 (org-current-line))
  14415. (loop for l from l1 to l2 do
  14416. (goto-line l)
  14417. (setq m (get-text-property (point) 'org-hd-marker))
  14418. (when (or (and (org-mode-p) (org-on-heading-p))
  14419. (and agendap m))
  14420. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14421. pos (if agendap m (point)))
  14422. (with-current-buffer buf
  14423. (save-excursion
  14424. (save-restriction
  14425. (goto-char pos)
  14426. (setq cnt (1+ cnt))
  14427. (org-toggle-tag tag (if off 'off 'on))
  14428. (setq newhead (org-get-heading)))))
  14429. (and agendap (org-agenda-change-all-lines newhead m))))
  14430. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14431. (defun org-tags-completion-function (string predicate &optional flag)
  14432. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14433. (confirm (lambda (x) (stringp (car x)))))
  14434. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14435. (setq s1 (match-string 1 string)
  14436. s2 (match-string 2 string))
  14437. (setq s1 "" s2 string))
  14438. (cond
  14439. ((eq flag nil)
  14440. ;; try completion
  14441. (setq rtn (try-completion s2 ctable confirm))
  14442. (if (stringp rtn)
  14443. (setq rtn
  14444. (concat s1 s2 (substring rtn (length s2))
  14445. (if (and org-add-colon-after-tag-completion
  14446. (assoc rtn ctable))
  14447. ":" ""))))
  14448. rtn)
  14449. ((eq flag t)
  14450. ;; all-completions
  14451. (all-completions s2 ctable confirm)
  14452. )
  14453. ((eq flag 'lambda)
  14454. ;; exact match?
  14455. (assoc s2 ctable)))
  14456. ))
  14457. (defun org-fast-tag-insert (kwd tags face &optional end)
  14458. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14459. (insert (format "%-12s" (concat kwd ":"))
  14460. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14461. (or end "")))
  14462. (defun org-fast-tag-show-exit (flag)
  14463. (save-excursion
  14464. (goto-line 3)
  14465. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14466. (replace-match ""))
  14467. (when flag
  14468. (end-of-line 1)
  14469. (move-to-column (- (window-width) 19) t)
  14470. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14471. (defun org-set-current-tags-overlay (current prefix)
  14472. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14473. (if (featurep 'xemacs)
  14474. (org-overlay-display org-tags-overlay (concat prefix s)
  14475. 'secondary-selection)
  14476. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14477. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14478. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14479. "Fast tag selection with single keys.
  14480. CURRENT is the current list of tags in the headline, INHERITED is the
  14481. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14482. possibly with grouping information. TODO-TABLE is a similar table with
  14483. TODO keywords, should these have keys assigned to them.
  14484. If the keys are nil, a-z are automatically assigned.
  14485. Returns the new tags string, or nil to not change the current settings."
  14486. (let* ((fulltable (append table todo-table))
  14487. (maxlen (apply 'max (mapcar
  14488. (lambda (x)
  14489. (if (stringp (car x)) (string-width (car x)) 0))
  14490. fulltable)))
  14491. (buf (current-buffer))
  14492. (expert (eq org-fast-tag-selection-single-key 'expert))
  14493. (buffer-tags nil)
  14494. (fwidth (+ maxlen 3 1 3))
  14495. (ncol (/ (- (window-width) 4) fwidth))
  14496. (i-face 'org-done)
  14497. (c-face 'org-todo)
  14498. tg cnt e c char c1 c2 ntable tbl rtn
  14499. ov-start ov-end ov-prefix
  14500. (exit-after-next org-fast-tag-selection-single-key)
  14501. (done-keywords org-done-keywords)
  14502. groups ingroup)
  14503. (save-excursion
  14504. (beginning-of-line 1)
  14505. (if (looking-at
  14506. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14507. (setq ov-start (match-beginning 1)
  14508. ov-end (match-end 1)
  14509. ov-prefix "")
  14510. (setq ov-start (1- (point-at-eol))
  14511. ov-end (1+ ov-start))
  14512. (skip-chars-forward "^\n\r")
  14513. (setq ov-prefix
  14514. (concat
  14515. (buffer-substring (1- (point)) (point))
  14516. (if (> (current-column) org-tags-column)
  14517. " "
  14518. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14519. (org-move-overlay org-tags-overlay ov-start ov-end)
  14520. (save-window-excursion
  14521. (if expert
  14522. (set-buffer (get-buffer-create " *Org tags*"))
  14523. (delete-other-windows)
  14524. (split-window-vertically)
  14525. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14526. (erase-buffer)
  14527. (org-set-local 'org-done-keywords done-keywords)
  14528. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14529. (org-fast-tag-insert "Current" current c-face "\n\n")
  14530. (org-fast-tag-show-exit exit-after-next)
  14531. (org-set-current-tags-overlay current ov-prefix)
  14532. (setq tbl fulltable char ?a cnt 0)
  14533. (while (setq e (pop tbl))
  14534. (cond
  14535. ((equal e '(:startgroup))
  14536. (push '() groups) (setq ingroup t)
  14537. (when (not (= cnt 0))
  14538. (setq cnt 0)
  14539. (insert "\n"))
  14540. (insert "{ "))
  14541. ((equal e '(:endgroup))
  14542. (setq ingroup nil cnt 0)
  14543. (insert "}\n"))
  14544. (t
  14545. (setq tg (car e) c2 nil)
  14546. (if (cdr e)
  14547. (setq c (cdr e))
  14548. ;; automatically assign a character.
  14549. (setq c1 (string-to-char
  14550. (downcase (substring
  14551. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14552. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14553. (while (or (rassoc char ntable) (rassoc char table))
  14554. (setq char (1+ char)))
  14555. (setq c2 c1))
  14556. (setq c (or c2 char)))
  14557. (if ingroup (push tg (car groups)))
  14558. (setq tg (org-add-props tg nil 'face
  14559. (cond
  14560. ((not (assoc tg table))
  14561. (org-get-todo-face tg))
  14562. ((member tg current) c-face)
  14563. ((member tg inherited) i-face)
  14564. (t nil))))
  14565. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14566. (insert "[" c "] " tg (make-string
  14567. (- fwidth 4 (length tg)) ?\ ))
  14568. (push (cons tg c) ntable)
  14569. (when (= (setq cnt (1+ cnt)) ncol)
  14570. (insert "\n")
  14571. (if ingroup (insert " "))
  14572. (setq cnt 0)))))
  14573. (setq ntable (nreverse ntable))
  14574. (insert "\n")
  14575. (goto-char (point-min))
  14576. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14577. (fit-window-to-buffer))
  14578. (setq rtn
  14579. (catch 'exit
  14580. (while t
  14581. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14582. (if groups " [!] no groups" " [!]groups")
  14583. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14584. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14585. (cond
  14586. ((= c ?\r) (throw 'exit t))
  14587. ((= c ?!)
  14588. (setq groups (not groups))
  14589. (goto-char (point-min))
  14590. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14591. ((= c ?\C-c)
  14592. (if (not expert)
  14593. (org-fast-tag-show-exit
  14594. (setq exit-after-next (not exit-after-next)))
  14595. (setq expert nil)
  14596. (delete-other-windows)
  14597. (split-window-vertically)
  14598. (org-switch-to-buffer-other-window " *Org tags*")
  14599. (and (fboundp 'fit-window-to-buffer)
  14600. (fit-window-to-buffer))))
  14601. ((or (= c ?\C-g)
  14602. (and (= c ?q) (not (rassoc c ntable))))
  14603. (org-detach-overlay org-tags-overlay)
  14604. (setq quit-flag t))
  14605. ((= c ?\ )
  14606. (setq current nil)
  14607. (if exit-after-next (setq exit-after-next 'now)))
  14608. ((= c ?\t)
  14609. (condition-case nil
  14610. (setq tg (completing-read
  14611. "Tag: "
  14612. (or buffer-tags
  14613. (with-current-buffer buf
  14614. (org-get-buffer-tags)))))
  14615. (quit (setq tg "")))
  14616. (when (string-match "\\S-" tg)
  14617. (add-to-list 'buffer-tags (list tg))
  14618. (if (member tg current)
  14619. (setq current (delete tg current))
  14620. (push tg current)))
  14621. (if exit-after-next (setq exit-after-next 'now)))
  14622. ((setq e (rassoc c todo-table) tg (car e))
  14623. (with-current-buffer buf
  14624. (save-excursion (org-todo tg)))
  14625. (if exit-after-next (setq exit-after-next 'now)))
  14626. ((setq e (rassoc c ntable) tg (car e))
  14627. (if (member tg current)
  14628. (setq current (delete tg current))
  14629. (loop for g in groups do
  14630. (if (member tg g)
  14631. (mapc (lambda (x)
  14632. (setq current (delete x current)))
  14633. g)))
  14634. (push tg current))
  14635. (if exit-after-next (setq exit-after-next 'now))))
  14636. ;; Create a sorted list
  14637. (setq current
  14638. (sort current
  14639. (lambda (a b)
  14640. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14641. (if (eq exit-after-next 'now) (throw 'exit t))
  14642. (goto-char (point-min))
  14643. (beginning-of-line 2)
  14644. (delete-region (point) (point-at-eol))
  14645. (org-fast-tag-insert "Current" current c-face)
  14646. (org-set-current-tags-overlay current ov-prefix)
  14647. (while (re-search-forward
  14648. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14649. (setq tg (match-string 1))
  14650. (add-text-properties
  14651. (match-beginning 1) (match-end 1)
  14652. (list 'face
  14653. (cond
  14654. ((member tg current) c-face)
  14655. ((member tg inherited) i-face)
  14656. (t (get-text-property (match-beginning 1) 'face))))))
  14657. (goto-char (point-min)))))
  14658. (org-detach-overlay org-tags-overlay)
  14659. (if rtn
  14660. (mapconcat 'identity current ":")
  14661. nil))))
  14662. (defun org-get-tags-string ()
  14663. "Get the TAGS string in the current headline."
  14664. (unless (org-on-heading-p t)
  14665. (error "Not on a heading"))
  14666. (save-excursion
  14667. (beginning-of-line 1)
  14668. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14669. (org-match-string-no-properties 1)
  14670. "")))
  14671. (defun org-get-tags ()
  14672. "Get the list of tags specified in the current headline."
  14673. (org-split-string (org-get-tags-string) ":"))
  14674. (defun org-get-buffer-tags ()
  14675. "Get a table of all tags used in the buffer, for completion."
  14676. (let (tags)
  14677. (save-excursion
  14678. (goto-char (point-min))
  14679. (while (re-search-forward
  14680. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14681. (when (equal (char-after (point-at-bol 0)) ?*)
  14682. (mapc (lambda (x) (add-to-list 'tags x))
  14683. (org-split-string (org-match-string-no-properties 1) ":")))))
  14684. (mapcar 'list tags)))
  14685. ;;;; Properties
  14686. ;;; Setting and retrieving properties
  14687. (defconst org-special-properties
  14688. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14689. "TIMESTAMP" "TIMESTAMP_IA")
  14690. "The special properties valid in Org-mode.
  14691. These are properties that are not defined in the property drawer,
  14692. but in some other way.")
  14693. (defconst org-default-properties
  14694. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14695. "LOCATION" "LOGGING" "COLUMNS")
  14696. "Some properties that are used by Org-mode for various purposes.
  14697. Being in this list makes sure that they are offered for completion.")
  14698. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14699. "Regular expression matching the first line of a property drawer.")
  14700. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14701. "Regular expression matching the first line of a property drawer.")
  14702. (defun org-property-action ()
  14703. "Do an action on properties."
  14704. (interactive)
  14705. (let (c)
  14706. (org-at-property-p)
  14707. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14708. (setq c (read-char-exclusive))
  14709. (cond
  14710. ((equal c ?s)
  14711. (call-interactively 'org-set-property))
  14712. ((equal c ?d)
  14713. (call-interactively 'org-delete-property))
  14714. ((equal c ?D)
  14715. (call-interactively 'org-delete-property-globally))
  14716. ((equal c ?c)
  14717. (call-interactively 'org-compute-property-at-point))
  14718. (t (error "No such property action %c" c)))))
  14719. (defun org-at-property-p ()
  14720. "Is the cursor in a property line?"
  14721. ;; FIXME: Does not check if we are actually in the drawer.
  14722. ;; FIXME: also returns true on any drawers.....
  14723. ;; This is used by C-c C-c for property action.
  14724. (save-excursion
  14725. (beginning-of-line 1)
  14726. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14727. (defmacro org-with-point-at (pom &rest body)
  14728. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14729. (declare (indent 1) (debug t))
  14730. `(save-excursion
  14731. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14732. (save-excursion
  14733. (goto-char (or pom (point)))
  14734. ,@body)))
  14735. (defun org-get-property-block (&optional beg end force)
  14736. "Return the (beg . end) range of the body of the property drawer.
  14737. BEG and END can be beginning and end of subtree, if not given
  14738. they will be found.
  14739. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14740. (catch 'exit
  14741. (save-excursion
  14742. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14743. (end (or end (progn (outline-next-heading) (point)))))
  14744. (goto-char beg)
  14745. (if (re-search-forward org-property-start-re end t)
  14746. (setq beg (1+ (match-end 0)))
  14747. (if force
  14748. (save-excursion
  14749. (org-insert-property-drawer)
  14750. (setq end (progn (outline-next-heading) (point))))
  14751. (throw 'exit nil))
  14752. (goto-char beg)
  14753. (if (re-search-forward org-property-start-re end t)
  14754. (setq beg (1+ (match-end 0)))))
  14755. (if (re-search-forward org-property-end-re end t)
  14756. (setq end (match-beginning 0))
  14757. (or force (throw 'exit nil))
  14758. (goto-char beg)
  14759. (setq end beg)
  14760. (org-indent-line-function)
  14761. (insert ":END:\n"))
  14762. (cons beg end)))))
  14763. (defun org-entry-properties (&optional pom which)
  14764. "Get all properties of the entry at point-or-marker POM.
  14765. This includes the TODO keyword, the tags, time strings for deadline,
  14766. scheduled, and clocking, and any additional properties defined in the
  14767. entry. The return value is an alist, keys may occur multiple times
  14768. if the property key was used several times.
  14769. POM may also be nil, in which case the current entry is used.
  14770. If WHICH is nil or `all', get all properties. If WHICH is
  14771. `special' or `standard', only get that subclass."
  14772. (setq which (or which 'all))
  14773. (org-with-point-at pom
  14774. (let ((clockstr (substring org-clock-string 0 -1))
  14775. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14776. beg end range props sum-props key value string clocksum)
  14777. (save-excursion
  14778. (when (condition-case nil (org-back-to-heading t) (error nil))
  14779. (setq beg (point))
  14780. (setq sum-props (get-text-property (point) 'org-summaries))
  14781. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14782. (outline-next-heading)
  14783. (setq end (point))
  14784. (when (memq which '(all special))
  14785. ;; Get the special properties, like TODO and tags
  14786. (goto-char beg)
  14787. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14788. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14789. (when (looking-at org-priority-regexp)
  14790. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14791. (when (and (setq value (org-get-tags-string))
  14792. (string-match "\\S-" value))
  14793. (push (cons "TAGS" value) props))
  14794. (when (setq value (org-get-tags-at))
  14795. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14796. props))
  14797. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14798. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14799. string (if (equal key clockstr)
  14800. (org-no-properties
  14801. (org-trim
  14802. (buffer-substring
  14803. (match-beginning 3) (goto-char (point-at-eol)))))
  14804. (substring (org-match-string-no-properties 3) 1 -1)))
  14805. (unless key
  14806. (if (= (char-after (match-beginning 3)) ?\[)
  14807. (setq key "TIMESTAMP_IA")
  14808. (setq key "TIMESTAMP")))
  14809. (when (or (equal key clockstr) (not (assoc key props)))
  14810. (push (cons key string) props)))
  14811. )
  14812. (when (memq which '(all standard))
  14813. ;; Get the standard properties, like :PORP: ...
  14814. (setq range (org-get-property-block beg end))
  14815. (when range
  14816. (goto-char (car range))
  14817. (while (re-search-forward
  14818. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14819. (cdr range) t)
  14820. (setq key (org-match-string-no-properties 1)
  14821. value (org-trim (or (org-match-string-no-properties 2) "")))
  14822. (unless (member key excluded)
  14823. (push (cons key (or value "")) props)))))
  14824. (if clocksum
  14825. (push (cons "CLOCKSUM"
  14826. (org-column-number-to-string (/ (float clocksum) 60.)
  14827. 'add_times))
  14828. props))
  14829. (append sum-props (nreverse props)))))))
  14830. (defun org-entry-get (pom property &optional inherit)
  14831. "Get value of PROPERTY for entry at point-or-marker POM.
  14832. If INHERIT is non-nil and the entry does not have the property,
  14833. then also check higher levels of the hierarchy.
  14834. If the property is present but empty, the return value is the empty string.
  14835. If the property is not present at all, nil is returned."
  14836. (org-with-point-at pom
  14837. (if inherit
  14838. (org-entry-get-with-inheritance property)
  14839. (if (member property org-special-properties)
  14840. ;; We need a special property. Use brute force, get all properties.
  14841. (cdr (assoc property (org-entry-properties nil 'special)))
  14842. (let ((range (org-get-property-block)))
  14843. (if (and range
  14844. (goto-char (car range))
  14845. (re-search-forward
  14846. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14847. (cdr range) t))
  14848. ;; Found the property, return it.
  14849. (if (match-end 1)
  14850. (org-match-string-no-properties 1)
  14851. "")))))))
  14852. (defun org-entry-delete (pom property)
  14853. "Delete the property PROPERTY from entry at point-or-marker POM."
  14854. (org-with-point-at pom
  14855. (if (member property org-special-properties)
  14856. nil ; cannot delete these properties.
  14857. (let ((range (org-get-property-block)))
  14858. (if (and range
  14859. (goto-char (car range))
  14860. (re-search-forward
  14861. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14862. (cdr range) t))
  14863. (progn
  14864. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14865. t)
  14866. nil)))))
  14867. ;; Multi-values properties are properties that contain multiple values
  14868. ;; These values are assumed to be single words, separated by whitespace.
  14869. (defun org-entry-add-to-multivalued-property (pom property value)
  14870. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14871. (let* ((old (org-entry-get pom property))
  14872. (values (and old (org-split-string old "[ \t]"))))
  14873. (unless (member value values)
  14874. (setq values (cons value values))
  14875. (org-entry-put pom property
  14876. (mapconcat 'identity values " ")))))
  14877. (defun org-entry-remove-from-multivalued-property (pom property value)
  14878. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14879. (let* ((old (org-entry-get pom property))
  14880. (values (and old (org-split-string old "[ \t]"))))
  14881. (when (member value values)
  14882. (setq values (delete value values))
  14883. (org-entry-put pom property
  14884. (mapconcat 'identity values " ")))))
  14885. (defun org-entry-member-in-multivalued-property (pom property value)
  14886. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14887. (let* ((old (org-entry-get pom property))
  14888. (values (and old (org-split-string old "[ \t]"))))
  14889. (member value values)))
  14890. (defvar org-entry-property-inherited-from (make-marker))
  14891. (defun org-entry-get-with-inheritance (property)
  14892. "Get entry property, and search higher levels if not present."
  14893. (let (tmp)
  14894. (save-excursion
  14895. (save-restriction
  14896. (widen)
  14897. (catch 'ex
  14898. (while t
  14899. (when (setq tmp (org-entry-get nil property))
  14900. (org-back-to-heading t)
  14901. (move-marker org-entry-property-inherited-from (point))
  14902. (throw 'ex tmp))
  14903. (or (org-up-heading-safe) (throw 'ex nil)))))
  14904. (or tmp (cdr (assoc property org-local-properties))
  14905. (cdr (assoc property org-global-properties))))))
  14906. (defun org-entry-put (pom property value)
  14907. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14908. (org-with-point-at pom
  14909. (org-back-to-heading t)
  14910. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14911. range)
  14912. (cond
  14913. ((equal property "TODO")
  14914. (when (and (stringp value) (string-match "\\S-" value)
  14915. (not (member value org-todo-keywords-1)))
  14916. (error "\"%s\" is not a valid TODO state" value))
  14917. (if (or (not value)
  14918. (not (string-match "\\S-" value)))
  14919. (setq value 'none))
  14920. (org-todo value)
  14921. (org-set-tags nil 'align))
  14922. ((equal property "PRIORITY")
  14923. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14924. (string-to-char value) ?\ ))
  14925. (org-set-tags nil 'align))
  14926. ((equal property "SCHEDULED")
  14927. (if (re-search-forward org-scheduled-time-regexp end t)
  14928. (cond
  14929. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14930. ((eq value 'later) (org-timestamp-change 1 'day))
  14931. (t (call-interactively 'org-schedule)))
  14932. (call-interactively 'org-schedule)))
  14933. ((equal property "DEADLINE")
  14934. (if (re-search-forward org-deadline-time-regexp end t)
  14935. (cond
  14936. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14937. ((eq value 'later) (org-timestamp-change 1 'day))
  14938. (t (call-interactively 'org-deadline)))
  14939. (call-interactively 'org-deadline)))
  14940. ((member property org-special-properties)
  14941. (error "The %s property can not yet be set with `org-entry-put'"
  14942. property))
  14943. (t ; a non-special property
  14944. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14945. (setq range (org-get-property-block beg end 'force))
  14946. (goto-char (car range))
  14947. (if (re-search-forward
  14948. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14949. (progn
  14950. (delete-region (match-beginning 1) (match-end 1))
  14951. (goto-char (match-beginning 1)))
  14952. (goto-char (cdr range))
  14953. (insert "\n")
  14954. (backward-char 1)
  14955. (org-indent-line-function)
  14956. (insert ":" property ":"))
  14957. (and value (insert " " value))
  14958. (org-indent-line-function)))))))
  14959. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14960. "Get all property keys in the current buffer.
  14961. With INCLUDE-SPECIALS, also list the special properties that relect things
  14962. like tags and TODO state.
  14963. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14964. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14965. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14966. formats in the current buffer."
  14967. (let (rtn range cfmt cols s p)
  14968. (save-excursion
  14969. (save-restriction
  14970. (widen)
  14971. (goto-char (point-min))
  14972. (while (re-search-forward org-property-start-re nil t)
  14973. (setq range (org-get-property-block))
  14974. (goto-char (car range))
  14975. (while (re-search-forward
  14976. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14977. (cdr range) t)
  14978. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14979. (outline-next-heading))))
  14980. (when include-specials
  14981. (setq rtn (append org-special-properties rtn)))
  14982. (when include-defaults
  14983. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14984. (when include-columns
  14985. (save-excursion
  14986. (save-restriction
  14987. (widen)
  14988. (goto-char (point-min))
  14989. (while (re-search-forward
  14990. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14991. nil t)
  14992. (setq cfmt (match-string 2) s 0)
  14993. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14994. cfmt s)
  14995. (setq s (match-end 0)
  14996. p (match-string 1 cfmt))
  14997. (unless (or (equal p "ITEM")
  14998. (member p org-special-properties))
  14999. (add-to-list 'rtn (match-string 1 cfmt))))))))
  15000. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  15001. (defun org-property-values (key)
  15002. "Return a list of all values of property KEY."
  15003. (save-excursion
  15004. (save-restriction
  15005. (widen)
  15006. (goto-char (point-min))
  15007. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  15008. values)
  15009. (while (re-search-forward re nil t)
  15010. (add-to-list 'values (org-trim (match-string 1))))
  15011. (delete "" values)))))
  15012. (defun org-insert-property-drawer ()
  15013. "Insert a property drawer into the current entry."
  15014. (interactive)
  15015. (org-back-to-heading t)
  15016. (looking-at outline-regexp)
  15017. (let ((indent (- (match-end 0)(match-beginning 0)))
  15018. (beg (point))
  15019. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15020. end hiddenp)
  15021. (outline-next-heading)
  15022. (setq end (point))
  15023. (goto-char beg)
  15024. (while (re-search-forward re end t))
  15025. (setq hiddenp (org-invisible-p))
  15026. (end-of-line 1)
  15027. (and (equal (char-after) ?\n) (forward-char 1))
  15028. (org-skip-over-state-notes)
  15029. (skip-chars-backward " \t\n\r")
  15030. (if (eq (char-before) ?*) (forward-char 1))
  15031. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15032. (beginning-of-line 0)
  15033. (indent-to-column indent)
  15034. (beginning-of-line 2)
  15035. (indent-to-column indent)
  15036. (beginning-of-line 0)
  15037. (if hiddenp
  15038. (save-excursion
  15039. (org-back-to-heading t)
  15040. (hide-entry))
  15041. (org-flag-drawer t))))
  15042. (defun org-set-property (property value)
  15043. "In the current entry, set PROPERTY to VALUE.
  15044. When called interactively, this will prompt for a property name, offering
  15045. completion on existing and default properties. And then it will prompt
  15046. for a value, offering competion either on allowed values (via an inherited
  15047. xxx_ALL property) or on existing values in other instances of this property
  15048. in the current file."
  15049. (interactive
  15050. (let* ((prop (completing-read
  15051. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15052. (cur (org-entry-get nil prop))
  15053. (allowed (org-property-get-allowed-values nil prop 'table))
  15054. (existing (mapcar 'list (org-property-values prop)))
  15055. (val (if allowed
  15056. (completing-read "Value: " allowed nil 'req-match)
  15057. (completing-read
  15058. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15059. (concat "[" cur "]") "")
  15060. ": ")
  15061. existing nil nil "" nil cur))))
  15062. (list prop (if (equal val "") cur val))))
  15063. (unless (equal (org-entry-get nil property) value)
  15064. (org-entry-put nil property value)))
  15065. (defun org-delete-property (property)
  15066. "In the current entry, delete PROPERTY."
  15067. (interactive
  15068. (let* ((prop (completing-read
  15069. "Property: " (org-entry-properties nil 'standard))))
  15070. (list prop)))
  15071. (message "Property %s %s" property
  15072. (if (org-entry-delete nil property)
  15073. "deleted"
  15074. "was not present in the entry")))
  15075. (defun org-delete-property-globally (property)
  15076. "Remove PROPERTY globally, from all entries."
  15077. (interactive
  15078. (let* ((prop (completing-read
  15079. "Globally remove property: "
  15080. (mapcar 'list (org-buffer-property-keys)))))
  15081. (list prop)))
  15082. (save-excursion
  15083. (save-restriction
  15084. (widen)
  15085. (goto-char (point-min))
  15086. (let ((cnt 0))
  15087. (while (re-search-forward
  15088. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15089. nil t)
  15090. (setq cnt (1+ cnt))
  15091. (replace-match ""))
  15092. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15093. (defvar org-columns-current-fmt-compiled) ; defined below
  15094. (defun org-compute-property-at-point ()
  15095. "Compute the property at point.
  15096. This looks for an enclosing column format, extracts the operator and
  15097. then applies it to the proerty in the column format's scope."
  15098. (interactive)
  15099. (unless (org-at-property-p)
  15100. (error "Not at a property"))
  15101. (let ((prop (org-match-string-no-properties 2)))
  15102. (org-columns-get-format-and-top-level)
  15103. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15104. (error "No operator defined for property %s" prop))
  15105. (org-columns-compute prop)))
  15106. (defun org-property-get-allowed-values (pom property &optional table)
  15107. "Get allowed values for the property PROPERTY.
  15108. When TABLE is non-nil, return an alist that can directly be used for
  15109. completion."
  15110. (let (vals)
  15111. (cond
  15112. ((equal property "TODO")
  15113. (setq vals (org-with-point-at pom
  15114. (append org-todo-keywords-1 '("")))))
  15115. ((equal property "PRIORITY")
  15116. (let ((n org-lowest-priority))
  15117. (while (>= n org-highest-priority)
  15118. (push (char-to-string n) vals)
  15119. (setq n (1- n)))))
  15120. ((member property org-special-properties))
  15121. (t
  15122. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15123. (when (and vals (string-match "\\S-" vals))
  15124. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15125. (setq vals (mapcar (lambda (x)
  15126. (cond ((stringp x) x)
  15127. ((numberp x) (number-to-string x))
  15128. ((symbolp x) (symbol-name x))
  15129. (t "???")))
  15130. vals)))))
  15131. (if table (mapcar 'list vals) vals)))
  15132. (defun org-property-previous-allowed-value (&optional previous)
  15133. "Switch to the next allowed value for this property."
  15134. (interactive)
  15135. (org-property-next-allowed-value t))
  15136. (defun org-property-next-allowed-value (&optional previous)
  15137. "Switch to the next allowed value for this property."
  15138. (interactive)
  15139. (unless (org-at-property-p)
  15140. (error "Not at a property"))
  15141. (let* ((key (match-string 2))
  15142. (value (match-string 3))
  15143. (allowed (or (org-property-get-allowed-values (point) key)
  15144. (and (member value '("[ ]" "[-]" "[X]"))
  15145. '("[ ]" "[X]"))))
  15146. nval)
  15147. (unless allowed
  15148. (error "Allowed values for this property have not been defined"))
  15149. (if previous (setq allowed (reverse allowed)))
  15150. (if (member value allowed)
  15151. (setq nval (car (cdr (member value allowed)))))
  15152. (setq nval (or nval (car allowed)))
  15153. (if (equal nval value)
  15154. (error "Only one allowed value for this property"))
  15155. (org-at-property-p)
  15156. (replace-match (concat " :" key ": " nval) t t)
  15157. (org-indent-line-function)
  15158. (beginning-of-line 1)
  15159. (skip-chars-forward " \t")))
  15160. (defun org-find-entry-with-id (ident)
  15161. "Locate the entry that contains the ID property with exact value IDENT.
  15162. IDENT can be a string, a symbol or a number, this function will search for
  15163. the string representation of it.
  15164. Return the position where this entry starts, or nil if there is no such entry."
  15165. (let ((id (cond
  15166. ((stringp ident) ident)
  15167. ((symbol-name ident) (symbol-name ident))
  15168. ((numberp ident) (number-to-string ident))
  15169. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15170. (case-fold-search nil))
  15171. (save-excursion
  15172. (save-restriction
  15173. (widen)
  15174. (goto-char (point-min))
  15175. (when (re-search-forward
  15176. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15177. nil t)
  15178. (org-back-to-heading)
  15179. (point))))))
  15180. ;;; Column View
  15181. (defvar org-columns-overlays nil
  15182. "Holds the list of current column overlays.")
  15183. (defvar org-columns-current-fmt nil
  15184. "Local variable, holds the currently active column format.")
  15185. (defvar org-columns-current-fmt-compiled nil
  15186. "Local variable, holds the currently active column format.
  15187. This is the compiled version of the format.")
  15188. (defvar org-columns-current-widths nil
  15189. "Loval variable, holds the currently widths of fields.")
  15190. (defvar org-columns-current-maxwidths nil
  15191. "Loval variable, holds the currently active maximum column widths.")
  15192. (defvar org-columns-begin-marker (make-marker)
  15193. "Points to the position where last a column creation command was called.")
  15194. (defvar org-columns-top-level-marker (make-marker)
  15195. "Points to the position where current columns region starts.")
  15196. (defvar org-columns-map (make-sparse-keymap)
  15197. "The keymap valid in column display.")
  15198. (defun org-columns-content ()
  15199. "Switch to contents view while in columns view."
  15200. (interactive)
  15201. (org-overview)
  15202. (org-content))
  15203. (org-defkey org-columns-map "c" 'org-columns-content)
  15204. (org-defkey org-columns-map "o" 'org-overview)
  15205. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15206. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15207. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15208. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15209. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15210. (org-defkey org-columns-map "q" 'org-columns-quit)
  15211. (org-defkey org-columns-map "r" 'org-columns-redo)
  15212. (org-defkey org-columns-map "g" 'org-columns-redo)
  15213. (org-defkey org-columns-map [left] 'backward-char)
  15214. (org-defkey org-columns-map "\M-b" 'backward-char)
  15215. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15216. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15217. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15218. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15219. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15220. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15221. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15222. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15223. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15224. (org-defkey org-columns-map ">" 'org-columns-widen)
  15225. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15226. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15227. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15228. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15229. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15230. '("Column"
  15231. ["Edit property" org-columns-edit-value t]
  15232. ["Next allowed value" org-columns-next-allowed-value t]
  15233. ["Previous allowed value" org-columns-previous-allowed-value t]
  15234. ["Show full value" org-columns-show-value t]
  15235. ["Edit allowed values" org-columns-edit-allowed t]
  15236. "--"
  15237. ["Edit column attributes" org-columns-edit-attributes t]
  15238. ["Increase column width" org-columns-widen t]
  15239. ["Decrease column width" org-columns-narrow t]
  15240. "--"
  15241. ["Move column right" org-columns-move-right t]
  15242. ["Move column left" org-columns-move-left t]
  15243. ["Add column" org-columns-new t]
  15244. ["Delete column" org-columns-delete t]
  15245. "--"
  15246. ["CONTENTS" org-columns-content t]
  15247. ["OVERVIEW" org-overview t]
  15248. ["Refresh columns display" org-columns-redo t]
  15249. "--"
  15250. ["Open link" org-columns-open-link t]
  15251. "--"
  15252. ["Quit" org-columns-quit t]))
  15253. (defun org-columns-new-overlay (beg end &optional string face)
  15254. "Create a new column overlay and add it to the list."
  15255. (let ((ov (org-make-overlay beg end)))
  15256. (org-overlay-put ov 'face (or face 'secondary-selection))
  15257. (org-overlay-display ov string face)
  15258. (push ov org-columns-overlays)
  15259. ov))
  15260. (defun org-columns-display-here (&optional props)
  15261. "Overlay the current line with column display."
  15262. (interactive)
  15263. (let* ((fmt org-columns-current-fmt-compiled)
  15264. (beg (point-at-bol))
  15265. (level-face (save-excursion
  15266. (beginning-of-line 1)
  15267. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15268. (org-get-level-face 2))))
  15269. (color (list :foreground
  15270. (face-attribute (or level-face 'default) :foreground)))
  15271. props pom property ass width f string ov column val modval)
  15272. ;; Check if the entry is in another buffer.
  15273. (unless props
  15274. (if (eq major-mode 'org-agenda-mode)
  15275. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15276. (get-text-property (point) 'org-marker))
  15277. props (if pom (org-entry-properties pom) nil))
  15278. (setq props (org-entry-properties nil))))
  15279. ;; Walk the format
  15280. (while (setq column (pop fmt))
  15281. (setq property (car column)
  15282. ass (if (equal property "ITEM")
  15283. (cons "ITEM"
  15284. (save-match-data
  15285. (org-no-properties
  15286. (org-remove-tabs
  15287. (buffer-substring-no-properties
  15288. (point-at-bol) (point-at-eol))))))
  15289. (assoc property props))
  15290. width (or (cdr (assoc property org-columns-current-maxwidths))
  15291. (nth 2 column)
  15292. (length property))
  15293. f (format "%%-%d.%ds | " width width)
  15294. val (or (cdr ass) "")
  15295. modval (if (equal property "ITEM")
  15296. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15297. string (format f (or modval val)))
  15298. ;; Create the overlay
  15299. (org-unmodified
  15300. (setq ov (org-columns-new-overlay
  15301. beg (setq beg (1+ beg)) string
  15302. (list color 'org-column)))
  15303. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15304. (org-overlay-put ov 'keymap org-columns-map)
  15305. (org-overlay-put ov 'org-columns-key property)
  15306. (org-overlay-put ov 'org-columns-value (cdr ass))
  15307. (org-overlay-put ov 'org-columns-value-modified modval)
  15308. (org-overlay-put ov 'org-columns-pom pom)
  15309. (org-overlay-put ov 'org-columns-format f))
  15310. (if (or (not (char-after beg))
  15311. (equal (char-after beg) ?\n))
  15312. (let ((inhibit-read-only t))
  15313. (save-excursion
  15314. (goto-char beg)
  15315. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15316. ;; Make the rest of the line disappear.
  15317. (org-unmodified
  15318. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15319. (org-overlay-put ov 'invisible t)
  15320. (org-overlay-put ov 'keymap org-columns-map)
  15321. (org-overlay-put ov 'intangible t)
  15322. (push ov org-columns-overlays)
  15323. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15324. (org-overlay-put ov 'keymap org-columns-map)
  15325. (push ov org-columns-overlays)
  15326. (let ((inhibit-read-only t))
  15327. (put-text-property (max (point-min) (1- (point-at-bol)))
  15328. (min (point-max) (1+ (point-at-eol)))
  15329. 'read-only "Type `e' to edit property")))))
  15330. (defvar org-previous-header-line-format nil
  15331. "The header line format before column view was turned on.")
  15332. (defvar org-columns-inhibit-recalculation nil
  15333. "Inhibit recomputing of columns on column view startup.")
  15334. (defvar header-line-format)
  15335. (defun org-columns-display-here-title ()
  15336. "Overlay the newline before the current line with the table title."
  15337. (interactive)
  15338. (let ((fmt org-columns-current-fmt-compiled)
  15339. string (title "")
  15340. property width f column str widths)
  15341. (while (setq column (pop fmt))
  15342. (setq property (car column)
  15343. str (or (nth 1 column) property)
  15344. width (or (cdr (assoc property org-columns-current-maxwidths))
  15345. (nth 2 column)
  15346. (length str))
  15347. widths (push width widths)
  15348. f (format "%%-%d.%ds | " width width)
  15349. string (format f str)
  15350. title (concat title string)))
  15351. (setq title (concat
  15352. (org-add-props " " nil 'display '(space :align-to 0))
  15353. (org-add-props title nil 'face '(:weight bold :underline t))))
  15354. (org-set-local 'org-previous-header-line-format header-line-format)
  15355. (org-set-local 'org-columns-current-widths (nreverse widths))
  15356. (setq header-line-format title)))
  15357. (defun org-columns-remove-overlays ()
  15358. "Remove all currently active column overlays."
  15359. (interactive)
  15360. (when (marker-buffer org-columns-begin-marker)
  15361. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15362. (when (local-variable-p 'org-previous-header-line-format)
  15363. (setq header-line-format org-previous-header-line-format)
  15364. (kill-local-variable 'org-previous-header-line-format))
  15365. (move-marker org-columns-begin-marker nil)
  15366. (move-marker org-columns-top-level-marker nil)
  15367. (org-unmodified
  15368. (mapc 'org-delete-overlay org-columns-overlays)
  15369. (setq org-columns-overlays nil)
  15370. (let ((inhibit-read-only t))
  15371. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15372. (defun org-columns-cleanup-item (item fmt)
  15373. "Remove from ITEM what is a column in the format FMT."
  15374. (if (not org-complex-heading-regexp)
  15375. item
  15376. (when (string-match org-complex-heading-regexp item)
  15377. (concat
  15378. (org-add-props (concat (match-string 1 item) " ") nil
  15379. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15380. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15381. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15382. " " (match-string 4 item)
  15383. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15384. (defun org-columns-show-value ()
  15385. "Show the full value of the property."
  15386. (interactive)
  15387. (let ((value (get-char-property (point) 'org-columns-value)))
  15388. (message "Value is: %s" (or value ""))))
  15389. (defun org-columns-quit ()
  15390. "Remove the column overlays and in this way exit column editing."
  15391. (interactive)
  15392. (org-unmodified
  15393. (org-columns-remove-overlays)
  15394. (let ((inhibit-read-only t))
  15395. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15396. (when (eq major-mode 'org-agenda-mode)
  15397. (message
  15398. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15399. (defun org-columns-check-computed ()
  15400. "Check if this column value is computed.
  15401. If yes, throw an error indicating that changing it does not make sense."
  15402. (let ((val (get-char-property (point) 'org-columns-value)))
  15403. (when (and (stringp val)
  15404. (get-char-property 0 'org-computed val))
  15405. (error "This value is computed from the entry's children"))))
  15406. (defun org-columns-todo (&optional arg)
  15407. "Change the TODO state during column view."
  15408. (interactive "P")
  15409. (org-columns-edit-value "TODO"))
  15410. (defun org-columns-set-tags-or-toggle (&optional arg)
  15411. "Toggle checkbox at point, or set tags for current headline."
  15412. (interactive "P")
  15413. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15414. (get-char-property (point) 'org-columns-value))
  15415. (org-columns-next-allowed-value)
  15416. (org-columns-edit-value "TAGS")))
  15417. (defun org-columns-edit-value (&optional key)
  15418. "Edit the value of the property at point in column view.
  15419. Where possible, use the standard interface for changing this line."
  15420. (interactive)
  15421. (org-columns-check-computed)
  15422. (let* ((external-key key)
  15423. (col (current-column))
  15424. (key (or key (get-char-property (point) 'org-columns-key)))
  15425. (value (get-char-property (point) 'org-columns-value))
  15426. (bol (point-at-bol)) (eol (point-at-eol))
  15427. (pom (or (get-text-property bol 'org-hd-marker)
  15428. (point))) ; keep despite of compiler waring
  15429. (line-overlays
  15430. (delq nil (mapcar (lambda (x)
  15431. (and (eq (overlay-buffer x) (current-buffer))
  15432. (>= (overlay-start x) bol)
  15433. (<= (overlay-start x) eol)
  15434. x))
  15435. org-columns-overlays)))
  15436. nval eval allowed)
  15437. (cond
  15438. ((equal key "CLOCKSUM")
  15439. (error "This special column cannot be edited"))
  15440. ((equal key "ITEM")
  15441. (setq eval '(org-with-point-at pom
  15442. (org-edit-headline))))
  15443. ((equal key "TODO")
  15444. (setq eval '(org-with-point-at pom
  15445. (let ((current-prefix-arg
  15446. (if external-key current-prefix-arg '(4))))
  15447. (call-interactively 'org-todo)))))
  15448. ((equal key "PRIORITY")
  15449. (setq eval '(org-with-point-at pom
  15450. (call-interactively 'org-priority))))
  15451. ((equal key "TAGS")
  15452. (setq eval '(org-with-point-at pom
  15453. (let ((org-fast-tag-selection-single-key
  15454. (if (eq org-fast-tag-selection-single-key 'expert)
  15455. t org-fast-tag-selection-single-key)))
  15456. (call-interactively 'org-set-tags)))))
  15457. ((equal key "DEADLINE")
  15458. (setq eval '(org-with-point-at pom
  15459. (call-interactively 'org-deadline))))
  15460. ((equal key "SCHEDULED")
  15461. (setq eval '(org-with-point-at pom
  15462. (call-interactively 'org-schedule))))
  15463. (t
  15464. (setq allowed (org-property-get-allowed-values pom key 'table))
  15465. (if allowed
  15466. (setq nval (completing-read "Value: " allowed nil t))
  15467. (setq nval (read-string "Edit: " value)))
  15468. (setq nval (org-trim nval))
  15469. (when (not (equal nval value))
  15470. (setq eval '(org-entry-put pom key nval)))))
  15471. (when eval
  15472. (let ((inhibit-read-only t))
  15473. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15474. (unwind-protect
  15475. (progn
  15476. (setq org-columns-overlays
  15477. (org-delete-all line-overlays org-columns-overlays))
  15478. (mapc 'org-delete-overlay line-overlays)
  15479. (org-columns-eval eval))
  15480. (org-columns-display-here))))
  15481. (move-to-column col)
  15482. (if (and (org-mode-p)
  15483. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15484. (org-columns-update key))))
  15485. (defun org-edit-headline () ; FIXME: this is not columns specific
  15486. "Edit the current headline, the part without TODO keyword, TAGS."
  15487. (org-back-to-heading)
  15488. (when (looking-at org-todo-line-regexp)
  15489. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15490. (txt (match-string 3))
  15491. (post "")
  15492. txt2)
  15493. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15494. (setq post (match-string 0 txt)
  15495. txt (substring txt 0 (match-beginning 0))))
  15496. (setq txt2 (read-string "Edit: " txt))
  15497. (when (not (equal txt txt2))
  15498. (beginning-of-line 1)
  15499. (insert pre txt2 post)
  15500. (delete-region (point) (point-at-eol))
  15501. (org-set-tags nil t)))))
  15502. (defun org-columns-edit-allowed ()
  15503. "Edit the list of allowed values for the current property."
  15504. (interactive)
  15505. (let* ((key (get-char-property (point) 'org-columns-key))
  15506. (key1 (concat key "_ALL"))
  15507. (allowed (org-entry-get (point) key1 t))
  15508. nval)
  15509. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15510. (setq nval (read-string "Allowed: " allowed))
  15511. (org-entry-put
  15512. (cond ((marker-position org-entry-property-inherited-from)
  15513. org-entry-property-inherited-from)
  15514. ((marker-position org-columns-top-level-marker)
  15515. org-columns-top-level-marker))
  15516. key1 nval)))
  15517. (defmacro org-no-warnings (&rest body)
  15518. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15519. (defun org-columns-eval (form)
  15520. (let (hidep)
  15521. (save-excursion
  15522. (beginning-of-line 1)
  15523. ;; `next-line' is needed here, because it skips invisible line.
  15524. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15525. (setq hidep (org-on-heading-p 1)))
  15526. (eval form)
  15527. (and hidep (hide-entry))))
  15528. (defun org-columns-previous-allowed-value ()
  15529. "Switch to the previous allowed value for this column."
  15530. (interactive)
  15531. (org-columns-next-allowed-value t))
  15532. (defun org-columns-next-allowed-value (&optional previous)
  15533. "Switch to the next allowed value for this column."
  15534. (interactive)
  15535. (org-columns-check-computed)
  15536. (let* ((col (current-column))
  15537. (key (get-char-property (point) 'org-columns-key))
  15538. (value (get-char-property (point) 'org-columns-value))
  15539. (bol (point-at-bol)) (eol (point-at-eol))
  15540. (pom (or (get-text-property bol 'org-hd-marker)
  15541. (point))) ; keep despite of compiler waring
  15542. (line-overlays
  15543. (delq nil (mapcar (lambda (x)
  15544. (and (eq (overlay-buffer x) (current-buffer))
  15545. (>= (overlay-start x) bol)
  15546. (<= (overlay-start x) eol)
  15547. x))
  15548. org-columns-overlays)))
  15549. (allowed (or (org-property-get-allowed-values pom key)
  15550. (and (memq
  15551. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15552. '(checkbox checkbox-n-of-m checkbox-percent))
  15553. '("[ ]" "[X]"))))
  15554. nval)
  15555. (when (equal key "ITEM")
  15556. (error "Cannot edit item headline from here"))
  15557. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15558. (error "Allowed values for this property have not been defined"))
  15559. (if (member key '("SCHEDULED" "DEADLINE"))
  15560. (setq nval (if previous 'earlier 'later))
  15561. (if previous (setq allowed (reverse allowed)))
  15562. (if (member value allowed)
  15563. (setq nval (car (cdr (member value allowed)))))
  15564. (setq nval (or nval (car allowed)))
  15565. (if (equal nval value)
  15566. (error "Only one allowed value for this property")))
  15567. (let ((inhibit-read-only t))
  15568. (remove-text-properties (1- bol) eol '(read-only t))
  15569. (unwind-protect
  15570. (progn
  15571. (setq org-columns-overlays
  15572. (org-delete-all line-overlays org-columns-overlays))
  15573. (mapc 'org-delete-overlay line-overlays)
  15574. (org-columns-eval '(org-entry-put pom key nval)))
  15575. (org-columns-display-here)))
  15576. (move-to-column col)
  15577. (if (and (org-mode-p)
  15578. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15579. (org-columns-update key))))
  15580. (defun org-verify-version (task)
  15581. (cond
  15582. ((eq task 'columns)
  15583. (if (or (featurep 'xemacs)
  15584. (< emacs-major-version 22))
  15585. (error "Emacs 22 is required for the columns feature")))))
  15586. (defun org-columns-open-link (&optional arg)
  15587. (interactive "P")
  15588. (let ((value (get-char-property (point) 'org-columns-value)))
  15589. (org-open-link-from-string value arg)))
  15590. (defun org-open-link-from-string (s &optional arg)
  15591. "Open a link in the string S, as if it was in Org-mode."
  15592. (interactive)
  15593. (with-temp-buffer
  15594. (let ((org-inhibit-startup t))
  15595. (org-mode)
  15596. (insert s)
  15597. (goto-char (point-min))
  15598. (org-open-at-point arg))))
  15599. (defun org-columns-get-format-and-top-level ()
  15600. (let (fmt)
  15601. (when (condition-case nil (org-back-to-heading) (error nil))
  15602. (move-marker org-entry-property-inherited-from nil)
  15603. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15604. (setq fmt (or fmt org-columns-default-format))
  15605. (org-set-local 'org-columns-current-fmt fmt)
  15606. (org-columns-compile-format fmt)
  15607. (if (marker-position org-entry-property-inherited-from)
  15608. (move-marker org-columns-top-level-marker
  15609. org-entry-property-inherited-from)
  15610. (move-marker org-columns-top-level-marker (point)))
  15611. fmt))
  15612. (defun org-columns ()
  15613. "Turn on column view on an org-mode file."
  15614. (interactive)
  15615. (org-verify-version 'columns)
  15616. (org-columns-remove-overlays)
  15617. (move-marker org-columns-begin-marker (point))
  15618. (let (beg end fmt cache maxwidths)
  15619. (setq fmt (org-columns-get-format-and-top-level))
  15620. (save-excursion
  15621. (goto-char org-columns-top-level-marker)
  15622. (setq beg (point))
  15623. (unless org-columns-inhibit-recalculation
  15624. (org-columns-compute-all))
  15625. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15626. (point-max)))
  15627. ;; Get and cache the properties
  15628. (goto-char beg)
  15629. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15630. (save-excursion
  15631. (save-restriction
  15632. (narrow-to-region beg end)
  15633. (org-clock-sum))))
  15634. (while (re-search-forward (concat "^" outline-regexp) end t)
  15635. (push (cons (org-current-line) (org-entry-properties)) cache))
  15636. (when cache
  15637. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15638. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15639. (org-columns-display-here-title)
  15640. (mapc (lambda (x)
  15641. (goto-line (car x))
  15642. (org-columns-display-here (cdr x)))
  15643. cache)))))
  15644. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15645. "Insert a new column, to the leeft o the current column."
  15646. (interactive)
  15647. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15648. cell)
  15649. (setq prop (completing-read
  15650. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15651. nil nil prop))
  15652. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15653. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15654. (if (string-match "\\S-" width)
  15655. (setq width (string-to-number width))
  15656. (setq width nil))
  15657. (setq fmt (completing-read "Summary [none]: "
  15658. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15659. nil t))
  15660. (if (string-match "\\S-" fmt)
  15661. (setq fmt (intern fmt))
  15662. (setq fmt nil))
  15663. (if (eq fmt 'none) (setq fmt nil))
  15664. (if editp
  15665. (progn
  15666. (setcar editp prop)
  15667. (setcdr editp (list title width nil fmt)))
  15668. (setq cell (nthcdr (1- (current-column))
  15669. org-columns-current-fmt-compiled))
  15670. (setcdr cell (cons (list prop title width nil fmt)
  15671. (cdr cell))))
  15672. (org-columns-store-format)
  15673. (org-columns-redo)))
  15674. (defun org-columns-delete ()
  15675. "Delete the column at point from columns view."
  15676. (interactive)
  15677. (let* ((n (current-column))
  15678. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15679. (when (y-or-n-p
  15680. (format "Are you sure you want to remove column \"%s\"? " title))
  15681. (setq org-columns-current-fmt-compiled
  15682. (delq (nth n org-columns-current-fmt-compiled)
  15683. org-columns-current-fmt-compiled))
  15684. (org-columns-store-format)
  15685. (org-columns-redo)
  15686. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15687. (backward-char 1)))))
  15688. (defun org-columns-edit-attributes ()
  15689. "Edit the attributes of the current column."
  15690. (interactive)
  15691. (let* ((n (current-column))
  15692. (info (nth n org-columns-current-fmt-compiled)))
  15693. (apply 'org-columns-new info)))
  15694. (defun org-columns-widen (arg)
  15695. "Make the column wider by ARG characters."
  15696. (interactive "p")
  15697. (let* ((n (current-column))
  15698. (entry (nth n org-columns-current-fmt-compiled))
  15699. (width (or (nth 2 entry)
  15700. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15701. (setq width (max 1 (+ width arg)))
  15702. (setcar (nthcdr 2 entry) width)
  15703. (org-columns-store-format)
  15704. (org-columns-redo)))
  15705. (defun org-columns-narrow (arg)
  15706. "Make the column nrrower by ARG characters."
  15707. (interactive "p")
  15708. (org-columns-widen (- arg)))
  15709. (defun org-columns-move-right ()
  15710. "Swap this column with the one to the right."
  15711. (interactive)
  15712. (let* ((n (current-column))
  15713. (cell (nthcdr n org-columns-current-fmt-compiled))
  15714. e)
  15715. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15716. (error "Cannot shift this column further to the right"))
  15717. (setq e (car cell))
  15718. (setcar cell (car (cdr cell)))
  15719. (setcdr cell (cons e (cdr (cdr cell))))
  15720. (org-columns-store-format)
  15721. (org-columns-redo)
  15722. (forward-char 1)))
  15723. (defun org-columns-move-left ()
  15724. "Swap this column with the one to the left."
  15725. (interactive)
  15726. (let* ((n (current-column)))
  15727. (when (= n 0)
  15728. (error "Cannot shift this column further to the left"))
  15729. (backward-char 1)
  15730. (org-columns-move-right)
  15731. (backward-char 1)))
  15732. (defun org-columns-store-format ()
  15733. "Store the text version of the current columns format in appropriate place.
  15734. This is either in the COLUMNS property of the node starting the current column
  15735. display, or in the #+COLUMNS line of the current buffer."
  15736. (let (fmt (cnt 0))
  15737. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15738. (org-set-local 'org-columns-current-fmt fmt)
  15739. (if (marker-position org-columns-top-level-marker)
  15740. (save-excursion
  15741. (goto-char org-columns-top-level-marker)
  15742. (if (and (org-at-heading-p)
  15743. (org-entry-get nil "COLUMNS"))
  15744. (org-entry-put nil "COLUMNS" fmt)
  15745. (goto-char (point-min))
  15746. ;; Overwrite all #+COLUMNS lines....
  15747. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15748. (setq cnt (1+ cnt))
  15749. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15750. (unless (> cnt 0)
  15751. (goto-char (point-min))
  15752. (or (org-on-heading-p t) (outline-next-heading))
  15753. (let ((inhibit-read-only t))
  15754. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15755. (org-set-local 'org-columns-default-format fmt))))))
  15756. (defvar org-overriding-columns-format nil
  15757. "When set, overrides any other definition.")
  15758. (defvar org-agenda-view-columns-initially nil
  15759. "When set, switch to columns view immediately after creating the agenda.")
  15760. (defun org-agenda-columns ()
  15761. "Turn on column view in the agenda."
  15762. (interactive)
  15763. (org-verify-version 'columns)
  15764. (org-columns-remove-overlays)
  15765. (move-marker org-columns-begin-marker (point))
  15766. (let (fmt cache maxwidths m)
  15767. (cond
  15768. ((and (local-variable-p 'org-overriding-columns-format)
  15769. org-overriding-columns-format)
  15770. (setq fmt org-overriding-columns-format))
  15771. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15772. (setq fmt (org-entry-get m "COLUMNS" t)))
  15773. ((and (boundp 'org-columns-current-fmt)
  15774. (local-variable-p 'org-columns-current-fmt)
  15775. org-columns-current-fmt)
  15776. (setq fmt org-columns-current-fmt))
  15777. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15778. (setq m (get-text-property m 'org-hd-marker))
  15779. (setq fmt (org-entry-get m "COLUMNS" t))))
  15780. (setq fmt (or fmt org-columns-default-format))
  15781. (org-set-local 'org-columns-current-fmt fmt)
  15782. (org-columns-compile-format fmt)
  15783. (save-excursion
  15784. ;; Get and cache the properties
  15785. (goto-char (point-min))
  15786. (while (not (eobp))
  15787. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15788. (get-text-property (point) 'org-marker)))
  15789. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15790. (beginning-of-line 2))
  15791. (when cache
  15792. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15793. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15794. (org-columns-display-here-title)
  15795. (mapc (lambda (x)
  15796. (goto-line (car x))
  15797. (org-columns-display-here (cdr x)))
  15798. cache)))))
  15799. (defun org-columns-get-autowidth-alist (s cache)
  15800. "Derive the maximum column widths from the format and the cache."
  15801. (let ((start 0) rtn)
  15802. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15803. (push (cons (match-string 1 s) 1) rtn)
  15804. (setq start (match-end 0)))
  15805. (mapc (lambda (x)
  15806. (setcdr x (apply 'max
  15807. (mapcar
  15808. (lambda (y)
  15809. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15810. cache))))
  15811. rtn)
  15812. rtn))
  15813. (defun org-columns-compute-all ()
  15814. "Compute all columns that have operators defined."
  15815. (org-unmodified
  15816. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15817. (let ((columns org-columns-current-fmt-compiled) col)
  15818. (while (setq col (pop columns))
  15819. (when (nth 3 col)
  15820. (save-excursion
  15821. (org-columns-compute (car col)))))))
  15822. (defun org-columns-update (property)
  15823. "Recompute PROPERTY, and update the columns display for it."
  15824. (org-columns-compute property)
  15825. (let (fmt val pos)
  15826. (save-excursion
  15827. (mapc (lambda (ov)
  15828. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15829. (setq pos (org-overlay-start ov))
  15830. (goto-char pos)
  15831. (when (setq val (cdr (assoc property
  15832. (get-text-property
  15833. (point-at-bol) 'org-summaries))))
  15834. (setq fmt (org-overlay-get ov 'org-columns-format))
  15835. (org-overlay-put ov 'org-columns-value val)
  15836. (org-overlay-put ov 'display (format fmt val)))))
  15837. org-columns-overlays))))
  15838. (defun org-columns-compute (property)
  15839. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15840. (interactive)
  15841. (let* ((re (concat "^" outline-regexp))
  15842. (lmax 30) ; Does anyone use deeper levels???
  15843. (lsum (make-vector lmax 0))
  15844. (lflag (make-vector lmax nil))
  15845. (level 0)
  15846. (ass (assoc property org-columns-current-fmt-compiled))
  15847. (format (nth 4 ass))
  15848. (printf (nth 5 ass))
  15849. (beg org-columns-top-level-marker)
  15850. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15851. (save-excursion
  15852. ;; Find the region to compute
  15853. (goto-char beg)
  15854. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15855. (goto-char end)
  15856. ;; Walk the tree from the back and do the computations
  15857. (while (re-search-backward re beg t)
  15858. (setq sumpos (match-beginning 0)
  15859. last-level level
  15860. level (org-outline-level)
  15861. val (org-entry-get nil property)
  15862. valflag (and val (string-match "\\S-" val)))
  15863. (cond
  15864. ((< level last-level)
  15865. ;; put the sum of lower levels here as a property
  15866. (setq sum (aref lsum last-level) ; current sum
  15867. flag (aref lflag last-level) ; any valid entries from children?
  15868. str (org-column-number-to-string sum format printf)
  15869. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15870. useval (if flag str1 (if valflag val ""))
  15871. sum-alist (get-text-property sumpos 'org-summaries))
  15872. (if (assoc property sum-alist)
  15873. (setcdr (assoc property sum-alist) useval)
  15874. (push (cons property useval) sum-alist)
  15875. (org-unmodified
  15876. (add-text-properties sumpos (1+ sumpos)
  15877. (list 'org-summaries sum-alist))))
  15878. (when val
  15879. (org-entry-put nil property (if flag str val)))
  15880. ;; add current to current level accumulator
  15881. (when (or flag valflag)
  15882. (aset lsum level (+ (aref lsum level)
  15883. (if flag sum (org-column-string-to-number
  15884. (if flag str val) format))))
  15885. (aset lflag level t))
  15886. ;; clear accumulators for deeper levels
  15887. (loop for l from (1+ level) to (1- lmax) do
  15888. (aset lsum l 0)
  15889. (aset lflag l nil)))
  15890. ((>= level last-level)
  15891. ;; add what we have here to the accumulator for this level
  15892. (aset lsum level (+ (aref lsum level)
  15893. (org-column-string-to-number (or val "0") format)))
  15894. (and valflag (aset lflag level t)))
  15895. (t (error "This should not happen")))))))
  15896. (defun org-columns-redo ()
  15897. "Construct the column display again."
  15898. (interactive)
  15899. (message "Recomputing columns...")
  15900. (save-excursion
  15901. (if (marker-position org-columns-begin-marker)
  15902. (goto-char org-columns-begin-marker))
  15903. (org-columns-remove-overlays)
  15904. (if (org-mode-p)
  15905. (call-interactively 'org-columns)
  15906. (call-interactively 'org-agenda-columns)))
  15907. (message "Recomputing columns...done"))
  15908. (defun org-columns-not-in-agenda ()
  15909. (if (eq major-mode 'org-agenda-mode)
  15910. (error "This command is only allowed in Org-mode buffers")))
  15911. (defun org-string-to-number (s)
  15912. "Convert string to number, and interpret hh:mm:ss."
  15913. (if (not (string-match ":" s))
  15914. (string-to-number s)
  15915. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15916. (while l
  15917. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15918. sum)))
  15919. (defun org-column-number-to-string (n fmt &optional printf)
  15920. "Convert a computed column number to a string value, according to FMT."
  15921. (cond
  15922. ((eq fmt 'add_times)
  15923. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15924. (format "%d:%02d" h m)))
  15925. ((eq fmt 'checkbox)
  15926. (cond ((= n (floor n)) "[X]")
  15927. ((> n 1.) "[-]")
  15928. (t "[ ]")))
  15929. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15930. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15931. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15932. (printf (format printf n))
  15933. ((eq fmt 'currency)
  15934. (format "%.2f" n))
  15935. (t (number-to-string n))))
  15936. (defun org-nofm-to-completion (n m &optional percent)
  15937. (if (not percent)
  15938. (format "[%d/%d]" n m)
  15939. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15940. (defun org-column-string-to-number (s fmt)
  15941. "Convert a column value to a number that can be used for column computing."
  15942. (cond
  15943. ((string-match ":" s)
  15944. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15945. (while l
  15946. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15947. sum))
  15948. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15949. (if (equal s "[X]") 1. 0.000001))
  15950. (t (string-to-number s))))
  15951. (defun org-columns-uncompile-format (cfmt)
  15952. "Turn the compiled columns format back into a string representation."
  15953. (let ((rtn "") e s prop title op width fmt printf)
  15954. (while (setq e (pop cfmt))
  15955. (setq prop (car e)
  15956. title (nth 1 e)
  15957. width (nth 2 e)
  15958. op (nth 3 e)
  15959. fmt (nth 4 e)
  15960. printf (nth 5 e))
  15961. (cond
  15962. ((eq fmt 'add_times) (setq op ":"))
  15963. ((eq fmt 'checkbox) (setq op "X"))
  15964. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15965. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15966. ((eq fmt 'add_numbers) (setq op "+"))
  15967. ((eq fmt 'currency) (setq op "$")))
  15968. (if (and op printf) (setq op (concat op ";" printf)))
  15969. (if (equal title prop) (setq title nil))
  15970. (setq s (concat "%" (if width (number-to-string width))
  15971. prop
  15972. (if title (concat "(" title ")"))
  15973. (if op (concat "{" op "}"))))
  15974. (setq rtn (concat rtn " " s)))
  15975. (org-trim rtn)))
  15976. (defun org-columns-compile-format (fmt)
  15977. "Turn a column format string into an alist of specifications.
  15978. The alist has one entry for each column in the format. The elements of
  15979. that list are:
  15980. property the property
  15981. title the title field for the columns
  15982. width the column width in characters, can be nil for automatic
  15983. operator the operator if any
  15984. format the output format for computed results, derived from operator
  15985. printf a printf format for computed values"
  15986. (let ((start 0) width prop title op f printf)
  15987. (setq org-columns-current-fmt-compiled nil)
  15988. (while (string-match
  15989. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15990. fmt start)
  15991. (setq start (match-end 0)
  15992. width (match-string 1 fmt)
  15993. prop (match-string 2 fmt)
  15994. title (or (match-string 3 fmt) prop)
  15995. op (match-string 4 fmt)
  15996. f nil
  15997. printf nil)
  15998. (if width (setq width (string-to-number width)))
  15999. (when (and op (string-match ";" op))
  16000. (setq printf (substring op (match-end 0))
  16001. op (substring op 0 (match-beginning 0))))
  16002. (cond
  16003. ((equal op "+") (setq f 'add_numbers))
  16004. ((equal op "$") (setq f 'currency))
  16005. ((equal op ":") (setq f 'add_times))
  16006. ((equal op "X") (setq f 'checkbox))
  16007. ((equal op "X/") (setq f 'checkbox-n-of-m))
  16008. ((equal op "X%") (setq f 'checkbox-percent))
  16009. )
  16010. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  16011. (setq org-columns-current-fmt-compiled
  16012. (nreverse org-columns-current-fmt-compiled))))
  16013. ;;; Dynamic block for Column view
  16014. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  16015. "Get the column view of the current buffer or subtree.
  16016. The first optional argument MAXLEVEL sets the level limit. A
  16017. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  16018. empty rows, an empty row being one where all the column view
  16019. specifiers except ITEM are empty. This function returns a list
  16020. containing the title row and all other rows. Each row is a list
  16021. of fields."
  16022. (save-excursion
  16023. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16024. (n (length title)) row tbl)
  16025. (goto-char (point-min))
  16026. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  16027. (or (null maxlevel)
  16028. (>= maxlevel
  16029. (if org-odd-levels-only
  16030. (/ (1+ (length (match-string 1))) 2)
  16031. (length (match-string 1))))))
  16032. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16033. (setq row nil)
  16034. (loop for i from 0 to (1- n) do
  16035. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16036. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16037. "")
  16038. row))
  16039. (setq row (nreverse row))
  16040. (unless (and skip-empty-rows
  16041. (eq 1 (length (delete "" (delete-dups row)))))
  16042. (push row tbl))))
  16043. (append (list title 'hline) (nreverse tbl)))))
  16044. (defun org-dblock-write:columnview (params)
  16045. "Write the column view table.
  16046. PARAMS is a property list of parameters:
  16047. :width enforce same column widths with <N> specifiers.
  16048. :id the :ID: property of the entry where the columns view
  16049. should be built, as a string. When `local', call locally.
  16050. When `global' call column view with the cursor at the beginning
  16051. of the buffer (usually this means that the whole buffer switches
  16052. to column view).
  16053. :hlines When t, insert a hline before each item. When a number, insert
  16054. a hline before each level <= that number.
  16055. :vlines When t, make each column a colgroup to enforce vertical lines.
  16056. :maxlevel When set to a number, don't capture headlines below this level.
  16057. :skip-empty-rows
  16058. When t, skip rows where all specifiers other than ITEM are empty."
  16059. (let ((pos (move-marker (make-marker) (point)))
  16060. (hlines (plist-get params :hlines))
  16061. (vlines (plist-get params :vlines))
  16062. (maxlevel (plist-get params :maxlevel))
  16063. (skip-empty-rows (plist-get params :skip-empty-rows))
  16064. tbl id idpos nfields tmp)
  16065. (save-excursion
  16066. (save-restriction
  16067. (when (setq id (plist-get params :id))
  16068. (cond ((not id) nil)
  16069. ((eq id 'global) (goto-char (point-min)))
  16070. ((eq id 'local) nil)
  16071. ((setq idpos (org-find-entry-with-id id))
  16072. (goto-char idpos))
  16073. (t (error "Cannot find entry with :ID: %s" id))))
  16074. (org-columns)
  16075. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  16076. (setq nfields (length (car tbl)))
  16077. (org-columns-quit)))
  16078. (goto-char pos)
  16079. (move-marker pos nil)
  16080. (when tbl
  16081. (when (plist-get params :hlines)
  16082. (setq tmp nil)
  16083. (while tbl
  16084. (if (eq (car tbl) 'hline)
  16085. (push (pop tbl) tmp)
  16086. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16087. (if (and (not (eq (car tmp) 'hline))
  16088. (or (eq hlines t)
  16089. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16090. (push 'hline tmp)))
  16091. (push (pop tbl) tmp)))
  16092. (setq tbl (nreverse tmp)))
  16093. (when vlines
  16094. (setq tbl (mapcar (lambda (x)
  16095. (if (eq 'hline x) x (cons "" x)))
  16096. tbl))
  16097. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16098. (setq pos (point))
  16099. (insert (org-listtable-to-string tbl))
  16100. (when (plist-get params :width)
  16101. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16102. org-columns-current-widths "|")))
  16103. (goto-char pos)
  16104. (org-table-align))))
  16105. (defun org-listtable-to-string (tbl)
  16106. "Convert a listtable TBL to a string that contains the Org-mode table.
  16107. The table still need to be alligned. The resulting string has no leading
  16108. and tailing newline characters."
  16109. (mapconcat
  16110. (lambda (x)
  16111. (cond
  16112. ((listp x)
  16113. (concat "|" (mapconcat 'identity x "|") "|"))
  16114. ((eq x 'hline) "|-|")
  16115. (t (error "Garbage in listtable: %s" x))))
  16116. tbl "\n"))
  16117. (defun org-insert-columns-dblock ()
  16118. "Create a dynamic block capturing a column view table."
  16119. (interactive)
  16120. (let ((defaults '(:name "columnview" :hlines 1))
  16121. (id (completing-read
  16122. "Capture columns (local, global, entry with :ID: property) [local]: "
  16123. (append '(("global") ("local"))
  16124. (mapcar 'list (org-property-values "ID"))))))
  16125. (if (equal id "") (setq id 'local))
  16126. (if (equal id "global") (setq id 'global))
  16127. (setq defaults (append defaults (list :id id)))
  16128. (org-create-dblock defaults)
  16129. (org-update-dblock)))
  16130. ;;;; Timestamps
  16131. (defvar org-last-changed-timestamp nil)
  16132. (defvar org-time-was-given) ; dynamically scoped parameter
  16133. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16134. (defvar org-ts-what) ; dynamically scoped parameter
  16135. (defun org-time-stamp (arg)
  16136. "Prompt for a date/time and insert a time stamp.
  16137. If the user specifies a time like HH:MM, or if this command is called
  16138. with a prefix argument, the time stamp will contain date and time.
  16139. Otherwise, only the date will be included. All parts of a date not
  16140. specified by the user will be filled in from the current date/time.
  16141. So if you press just return without typing anything, the time stamp
  16142. will represent the current date/time. If there is already a timestamp
  16143. at the cursor, it will be modified."
  16144. (interactive "P")
  16145. (let* ((ts nil)
  16146. (default-time
  16147. ;; Default time is either today, or, when entering a range,
  16148. ;; the range start.
  16149. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16150. (save-excursion
  16151. (re-search-backward
  16152. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16153. (- (point) 20) t)))
  16154. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16155. (current-time)))
  16156. (default-input (and ts (org-get-compact-tod ts)))
  16157. org-time-was-given org-end-time-was-given time)
  16158. (cond
  16159. ((and (org-at-timestamp-p)
  16160. (eq last-command 'org-time-stamp)
  16161. (eq this-command 'org-time-stamp))
  16162. (insert "--")
  16163. (setq time (let ((this-command this-command))
  16164. (org-read-date arg 'totime nil nil default-time default-input)))
  16165. (org-insert-time-stamp time (or org-time-was-given arg)))
  16166. ((org-at-timestamp-p)
  16167. (setq time (let ((this-command this-command))
  16168. (org-read-date arg 'totime nil nil default-time default-input)))
  16169. (when (org-at-timestamp-p) ; just to get the match data
  16170. (replace-match "")
  16171. (setq org-last-changed-timestamp
  16172. (org-insert-time-stamp
  16173. time (or org-time-was-given arg)
  16174. nil nil nil (list org-end-time-was-given))))
  16175. (message "Timestamp updated"))
  16176. (t
  16177. (setq time (let ((this-command this-command))
  16178. (org-read-date arg 'totime nil nil default-time default-input)))
  16179. (org-insert-time-stamp time (or org-time-was-given arg)
  16180. nil nil nil (list org-end-time-was-given))))))
  16181. ;; FIXME: can we use this for something else????
  16182. ;; like computing time differences?????
  16183. (defun org-get-compact-tod (s)
  16184. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16185. (let* ((t1 (match-string 1 s))
  16186. (h1 (string-to-number (match-string 2 s)))
  16187. (m1 (string-to-number (match-string 3 s)))
  16188. (t2 (and (match-end 4) (match-string 5 s)))
  16189. (h2 (and t2 (string-to-number (match-string 6 s))))
  16190. (m2 (and t2 (string-to-number (match-string 7 s))))
  16191. dh dm)
  16192. (if (not t2)
  16193. t1
  16194. (setq dh (- h2 h1) dm (- m2 m1))
  16195. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16196. (concat t1 "+" (number-to-string dh)
  16197. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16198. (defun org-time-stamp-inactive (&optional arg)
  16199. "Insert an inactive time stamp.
  16200. An inactive time stamp is enclosed in square brackets instead of angle
  16201. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16202. does not link to the calendar and cannot be changed with the S-cursor keys.
  16203. So these are more for recording a certain time/date."
  16204. (interactive "P")
  16205. (let (org-time-was-given org-end-time-was-given time)
  16206. (setq time (org-read-date arg 'totime))
  16207. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16208. nil nil (list org-end-time-was-given))))
  16209. (defvar org-date-ovl (org-make-overlay 1 1))
  16210. (org-overlay-put org-date-ovl 'face 'org-warning)
  16211. (org-detach-overlay org-date-ovl)
  16212. (defvar org-ans1) ; dynamically scoped parameter
  16213. (defvar org-ans2) ; dynamically scoped parameter
  16214. (defvar org-plain-time-of-day-regexp) ; defined below
  16215. (defvar org-read-date-overlay nil)
  16216. (defvar org-dcst nil) ; dynamically scoped
  16217. (defun org-read-date (&optional with-time to-time from-string prompt
  16218. default-time default-input)
  16219. "Read a date, possibly a time, and make things smooth for the user.
  16220. The prompt will suggest to enter an ISO date, but you can also enter anything
  16221. which will at least partially be understood by `parse-time-string'.
  16222. Unrecognized parts of the date will default to the current day, month, year,
  16223. hour and minute. If this command is called to replace a timestamp at point,
  16224. of to enter the second timestamp of a range, the default time is taken from the
  16225. existing stamp. For example,
  16226. 3-2-5 --> 2003-02-05
  16227. feb 15 --> currentyear-02-15
  16228. sep 12 9 --> 2009-09-12
  16229. 12:45 --> today 12:45
  16230. 22 sept 0:34 --> currentyear-09-22 0:34
  16231. 12 --> currentyear-currentmonth-12
  16232. Fri --> nearest Friday (today or later)
  16233. etc.
  16234. Furthermore you can specify a relative date by giving, as the *first* thing
  16235. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16236. change in days weeks, months, years.
  16237. With a single plus or minus, the date is relative to today. With a double
  16238. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16239. +4d --> four days from today
  16240. +4 --> same as above
  16241. +2w --> two weeks from today
  16242. ++5 --> five days from default date
  16243. The function understands only English month and weekday abbreviations,
  16244. but this can be configured with the variables `parse-time-months' and
  16245. `parse-time-weekdays'.
  16246. While prompting, a calendar is popped up - you can also select the
  16247. date with the mouse (button 1). The calendar shows a period of three
  16248. months. To scroll it to other months, use the keys `>' and `<'.
  16249. If you don't like the calendar, turn it off with
  16250. \(setq org-read-date-popup-calendar nil)
  16251. With optional argument TO-TIME, the date will immediately be converted
  16252. to an internal time.
  16253. With an optional argument WITH-TIME, the prompt will suggest to also
  16254. insert a time. Note that when WITH-TIME is not set, you can still
  16255. enter a time, and this function will inform the calling routine about
  16256. this change. The calling routine may then choose to change the format
  16257. used to insert the time stamp into the buffer to include the time.
  16258. With optional argument FROM-STRING, read from this string instead from
  16259. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16260. the time/date that is used for everything that is not specified by the
  16261. user."
  16262. (require 'parse-time)
  16263. (let* ((org-time-stamp-rounding-minutes
  16264. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  16265. (org-dcst org-display-custom-times)
  16266. (ct (org-current-time))
  16267. (def (or default-time ct))
  16268. (defdecode (decode-time def))
  16269. (dummy (progn
  16270. (when (< (nth 2 defdecode) org-extend-today-until)
  16271. (setcar (nthcdr 2 defdecode) -1)
  16272. (setcar (nthcdr 1 defdecode) 59)
  16273. (setq def (apply 'encode-time defdecode)
  16274. defdecode (decode-time def)))))
  16275. (calendar-move-hook nil)
  16276. (view-diary-entries-initially nil)
  16277. (view-calendar-holidays-initially nil)
  16278. (timestr (format-time-string
  16279. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16280. (prompt (concat (if prompt (concat prompt " ") "")
  16281. (format "Date+time [%s]: " timestr)))
  16282. ans (org-ans0 "") org-ans1 org-ans2 final)
  16283. (cond
  16284. (from-string (setq ans from-string))
  16285. (org-read-date-popup-calendar
  16286. (save-excursion
  16287. (save-window-excursion
  16288. (calendar)
  16289. (calendar-forward-day (- (time-to-days def)
  16290. (calendar-absolute-from-gregorian
  16291. (calendar-current-date))))
  16292. (org-eval-in-calendar nil t)
  16293. (let* ((old-map (current-local-map))
  16294. (map (copy-keymap calendar-mode-map))
  16295. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16296. (org-defkey map (kbd "RET") 'org-calendar-select)
  16297. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16298. 'org-calendar-select-mouse)
  16299. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16300. 'org-calendar-select-mouse)
  16301. (org-defkey minibuffer-local-map [(meta shift left)]
  16302. (lambda () (interactive)
  16303. (org-eval-in-calendar '(calendar-backward-month 1))))
  16304. (org-defkey minibuffer-local-map [(meta shift right)]
  16305. (lambda () (interactive)
  16306. (org-eval-in-calendar '(calendar-forward-month 1))))
  16307. (org-defkey minibuffer-local-map [(meta shift up)]
  16308. (lambda () (interactive)
  16309. (org-eval-in-calendar '(calendar-backward-year 1))))
  16310. (org-defkey minibuffer-local-map [(meta shift down)]
  16311. (lambda () (interactive)
  16312. (org-eval-in-calendar '(calendar-forward-year 1))))
  16313. (org-defkey minibuffer-local-map [(shift up)]
  16314. (lambda () (interactive)
  16315. (org-eval-in-calendar '(calendar-backward-week 1))))
  16316. (org-defkey minibuffer-local-map [(shift down)]
  16317. (lambda () (interactive)
  16318. (org-eval-in-calendar '(calendar-forward-week 1))))
  16319. (org-defkey minibuffer-local-map [(shift left)]
  16320. (lambda () (interactive)
  16321. (org-eval-in-calendar '(calendar-backward-day 1))))
  16322. (org-defkey minibuffer-local-map [(shift right)]
  16323. (lambda () (interactive)
  16324. (org-eval-in-calendar '(calendar-forward-day 1))))
  16325. (org-defkey minibuffer-local-map ">"
  16326. (lambda () (interactive)
  16327. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16328. (org-defkey minibuffer-local-map "<"
  16329. (lambda () (interactive)
  16330. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16331. (unwind-protect
  16332. (progn
  16333. (use-local-map map)
  16334. (add-hook 'post-command-hook 'org-read-date-display)
  16335. (setq org-ans0 (read-string prompt default-input nil nil))
  16336. ;; org-ans0: from prompt
  16337. ;; org-ans1: from mouse click
  16338. ;; org-ans2: from calendar motion
  16339. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16340. (remove-hook 'post-command-hook 'org-read-date-display)
  16341. (use-local-map old-map)
  16342. (when org-read-date-overlay
  16343. (org-delete-overlay org-read-date-overlay)
  16344. (setq org-read-date-overlay nil)))))))
  16345. (t ; Naked prompt only
  16346. (unwind-protect
  16347. (setq ans (read-string prompt default-input nil timestr))
  16348. (when org-read-date-overlay
  16349. (org-delete-overlay org-read-date-overlay)
  16350. (setq org-read-date-overlay nil)))))
  16351. (setq final (org-read-date-analyze ans def defdecode))
  16352. (if to-time
  16353. (apply 'encode-time final)
  16354. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16355. (format "%04d-%02d-%02d %02d:%02d"
  16356. (nth 5 final) (nth 4 final) (nth 3 final)
  16357. (nth 2 final) (nth 1 final))
  16358. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16359. (defvar def)
  16360. (defvar defdecode)
  16361. (defvar with-time)
  16362. (defun org-read-date-display ()
  16363. "Display the currrent date prompt interpretation in the minibuffer."
  16364. (when org-read-date-display-live
  16365. (when org-read-date-overlay
  16366. (org-delete-overlay org-read-date-overlay))
  16367. (let ((p (point)))
  16368. (end-of-line 1)
  16369. (while (not (equal (buffer-substring
  16370. (max (point-min) (- (point) 4)) (point))
  16371. " "))
  16372. (insert " "))
  16373. (goto-char p))
  16374. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16375. " " (or org-ans1 org-ans2)))
  16376. (org-end-time-was-given nil)
  16377. (f (org-read-date-analyze ans def defdecode))
  16378. (fmts (if org-dcst
  16379. org-time-stamp-custom-formats
  16380. org-time-stamp-formats))
  16381. (fmt (if (or with-time
  16382. (and (boundp 'org-time-was-given) org-time-was-given))
  16383. (cdr fmts)
  16384. (car fmts)))
  16385. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16386. (when (and org-end-time-was-given
  16387. (string-match org-plain-time-of-day-regexp txt))
  16388. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16389. org-end-time-was-given
  16390. (substring txt (match-end 0)))))
  16391. (setq org-read-date-overlay
  16392. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16393. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16394. (defun org-read-date-analyze (ans def defdecode)
  16395. "Analyze the combined answer of the date prompt."
  16396. ;; FIXME: cleanup and comment
  16397. (let (delta deltan deltaw deltadef year month day
  16398. hour minute second wday pm h2 m2 tl wday1)
  16399. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16400. (setq ans (replace-match "" t t ans)
  16401. deltan (car delta)
  16402. deltaw (nth 1 delta)
  16403. deltadef (nth 2 delta)))
  16404. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16405. (when (string-match
  16406. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16407. (setq year (if (match-end 2)
  16408. (string-to-number (match-string 2 ans))
  16409. (string-to-number (format-time-string "%Y")))
  16410. month (string-to-number (match-string 3 ans))
  16411. day (string-to-number (match-string 4 ans)))
  16412. (if (< year 100) (setq year (+ 2000 year)))
  16413. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16414. t nil ans)))
  16415. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16416. ;; If there is a time with am/pm, and *no* time without it, we convert
  16417. ;; so that matching will be successful.
  16418. (loop for i from 1 to 2 do ; twice, for end time as well
  16419. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16420. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16421. (setq hour (string-to-number (match-string 1 ans))
  16422. minute (if (match-end 3)
  16423. (string-to-number (match-string 3 ans))
  16424. 0)
  16425. pm (equal ?p
  16426. (string-to-char (downcase (match-string 4 ans)))))
  16427. (if (and (= hour 12) (not pm))
  16428. (setq hour 0)
  16429. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16430. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16431. t t ans))))
  16432. ;; Check if a time range is given as a duration
  16433. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16434. (setq hour (string-to-number (match-string 1 ans))
  16435. h2 (+ hour (string-to-number (match-string 3 ans)))
  16436. minute (string-to-number (match-string 2 ans))
  16437. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16438. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16439. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16440. ;; Check if there is a time range
  16441. (when (boundp 'org-end-time-was-given)
  16442. (setq org-time-was-given nil)
  16443. (when (and (string-match org-plain-time-of-day-regexp ans)
  16444. (match-end 8))
  16445. (setq org-end-time-was-given (match-string 8 ans))
  16446. (setq ans (concat (substring ans 0 (match-beginning 7))
  16447. (substring ans (match-end 7))))))
  16448. (setq tl (parse-time-string ans)
  16449. day (or (nth 3 tl) (nth 3 defdecode))
  16450. month (or (nth 4 tl)
  16451. (if (and org-read-date-prefer-future
  16452. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16453. (1+ (nth 4 defdecode))
  16454. (nth 4 defdecode)))
  16455. year (or (nth 5 tl)
  16456. (if (and org-read-date-prefer-future
  16457. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16458. (1+ (nth 5 defdecode))
  16459. (nth 5 defdecode)))
  16460. hour (or (nth 2 tl) (nth 2 defdecode))
  16461. minute (or (nth 1 tl) (nth 1 defdecode))
  16462. second (or (nth 0 tl) 0)
  16463. wday (nth 6 tl))
  16464. (when deltan
  16465. (unless deltadef
  16466. (let ((now (decode-time (current-time))))
  16467. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16468. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16469. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16470. ((equal deltaw "m") (setq month (+ month deltan)))
  16471. ((equal deltaw "y") (setq year (+ year deltan)))))
  16472. (when (and wday (not (nth 3 tl)))
  16473. ;; Weekday was given, but no day, so pick that day in the week
  16474. ;; on or after the derived date.
  16475. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16476. (unless (equal wday wday1)
  16477. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16478. (if (and (boundp 'org-time-was-given)
  16479. (nth 2 tl))
  16480. (setq org-time-was-given t))
  16481. (if (< year 100) (setq year (+ 2000 year)))
  16482. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16483. (list second minute hour day month year)))
  16484. (defvar parse-time-weekdays)
  16485. (defun org-read-date-get-relative (s today default)
  16486. "Check string S for special relative date string.
  16487. TODAY and DEFAULT are internal times, for today and for a default.
  16488. Return shift list (N what def-flag)
  16489. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16490. N is the number of WHATs to shift.
  16491. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16492. the DEFAULT date rather than TODAY."
  16493. (when (string-match
  16494. (concat
  16495. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16496. "\\([0-9]+\\)?"
  16497. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16498. "\\([ \t]\\|$\\)") s)
  16499. (let* ((dir (if (match-end 1)
  16500. (string-to-char (substring (match-string 1 s) -1))
  16501. ?+))
  16502. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16503. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16504. (what (if (match-end 3) (match-string 3 s) "d"))
  16505. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16506. (date (if rel default today))
  16507. (wday (nth 6 (decode-time date)))
  16508. delta)
  16509. (if wday1
  16510. (progn
  16511. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16512. (if (= dir ?-) (setq delta (- delta 7)))
  16513. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16514. (list delta "d" rel))
  16515. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16516. (defun org-eval-in-calendar (form &optional keepdate)
  16517. "Eval FORM in the calendar window and return to current window.
  16518. Also, store the cursor date in variable org-ans2."
  16519. (let ((sw (selected-window)))
  16520. (select-window (get-buffer-window "*Calendar*"))
  16521. (eval form)
  16522. (when (and (not keepdate) (calendar-cursor-to-date))
  16523. (let* ((date (calendar-cursor-to-date))
  16524. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16525. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16526. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16527. (select-window sw)))
  16528. ; ;; Update the prompt to show new default date
  16529. ; (save-excursion
  16530. ; (goto-char (point-min))
  16531. ; (when (and org-ans2
  16532. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16533. ; (get-text-property (match-end 0) 'field))
  16534. ; (let ((inhibit-read-only t))
  16535. ; (replace-match (concat "[" org-ans2 "]") t t)
  16536. ; (add-text-properties (point-min) (1+ (match-end 0))
  16537. ; (text-properties-at (1+ (point-min)))))))))
  16538. (defun org-calendar-select ()
  16539. "Return to `org-read-date' with the date currently selected.
  16540. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16541. (interactive)
  16542. (when (calendar-cursor-to-date)
  16543. (let* ((date (calendar-cursor-to-date))
  16544. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16545. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16546. (if (active-minibuffer-window) (exit-minibuffer))))
  16547. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16548. "Insert a date stamp for the date given by the internal TIME.
  16549. WITH-HM means, use the stamp format that includes the time of the day.
  16550. INACTIVE means use square brackets instead of angular ones, so that the
  16551. stamp will not contribute to the agenda.
  16552. PRE and POST are optional strings to be inserted before and after the
  16553. stamp.
  16554. The command returns the inserted time stamp."
  16555. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16556. stamp)
  16557. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16558. (insert-before-markers (or pre ""))
  16559. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16560. (when (listp extra)
  16561. (setq extra (car extra))
  16562. (if (and (stringp extra)
  16563. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16564. (setq extra (format "-%02d:%02d"
  16565. (string-to-number (match-string 1 extra))
  16566. (string-to-number (match-string 2 extra))))
  16567. (setq extra nil)))
  16568. (when extra
  16569. (backward-char 1)
  16570. (insert-before-markers extra)
  16571. (forward-char 1))
  16572. (insert-before-markers (or post ""))
  16573. stamp))
  16574. (defun org-toggle-time-stamp-overlays ()
  16575. "Toggle the use of custom time stamp formats."
  16576. (interactive)
  16577. (setq org-display-custom-times (not org-display-custom-times))
  16578. (unless org-display-custom-times
  16579. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16580. (while (setq p (next-single-property-change p 'display))
  16581. (if (and (get-text-property p 'display)
  16582. (eq (get-text-property p 'face) 'org-date))
  16583. (remove-text-properties
  16584. p (setq p (next-single-property-change p 'display))
  16585. '(display t))))
  16586. (set-buffer-modified-p bmp)))
  16587. (if (featurep 'xemacs)
  16588. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16589. (org-restart-font-lock)
  16590. (setq org-table-may-need-update t)
  16591. (if org-display-custom-times
  16592. (message "Time stamps are overlayed with custom format")
  16593. (message "Time stamp overlays removed")))
  16594. (defun org-display-custom-time (beg end)
  16595. "Overlay modified time stamp format over timestamp between BED and END."
  16596. (let* ((ts (buffer-substring beg end))
  16597. t1 w1 with-hm tf time str w2 (off 0))
  16598. (save-match-data
  16599. (setq t1 (org-parse-time-string ts t))
  16600. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16601. (setq off (- (match-end 0) (match-beginning 0)))))
  16602. (setq end (- end off))
  16603. (setq w1 (- end beg)
  16604. with-hm (and (nth 1 t1) (nth 2 t1))
  16605. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16606. time (org-fix-decoded-time t1)
  16607. str (org-add-props
  16608. (format-time-string
  16609. (substring tf 1 -1) (apply 'encode-time time))
  16610. nil 'mouse-face 'highlight)
  16611. w2 (length str))
  16612. (if (not (= w2 w1))
  16613. (add-text-properties (1+ beg) (+ 2 beg)
  16614. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16615. (if (featurep 'xemacs)
  16616. (progn
  16617. (put-text-property beg end 'invisible t)
  16618. (put-text-property beg end 'end-glyph (make-glyph str)))
  16619. (put-text-property beg end 'display str))))
  16620. (defun org-translate-time (string)
  16621. "Translate all timestamps in STRING to custom format.
  16622. But do this only if the variable `org-display-custom-times' is set."
  16623. (when org-display-custom-times
  16624. (save-match-data
  16625. (let* ((start 0)
  16626. (re org-ts-regexp-both)
  16627. t1 with-hm inactive tf time str beg end)
  16628. (while (setq start (string-match re string start))
  16629. (setq beg (match-beginning 0)
  16630. end (match-end 0)
  16631. t1 (save-match-data
  16632. (org-parse-time-string (substring string beg end) t))
  16633. with-hm (and (nth 1 t1) (nth 2 t1))
  16634. inactive (equal (substring string beg (1+ beg)) "[")
  16635. tf (funcall (if with-hm 'cdr 'car)
  16636. org-time-stamp-custom-formats)
  16637. time (org-fix-decoded-time t1)
  16638. str (format-time-string
  16639. (concat
  16640. (if inactive "[" "<") (substring tf 1 -1)
  16641. (if inactive "]" ">"))
  16642. (apply 'encode-time time))
  16643. string (replace-match str t t string)
  16644. start (+ start (length str)))))))
  16645. string)
  16646. (defun org-fix-decoded-time (time)
  16647. "Set 0 instead of nil for the first 6 elements of time.
  16648. Don't touch the rest."
  16649. (let ((n 0))
  16650. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16651. (defun org-days-to-time (timestamp-string)
  16652. "Difference between TIMESTAMP-STRING and now in days."
  16653. (- (time-to-days (org-time-string-to-time timestamp-string))
  16654. (time-to-days (current-time))))
  16655. (defun org-deadline-close (timestamp-string &optional ndays)
  16656. "Is the time in TIMESTAMP-STRING close to the current date?"
  16657. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16658. (and (< (org-days-to-time timestamp-string) ndays)
  16659. (not (org-entry-is-done-p))))
  16660. (defun org-get-wdays (ts)
  16661. "Get the deadline lead time appropriate for timestring TS."
  16662. (cond
  16663. ((<= org-deadline-warning-days 0)
  16664. ;; 0 or negative, enforce this value no matter what
  16665. (- org-deadline-warning-days))
  16666. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16667. ;; lead time is specified.
  16668. (floor (* (string-to-number (match-string 1 ts))
  16669. (cdr (assoc (match-string 2 ts)
  16670. '(("d" . 1) ("w" . 7)
  16671. ("m" . 30.4) ("y" . 365.25)))))))
  16672. ;; go for the default.
  16673. (t org-deadline-warning-days)))
  16674. (defun org-calendar-select-mouse (ev)
  16675. "Return to `org-read-date' with the date currently selected.
  16676. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16677. (interactive "e")
  16678. (mouse-set-point ev)
  16679. (when (calendar-cursor-to-date)
  16680. (let* ((date (calendar-cursor-to-date))
  16681. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16682. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16683. (if (active-minibuffer-window) (exit-minibuffer))))
  16684. (defun org-check-deadlines (ndays)
  16685. "Check if there are any deadlines due or past due.
  16686. A deadline is considered due if it happens within `org-deadline-warning-days'
  16687. days from today's date. If the deadline appears in an entry marked DONE,
  16688. it is not shown. The prefix arg NDAYS can be used to test that many
  16689. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16690. (interactive "P")
  16691. (let* ((org-warn-days
  16692. (cond
  16693. ((equal ndays '(4)) 100000)
  16694. (ndays (prefix-numeric-value ndays))
  16695. (t (abs org-deadline-warning-days))))
  16696. (case-fold-search nil)
  16697. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16698. (callback
  16699. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16700. (message "%d deadlines past-due or due within %d days"
  16701. (org-occur regexp nil callback)
  16702. org-warn-days)))
  16703. (defun org-check-before-date (date)
  16704. "Check if there are deadlines or scheduled entries before DATE."
  16705. (interactive (list (org-read-date)))
  16706. (let ((case-fold-search nil)
  16707. (regexp (concat "\\<\\(" org-deadline-string
  16708. "\\|" org-scheduled-string
  16709. "\\) *<\\([^>]+\\)>"))
  16710. (callback
  16711. (lambda () (time-less-p
  16712. (org-time-string-to-time (match-string 2))
  16713. (org-time-string-to-time date)))))
  16714. (message "%d entries before %s"
  16715. (org-occur regexp nil callback) date)))
  16716. (defun org-evaluate-time-range (&optional to-buffer)
  16717. "Evaluate a time range by computing the difference between start and end.
  16718. Normally the result is just printed in the echo area, but with prefix arg
  16719. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16720. If the time range is actually in a table, the result is inserted into the
  16721. next column.
  16722. For time difference computation, a year is assumed to be exactly 365
  16723. days in order to avoid rounding problems."
  16724. (interactive "P")
  16725. (or
  16726. (org-clock-update-time-maybe)
  16727. (save-excursion
  16728. (unless (org-at-date-range-p t)
  16729. (goto-char (point-at-bol))
  16730. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16731. (if (not (org-at-date-range-p t))
  16732. (error "Not at a time-stamp range, and none found in current line")))
  16733. (let* ((ts1 (match-string 1))
  16734. (ts2 (match-string 2))
  16735. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16736. (match-end (match-end 0))
  16737. (time1 (org-time-string-to-time ts1))
  16738. (time2 (org-time-string-to-time ts2))
  16739. (t1 (time-to-seconds time1))
  16740. (t2 (time-to-seconds time2))
  16741. (diff (abs (- t2 t1)))
  16742. (negative (< (- t2 t1) 0))
  16743. ;; (ys (floor (* 365 24 60 60)))
  16744. (ds (* 24 60 60))
  16745. (hs (* 60 60))
  16746. (fy "%dy %dd %02d:%02d")
  16747. (fy1 "%dy %dd")
  16748. (fd "%dd %02d:%02d")
  16749. (fd1 "%dd")
  16750. (fh "%02d:%02d")
  16751. y d h m align)
  16752. (if havetime
  16753. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16754. y 0
  16755. d (floor (/ diff ds)) diff (mod diff ds)
  16756. h (floor (/ diff hs)) diff (mod diff hs)
  16757. m (floor (/ diff 60)))
  16758. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16759. y 0
  16760. d (floor (+ (/ diff ds) 0.5))
  16761. h 0 m 0))
  16762. (if (not to-buffer)
  16763. (message "%s" (org-make-tdiff-string y d h m))
  16764. (if (org-at-table-p)
  16765. (progn
  16766. (goto-char match-end)
  16767. (setq align t)
  16768. (and (looking-at " *|") (goto-char (match-end 0))))
  16769. (goto-char match-end))
  16770. (if (looking-at
  16771. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16772. (replace-match ""))
  16773. (if negative (insert " -"))
  16774. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16775. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16776. (insert " " (format fh h m))))
  16777. (if align (org-table-align))
  16778. (message "Time difference inserted")))))
  16779. (defun org-make-tdiff-string (y d h m)
  16780. (let ((fmt "")
  16781. (l nil))
  16782. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16783. l (push y l)))
  16784. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16785. l (push d l)))
  16786. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16787. l (push h l)))
  16788. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16789. l (push m l)))
  16790. (apply 'format fmt (nreverse l))))
  16791. (defun org-time-string-to-time (s)
  16792. (apply 'encode-time (org-parse-time-string s)))
  16793. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16794. "Convert a time stamp to an absolute day number.
  16795. If there is a specifyer for a cyclic time stamp, get the closest date to
  16796. DAYNR."
  16797. (cond
  16798. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16799. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16800. daynr
  16801. (+ daynr 1000)))
  16802. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16803. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16804. (time-to-days (current-time))) (match-string 0 s)
  16805. prefer))
  16806. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16807. (defun org-time-from-absolute (d)
  16808. "Return the time corresponding to date D.
  16809. D may be an absolute day number, or a calendar-type list (month day year)."
  16810. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16811. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16812. (defun org-calendar-holiday ()
  16813. "List of holidays, for Diary display in Org-mode."
  16814. (require 'holidays)
  16815. (let ((hl (funcall
  16816. (if (fboundp 'calendar-check-holidays)
  16817. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16818. (if hl (mapconcat 'identity hl "; "))))
  16819. (defun org-diary-sexp-entry (sexp entry date)
  16820. "Process a SEXP diary ENTRY for DATE."
  16821. (require 'diary-lib)
  16822. (let ((result (if calendar-debug-sexp
  16823. (let ((stack-trace-on-error t))
  16824. (eval (car (read-from-string sexp))))
  16825. (condition-case nil
  16826. (eval (car (read-from-string sexp)))
  16827. (error
  16828. (beep)
  16829. (message "Bad sexp at line %d in %s: %s"
  16830. (org-current-line)
  16831. (buffer-file-name) sexp)
  16832. (sleep-for 2))))))
  16833. (cond ((stringp result) result)
  16834. ((and (consp result)
  16835. (stringp (cdr result))) (cdr result))
  16836. (result entry)
  16837. (t nil))))
  16838. (defun org-diary-to-ical-string (frombuf)
  16839. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16840. This uses the icalendar.el library."
  16841. (let* ((tmpdir (if (featurep 'xemacs)
  16842. (temp-directory)
  16843. temporary-file-directory))
  16844. (tmpfile (make-temp-name
  16845. (expand-file-name "orgics" tmpdir)))
  16846. buf rtn b e)
  16847. (save-excursion
  16848. (set-buffer frombuf)
  16849. (icalendar-export-region (point-min) (point-max) tmpfile)
  16850. (setq buf (find-buffer-visiting tmpfile))
  16851. (set-buffer buf)
  16852. (goto-char (point-min))
  16853. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16854. (setq b (match-beginning 0)))
  16855. (goto-char (point-max))
  16856. (if (re-search-backward "^END:VEVENT" nil t)
  16857. (setq e (match-end 0)))
  16858. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16859. (kill-buffer buf)
  16860. (kill-buffer frombuf)
  16861. (delete-file tmpfile)
  16862. rtn))
  16863. (defun org-closest-date (start current change prefer)
  16864. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16865. When PREFER is `past' return a date that is either CURRENT or past.
  16866. When PREFER is `future', return a date that is either CURRENT or future."
  16867. ;; Make the proper lists from the dates
  16868. (catch 'exit
  16869. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16870. dn dw sday cday n1 n2
  16871. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16872. (setq start (org-date-to-gregorian start)
  16873. current (org-date-to-gregorian
  16874. (if org-agenda-repeating-timestamp-show-all
  16875. current
  16876. (time-to-days (current-time))))
  16877. sday (calendar-absolute-from-gregorian start)
  16878. cday (calendar-absolute-from-gregorian current))
  16879. (if (<= cday sday) (throw 'exit sday))
  16880. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16881. (setq dn (string-to-number (match-string 1 change))
  16882. dw (cdr (assoc (match-string 2 change) a1)))
  16883. (error "Invalid change specifyer: %s" change))
  16884. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16885. (cond
  16886. ((eq dw 'day)
  16887. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16888. n2 (+ n1 dn)))
  16889. ((eq dw 'year)
  16890. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16891. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16892. (setq date1 (list m d y1)
  16893. n1 (calendar-absolute-from-gregorian date1)
  16894. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16895. n2 (calendar-absolute-from-gregorian date2)))
  16896. ((eq dw 'month)
  16897. ;; approx number of month between the tow dates
  16898. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16899. ;; How often does dn fit in there?
  16900. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16901. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16902. m (+ m nm)
  16903. ny (floor (/ m 12))
  16904. y (+ y ny)
  16905. m (- m (* ny 12)))
  16906. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16907. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16908. (setq m2 (+ m dn) y2 y)
  16909. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16910. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16911. (while (< n2 cday)
  16912. (setq n1 n2 m m2 y y2)
  16913. (setq m2 (+ m dn) y2 y)
  16914. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16915. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16916. (if org-agenda-repeating-timestamp-show-all
  16917. (cond
  16918. ((eq prefer 'past) n1)
  16919. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16920. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16921. (cond
  16922. ((eq prefer 'past) n1)
  16923. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16924. (t (if (= cday n1) n1 n2)))))))
  16925. (defun org-date-to-gregorian (date)
  16926. "Turn any specification of DATE into a gregorian date for the calendar."
  16927. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16928. ((and (listp date) (= (length date) 3)) date)
  16929. ((stringp date)
  16930. (setq date (org-parse-time-string date))
  16931. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16932. ((listp date)
  16933. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16934. (defun org-parse-time-string (s &optional nodefault)
  16935. "Parse the standard Org-mode time string.
  16936. This should be a lot faster than the normal `parse-time-string'.
  16937. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16938. hour and minute fields will be nil if not given."
  16939. (if (string-match org-ts-regexp0 s)
  16940. (list 0
  16941. (if (or (match-beginning 8) (not nodefault))
  16942. (string-to-number (or (match-string 8 s) "0")))
  16943. (if (or (match-beginning 7) (not nodefault))
  16944. (string-to-number (or (match-string 7 s) "0")))
  16945. (string-to-number (match-string 4 s))
  16946. (string-to-number (match-string 3 s))
  16947. (string-to-number (match-string 2 s))
  16948. nil nil nil)
  16949. (make-list 9 0)))
  16950. (defun org-timestamp-up (&optional arg)
  16951. "Increase the date item at the cursor by one.
  16952. If the cursor is on the year, change the year. If it is on the month or
  16953. the day, change that.
  16954. With prefix ARG, change by that many units."
  16955. (interactive "p")
  16956. (org-timestamp-change (prefix-numeric-value arg)))
  16957. (defun org-timestamp-down (&optional arg)
  16958. "Decrease the date item at the cursor by one.
  16959. If the cursor is on the year, change the year. If it is on the month or
  16960. the day, change that.
  16961. With prefix ARG, change by that many units."
  16962. (interactive "p")
  16963. (org-timestamp-change (- (prefix-numeric-value arg))))
  16964. (defun org-timestamp-up-day (&optional arg)
  16965. "Increase the date in the time stamp by one day.
  16966. With prefix ARG, change that many days."
  16967. (interactive "p")
  16968. (if (and (not (org-at-timestamp-p t))
  16969. (org-on-heading-p))
  16970. (org-todo 'up)
  16971. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16972. (defun org-timestamp-down-day (&optional arg)
  16973. "Decrease the date in the time stamp by one day.
  16974. With prefix ARG, change that many days."
  16975. (interactive "p")
  16976. (if (and (not (org-at-timestamp-p t))
  16977. (org-on-heading-p))
  16978. (org-todo 'down)
  16979. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16980. (defsubst org-pos-in-match-range (pos n)
  16981. (and (match-beginning n)
  16982. (<= (match-beginning n) pos)
  16983. (>= (match-end n) pos)))
  16984. (defun org-at-timestamp-p (&optional inactive-ok)
  16985. "Determine if the cursor is in or at a timestamp."
  16986. (interactive)
  16987. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16988. (pos (point))
  16989. (ans (or (looking-at tsr)
  16990. (save-excursion
  16991. (skip-chars-backward "^[<\n\r\t")
  16992. (if (> (point) (point-min)) (backward-char 1))
  16993. (and (looking-at tsr)
  16994. (> (- (match-end 0) pos) -1))))))
  16995. (and ans
  16996. (boundp 'org-ts-what)
  16997. (setq org-ts-what
  16998. (cond
  16999. ((= pos (match-beginning 0)) 'bracket)
  17000. ((= pos (1- (match-end 0))) 'bracket)
  17001. ((org-pos-in-match-range pos 2) 'year)
  17002. ((org-pos-in-match-range pos 3) 'month)
  17003. ((org-pos-in-match-range pos 7) 'hour)
  17004. ((org-pos-in-match-range pos 8) 'minute)
  17005. ((or (org-pos-in-match-range pos 4)
  17006. (org-pos-in-match-range pos 5)) 'day)
  17007. ((and (> pos (or (match-end 8) (match-end 5)))
  17008. (< pos (match-end 0)))
  17009. (- pos (or (match-end 8) (match-end 5))))
  17010. (t 'day))))
  17011. ans))
  17012. (defun org-toggle-timestamp-type ()
  17013. "Toggle the type (<active> or [inactive]) of a time stamp."
  17014. (interactive)
  17015. (when (org-at-timestamp-p t)
  17016. (save-excursion
  17017. (goto-char (match-beginning 0))
  17018. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  17019. (goto-char (1- (match-end 0)))
  17020. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  17021. (message "Timestamp is now %sactive"
  17022. (if (equal (char-before) ?>) "in" ""))))
  17023. (defun org-timestamp-change (n &optional what)
  17024. "Change the date in the time stamp at point.
  17025. The date will be changed by N times WHAT. WHAT can be `day', `month',
  17026. `year', `minute', `second'. If WHAT is not given, the cursor position
  17027. in the timestamp determines what will be changed."
  17028. (let ((pos (point))
  17029. with-hm inactive
  17030. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  17031. org-ts-what
  17032. extra rem
  17033. ts time time0)
  17034. (if (not (org-at-timestamp-p t))
  17035. (error "Not at a timestamp"))
  17036. (if (and (not what) (eq org-ts-what 'bracket))
  17037. (org-toggle-timestamp-type)
  17038. (if (and (not what) (not (eq org-ts-what 'day))
  17039. org-display-custom-times
  17040. (get-text-property (point) 'display)
  17041. (not (get-text-property (1- (point)) 'display)))
  17042. (setq org-ts-what 'day))
  17043. (setq org-ts-what (or what org-ts-what)
  17044. inactive (= (char-after (match-beginning 0)) ?\[)
  17045. ts (match-string 0))
  17046. (replace-match "")
  17047. (if (string-match
  17048. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17049. ts)
  17050. (setq extra (match-string 1 ts)))
  17051. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17052. (setq with-hm t))
  17053. (setq time0 (org-parse-time-string ts))
  17054. (when (and (eq org-ts-what 'minute)
  17055. (eq current-prefix-arg nil))
  17056. (setq n (* dm (org-no-warnings (signum n))))
  17057. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  17058. (setcar (cdr time0) (+ (nth 1 time0)
  17059. (if (> n 0) (- rem) (- dm rem))))))
  17060. (setq time
  17061. (encode-time (or (car time0) 0)
  17062. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17063. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17064. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17065. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17066. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17067. (nthcdr 6 time0)))
  17068. (when (integerp org-ts-what)
  17069. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17070. (if (eq what 'calendar)
  17071. (let ((cal-date (org-get-date-from-calendar)))
  17072. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17073. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17074. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17075. (setcar time0 (or (car time0) 0))
  17076. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17077. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17078. (setq time (apply 'encode-time time0))))
  17079. (setq org-last-changed-timestamp
  17080. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17081. (org-clock-update-time-maybe)
  17082. (goto-char pos)
  17083. ;; Try to recenter the calendar window, if any
  17084. (if (and org-calendar-follow-timestamp-change
  17085. (get-buffer-window "*Calendar*" t)
  17086. (memq org-ts-what '(day month year)))
  17087. (org-recenter-calendar (time-to-days time))))))
  17088. ;; FIXME: does not yet work for lead times
  17089. (defun org-modify-ts-extra (s pos n)
  17090. "Change the different parts of the lead-time and repeat fields in timestamp."
  17091. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17092. ng h m new)
  17093. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17094. (cond
  17095. ((or (org-pos-in-match-range pos 2)
  17096. (org-pos-in-match-range pos 3))
  17097. (setq m (string-to-number (match-string 3 s))
  17098. h (string-to-number (match-string 2 s)))
  17099. (if (org-pos-in-match-range pos 2)
  17100. (setq h (+ h n))
  17101. (setq m (+ m n)))
  17102. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17103. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17104. (setq h (min 24 (max 0 h)))
  17105. (setq ng 1 new (format "-%02d:%02d" h m)))
  17106. ((org-pos-in-match-range pos 6)
  17107. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17108. ((org-pos-in-match-range pos 5)
  17109. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17110. (when ng
  17111. (setq s (concat
  17112. (substring s 0 (match-beginning ng))
  17113. new
  17114. (substring s (match-end ng))))))
  17115. s))
  17116. (defun org-recenter-calendar (date)
  17117. "If the calendar is visible, recenter it to DATE."
  17118. (let* ((win (selected-window))
  17119. (cwin (get-buffer-window "*Calendar*" t))
  17120. (calendar-move-hook nil))
  17121. (when cwin
  17122. (select-window cwin)
  17123. (calendar-goto-date (if (listp date) date
  17124. (calendar-gregorian-from-absolute date)))
  17125. (select-window win))))
  17126. (defun org-goto-calendar (&optional arg)
  17127. "Go to the Emacs calendar at the current date.
  17128. If there is a time stamp in the current line, go to that date.
  17129. A prefix ARG can be used to force the current date."
  17130. (interactive "P")
  17131. (let ((tsr org-ts-regexp) diff
  17132. (calendar-move-hook nil)
  17133. (view-calendar-holidays-initially nil)
  17134. (view-diary-entries-initially nil))
  17135. (if (or (org-at-timestamp-p)
  17136. (save-excursion
  17137. (beginning-of-line 1)
  17138. (looking-at (concat ".*" tsr))))
  17139. (let ((d1 (time-to-days (current-time)))
  17140. (d2 (time-to-days
  17141. (org-time-string-to-time (match-string 1)))))
  17142. (setq diff (- d2 d1))))
  17143. (calendar)
  17144. (calendar-goto-today)
  17145. (if (and diff (not arg)) (calendar-forward-day diff))))
  17146. (defun org-get-date-from-calendar ()
  17147. "Return a list (month day year) of date at point in calendar."
  17148. (with-current-buffer "*Calendar*"
  17149. (save-match-data
  17150. (calendar-cursor-to-date))))
  17151. (defun org-date-from-calendar ()
  17152. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17153. If there is already a time stamp at the cursor position, update it."
  17154. (interactive)
  17155. (if (org-at-timestamp-p t)
  17156. (org-timestamp-change 0 'calendar)
  17157. (let ((cal-date (org-get-date-from-calendar)))
  17158. (org-insert-time-stamp
  17159. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17160. (defvar appt-time-msg-list)
  17161. ;;;###autoload
  17162. (defun org-agenda-to-appt (&optional refresh filter)
  17163. "Activate appointments found in `org-agenda-files'.
  17164. With a \\[universal-argument] prefix, refresh the list of
  17165. appointements.
  17166. If FILTER is t, interactively prompt the user for a regular
  17167. expression, and filter out entries that don't match it.
  17168. If FILTER is a string, use this string as a regular expression
  17169. for filtering entries out.
  17170. FILTER can also be an alist with the car of each cell being
  17171. either 'headline or 'category. For example:
  17172. '((headline \"IMPORTANT\")
  17173. (category \"Work\"))
  17174. will only add headlines containing IMPORTANT or headlines
  17175. belonging to the \"Work\" category."
  17176. (interactive "P")
  17177. (require 'calendar)
  17178. (if refresh (setq appt-time-msg-list nil))
  17179. (if (eq filter t)
  17180. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17181. (let* ((cnt 0) ; count added events
  17182. (org-agenda-new-buffers nil)
  17183. (org-deadline-warning-days 0)
  17184. (today (org-date-to-gregorian
  17185. (time-to-days (current-time))))
  17186. (files (org-agenda-files)) entries file)
  17187. ;; Get all entries which may contain an appt
  17188. (while (setq file (pop files))
  17189. (setq entries
  17190. (append entries
  17191. (org-agenda-get-day-entries
  17192. file today :timestamp :scheduled :deadline))))
  17193. (setq entries (delq nil entries))
  17194. ;; Map thru entries and find if we should filter them out
  17195. (mapc
  17196. (lambda(x)
  17197. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17198. (cat (get-text-property 1 'org-category x))
  17199. (tod (get-text-property 1 'time-of-day x))
  17200. (ok (or (null filter)
  17201. (and (stringp filter) (string-match filter evt))
  17202. (and (listp filter)
  17203. (or (string-match
  17204. (cadr (assoc 'category filter)) cat)
  17205. (string-match
  17206. (cadr (assoc 'headline filter)) evt))))))
  17207. ;; FIXME: Shall we remove text-properties for the appt text?
  17208. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17209. (when (and ok tod)
  17210. (setq tod (number-to-string tod)
  17211. tod (when (string-match
  17212. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17213. (concat (match-string 1 tod) ":"
  17214. (match-string 2 tod))))
  17215. (appt-add tod evt)
  17216. (setq cnt (1+ cnt))))) entries)
  17217. (org-release-buffers org-agenda-new-buffers)
  17218. (if (eq cnt 0)
  17219. (message "No event to add")
  17220. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17221. ;;; The clock for measuring work time.
  17222. (defvar org-mode-line-string "")
  17223. (put 'org-mode-line-string 'risky-local-variable t)
  17224. (defvar org-mode-line-timer nil)
  17225. (defvar org-clock-heading "")
  17226. (defvar org-clock-start-time "")
  17227. (defun org-update-mode-line ()
  17228. (let* ((delta (- (time-to-seconds (current-time))
  17229. (time-to-seconds org-clock-start-time)))
  17230. (h (floor delta 3600))
  17231. (m (floor (- delta (* 3600 h)) 60)))
  17232. (setq org-mode-line-string
  17233. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17234. 'help-echo "Org-mode clock is running"))
  17235. (force-mode-line-update)))
  17236. (defvar org-clock-marker (make-marker)
  17237. "Marker recording the last clock-in.")
  17238. (defvar org-clock-mode-line-entry nil
  17239. "Information for the modeline about the running clock.")
  17240. (defun org-clock-in ()
  17241. "Start the clock on the current item.
  17242. If necessary, clock-out of the currently active clock."
  17243. (interactive)
  17244. (org-clock-out t)
  17245. (let (ts)
  17246. (save-excursion
  17247. (org-back-to-heading t)
  17248. (when (and org-clock-in-switch-to-state
  17249. (not (looking-at (concat outline-regexp "[ \t]*"
  17250. org-clock-in-switch-to-state
  17251. "\\>"))))
  17252. (org-todo org-clock-in-switch-to-state))
  17253. (if (and org-clock-heading-function
  17254. (functionp org-clock-heading-function))
  17255. (setq org-clock-heading (funcall org-clock-heading-function))
  17256. (if (looking-at org-complex-heading-regexp)
  17257. (setq org-clock-heading (match-string 4))
  17258. (setq org-clock-heading "???")))
  17259. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17260. (org-clock-find-position)
  17261. (insert "\n") (backward-char 1)
  17262. (indent-relative)
  17263. (insert org-clock-string " ")
  17264. (setq org-clock-start-time (current-time))
  17265. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17266. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17267. (or global-mode-string (setq global-mode-string '("")))
  17268. (or (memq 'org-mode-line-string global-mode-string)
  17269. (setq global-mode-string
  17270. (append global-mode-string '(org-mode-line-string))))
  17271. (org-update-mode-line)
  17272. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17273. (message "Clock started at %s" ts))))
  17274. (defun org-clock-find-position ()
  17275. "Find the location where the next clock line should be inserted."
  17276. (org-back-to-heading t)
  17277. (catch 'exit
  17278. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17279. (re (concat "^[ \t]*" org-clock-string))
  17280. (cnt 0)
  17281. first last)
  17282. (goto-char beg)
  17283. (when (eobp) (newline) (setq end (max (point) end)))
  17284. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17285. ;; we seem to have a CLOCK drawer, so go there.
  17286. (beginning-of-line 2)
  17287. (throw 'exit t))
  17288. ;; Lets count the CLOCK lines
  17289. (goto-char beg)
  17290. (while (re-search-forward re end t)
  17291. (setq first (or first (match-beginning 0))
  17292. last (match-beginning 0)
  17293. cnt (1+ cnt)))
  17294. (when (and (integerp org-clock-into-drawer)
  17295. (>= (1+ cnt) org-clock-into-drawer))
  17296. ;; Wrap current entries into a new drawer
  17297. (goto-char last)
  17298. (beginning-of-line 2)
  17299. (if (org-at-item-p) (org-end-of-item))
  17300. (insert ":END:\n")
  17301. (beginning-of-line 0)
  17302. (org-indent-line-function)
  17303. (goto-char first)
  17304. (insert ":CLOCK:\n")
  17305. (beginning-of-line 0)
  17306. (org-indent-line-function)
  17307. (org-flag-drawer t)
  17308. (beginning-of-line 2)
  17309. (throw 'exit nil))
  17310. (goto-char beg)
  17311. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17312. (not (equal (match-string 1) org-clock-string)))
  17313. ;; Planning info, skip to after it
  17314. (beginning-of-line 2)
  17315. (or (bolp) (newline)))
  17316. (when (eq t org-clock-into-drawer)
  17317. (insert ":CLOCK:\n:END:\n")
  17318. (beginning-of-line -1)
  17319. (org-indent-line-function)
  17320. (org-flag-drawer t)
  17321. (beginning-of-line 2)
  17322. (org-indent-line-function)))))
  17323. (defun org-clock-out (&optional fail-quietly)
  17324. "Stop the currently running clock.
  17325. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17326. (interactive)
  17327. (catch 'exit
  17328. (if (not (marker-buffer org-clock-marker))
  17329. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17330. (let (ts te s h m)
  17331. (save-excursion
  17332. (set-buffer (marker-buffer org-clock-marker))
  17333. (goto-char org-clock-marker)
  17334. (beginning-of-line 1)
  17335. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17336. (equal (match-string 1) org-clock-string))
  17337. (setq ts (match-string 2))
  17338. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17339. (goto-char (match-end 0))
  17340. (delete-region (point) (point-at-eol))
  17341. (insert "--")
  17342. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17343. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17344. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17345. h (floor (/ s 3600))
  17346. s (- s (* 3600 h))
  17347. m (floor (/ s 60))
  17348. s (- s (* 60 s)))
  17349. (insert " => " (format "%2d:%02d" h m))
  17350. (move-marker org-clock-marker nil)
  17351. (when org-log-note-clock-out
  17352. (org-add-log-maybe 'clock-out))
  17353. (when org-mode-line-timer
  17354. (cancel-timer org-mode-line-timer)
  17355. (setq org-mode-line-timer nil))
  17356. (setq global-mode-string
  17357. (delq 'org-mode-line-string global-mode-string))
  17358. (force-mode-line-update)
  17359. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17360. (defun org-clock-cancel ()
  17361. "Cancel the running clock be removing the start timestamp."
  17362. (interactive)
  17363. (if (not (marker-buffer org-clock-marker))
  17364. (error "No active clock"))
  17365. (save-excursion
  17366. (set-buffer (marker-buffer org-clock-marker))
  17367. (goto-char org-clock-marker)
  17368. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17369. (setq global-mode-string
  17370. (delq 'org-mode-line-string global-mode-string))
  17371. (force-mode-line-update)
  17372. (message "Clock canceled"))
  17373. (defun org-clock-goto (&optional delete-windows)
  17374. "Go to the currently clocked-in entry."
  17375. (interactive "P")
  17376. (if (not (marker-buffer org-clock-marker))
  17377. (error "No active clock"))
  17378. (switch-to-buffer-other-window
  17379. (marker-buffer org-clock-marker))
  17380. (if delete-windows (delete-other-windows))
  17381. (goto-char org-clock-marker)
  17382. (org-show-entry)
  17383. (org-back-to-heading)
  17384. (recenter))
  17385. (defvar org-clock-file-total-minutes nil
  17386. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17387. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17388. (defun org-clock-sum (&optional tstart tend)
  17389. "Sum the times for each subtree.
  17390. Puts the resulting times in minutes as a text property on each headline."
  17391. (interactive)
  17392. (let* ((bmp (buffer-modified-p))
  17393. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17394. org-clock-string
  17395. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17396. (lmax 30)
  17397. (ltimes (make-vector lmax 0))
  17398. (t1 0)
  17399. (level 0)
  17400. ts te dt
  17401. time)
  17402. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17403. (save-excursion
  17404. (goto-char (point-max))
  17405. (while (re-search-backward re nil t)
  17406. (cond
  17407. ((match-end 2)
  17408. ;; Two time stamps
  17409. (setq ts (match-string 2)
  17410. te (match-string 3)
  17411. ts (time-to-seconds
  17412. (apply 'encode-time (org-parse-time-string ts)))
  17413. te (time-to-seconds
  17414. (apply 'encode-time (org-parse-time-string te)))
  17415. ts (if tstart (max ts tstart) ts)
  17416. te (if tend (min te tend) te)
  17417. dt (- te ts)
  17418. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17419. ((match-end 4)
  17420. ;; A naket time
  17421. (setq t1 (+ t1 (string-to-number (match-string 5))
  17422. (* 60 (string-to-number (match-string 4))))))
  17423. (t ;; A headline
  17424. (setq level (- (match-end 1) (match-beginning 1)))
  17425. (when (or (> t1 0) (> (aref ltimes level) 0))
  17426. (loop for l from 0 to level do
  17427. (aset ltimes l (+ (aref ltimes l) t1)))
  17428. (setq t1 0 time (aref ltimes level))
  17429. (loop for l from level to (1- lmax) do
  17430. (aset ltimes l 0))
  17431. (goto-char (match-beginning 0))
  17432. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17433. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17434. (set-buffer-modified-p bmp)))
  17435. (defun org-clock-display (&optional total-only)
  17436. "Show subtree times in the entire buffer.
  17437. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17438. in the echo area."
  17439. (interactive)
  17440. (org-remove-clock-overlays)
  17441. (let (time h m p)
  17442. (org-clock-sum)
  17443. (unless total-only
  17444. (save-excursion
  17445. (goto-char (point-min))
  17446. (while (or (and (equal (setq p (point)) (point-min))
  17447. (get-text-property p :org-clock-minutes))
  17448. (setq p (next-single-property-change
  17449. (point) :org-clock-minutes)))
  17450. (goto-char p)
  17451. (when (setq time (get-text-property p :org-clock-minutes))
  17452. (org-put-clock-overlay time (funcall outline-level))))
  17453. (setq h (/ org-clock-file-total-minutes 60)
  17454. m (- org-clock-file-total-minutes (* 60 h)))
  17455. ;; Arrange to remove the overlays upon next change.
  17456. (when org-remove-highlights-with-change
  17457. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17458. nil 'local))))
  17459. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17460. (defvar org-clock-overlays nil)
  17461. (make-variable-buffer-local 'org-clock-overlays)
  17462. (defun org-put-clock-overlay (time &optional level)
  17463. "Put an overlays on the current line, displaying TIME.
  17464. If LEVEL is given, prefix time with a corresponding number of stars.
  17465. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17466. will be easy to remove."
  17467. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17468. (l (if level (org-get-legal-level level 0) 0))
  17469. (off 0)
  17470. ov tx)
  17471. (move-to-column c)
  17472. (unless (eolp) (skip-chars-backward "^ \t"))
  17473. (skip-chars-backward " \t")
  17474. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17475. tx (concat (buffer-substring (1- (point)) (point))
  17476. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17477. (org-add-props (format "%s %2d:%02d%s"
  17478. (make-string l ?*) h m
  17479. (make-string (- 16 l) ?\ ))
  17480. '(face secondary-selection))
  17481. ""))
  17482. (if (not (featurep 'xemacs))
  17483. (org-overlay-put ov 'display tx)
  17484. (org-overlay-put ov 'invisible t)
  17485. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17486. (push ov org-clock-overlays)))
  17487. (defun org-remove-clock-overlays (&optional beg end noremove)
  17488. "Remove the occur highlights from the buffer.
  17489. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17490. from the `before-change-functions' in the current buffer."
  17491. (interactive)
  17492. (unless org-inhibit-highlight-removal
  17493. (mapc 'org-delete-overlay org-clock-overlays)
  17494. (setq org-clock-overlays nil)
  17495. (unless noremove
  17496. (remove-hook 'before-change-functions
  17497. 'org-remove-clock-overlays 'local))))
  17498. (defun org-clock-out-if-current ()
  17499. "Clock out if the current entry contains the running clock.
  17500. This is used to stop the clock after a TODO entry is marked DONE,
  17501. and is only done if the variable `org-clock-out-when-done' is not nil."
  17502. (when (and org-clock-out-when-done
  17503. (member state org-done-keywords)
  17504. (equal (marker-buffer org-clock-marker) (current-buffer))
  17505. (< (point) org-clock-marker)
  17506. (> (save-excursion (outline-next-heading) (point))
  17507. org-clock-marker))
  17508. ;; Clock out, but don't accept a logging message for this.
  17509. (let ((org-log-note-clock-out nil))
  17510. (org-clock-out))))
  17511. (add-hook 'org-after-todo-state-change-hook
  17512. 'org-clock-out-if-current)
  17513. (defun org-check-running-clock ()
  17514. "Check if the current buffer contains the running clock.
  17515. If yes, offer to stop it and to save the buffer with the changes."
  17516. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17517. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17518. (buffer-name))))
  17519. (org-clock-out)
  17520. (when (y-or-n-p "Save changed buffer?")
  17521. (save-buffer))))
  17522. (defun org-clock-report (&optional arg)
  17523. "Create a table containing a report about clocked time.
  17524. If the cursor is inside an existing clocktable block, then the table
  17525. will be updated. If not, a new clocktable will be inserted.
  17526. When called with a prefix argument, move to the first clock table in the
  17527. buffer and update it."
  17528. (interactive "P")
  17529. (org-remove-clock-overlays)
  17530. (when arg
  17531. (org-find-dblock "clocktable")
  17532. (org-show-entry))
  17533. (if (org-in-clocktable-p)
  17534. (goto-char (org-in-clocktable-p))
  17535. (org-create-dblock (list :name "clocktable"
  17536. :maxlevel 2 :scope 'file)))
  17537. (org-update-dblock))
  17538. (defun org-in-clocktable-p ()
  17539. "Check if the cursor is in a clocktable."
  17540. (let ((pos (point)) start)
  17541. (save-excursion
  17542. (end-of-line 1)
  17543. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17544. (setq start (match-beginning 0))
  17545. (re-search-forward "^#\\+END:.*" nil t)
  17546. (>= (match-end 0) pos)
  17547. start))))
  17548. (defun org-clock-update-time-maybe ()
  17549. "If this is a CLOCK line, update it and return t.
  17550. Otherwise, return nil."
  17551. (interactive)
  17552. (save-excursion
  17553. (beginning-of-line 1)
  17554. (skip-chars-forward " \t")
  17555. (when (looking-at org-clock-string)
  17556. (let ((re (concat "[ \t]*" org-clock-string
  17557. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17558. "\\([ \t]*=>.*\\)?"))
  17559. ts te h m s)
  17560. (if (not (looking-at re))
  17561. nil
  17562. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17563. (end-of-line 1)
  17564. (setq ts (match-string 1)
  17565. te (match-string 2))
  17566. (setq s (- (time-to-seconds
  17567. (apply 'encode-time (org-parse-time-string te)))
  17568. (time-to-seconds
  17569. (apply 'encode-time (org-parse-time-string ts))))
  17570. h (floor (/ s 3600))
  17571. s (- s (* 3600 h))
  17572. m (floor (/ s 60))
  17573. s (- s (* 60 s)))
  17574. (insert " => " (format "%2d:%02d" h m))
  17575. t)))))
  17576. (defun org-clock-special-range (key &optional time as-strings)
  17577. "Return two times bordering a special time range.
  17578. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17579. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17580. A week starts Monday 0:00 and ends Sunday 24:00.
  17581. The range is determined relative to TIME. TIME defaults to the current time.
  17582. The return value is a cons cell with two internal times like the ones
  17583. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17584. the returned times will be formatted strings."
  17585. (let* ((tm (decode-time (or time (current-time))))
  17586. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17587. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17588. (dow (nth 6 tm))
  17589. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17590. (cond
  17591. ((eq key 'today)
  17592. (setq h 0 m 0 h1 24 m1 0))
  17593. ((eq key 'yesterday)
  17594. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17595. ((eq key 'thisweek)
  17596. (setq diff (if (= dow 0) 6 (1- dow))
  17597. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17598. ((eq key 'lastweek)
  17599. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17600. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17601. ((eq key 'thismonth)
  17602. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17603. ((eq key 'lastmonth)
  17604. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17605. ((eq key 'thisyear)
  17606. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17607. ((eq key 'lastyear)
  17608. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17609. (t (error "No such time block %s" key)))
  17610. (setq ts (encode-time s m h d month y)
  17611. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17612. (or d1 d) (or month1 month) (or y1 y)))
  17613. (setq fm (cdr org-time-stamp-formats))
  17614. (if as-strings
  17615. (cons (format-time-string fm ts) (format-time-string fm te))
  17616. (cons ts te))))
  17617. (defun org-dblock-write:clocktable (params)
  17618. "Write the standard clocktable."
  17619. (catch 'exit
  17620. (let* ((hlchars '((1 . "*") (2 . "/")))
  17621. (ins (make-marker))
  17622. (total-time nil)
  17623. (scope (plist-get params :scope))
  17624. (tostring (plist-get params :tostring))
  17625. (multifile (plist-get params :multifile))
  17626. (header (plist-get params :header))
  17627. (maxlevel (or (plist-get params :maxlevel) 3))
  17628. (step (plist-get params :step))
  17629. (emph (plist-get params :emphasize))
  17630. (ts (plist-get params :tstart))
  17631. (te (plist-get params :tend))
  17632. (block (plist-get params :block))
  17633. ipos time h m p level hlc hdl
  17634. cc beg end pos tbl)
  17635. (when step
  17636. (org-clocktable-steps params)
  17637. (throw 'exit nil))
  17638. (when block
  17639. (setq cc (org-clock-special-range block nil t)
  17640. ts (car cc) te (cdr cc)))
  17641. (if ts (setq ts (time-to-seconds
  17642. (apply 'encode-time (org-parse-time-string ts)))))
  17643. (if te (setq te (time-to-seconds
  17644. (apply 'encode-time (org-parse-time-string te)))))
  17645. (move-marker ins (point))
  17646. (setq ipos (point))
  17647. ;; Get the right scope
  17648. (setq pos (point))
  17649. (save-restriction
  17650. (cond
  17651. ((not scope))
  17652. ((eq scope 'file) (widen))
  17653. ((eq scope 'subtree) (org-narrow-to-subtree))
  17654. ((eq scope 'tree)
  17655. (while (org-up-heading-safe))
  17656. (org-narrow-to-subtree))
  17657. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17658. (symbol-name scope)))
  17659. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17660. (catch 'exit
  17661. (while (org-up-heading-safe)
  17662. (looking-at outline-regexp)
  17663. (if (<= (org-reduced-level (funcall outline-level)) level)
  17664. (throw 'exit nil))))
  17665. (org-narrow-to-subtree))
  17666. ((or (listp scope) (eq scope 'agenda))
  17667. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17668. (scope 'agenda)
  17669. (p1 (copy-sequence params))
  17670. file)
  17671. (plist-put p1 :tostring t)
  17672. (plist-put p1 :multifile t)
  17673. (plist-put p1 :scope 'file)
  17674. (org-prepare-agenda-buffers files)
  17675. (while (setq file (pop files))
  17676. (with-current-buffer (find-buffer-visiting file)
  17677. (push (org-clocktable-add-file
  17678. file (org-dblock-write:clocktable p1)) tbl)
  17679. (setq total-time (+ (or total-time 0)
  17680. org-clock-file-total-minutes)))))))
  17681. (goto-char pos)
  17682. (unless (eq scope 'agenda)
  17683. (org-clock-sum ts te)
  17684. (goto-char (point-min))
  17685. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17686. (goto-char p)
  17687. (when (setq time (get-text-property p :org-clock-minutes))
  17688. (save-excursion
  17689. (beginning-of-line 1)
  17690. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17691. (setq level (org-reduced-level
  17692. (- (match-end 1) (match-beginning 1))))
  17693. (<= level maxlevel))
  17694. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17695. hdl (match-string 2)
  17696. h (/ time 60)
  17697. m (- time (* 60 h)))
  17698. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17699. (push (concat
  17700. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17701. (make-string (1- level) ?|)
  17702. hlc (format "%d:%02d" h m) hlc
  17703. " |") tbl))))))
  17704. (setq tbl (nreverse tbl))
  17705. (if tostring
  17706. (if tbl (mapconcat 'identity tbl "\n") nil)
  17707. (goto-char ins)
  17708. (insert-before-markers
  17709. (or header
  17710. (concat
  17711. "Clock summary at ["
  17712. (substring
  17713. (format-time-string (cdr org-time-stamp-formats))
  17714. 1 -1)
  17715. "]."
  17716. (if block
  17717. (format " Considered range is /%s/." block)
  17718. "")
  17719. "\n\n"))
  17720. (if (eq scope 'agenda) "|File" "")
  17721. "|L|Headline|Time|\n")
  17722. (setq total-time (or total-time org-clock-file-total-minutes)
  17723. h (/ total-time 60)
  17724. m (- total-time (* 60 h)))
  17725. (insert-before-markers
  17726. "|-\n|"
  17727. (if (eq scope 'agenda) "|" "")
  17728. "|"
  17729. "*Total time*| "
  17730. (format "*%d:%02d*" h m)
  17731. "|\n|-\n")
  17732. (setq tbl (delq nil tbl))
  17733. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17734. (equal (substring (car tbl) 0 2) "|-"))
  17735. (pop tbl))
  17736. (insert-before-markers (mapconcat
  17737. 'identity (delq nil tbl)
  17738. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17739. (backward-delete-char 1)
  17740. (goto-char ipos)
  17741. (skip-chars-forward "^|")
  17742. (org-table-align))))))
  17743. (defun org-clocktable-steps (params)
  17744. (let* ((p1 (copy-sequence params))
  17745. (ts (plist-get p1 :tstart))
  17746. (te (plist-get p1 :tend))
  17747. (step0 (plist-get p1 :step))
  17748. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17749. (block (plist-get p1 :block))
  17750. cc)
  17751. (when block
  17752. (setq cc (org-clock-special-range block nil t)
  17753. ts (car cc) te (cdr cc)))
  17754. (if ts (setq ts (time-to-seconds
  17755. (apply 'encode-time (org-parse-time-string ts)))))
  17756. (if te (setq te (time-to-seconds
  17757. (apply 'encode-time (org-parse-time-string te)))))
  17758. (plist-put p1 :header "")
  17759. (plist-put p1 :step nil)
  17760. (plist-put p1 :block nil)
  17761. (while (< ts te)
  17762. (or (bolp) (insert "\n"))
  17763. (plist-put p1 :tstart (format-time-string
  17764. (car org-time-stamp-formats)
  17765. (seconds-to-time ts)))
  17766. (plist-put p1 :tend (format-time-string
  17767. (car org-time-stamp-formats)
  17768. (seconds-to-time (setq ts (+ ts step)))))
  17769. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17770. (plist-get p1 :tstart) "\n")
  17771. (org-dblock-write:clocktable p1)
  17772. (re-search-forward "#\\+END:")
  17773. (end-of-line 0))))
  17774. (defun org-clocktable-add-file (file table)
  17775. (if table
  17776. (let ((lines (org-split-string table "\n"))
  17777. (ff (file-name-nondirectory file)))
  17778. (mapconcat 'identity
  17779. (mapcar (lambda (x)
  17780. (if (string-match org-table-dataline-regexp x)
  17781. (concat "|" ff x)
  17782. x))
  17783. lines)
  17784. "\n"))))
  17785. ;; FIXME: I don't think anybody uses this, ask David
  17786. (defun org-collect-clock-time-entries ()
  17787. "Return an internal list with clocking information.
  17788. This list has one entry for each CLOCK interval.
  17789. FIXME: describe the elements."
  17790. (interactive)
  17791. (let ((re (concat "^[ \t]*" org-clock-string
  17792. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17793. rtn beg end next cont level title total closedp leafp
  17794. clockpos titlepos h m donep)
  17795. (save-excursion
  17796. (org-clock-sum)
  17797. (goto-char (point-min))
  17798. (while (re-search-forward re nil t)
  17799. (setq clockpos (match-beginning 0)
  17800. beg (match-string 1) end (match-string 2)
  17801. cont (match-end 0))
  17802. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17803. end (apply 'encode-time (org-parse-time-string end)))
  17804. (org-back-to-heading t)
  17805. (setq donep (org-entry-is-done-p))
  17806. (setq titlepos (point)
  17807. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17808. h (/ total 60) m (- total (* 60 h))
  17809. total (cons h m))
  17810. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17811. (setq level (- (match-end 1) (match-beginning 1))
  17812. title (org-match-string-no-properties 2))
  17813. (save-excursion (outline-next-heading) (setq next (point)))
  17814. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17815. (goto-char next)
  17816. (setq leafp (and (looking-at "^\\*+ ")
  17817. (<= (- (match-end 0) (point)) level)))
  17818. (push (list beg end clockpos closedp donep
  17819. total title titlepos level leafp)
  17820. rtn)
  17821. (goto-char cont)))
  17822. (nreverse rtn)))
  17823. ;;;; Agenda, and Diary Integration
  17824. ;;; Define the Org-agenda-mode
  17825. (defvar org-agenda-mode-map (make-sparse-keymap)
  17826. "Keymap for `org-agenda-mode'.")
  17827. (defvar org-agenda-menu) ; defined later in this file.
  17828. (defvar org-agenda-follow-mode nil)
  17829. (defvar org-agenda-show-log nil)
  17830. (defvar org-agenda-redo-command nil)
  17831. (defvar org-agenda-query-string nil)
  17832. (defvar org-agenda-mode-hook nil)
  17833. (defvar org-agenda-type nil)
  17834. (defvar org-agenda-force-single-file nil)
  17835. (defun org-agenda-mode ()
  17836. "Mode for time-sorted view on action items in Org-mode files.
  17837. The following commands are available:
  17838. \\{org-agenda-mode-map}"
  17839. (interactive)
  17840. (kill-all-local-variables)
  17841. (setq org-agenda-undo-list nil
  17842. org-agenda-pending-undo-list nil)
  17843. (setq major-mode 'org-agenda-mode)
  17844. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17845. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17846. (setq mode-name "Org-Agenda")
  17847. (use-local-map org-agenda-mode-map)
  17848. (easy-menu-add org-agenda-menu)
  17849. (if org-startup-truncated (setq truncate-lines t))
  17850. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17851. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17852. ;; Make sure properties are removed when copying text
  17853. (when (boundp 'buffer-substring-filters)
  17854. (org-set-local 'buffer-substring-filters
  17855. (cons (lambda (x)
  17856. (set-text-properties 0 (length x) nil x) x)
  17857. buffer-substring-filters)))
  17858. (unless org-agenda-keep-modes
  17859. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17860. org-agenda-show-log nil))
  17861. (easy-menu-change
  17862. '("Agenda") "Agenda Files"
  17863. (append
  17864. (list
  17865. (vector
  17866. (if (get 'org-agenda-files 'org-restrict)
  17867. "Restricted to single file"
  17868. "Edit File List")
  17869. '(org-edit-agenda-file-list)
  17870. (not (get 'org-agenda-files 'org-restrict)))
  17871. "--")
  17872. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17873. (org-agenda-set-mode-name)
  17874. (apply
  17875. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17876. (list 'org-agenda-mode-hook)))
  17877. (substitute-key-definition 'undo 'org-agenda-undo
  17878. org-agenda-mode-map global-map)
  17879. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17880. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17881. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17882. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17883. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17884. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17885. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17886. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17887. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17888. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17889. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17890. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17891. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17892. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17893. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17894. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17895. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17896. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17897. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17898. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17899. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17900. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17901. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17902. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17903. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17904. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17905. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17906. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17907. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17908. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17909. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17910. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17911. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17912. (while l (org-defkey org-agenda-mode-map
  17913. (int-to-string (pop l)) 'digit-argument)))
  17914. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17915. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17916. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17917. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17918. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17919. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17920. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17921. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17922. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17923. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17924. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17925. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17926. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17927. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17928. (org-defkey org-agenda-mode-map "n" 'next-line)
  17929. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17930. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17931. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17932. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17933. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17934. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17935. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17936. (eval-after-load "calendar"
  17937. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17938. 'org-calendar-goto-agenda))
  17939. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17940. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17941. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17942. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17943. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17944. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17945. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17946. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17947. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17948. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17949. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17950. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17951. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17952. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17953. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17954. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17955. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17956. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17957. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17958. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17959. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17960. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17961. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17962. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17963. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17964. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17965. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17966. "Local keymap for agenda entries from Org-mode.")
  17967. (org-defkey org-agenda-keymap
  17968. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17969. (org-defkey org-agenda-keymap
  17970. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17971. (when org-agenda-mouse-1-follows-link
  17972. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17973. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17974. '("Agenda"
  17975. ("Agenda Files")
  17976. "--"
  17977. ["Show" org-agenda-show t]
  17978. ["Go To (other window)" org-agenda-goto t]
  17979. ["Go To (this window)" org-agenda-switch-to t]
  17980. ["Follow Mode" org-agenda-follow-mode
  17981. :style toggle :selected org-agenda-follow-mode :active t]
  17982. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17983. "--"
  17984. ["Cycle TODO" org-agenda-todo t]
  17985. ["Archive subtree" org-agenda-archive t]
  17986. ["Delete subtree" org-agenda-kill t]
  17987. "--"
  17988. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17989. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17990. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17991. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17992. "--"
  17993. ("Tags and Properties"
  17994. ["Show all Tags" org-agenda-show-tags t]
  17995. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17996. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17997. "--"
  17998. ["Column View" org-columns t])
  17999. ("Date/Schedule"
  18000. ["Schedule" org-agenda-schedule t]
  18001. ["Set Deadline" org-agenda-deadline t]
  18002. "--"
  18003. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  18004. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  18005. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  18006. ("Clock"
  18007. ["Clock in" org-agenda-clock-in t]
  18008. ["Clock out" org-agenda-clock-out t]
  18009. ["Clock cancel" org-agenda-clock-cancel t]
  18010. ["Goto running clock" org-clock-goto t])
  18011. ("Priority"
  18012. ["Set Priority" org-agenda-priority t]
  18013. ["Increase Priority" org-agenda-priority-up t]
  18014. ["Decrease Priority" org-agenda-priority-down t]
  18015. ["Show Priority" org-agenda-show-priority t])
  18016. ("Calendar/Diary"
  18017. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  18018. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  18019. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  18020. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  18021. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  18022. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  18023. "--"
  18024. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  18025. "--"
  18026. ("View"
  18027. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  18028. :style radio :selected (equal org-agenda-ndays 1)]
  18029. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  18030. :style radio :selected (equal org-agenda-ndays 7)]
  18031. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  18032. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  18033. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  18034. :style radio :selected (member org-agenda-ndays '(365 366))]
  18035. "--"
  18036. ["Show Logbook entries" org-agenda-log-mode
  18037. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  18038. ["Include Diary" org-agenda-toggle-diary
  18039. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  18040. ["Use Time Grid" org-agenda-toggle-time-grid
  18041. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  18042. ["Write view to file" org-write-agenda t]
  18043. ["Rebuild buffer" org-agenda-redo t]
  18044. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18045. "--"
  18046. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18047. "--"
  18048. ["Quit" org-agenda-quit t]
  18049. ["Exit and Release Buffers" org-agenda-exit t]
  18050. ))
  18051. ;;; Agenda undo
  18052. (defvar org-agenda-allow-remote-undo t
  18053. "Non-nil means, allow remote undo from the agenda buffer.")
  18054. (defvar org-agenda-undo-list nil
  18055. "List of undoable operations in the agenda since last refresh.")
  18056. (defvar org-agenda-undo-has-started-in nil
  18057. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18058. (defvar org-agenda-pending-undo-list nil
  18059. "In a series of undo commands, this is the list of remaning undo items.")
  18060. (defmacro org-if-unprotected (&rest body)
  18061. "Execute BODY if there is no `org-protected' text property at point."
  18062. (declare (debug t))
  18063. `(unless (get-text-property (point) 'org-protected)
  18064. ,@body))
  18065. (defmacro org-with-remote-undo (_buffer &rest _body)
  18066. "Execute BODY while recording undo information in two buffers."
  18067. (declare (indent 1) (debug t))
  18068. `(let ((_cline (org-current-line))
  18069. (_cmd this-command)
  18070. (_buf1 (current-buffer))
  18071. (_buf2 ,_buffer)
  18072. (_undo1 buffer-undo-list)
  18073. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18074. _c1 _c2)
  18075. ,@_body
  18076. (when org-agenda-allow-remote-undo
  18077. (setq _c1 (org-verify-change-for-undo
  18078. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18079. _c2 (org-verify-change-for-undo
  18080. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18081. (when (or _c1 _c2)
  18082. ;; make sure there are undo boundaries
  18083. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18084. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18085. ;; remember which buffer to undo
  18086. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18087. org-agenda-undo-list)))))
  18088. (defun org-agenda-undo ()
  18089. "Undo a remote editing step in the agenda.
  18090. This undoes changes both in the agenda buffer and in the remote buffer
  18091. that have been changed along."
  18092. (interactive)
  18093. (or org-agenda-allow-remote-undo
  18094. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18095. (if (not (eq this-command last-command))
  18096. (setq org-agenda-undo-has-started-in nil
  18097. org-agenda-pending-undo-list org-agenda-undo-list))
  18098. (if (not org-agenda-pending-undo-list)
  18099. (error "No further undo information"))
  18100. (let* ((entry (pop org-agenda-pending-undo-list))
  18101. buf line cmd rembuf)
  18102. (setq cmd (pop entry) line (pop entry))
  18103. (setq rembuf (nth 2 entry))
  18104. (org-with-remote-undo rembuf
  18105. (while (bufferp (setq buf (pop entry)))
  18106. (if (pop entry)
  18107. (with-current-buffer buf
  18108. (let ((last-undo-buffer buf)
  18109. (inhibit-read-only t))
  18110. (unless (memq buf org-agenda-undo-has-started-in)
  18111. (push buf org-agenda-undo-has-started-in)
  18112. (make-local-variable 'pending-undo-list)
  18113. (undo-start))
  18114. (while (and pending-undo-list
  18115. (listp pending-undo-list)
  18116. (not (car pending-undo-list)))
  18117. (pop pending-undo-list))
  18118. (undo-more 1))))))
  18119. (goto-line line)
  18120. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18121. (defun org-verify-change-for-undo (l1 l2)
  18122. "Verify that a real change occurred between the undo lists L1 and L2."
  18123. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18124. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18125. (not (eq l1 l2)))
  18126. ;;; Agenda dispatch
  18127. (defvar org-agenda-restrict nil)
  18128. (defvar org-agenda-restrict-begin (make-marker))
  18129. (defvar org-agenda-restrict-end (make-marker))
  18130. (defvar org-agenda-last-dispatch-buffer nil)
  18131. (defvar org-agenda-overriding-restriction nil)
  18132. ;;;###autoload
  18133. (defun org-agenda (arg &optional keys restriction)
  18134. "Dispatch agenda commands to collect entries to the agenda buffer.
  18135. Prompts for a command to execute. Any prefix arg will be passed
  18136. on to the selected command. The default selections are:
  18137. a Call `org-agenda-list' to display the agenda for current day or week.
  18138. t Call `org-todo-list' to display the global todo list.
  18139. T Call `org-todo-list' to display the global todo list, select only
  18140. entries with a specific TODO keyword (the user gets a prompt).
  18141. m Call `org-tags-view' to display headlines with tags matching
  18142. a condition (the user is prompted for the condition).
  18143. M Like `m', but select only TODO entries, no ordinary headlines.
  18144. L Create a timeline for the current buffer.
  18145. e Export views to associated files.
  18146. More commands can be added by configuring the variable
  18147. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18148. searches can be pre-defined in this way.
  18149. If the current buffer is in Org-mode and visiting a file, you can also
  18150. first press `<' once to indicate that the agenda should be temporarily
  18151. \(until the next use of \\[org-agenda]) restricted to the current file.
  18152. Pressing `<' twice means to restrict to the current subtree or region
  18153. \(if active)."
  18154. (interactive "P")
  18155. (catch 'exit
  18156. (let* ((prefix-descriptions nil)
  18157. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18158. (org-agenda-custom-commands
  18159. ;; normalize different versions
  18160. (delq nil
  18161. (mapcar
  18162. (lambda (x)
  18163. (cond ((stringp (cdr x))
  18164. (push x prefix-descriptions)
  18165. nil)
  18166. ((stringp (nth 1 x)) x)
  18167. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18168. (t (cons (car x) (cons "" (cdr x))))))
  18169. org-agenda-custom-commands)))
  18170. (buf (current-buffer))
  18171. (bfn (buffer-file-name (buffer-base-buffer)))
  18172. entry key type match lprops ans)
  18173. ;; Turn off restriction unless there is an overriding one
  18174. (unless org-agenda-overriding-restriction
  18175. (put 'org-agenda-files 'org-restrict nil)
  18176. (setq org-agenda-restrict nil)
  18177. (move-marker org-agenda-restrict-begin nil)
  18178. (move-marker org-agenda-restrict-end nil))
  18179. ;; Delete old local properties
  18180. (put 'org-agenda-redo-command 'org-lprops nil)
  18181. ;; Remember where this call originated
  18182. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18183. (unless keys
  18184. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18185. keys (car ans)
  18186. restriction (cdr ans)))
  18187. ;; Estabish the restriction, if any
  18188. (when (and (not org-agenda-overriding-restriction) restriction)
  18189. (put 'org-agenda-files 'org-restrict (list bfn))
  18190. (cond
  18191. ((eq restriction 'region)
  18192. (setq org-agenda-restrict t)
  18193. (move-marker org-agenda-restrict-begin (region-beginning))
  18194. (move-marker org-agenda-restrict-end (region-end)))
  18195. ((eq restriction 'subtree)
  18196. (save-excursion
  18197. (setq org-agenda-restrict t)
  18198. (org-back-to-heading t)
  18199. (move-marker org-agenda-restrict-begin (point))
  18200. (move-marker org-agenda-restrict-end
  18201. (progn (org-end-of-subtree t)))))))
  18202. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18203. ;; For example the todo list should not need it (but does...)
  18204. (cond
  18205. ((setq entry (assoc keys org-agenda-custom-commands))
  18206. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18207. (progn
  18208. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18209. (put 'org-agenda-redo-command 'org-lprops lprops)
  18210. (cond
  18211. ((eq type 'agenda)
  18212. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18213. ((eq type 'alltodo)
  18214. (org-let lprops '(org-todo-list current-prefix-arg)))
  18215. ((eq type 'search)
  18216. (org-let lprops '(org-search-view current-prefix-arg match)))
  18217. ((eq type 'stuck)
  18218. (org-let lprops '(org-agenda-list-stuck-projects
  18219. current-prefix-arg)))
  18220. ((eq type 'tags)
  18221. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18222. ((eq type 'tags-todo)
  18223. (org-let lprops '(org-tags-view '(4) match)))
  18224. ((eq type 'todo)
  18225. (org-let lprops '(org-todo-list match)))
  18226. ((eq type 'tags-tree)
  18227. (org-check-for-org-mode)
  18228. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18229. ((eq type 'todo-tree)
  18230. (org-check-for-org-mode)
  18231. (org-let lprops
  18232. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18233. (regexp-quote match) "\\>"))))
  18234. ((eq type 'occur-tree)
  18235. (org-check-for-org-mode)
  18236. (org-let lprops '(org-occur match)))
  18237. ((functionp type)
  18238. (org-let lprops '(funcall type match)))
  18239. ((fboundp type)
  18240. (org-let lprops '(funcall type match)))
  18241. (t (error "Invalid custom agenda command type %s" type))))
  18242. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18243. ((equal keys "C")
  18244. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18245. (customize-variable 'org-agenda-custom-commands))
  18246. ((equal keys "a") (call-interactively 'org-agenda-list))
  18247. ((equal keys "s") (call-interactively 'org-search-view))
  18248. ((equal keys "t") (call-interactively 'org-todo-list))
  18249. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18250. ((equal keys "m") (call-interactively 'org-tags-view))
  18251. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18252. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18253. ((equal keys "L")
  18254. (unless (org-mode-p)
  18255. (error "This is not an Org-mode file"))
  18256. (unless restriction
  18257. (put 'org-agenda-files 'org-restrict (list bfn))
  18258. (org-call-with-arg 'org-timeline arg)))
  18259. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18260. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18261. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18262. (t (error "Invalid agenda key"))))))
  18263. (defun org-agenda-normalize-custom-commands (cmds)
  18264. (delq nil
  18265. (mapcar
  18266. (lambda (x)
  18267. (cond ((stringp (cdr x)) nil)
  18268. ((stringp (nth 1 x)) x)
  18269. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18270. (t (cons (car x) (cons "" (cdr x))))))
  18271. cmds)))
  18272. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18273. "The user interface for selecting an agenda command."
  18274. (catch 'exit
  18275. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18276. (restrict-ok (and bfn (org-mode-p)))
  18277. (region-p (org-region-active-p))
  18278. (custom org-agenda-custom-commands)
  18279. (selstring "")
  18280. restriction second-time
  18281. c entry key type match prefixes rmheader header-end custom1 desc)
  18282. (save-window-excursion
  18283. (delete-other-windows)
  18284. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18285. (erase-buffer)
  18286. (insert (eval-when-compile
  18287. (let ((header
  18288. "
  18289. Press key for an agenda command: < Buffer,subtree/region restriction
  18290. -------------------------------- > Remove restriction
  18291. a Agenda for current week or day e Export agenda views
  18292. t List of all TODO entries T Entries with special TODO kwd
  18293. m Match a TAGS query M Like m, but only TODO entries
  18294. L Timeline for current buffer # List stuck projects (!=configure)
  18295. s Search for keywords C Configure custom agenda commands
  18296. / Multi-occur
  18297. ")
  18298. (start 0))
  18299. (while (string-match
  18300. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18301. header start)
  18302. (setq start (match-end 0))
  18303. (add-text-properties (match-beginning 2) (match-end 2)
  18304. '(face bold) header))
  18305. header)))
  18306. (setq header-end (move-marker (make-marker) (point)))
  18307. (while t
  18308. (setq custom1 custom)
  18309. (when (eq rmheader t)
  18310. (goto-line 1)
  18311. (re-search-forward ":" nil t)
  18312. (delete-region (match-end 0) (point-at-eol))
  18313. (forward-char 1)
  18314. (looking-at "-+")
  18315. (delete-region (match-end 0) (point-at-eol))
  18316. (move-marker header-end (match-end 0)))
  18317. (goto-char header-end)
  18318. (delete-region (point) (point-max))
  18319. (while (setq entry (pop custom1))
  18320. (setq key (car entry) desc (nth 1 entry)
  18321. type (nth 2 entry) match (nth 3 entry))
  18322. (if (> (length key) 1)
  18323. (add-to-list 'prefixes (string-to-char key))
  18324. (insert
  18325. (format
  18326. "\n%-4s%-14s: %s"
  18327. (org-add-props (copy-sequence key)
  18328. '(face bold))
  18329. (cond
  18330. ((string-match "\\S-" desc) desc)
  18331. ((eq type 'agenda) "Agenda for current week or day")
  18332. ((eq type 'alltodo) "List of all TODO entries")
  18333. ((eq type 'search) "Word search")
  18334. ((eq type 'stuck) "List of stuck projects")
  18335. ((eq type 'todo) "TODO keyword")
  18336. ((eq type 'tags) "Tags query")
  18337. ((eq type 'tags-todo) "Tags (TODO)")
  18338. ((eq type 'tags-tree) "Tags tree")
  18339. ((eq type 'todo-tree) "TODO kwd tree")
  18340. ((eq type 'occur-tree) "Occur tree")
  18341. ((functionp type) (if (symbolp type)
  18342. (symbol-name type)
  18343. "Lambda expression"))
  18344. (t "???"))
  18345. (cond
  18346. ((stringp match)
  18347. (org-add-props match nil 'face 'org-warning))
  18348. (match
  18349. (format "set of %d commands" (length match)))
  18350. (t ""))))))
  18351. (when prefixes
  18352. (mapc (lambda (x)
  18353. (insert
  18354. (format "\n%s %s"
  18355. (org-add-props (char-to-string x)
  18356. nil 'face 'bold)
  18357. (or (cdr (assoc (concat selstring (char-to-string x))
  18358. prefix-descriptions))
  18359. "Prefix key"))))
  18360. prefixes))
  18361. (goto-char (point-min))
  18362. (when (fboundp 'fit-window-to-buffer)
  18363. (if second-time
  18364. (if (not (pos-visible-in-window-p (point-max)))
  18365. (fit-window-to-buffer))
  18366. (setq second-time t)
  18367. (fit-window-to-buffer)))
  18368. (message "Press key for agenda command%s:"
  18369. (if (or restrict-ok org-agenda-overriding-restriction)
  18370. (if org-agenda-overriding-restriction
  18371. " (restriction lock active)"
  18372. (if restriction
  18373. (format " (restricted to %s)" restriction)
  18374. " (unrestricted)"))
  18375. ""))
  18376. (setq c (read-char-exclusive))
  18377. (message "")
  18378. (cond
  18379. ((assoc (char-to-string c) custom)
  18380. (setq selstring (concat selstring (char-to-string c)))
  18381. (throw 'exit (cons selstring restriction)))
  18382. ((memq c prefixes)
  18383. (setq selstring (concat selstring (char-to-string c))
  18384. prefixes nil
  18385. rmheader (or rmheader t)
  18386. custom (delq nil (mapcar
  18387. (lambda (x)
  18388. (if (or (= (length (car x)) 1)
  18389. (/= (string-to-char (car x)) c))
  18390. nil
  18391. (cons (substring (car x) 1) (cdr x))))
  18392. custom))))
  18393. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18394. (message "Restriction is only possible in Org-mode buffers")
  18395. (ding) (sit-for 1))
  18396. ((eq c ?1)
  18397. (org-agenda-remove-restriction-lock 'noupdate)
  18398. (setq restriction 'buffer))
  18399. ((eq c ?0)
  18400. (org-agenda-remove-restriction-lock 'noupdate)
  18401. (setq restriction (if region-p 'region 'subtree)))
  18402. ((eq c ?<)
  18403. (org-agenda-remove-restriction-lock 'noupdate)
  18404. (setq restriction
  18405. (cond
  18406. ((eq restriction 'buffer)
  18407. (if region-p 'region 'subtree))
  18408. ((memq restriction '(subtree region))
  18409. nil)
  18410. (t 'buffer))))
  18411. ((eq c ?>)
  18412. (org-agenda-remove-restriction-lock 'noupdate)
  18413. (setq restriction nil))
  18414. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18415. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18416. ((and (> (length selstring) 0) (eq c ?\d))
  18417. (delete-window)
  18418. (org-agenda-get-restriction-and-command prefix-descriptions))
  18419. ((equal c ?q) (error "Abort"))
  18420. (t (error "Invalid key %c" c))))))))
  18421. (defun org-run-agenda-series (name series)
  18422. (org-prepare-agenda name)
  18423. (let* ((org-agenda-multi t)
  18424. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18425. (cmds (car series))
  18426. (gprops (nth 1 series))
  18427. match ;; The byte compiler incorrectly complains about this. Keep it!
  18428. cmd type lprops)
  18429. (while (setq cmd (pop cmds))
  18430. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18431. (cond
  18432. ((eq type 'agenda)
  18433. (org-let2 gprops lprops
  18434. '(call-interactively 'org-agenda-list)))
  18435. ((eq type 'alltodo)
  18436. (org-let2 gprops lprops
  18437. '(call-interactively 'org-todo-list)))
  18438. ((eq type 'search)
  18439. (org-let2 gprops lprops
  18440. '(org-search-view current-prefix-arg match)))
  18441. ((eq type 'stuck)
  18442. (org-let2 gprops lprops
  18443. '(call-interactively 'org-agenda-list-stuck-projects)))
  18444. ((eq type 'tags)
  18445. (org-let2 gprops lprops
  18446. '(org-tags-view current-prefix-arg match)))
  18447. ((eq type 'tags-todo)
  18448. (org-let2 gprops lprops
  18449. '(org-tags-view '(4) match)))
  18450. ((eq type 'todo)
  18451. (org-let2 gprops lprops
  18452. '(org-todo-list match)))
  18453. ((fboundp type)
  18454. (org-let2 gprops lprops
  18455. '(funcall type match)))
  18456. (t (error "Invalid type in command series"))))
  18457. (widen)
  18458. (setq org-agenda-redo-command redo)
  18459. (goto-char (point-min)))
  18460. (org-finalize-agenda))
  18461. ;;;###autoload
  18462. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18463. "Run an agenda command in batch mode and send the result to STDOUT.
  18464. If CMD-KEY is a string of length 1, it is used as a key in
  18465. `org-agenda-custom-commands' and triggers this command. If it is a
  18466. longer string it is used as a tags/todo match string.
  18467. Paramters are alternating variable names and values that will be bound
  18468. before running the agenda command."
  18469. (let (pars)
  18470. (while parameters
  18471. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18472. (if (> (length cmd-key) 2)
  18473. (eval (list 'let (nreverse pars)
  18474. (list 'org-tags-view nil cmd-key)))
  18475. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18476. (set-buffer org-agenda-buffer-name)
  18477. (princ (org-encode-for-stdout (buffer-string)))))
  18478. (defun org-encode-for-stdout (string)
  18479. (if (fboundp 'encode-coding-string)
  18480. (encode-coding-string string buffer-file-coding-system)
  18481. string))
  18482. (defvar org-agenda-info nil)
  18483. ;;;###autoload
  18484. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18485. "Run an agenda command in batch mode and send the result to STDOUT.
  18486. If CMD-KEY is a string of length 1, it is used as a key in
  18487. `org-agenda-custom-commands' and triggers this command. If it is a
  18488. longer string it is used as a tags/todo match string.
  18489. Paramters are alternating variable names and values that will be bound
  18490. before running the agenda command.
  18491. The output gives a line for each selected agenda item. Each
  18492. item is a list of comma-separated values, like this:
  18493. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18494. category The category of the item
  18495. head The headline, without TODO kwd, TAGS and PRIORITY
  18496. type The type of the agenda entry, can be
  18497. todo selected in TODO match
  18498. tagsmatch selected in tags match
  18499. diary imported from diary
  18500. deadline a deadline on given date
  18501. scheduled scheduled on given date
  18502. timestamp entry has timestamp on given date
  18503. closed entry was closed on given date
  18504. upcoming-deadline warning about deadline
  18505. past-scheduled forwarded scheduled item
  18506. block entry has date block including g. date
  18507. todo The todo keyword, if any
  18508. tags All tags including inherited ones, separated by colons
  18509. date The relevant date, like 2007-2-14
  18510. time The time, like 15:00-16:50
  18511. extra Sting with extra planning info
  18512. priority-l The priority letter if any was given
  18513. priority-n The computed numerical priority
  18514. agenda-day The day in the agenda where this is listed"
  18515. (let (pars)
  18516. (while parameters
  18517. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18518. (push (list 'org-agenda-remove-tags t) pars)
  18519. (if (> (length cmd-key) 2)
  18520. (eval (list 'let (nreverse pars)
  18521. (list 'org-tags-view nil cmd-key)))
  18522. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18523. (set-buffer org-agenda-buffer-name)
  18524. (let* ((lines (org-split-string (buffer-string) "\n"))
  18525. line)
  18526. (while (setq line (pop lines))
  18527. (catch 'next
  18528. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18529. (setq org-agenda-info
  18530. (org-fix-agenda-info (text-properties-at 0 line)))
  18531. (princ
  18532. (org-encode-for-stdout
  18533. (mapconcat 'org-agenda-export-csv-mapper
  18534. '(org-category txt type todo tags date time-of-day extra
  18535. priority-letter priority agenda-day)
  18536. ",")))
  18537. (princ "\n"))))))
  18538. (defun org-fix-agenda-info (props)
  18539. "Make sure all properties on an agenda item have a canonical form,
  18540. so the export commands can easily use it."
  18541. (let (tmp re)
  18542. (when (setq tmp (plist-get props 'tags))
  18543. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18544. (when (setq tmp (plist-get props 'date))
  18545. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18546. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18547. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18548. (setq tmp (calendar-date-string tmp)))
  18549. (setq props (plist-put props 'date tmp)))
  18550. (when (setq tmp (plist-get props 'day))
  18551. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18552. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18553. (setq tmp (calendar-date-string tmp)))
  18554. (setq props (plist-put props 'day tmp))
  18555. (setq props (plist-put props 'agenda-day tmp)))
  18556. (when (setq tmp (plist-get props 'txt))
  18557. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18558. (plist-put props 'priority-letter (match-string 1 tmp))
  18559. (setq tmp (replace-match "" t t tmp)))
  18560. (when (and (setq re (plist-get props 'org-todo-regexp))
  18561. (setq re (concat "\\`\\.*" re " ?"))
  18562. (string-match re tmp))
  18563. (plist-put props 'todo (match-string 1 tmp))
  18564. (setq tmp (replace-match "" t t tmp)))
  18565. (plist-put props 'txt tmp)))
  18566. props)
  18567. (defun org-agenda-export-csv-mapper (prop)
  18568. (let ((res (plist-get org-agenda-info prop)))
  18569. (setq res
  18570. (cond
  18571. ((not res) "")
  18572. ((stringp res) res)
  18573. (t (prin1-to-string res))))
  18574. (while (string-match "," res)
  18575. (setq res (replace-match ";" t t res)))
  18576. (org-trim res)))
  18577. ;;;###autoload
  18578. (defun org-store-agenda-views (&rest parameters)
  18579. (interactive)
  18580. (eval (list 'org-batch-store-agenda-views)))
  18581. ;; FIXME, why is this a macro?????
  18582. ;;;###autoload
  18583. (defmacro org-batch-store-agenda-views (&rest parameters)
  18584. "Run all custom agenda commands that have a file argument."
  18585. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18586. (pop-up-frames nil)
  18587. (dir default-directory)
  18588. pars cmd thiscmdkey files opts)
  18589. (while parameters
  18590. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18591. (setq pars (reverse pars))
  18592. (save-window-excursion
  18593. (while cmds
  18594. (setq cmd (pop cmds)
  18595. thiscmdkey (car cmd)
  18596. opts (nth 4 cmd)
  18597. files (nth 5 cmd))
  18598. (if (stringp files) (setq files (list files)))
  18599. (when files
  18600. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18601. (list 'org-agenda nil thiscmdkey)))
  18602. (set-buffer org-agenda-buffer-name)
  18603. (while files
  18604. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18605. (list 'org-write-agenda
  18606. (expand-file-name (pop files) dir) t))))
  18607. (and (get-buffer org-agenda-buffer-name)
  18608. (kill-buffer org-agenda-buffer-name)))))))
  18609. (defun org-write-agenda (file &optional nosettings)
  18610. "Write the current buffer (an agenda view) as a file.
  18611. Depending on the extension of the file name, plain text (.txt),
  18612. HTML (.html or .htm) or Postscript (.ps) is produced.
  18613. If NOSETTINGS is given, do not scope the settings of
  18614. `org-agenda-exporter-settings' into the export commands. This is used when
  18615. the settings have already been scoped and we do not wish to overrule other,
  18616. higher priority settings."
  18617. (interactive "FWrite agenda to file: ")
  18618. (if (not (file-writable-p file))
  18619. (error "Cannot write agenda to file %s" file))
  18620. (cond
  18621. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18622. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18623. (org-let (if nosettings nil org-agenda-exporter-settings)
  18624. '(save-excursion
  18625. (save-window-excursion
  18626. (cond
  18627. ((string-match "\\.html?\\'" file)
  18628. (set-buffer (htmlize-buffer (current-buffer)))
  18629. (when (and org-agenda-export-html-style
  18630. (string-match "<style>" org-agenda-export-html-style))
  18631. ;; replace <style> section with org-agenda-export-html-style
  18632. (goto-char (point-min))
  18633. (kill-region (- (search-forward "<style") 6)
  18634. (search-forward "</style>"))
  18635. (insert org-agenda-export-html-style))
  18636. (write-file file)
  18637. (kill-buffer (current-buffer))
  18638. (message "HTML written to %s" file))
  18639. ((string-match "\\.ps\\'" file)
  18640. (ps-print-buffer-with-faces file)
  18641. (message "Postscript written to %s" file))
  18642. (t
  18643. (let ((bs (buffer-string)))
  18644. (find-file file)
  18645. (insert bs)
  18646. (save-buffer 0)
  18647. (kill-buffer (current-buffer))
  18648. (message "Plain text written to %s" file))))))
  18649. (set-buffer org-agenda-buffer-name)))
  18650. (defmacro org-no-read-only (&rest body)
  18651. "Inhibit read-only for BODY."
  18652. `(let ((inhibit-read-only t)) ,@body))
  18653. (defun org-check-for-org-mode ()
  18654. "Make sure current buffer is in org-mode. Error if not."
  18655. (or (org-mode-p)
  18656. (error "Cannot execute org-mode agenda command on buffer in %s."
  18657. major-mode)))
  18658. (defun org-fit-agenda-window ()
  18659. "Fit the window to the buffer size."
  18660. (and (memq org-agenda-window-setup '(reorganize-frame))
  18661. (fboundp 'fit-window-to-buffer)
  18662. (fit-window-to-buffer
  18663. nil
  18664. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18665. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18666. ;;; Agenda file list
  18667. (defun org-agenda-files (&optional unrestricted)
  18668. "Get the list of agenda files.
  18669. Optional UNRESTRICTED means return the full list even if a restriction
  18670. is currently in place."
  18671. (let ((files
  18672. (cond
  18673. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18674. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18675. ((listp org-agenda-files) org-agenda-files)
  18676. (t (error "Invalid value of `org-agenda-files'")))))
  18677. (setq files (apply 'append
  18678. (mapcar (lambda (f)
  18679. (if (file-directory-p f)
  18680. (directory-files f t
  18681. org-agenda-file-regexp)
  18682. (list f)))
  18683. files)))
  18684. (if org-agenda-skip-unavailable-files
  18685. (delq nil
  18686. (mapcar (function
  18687. (lambda (file)
  18688. (and (file-readable-p file) file)))
  18689. files))
  18690. files))) ; `org-check-agenda-file' will remove them from the list
  18691. (defun org-edit-agenda-file-list ()
  18692. "Edit the list of agenda files.
  18693. Depending on setup, this either uses customize to edit the variable
  18694. `org-agenda-files', or it visits the file that is holding the list. In the
  18695. latter case, the buffer is set up in a way that saving it automatically kills
  18696. the buffer and restores the previous window configuration."
  18697. (interactive)
  18698. (if (stringp org-agenda-files)
  18699. (let ((cw (current-window-configuration)))
  18700. (find-file org-agenda-files)
  18701. (org-set-local 'org-window-configuration cw)
  18702. (org-add-hook 'after-save-hook
  18703. (lambda ()
  18704. (set-window-configuration
  18705. (prog1 org-window-configuration
  18706. (kill-buffer (current-buffer))))
  18707. (org-install-agenda-files-menu)
  18708. (message "New agenda file list installed"))
  18709. nil 'local)
  18710. (message "%s" (substitute-command-keys
  18711. "Edit list and finish with \\[save-buffer]")))
  18712. (customize-variable 'org-agenda-files)))
  18713. (defun org-store-new-agenda-file-list (list)
  18714. "Set new value for the agenda file list and save it correcly."
  18715. (if (stringp org-agenda-files)
  18716. (let ((f org-agenda-files) b)
  18717. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18718. (with-temp-file f
  18719. (insert (mapconcat 'identity list "\n") "\n")))
  18720. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18721. (setq org-agenda-files list)
  18722. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18723. (defun org-read-agenda-file-list ()
  18724. "Read the list of agenda files from a file."
  18725. (when (stringp org-agenda-files)
  18726. (with-temp-buffer
  18727. (insert-file-contents org-agenda-files)
  18728. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18729. ;;;###autoload
  18730. (defun org-cycle-agenda-files ()
  18731. "Cycle through the files in `org-agenda-files'.
  18732. If the current buffer visits an agenda file, find the next one in the list.
  18733. If the current buffer does not, find the first agenda file."
  18734. (interactive)
  18735. (let* ((fs (org-agenda-files t))
  18736. (files (append fs (list (car fs))))
  18737. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18738. file)
  18739. (unless files (error "No agenda files"))
  18740. (catch 'exit
  18741. (while (setq file (pop files))
  18742. (if (equal (file-truename file) tcf)
  18743. (when (car files)
  18744. (find-file (car files))
  18745. (throw 'exit t))))
  18746. (find-file (car fs)))
  18747. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18748. (defun org-agenda-file-to-front (&optional to-end)
  18749. "Move/add the current file to the top of the agenda file list.
  18750. If the file is not present in the list, it is added to the front. If it is
  18751. present, it is moved there. With optional argument TO-END, add/move to the
  18752. end of the list."
  18753. (interactive "P")
  18754. (let ((org-agenda-skip-unavailable-files nil)
  18755. (file-alist (mapcar (lambda (x)
  18756. (cons (file-truename x) x))
  18757. (org-agenda-files t)))
  18758. (ctf (file-truename buffer-file-name))
  18759. x had)
  18760. (setq x (assoc ctf file-alist) had x)
  18761. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18762. (if to-end
  18763. (setq file-alist (append (delq x file-alist) (list x)))
  18764. (setq file-alist (cons x (delq x file-alist))))
  18765. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18766. (org-install-agenda-files-menu)
  18767. (message "File %s to %s of agenda file list"
  18768. (if had "moved" "added") (if to-end "end" "front"))))
  18769. (defun org-remove-file (&optional file)
  18770. "Remove current file from the list of files in variable `org-agenda-files'.
  18771. These are the files which are being checked for agenda entries.
  18772. Optional argument FILE means, use this file instead of the current."
  18773. (interactive)
  18774. (let* ((org-agenda-skip-unavailable-files nil)
  18775. (file (or file buffer-file-name))
  18776. (true-file (file-truename file))
  18777. (afile (abbreviate-file-name file))
  18778. (files (delq nil (mapcar
  18779. (lambda (x)
  18780. (if (equal true-file
  18781. (file-truename x))
  18782. nil x))
  18783. (org-agenda-files t)))))
  18784. (if (not (= (length files) (length (org-agenda-files t))))
  18785. (progn
  18786. (org-store-new-agenda-file-list files)
  18787. (org-install-agenda-files-menu)
  18788. (message "Removed file: %s" afile))
  18789. (message "File was not in list: %s (not removed)" afile))))
  18790. (defun org-file-menu-entry (file)
  18791. (vector file (list 'find-file file) t))
  18792. (defun org-check-agenda-file (file)
  18793. "Make sure FILE exists. If not, ask user what to do."
  18794. (when (not (file-exists-p file))
  18795. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18796. (abbreviate-file-name file))
  18797. (let ((r (downcase (read-char-exclusive))))
  18798. (cond
  18799. ((equal r ?r)
  18800. (org-remove-file file)
  18801. (throw 'nextfile t))
  18802. (t (error "Abort"))))))
  18803. ;;; Agenda prepare and finalize
  18804. (defvar org-agenda-multi nil) ; dynammically scoped
  18805. (defvar org-agenda-buffer-name "*Org Agenda*")
  18806. (defvar org-pre-agenda-window-conf nil)
  18807. (defvar org-agenda-name nil)
  18808. (defun org-prepare-agenda (&optional name)
  18809. (setq org-todo-keywords-for-agenda nil)
  18810. (setq org-done-keywords-for-agenda nil)
  18811. (if org-agenda-multi
  18812. (progn
  18813. (setq buffer-read-only nil)
  18814. (goto-char (point-max))
  18815. (unless (or (bobp) org-agenda-compact-blocks)
  18816. (insert "\n" (make-string (window-width) ?=) "\n"))
  18817. (narrow-to-region (point) (point-max)))
  18818. (org-agenda-reset-markers)
  18819. (org-prepare-agenda-buffers (org-agenda-files))
  18820. (setq org-todo-keywords-for-agenda
  18821. (org-uniquify org-todo-keywords-for-agenda))
  18822. (setq org-done-keywords-for-agenda
  18823. (org-uniquify org-done-keywords-for-agenda))
  18824. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18825. (awin (get-buffer-window abuf)))
  18826. (cond
  18827. ((equal (current-buffer) abuf) nil)
  18828. (awin (select-window awin))
  18829. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18830. ((equal org-agenda-window-setup 'current-window)
  18831. (switch-to-buffer abuf))
  18832. ((equal org-agenda-window-setup 'other-window)
  18833. (org-switch-to-buffer-other-window abuf))
  18834. ((equal org-agenda-window-setup 'other-frame)
  18835. (switch-to-buffer-other-frame abuf))
  18836. ((equal org-agenda-window-setup 'reorganize-frame)
  18837. (delete-other-windows)
  18838. (org-switch-to-buffer-other-window abuf))))
  18839. (setq buffer-read-only nil)
  18840. (erase-buffer)
  18841. (org-agenda-mode)
  18842. (and name (not org-agenda-name)
  18843. (org-set-local 'org-agenda-name name)))
  18844. (setq buffer-read-only nil))
  18845. (defun org-finalize-agenda ()
  18846. "Finishing touch for the agenda buffer, called just before displaying it."
  18847. (unless org-agenda-multi
  18848. (save-excursion
  18849. (let ((inhibit-read-only t))
  18850. (goto-char (point-min))
  18851. (while (org-activate-bracket-links (point-max))
  18852. (add-text-properties (match-beginning 0) (match-end 0)
  18853. '(face org-link)))
  18854. (org-agenda-align-tags)
  18855. (unless org-agenda-with-colors
  18856. (remove-text-properties (point-min) (point-max) '(face nil))))
  18857. (if (and (boundp 'org-overriding-columns-format)
  18858. org-overriding-columns-format)
  18859. (org-set-local 'org-overriding-columns-format
  18860. org-overriding-columns-format))
  18861. (if (and (boundp 'org-agenda-view-columns-initially)
  18862. org-agenda-view-columns-initially)
  18863. (org-agenda-columns))
  18864. (when org-agenda-fontify-priorities
  18865. (org-fontify-priorities))
  18866. (run-hooks 'org-finalize-agenda-hook)
  18867. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18868. )))
  18869. (defun org-fontify-priorities ()
  18870. "Make highest priority lines bold, and lowest italic."
  18871. (interactive)
  18872. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18873. (org-delete-overlay o)))
  18874. (org-overlays-in (point-min) (point-max)))
  18875. (save-excursion
  18876. (let ((inhibit-read-only t)
  18877. b e p ov h l)
  18878. (goto-char (point-min))
  18879. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18880. (setq h (or (get-char-property (point) 'org-highest-priority)
  18881. org-highest-priority)
  18882. l (or (get-char-property (point) 'org-lowest-priority)
  18883. org-lowest-priority)
  18884. p (string-to-char (match-string 1))
  18885. b (match-beginning 0) e (point-at-eol)
  18886. ov (org-make-overlay b e))
  18887. (org-overlay-put
  18888. ov 'face
  18889. (cond ((listp org-agenda-fontify-priorities)
  18890. (cdr (assoc p org-agenda-fontify-priorities)))
  18891. ((equal p l) 'italic)
  18892. ((equal p h) 'bold)))
  18893. (org-overlay-put ov 'org-type 'org-priority)))))
  18894. (defun org-prepare-agenda-buffers (files)
  18895. "Create buffers for all agenda files, protect archived trees and comments."
  18896. (interactive)
  18897. (let ((pa '(:org-archived t))
  18898. (pc '(:org-comment t))
  18899. (pall '(:org-archived t :org-comment t))
  18900. (inhibit-read-only t)
  18901. (rea (concat ":" org-archive-tag ":"))
  18902. bmp file re)
  18903. (save-excursion
  18904. (save-restriction
  18905. (while (setq file (pop files))
  18906. (if (bufferp file)
  18907. (set-buffer file)
  18908. (org-check-agenda-file file)
  18909. (set-buffer (org-get-agenda-file-buffer file)))
  18910. (widen)
  18911. (setq bmp (buffer-modified-p))
  18912. (org-refresh-category-properties)
  18913. (setq org-todo-keywords-for-agenda
  18914. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18915. (setq org-done-keywords-for-agenda
  18916. (append org-done-keywords-for-agenda org-done-keywords))
  18917. (save-excursion
  18918. (remove-text-properties (point-min) (point-max) pall)
  18919. (when org-agenda-skip-archived-trees
  18920. (goto-char (point-min))
  18921. (while (re-search-forward rea nil t)
  18922. (if (org-on-heading-p t)
  18923. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18924. (goto-char (point-min))
  18925. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18926. (while (re-search-forward re nil t)
  18927. (add-text-properties
  18928. (match-beginning 0) (org-end-of-subtree t) pc)))
  18929. (set-buffer-modified-p bmp))))))
  18930. (defvar org-agenda-skip-function nil
  18931. "Function to be called at each match during agenda construction.
  18932. If this function returns nil, the current match should not be skipped.
  18933. Otherwise, the function must return a position from where the search
  18934. should be continued.
  18935. This may also be a Lisp form, it will be evaluated.
  18936. Never set this variable using `setq' or so, because then it will apply
  18937. to all future agenda commands. Instead, bind it with `let' to scope
  18938. it dynamically into the agenda-constructing command. A good way to set
  18939. it is through options in org-agenda-custom-commands.")
  18940. (defun org-agenda-skip ()
  18941. "Throw to `:skip' in places that should be skipped.
  18942. Also moves point to the end of the skipped region, so that search can
  18943. continue from there."
  18944. (let ((p (point-at-bol)) to fp)
  18945. (and org-agenda-skip-archived-trees
  18946. (get-text-property p :org-archived)
  18947. (org-end-of-subtree t)
  18948. (throw :skip t))
  18949. (and (get-text-property p :org-comment)
  18950. (org-end-of-subtree t)
  18951. (throw :skip t))
  18952. (if (equal (char-after p) ?#) (throw :skip t))
  18953. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18954. (consp org-agenda-skip-function))
  18955. (setq to (save-excursion
  18956. (save-match-data
  18957. (if fp
  18958. (funcall org-agenda-skip-function)
  18959. (eval org-agenda-skip-function))))))
  18960. (goto-char to)
  18961. (throw :skip t))))
  18962. (defvar org-agenda-markers nil
  18963. "List of all currently active markers created by `org-agenda'.")
  18964. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18965. "Creation time of the last agenda marker.")
  18966. (defun org-agenda-new-marker (&optional pos)
  18967. "Return a new agenda marker.
  18968. Org-mode keeps a list of these markers and resets them when they are
  18969. no longer in use."
  18970. (let ((m (copy-marker (or pos (point)))))
  18971. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18972. (push m org-agenda-markers)
  18973. m))
  18974. (defun org-agenda-reset-markers ()
  18975. "Reset markers created by `org-agenda'."
  18976. (while org-agenda-markers
  18977. (move-marker (pop org-agenda-markers) nil)))
  18978. (defun org-get-agenda-file-buffer (file)
  18979. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18980. it to the list of buffers which might be released later."
  18981. (let ((buf (org-find-base-buffer-visiting file)))
  18982. (if buf
  18983. buf ; just return it
  18984. ;; Make a new buffer and remember it
  18985. (setq buf (find-file-noselect file))
  18986. (if buf (push buf org-agenda-new-buffers))
  18987. buf)))
  18988. (defun org-release-buffers (blist)
  18989. "Release all buffers in list, asking the user for confirmation when needed.
  18990. When a buffer is unmodified, it is just killed. When modified, it is saved
  18991. \(if the user agrees) and then killed."
  18992. (let (buf file)
  18993. (while (setq buf (pop blist))
  18994. (setq file (buffer-file-name buf))
  18995. (when (and (buffer-modified-p buf)
  18996. file
  18997. (y-or-n-p (format "Save file %s? " file)))
  18998. (with-current-buffer buf (save-buffer)))
  18999. (kill-buffer buf))))
  19000. (defun org-get-category (&optional pos)
  19001. "Get the category applying to position POS."
  19002. (get-text-property (or pos (point)) 'org-category))
  19003. ;;; Agenda timeline
  19004. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  19005. (defun org-timeline (&optional include-all)
  19006. "Show a time-sorted view of the entries in the current org file.
  19007. Only entries with a time stamp of today or later will be listed. With
  19008. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  19009. under the current date.
  19010. If the buffer contains an active region, only check the region for
  19011. dates."
  19012. (interactive "P")
  19013. (require 'calendar)
  19014. (org-compile-prefix-format 'timeline)
  19015. (org-set-sorting-strategy 'timeline)
  19016. (let* ((dopast t)
  19017. (dotodo include-all)
  19018. (doclosed org-agenda-show-log)
  19019. (entry buffer-file-name)
  19020. (date (calendar-current-date))
  19021. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  19022. (end (if (org-region-active-p) (region-end) (point-max)))
  19023. (day-numbers (org-get-all-dates beg end 'no-ranges
  19024. t doclosed ; always include today
  19025. org-timeline-show-empty-dates))
  19026. (org-deadline-warning-days 0)
  19027. (org-agenda-only-exact-dates t)
  19028. (today (time-to-days (current-time)))
  19029. (past t)
  19030. args
  19031. s e rtn d emptyp)
  19032. (setq org-agenda-redo-command
  19033. (list 'progn
  19034. (list 'org-switch-to-buffer-other-window (current-buffer))
  19035. (list 'org-timeline (list 'quote include-all))))
  19036. (if (not dopast)
  19037. ;; Remove past dates from the list of dates.
  19038. (setq day-numbers (delq nil (mapcar (lambda(x)
  19039. (if (>= x today) x nil))
  19040. day-numbers))))
  19041. (org-prepare-agenda (concat "Timeline "
  19042. (file-name-nondirectory buffer-file-name)))
  19043. (if doclosed (push :closed args))
  19044. (push :timestamp args)
  19045. (push :deadline args)
  19046. (push :scheduled args)
  19047. (push :sexp args)
  19048. (if dotodo (push :todo args))
  19049. (while (setq d (pop day-numbers))
  19050. (if (and (listp d) (eq (car d) :omitted))
  19051. (progn
  19052. (setq s (point))
  19053. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19054. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19055. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19056. (if (and (>= d today)
  19057. dopast
  19058. past)
  19059. (progn
  19060. (setq past nil)
  19061. (insert (make-string 79 ?-) "\n")))
  19062. (setq date (calendar-gregorian-from-absolute d))
  19063. (setq s (point))
  19064. (setq rtn (and (not emptyp)
  19065. (apply 'org-agenda-get-day-entries entry
  19066. date args)))
  19067. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19068. (progn
  19069. (insert
  19070. (if (stringp org-agenda-format-date)
  19071. (format-time-string org-agenda-format-date
  19072. (org-time-from-absolute date))
  19073. (funcall org-agenda-format-date date))
  19074. "\n")
  19075. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19076. (put-text-property s (1- (point)) 'org-date-line t)
  19077. (if (equal d today)
  19078. (put-text-property s (1- (point)) 'org-today t))
  19079. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19080. (put-text-property s (1- (point)) 'day d)))))
  19081. (goto-char (point-min))
  19082. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19083. (point-min)))
  19084. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19085. (org-finalize-agenda)
  19086. (setq buffer-read-only t)))
  19087. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19088. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19089. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19090. not every single day in the range. If FORCE-TODAY is non-nil, make
  19091. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19092. inactive time stamps (those in square brackets) are included.
  19093. When EMPTY is non-nil, also include days without any entries."
  19094. (let ((re (concat
  19095. (if pre-re pre-re "")
  19096. (if inactive org-ts-regexp-both org-ts-regexp)))
  19097. dates dates1 date day day1 day2 ts1 ts2)
  19098. (if force-today
  19099. (setq dates (list (time-to-days (current-time)))))
  19100. (save-excursion
  19101. (goto-char beg)
  19102. (while (re-search-forward re end t)
  19103. (setq day (time-to-days (org-time-string-to-time
  19104. (substring (match-string 1) 0 10))))
  19105. (or (memq day dates) (push day dates)))
  19106. (unless no-ranges
  19107. (goto-char beg)
  19108. (while (re-search-forward org-tr-regexp end t)
  19109. (setq ts1 (substring (match-string 1) 0 10)
  19110. ts2 (substring (match-string 2) 0 10)
  19111. day1 (time-to-days (org-time-string-to-time ts1))
  19112. day2 (time-to-days (org-time-string-to-time ts2)))
  19113. (while (< (setq day1 (1+ day1)) day2)
  19114. (or (memq day1 dates) (push day1 dates)))))
  19115. (setq dates (sort dates '<))
  19116. (when empty
  19117. (while (setq day (pop dates))
  19118. (setq day2 (car dates))
  19119. (push day dates1)
  19120. (when (and day2 empty)
  19121. (if (or (eq empty t)
  19122. (and (numberp empty) (<= (- day2 day) empty)))
  19123. (while (< (setq day (1+ day)) day2)
  19124. (push (list day) dates1))
  19125. (push (cons :omitted (- day2 day)) dates1))))
  19126. (setq dates (nreverse dates1)))
  19127. dates)))
  19128. ;;; Agenda Daily/Weekly
  19129. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19130. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19131. (defvar org-agenda-last-arguments nil
  19132. "The arguments of the previous call to org-agenda")
  19133. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19134. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19135. (defvar org-include-all-loc nil) ; local variable
  19136. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19137. ;;;###autoload
  19138. (defun org-agenda-list (&optional include-all start-day ndays)
  19139. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19140. The view will be for the current day or week, but from the overview buffer
  19141. you will be able to go to other days/weeks.
  19142. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19143. all unfinished TODO items will also be shown, before the agenda.
  19144. This feature is considered obsolete, please use the TODO list or a block
  19145. agenda instead.
  19146. With a numeric prefix argument in an interactive call, the agenda will
  19147. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19148. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19149. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19150. given in `org-agenda-start-on-weekday'."
  19151. (interactive "P")
  19152. (if (and (integerp include-all) (> include-all 0))
  19153. (setq ndays include-all include-all nil))
  19154. (setq ndays (or ndays org-agenda-ndays)
  19155. start-day (or start-day org-agenda-start-day))
  19156. (if org-agenda-overriding-arguments
  19157. (setq include-all (car org-agenda-overriding-arguments)
  19158. start-day (nth 1 org-agenda-overriding-arguments)
  19159. ndays (nth 2 org-agenda-overriding-arguments)))
  19160. (if (stringp start-day)
  19161. ;; Convert to an absolute day number
  19162. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19163. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19164. (org-compile-prefix-format 'agenda)
  19165. (org-set-sorting-strategy 'agenda)
  19166. (require 'calendar)
  19167. (let* ((org-agenda-start-on-weekday
  19168. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19169. org-agenda-start-on-weekday nil))
  19170. (thefiles (org-agenda-files))
  19171. (files thefiles)
  19172. (today (time-to-days
  19173. (time-subtract (current-time)
  19174. (list 0 (* 3600 org-extend-today-until) 0))))
  19175. (sd (or start-day today))
  19176. (start (if (or (null org-agenda-start-on-weekday)
  19177. (< org-agenda-ndays 7))
  19178. sd
  19179. (let* ((nt (calendar-day-of-week
  19180. (calendar-gregorian-from-absolute sd)))
  19181. (n1 org-agenda-start-on-weekday)
  19182. (d (- nt n1)))
  19183. (- sd (+ (if (< d 0) 7 0) d)))))
  19184. (day-numbers (list start))
  19185. (day-cnt 0)
  19186. (inhibit-redisplay (not debug-on-error))
  19187. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19188. (setq org-agenda-redo-command
  19189. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19190. ;; Make the list of days
  19191. (setq ndays (or ndays org-agenda-ndays)
  19192. nd ndays)
  19193. (while (> ndays 1)
  19194. (push (1+ (car day-numbers)) day-numbers)
  19195. (setq ndays (1- ndays)))
  19196. (setq day-numbers (nreverse day-numbers))
  19197. (org-prepare-agenda "Day/Week")
  19198. (org-set-local 'org-starting-day (car day-numbers))
  19199. (org-set-local 'org-include-all-loc include-all)
  19200. (org-set-local 'org-agenda-span
  19201. (org-agenda-ndays-to-span nd))
  19202. (when (and (or include-all org-agenda-include-all-todo)
  19203. (member today day-numbers))
  19204. (setq files thefiles
  19205. rtnall nil)
  19206. (while (setq file (pop files))
  19207. (catch 'nextfile
  19208. (org-check-agenda-file file)
  19209. (setq date (calendar-gregorian-from-absolute today)
  19210. rtn (org-agenda-get-day-entries
  19211. file date :todo))
  19212. (setq rtnall (append rtnall rtn))))
  19213. (when rtnall
  19214. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19215. (add-text-properties (point-min) (1- (point))
  19216. (list 'face 'org-agenda-structure))
  19217. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19218. (unless org-agenda-compact-blocks
  19219. (setq s (point))
  19220. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19221. "-agenda:\n")
  19222. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19223. 'org-date-line t)))
  19224. (while (setq d (pop day-numbers))
  19225. (setq date (calendar-gregorian-from-absolute d)
  19226. s (point))
  19227. (if (or (setq todayp (= d today))
  19228. (and (not start-pos) (= d sd)))
  19229. (setq start-pos (point))
  19230. (if (and start-pos (not end-pos))
  19231. (setq end-pos (point))))
  19232. (setq files thefiles
  19233. rtnall nil)
  19234. (while (setq file (pop files))
  19235. (catch 'nextfile
  19236. (org-check-agenda-file file)
  19237. (if org-agenda-show-log
  19238. (setq rtn (org-agenda-get-day-entries
  19239. file date
  19240. :deadline :scheduled :timestamp :sexp :closed))
  19241. (setq rtn (org-agenda-get-day-entries
  19242. file date
  19243. :deadline :scheduled :sexp :timestamp)))
  19244. (setq rtnall (append rtnall rtn))))
  19245. (if org-agenda-include-diary
  19246. (progn
  19247. (require 'diary-lib)
  19248. (setq rtn (org-get-entries-from-diary date))
  19249. (setq rtnall (append rtnall rtn))))
  19250. (if (or rtnall org-agenda-show-all-dates)
  19251. (progn
  19252. (setq day-cnt (1+ day-cnt))
  19253. (insert
  19254. (if (stringp org-agenda-format-date)
  19255. (format-time-string org-agenda-format-date
  19256. (org-time-from-absolute date))
  19257. (funcall org-agenda-format-date date))
  19258. "\n")
  19259. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19260. (put-text-property s (1- (point)) 'org-date-line t)
  19261. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19262. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19263. (if rtnall (insert
  19264. (org-finalize-agenda-entries
  19265. (org-agenda-add-time-grid-maybe
  19266. rtnall nd todayp))
  19267. "\n"))
  19268. (put-text-property s (1- (point)) 'day d)
  19269. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19270. (goto-char (point-min))
  19271. (org-fit-agenda-window)
  19272. (unless (and (pos-visible-in-window-p (point-min))
  19273. (pos-visible-in-window-p (point-max)))
  19274. (goto-char (1- (point-max)))
  19275. (recenter -1)
  19276. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19277. (progn
  19278. (goto-char (or start-pos 1))
  19279. (recenter 1))))
  19280. (goto-char (or start-pos 1))
  19281. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19282. (org-finalize-agenda)
  19283. (setq buffer-read-only t)
  19284. (message "")))
  19285. (defun org-agenda-ndays-to-span (n)
  19286. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19287. ;;; Agenda word search
  19288. (defvar org-agenda-search-history nil)
  19289. ;;;###autoload
  19290. (defun org-search-view (&optional arg string)
  19291. "Show all entries that contain words or regular expressions.
  19292. If the first character of the search string is an asterisks,
  19293. search only the headlines.
  19294. The search string is broken into \"words\" by splitting at whitespace.
  19295. The individual words are then interpreted as a boolean expression with
  19296. logical AND. Words prefixed with a minus must not occur in the entry.
  19297. Words without a prefix or prefixed with a plus must occur in the entry.
  19298. Matching is case-insensitive and the words are enclosed by word delimiters.
  19299. Words enclosed by curly braces are interpreted as regular expressions
  19300. that must or must not match in the entry.
  19301. This command searches the agenda files, and in addition the files listed
  19302. in `org-agenda-text-search-extra-files'."
  19303. (interactive "P")
  19304. (org-compile-prefix-format 'search)
  19305. (org-set-sorting-strategy 'search)
  19306. (org-prepare-agenda "SEARCH")
  19307. (let* ((props (list 'face nil
  19308. 'done-face 'org-done
  19309. 'org-not-done-regexp org-not-done-regexp
  19310. 'org-todo-regexp org-todo-regexp
  19311. 'mouse-face 'highlight
  19312. 'keymap org-agenda-keymap
  19313. 'help-echo (format "mouse-2 or RET jump to location")))
  19314. regexp rtn rtnall files file pos
  19315. marker priority category tags c neg re
  19316. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19317. (unless (and (not arg)
  19318. (stringp string)
  19319. (string-match "\\S-" string))
  19320. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19321. (cond
  19322. ((integerp arg) (cons string arg))
  19323. (arg string))
  19324. 'org-agenda-search-history)))
  19325. (setq org-agenda-redo-command
  19326. (list 'org-search-view 'current-prefix-arg string))
  19327. (setq org-agenda-query-string string)
  19328. (if (equal (string-to-char string) ?*)
  19329. (setq hdl-only t
  19330. words (substring string 1))
  19331. (setq words string))
  19332. (setq words (org-split-string words))
  19333. (mapc (lambda (w)
  19334. (setq c (string-to-char w))
  19335. (if (equal c ?-)
  19336. (setq neg t w (substring w 1))
  19337. (if (equal c ?+)
  19338. (setq neg nil w (substring w 1))
  19339. (setq neg nil)))
  19340. (if (string-match "\\`{.*}\\'" w)
  19341. (setq re (substring w 1 -1))
  19342. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19343. (if neg (push re regexps-) (push re regexps+)))
  19344. words)
  19345. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19346. (if (not regexps+)
  19347. (setq regexp (concat "^" org-outline-regexp))
  19348. (setq regexp (pop regexps+))
  19349. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19350. regexp))))
  19351. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19352. rtnall nil)
  19353. (while (setq file (pop files))
  19354. (setq ee nil)
  19355. (catch 'nextfile
  19356. (org-check-agenda-file file)
  19357. (setq buffer (if (file-exists-p file)
  19358. (org-get-agenda-file-buffer file)
  19359. (error "No such file %s" file)))
  19360. (if (not buffer)
  19361. ;; If file does not exist, make sure an error message is sent
  19362. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19363. file))))
  19364. (with-current-buffer buffer
  19365. (unless (org-mode-p)
  19366. (error "Agenda file %s is not in `org-mode'" file))
  19367. (let ((case-fold-search t))
  19368. (save-excursion
  19369. (save-restriction
  19370. (if org-agenda-restrict
  19371. (narrow-to-region org-agenda-restrict-begin
  19372. org-agenda-restrict-end)
  19373. (widen))
  19374. (goto-char (point-min))
  19375. (unless (or (org-on-heading-p)
  19376. (outline-next-heading))
  19377. (throw 'nextfile t))
  19378. (goto-char (max (point-min) (1- (point))))
  19379. (while (re-search-forward regexp nil t)
  19380. (org-back-to-heading t)
  19381. (skip-chars-forward "* ")
  19382. (setq beg (point-at-bol)
  19383. beg1 (point)
  19384. end (progn (outline-next-heading) (point)))
  19385. (catch :skip
  19386. (goto-char beg)
  19387. (org-agenda-skip)
  19388. (setq str (buffer-substring-no-properties
  19389. (point-at-bol)
  19390. (if hdl-only (point-at-eol) end)))
  19391. (mapc (lambda (wr) (when (string-match wr str)
  19392. (goto-char (1- end))
  19393. (throw :skip t)))
  19394. regexps-)
  19395. (mapc (lambda (wr) (unless (string-match wr str)
  19396. (goto-char (1- end))
  19397. (throw :skip t)))
  19398. regexps+)
  19399. (goto-char beg)
  19400. (setq marker (org-agenda-new-marker (point))
  19401. category (org-get-category)
  19402. tags (org-get-tags-at (point))
  19403. txt (org-format-agenda-item
  19404. ""
  19405. (buffer-substring-no-properties
  19406. beg1 (point-at-eol))
  19407. category tags))
  19408. (org-add-props txt props
  19409. 'org-marker marker 'org-hd-marker marker
  19410. 'priority 1000 'org-category category
  19411. 'type "search")
  19412. (push txt ee)
  19413. (goto-char (1- end)))))))))
  19414. (setq rtn (nreverse ee))
  19415. (setq rtnall (append rtnall rtn)))
  19416. (if org-agenda-overriding-header
  19417. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19418. nil 'face 'org-agenda-structure) "\n")
  19419. (insert "Search words: ")
  19420. (add-text-properties (point-min) (1- (point))
  19421. (list 'face 'org-agenda-structure))
  19422. (setq pos (point))
  19423. (insert string "\n")
  19424. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19425. (setq pos (point))
  19426. (unless org-agenda-multi
  19427. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19428. (add-text-properties pos (1- (point))
  19429. (list 'face 'org-agenda-structure))))
  19430. (when rtnall
  19431. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19432. (goto-char (point-min))
  19433. (org-fit-agenda-window)
  19434. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19435. (org-finalize-agenda)
  19436. (setq buffer-read-only t)))
  19437. ;;; Agenda TODO list
  19438. (defvar org-select-this-todo-keyword nil)
  19439. (defvar org-last-arg nil)
  19440. ;;;###autoload
  19441. (defun org-todo-list (arg)
  19442. "Show all TODO entries from all agenda file in a single list.
  19443. The prefix arg can be used to select a specific TODO keyword and limit
  19444. the list to these. When using \\[universal-argument], you will be prompted
  19445. for a keyword. A numeric prefix directly selects the Nth keyword in
  19446. `org-todo-keywords-1'."
  19447. (interactive "P")
  19448. (require 'calendar)
  19449. (org-compile-prefix-format 'todo)
  19450. (org-set-sorting-strategy 'todo)
  19451. (org-prepare-agenda "TODO")
  19452. (let* ((today (time-to-days (current-time)))
  19453. (date (calendar-gregorian-from-absolute today))
  19454. (kwds org-todo-keywords-for-agenda)
  19455. (completion-ignore-case t)
  19456. (org-select-this-todo-keyword
  19457. (if (stringp arg) arg
  19458. (and arg (integerp arg) (> arg 0)
  19459. (nth (1- arg) kwds))))
  19460. rtn rtnall files file pos)
  19461. (when (equal arg '(4))
  19462. (setq org-select-this-todo-keyword
  19463. (completing-read "Keyword (or KWD1|K2D2|...): "
  19464. (mapcar 'list kwds) nil nil)))
  19465. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19466. (org-set-local 'org-last-arg arg)
  19467. (setq org-agenda-redo-command
  19468. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19469. (setq files (org-agenda-files)
  19470. rtnall nil)
  19471. (while (setq file (pop files))
  19472. (catch 'nextfile
  19473. (org-check-agenda-file file)
  19474. (setq rtn (org-agenda-get-day-entries file date :todo))
  19475. (setq rtnall (append rtnall rtn))))
  19476. (if org-agenda-overriding-header
  19477. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19478. nil 'face 'org-agenda-structure) "\n")
  19479. (insert "Global list of TODO items of type: ")
  19480. (add-text-properties (point-min) (1- (point))
  19481. (list 'face 'org-agenda-structure))
  19482. (setq pos (point))
  19483. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19484. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19485. (setq pos (point))
  19486. (unless org-agenda-multi
  19487. (insert "Available with `N r': (0)ALL")
  19488. (let ((n 0) s)
  19489. (mapc (lambda (x)
  19490. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19491. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19492. (insert "\n "))
  19493. (insert " " s))
  19494. kwds))
  19495. (insert "\n"))
  19496. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19497. (when rtnall
  19498. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19499. (goto-char (point-min))
  19500. (org-fit-agenda-window)
  19501. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19502. (org-finalize-agenda)
  19503. (setq buffer-read-only t)))
  19504. ;;; Agenda tags match
  19505. ;;;###autoload
  19506. (defun org-tags-view (&optional todo-only match)
  19507. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19508. The prefix arg TODO-ONLY limits the search to TODO entries."
  19509. (interactive "P")
  19510. (org-compile-prefix-format 'tags)
  19511. (org-set-sorting-strategy 'tags)
  19512. (let* ((org-tags-match-list-sublevels
  19513. (if todo-only t org-tags-match-list-sublevels))
  19514. (completion-ignore-case t)
  19515. rtn rtnall files file pos matcher
  19516. buffer)
  19517. (setq matcher (org-make-tags-matcher match)
  19518. match (car matcher) matcher (cdr matcher))
  19519. (org-prepare-agenda (concat "TAGS " match))
  19520. (setq org-agenda-redo-command
  19521. (list 'org-tags-view (list 'quote todo-only)
  19522. (list 'if 'current-prefix-arg nil match)))
  19523. (setq files (org-agenda-files)
  19524. rtnall nil)
  19525. (while (setq file (pop files))
  19526. (catch 'nextfile
  19527. (org-check-agenda-file file)
  19528. (setq buffer (if (file-exists-p file)
  19529. (org-get-agenda-file-buffer file)
  19530. (error "No such file %s" file)))
  19531. (if (not buffer)
  19532. ;; If file does not exist, merror message to agenda
  19533. (setq rtn (list
  19534. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19535. rtnall (append rtnall rtn))
  19536. (with-current-buffer buffer
  19537. (unless (org-mode-p)
  19538. (error "Agenda file %s is not in `org-mode'" file))
  19539. (save-excursion
  19540. (save-restriction
  19541. (if org-agenda-restrict
  19542. (narrow-to-region org-agenda-restrict-begin
  19543. org-agenda-restrict-end)
  19544. (widen))
  19545. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19546. (setq rtnall (append rtnall rtn))))))))
  19547. (if org-agenda-overriding-header
  19548. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19549. nil 'face 'org-agenda-structure) "\n")
  19550. (insert "Headlines with TAGS match: ")
  19551. (add-text-properties (point-min) (1- (point))
  19552. (list 'face 'org-agenda-structure))
  19553. (setq pos (point))
  19554. (insert match "\n")
  19555. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19556. (setq pos (point))
  19557. (unless org-agenda-multi
  19558. (insert "Press `C-u r' to search again with new search string\n"))
  19559. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19560. (when rtnall
  19561. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19562. (goto-char (point-min))
  19563. (org-fit-agenda-window)
  19564. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19565. (org-finalize-agenda)
  19566. (setq buffer-read-only t)))
  19567. ;;; Agenda Finding stuck projects
  19568. (defvar org-agenda-skip-regexp nil
  19569. "Regular expression used in skipping subtrees for the agenda.
  19570. This is basically a temporary global variable that can be set and then
  19571. used by user-defined selections using `org-agenda-skip-function'.")
  19572. (defvar org-agenda-overriding-header nil
  19573. "When this is set during todo and tags searches, will replace header.")
  19574. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19575. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19576. If yes, it returns the end position of this tree, causing agenda commands
  19577. to skip this subtree. This is a function that can be put into
  19578. `org-agenda-skip-function' for the duration of a command."
  19579. (let ((end (save-excursion (org-end-of-subtree t)))
  19580. skip)
  19581. (save-excursion
  19582. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19583. (and skip end)))
  19584. (defun org-agenda-skip-entry-if (&rest conditions)
  19585. "Skip entry if any of CONDITIONS is true.
  19586. See `org-agenda-skip-if' for details."
  19587. (org-agenda-skip-if nil conditions))
  19588. (defun org-agenda-skip-subtree-if (&rest conditions)
  19589. "Skip entry if any of CONDITIONS is true.
  19590. See `org-agenda-skip-if' for details."
  19591. (org-agenda-skip-if t conditions))
  19592. (defun org-agenda-skip-if (subtree conditions)
  19593. "Checks current entity for CONDITIONS.
  19594. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19595. the entry, i.e. the text before the next heading is checked.
  19596. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19597. from different tests. Valid conditions are:
  19598. scheduled Check if there is a scheduled cookie
  19599. notscheduled Check if there is no scheduled cookie
  19600. deadline Check if there is a deadline
  19601. notdeadline Check if there is no deadline
  19602. regexp Check if regexp matches
  19603. notregexp Check if regexp does not match.
  19604. The regexp is taken from the conditions list, it must come right after
  19605. the `regexp' or `notregexp' element.
  19606. If any of these conditions is met, this function returns the end point of
  19607. the entity, causing the search to continue from there. This is a function
  19608. that can be put into `org-agenda-skip-function' for the duration of a command."
  19609. (let (beg end m)
  19610. (org-back-to-heading t)
  19611. (setq beg (point)
  19612. end (if subtree
  19613. (progn (org-end-of-subtree t) (point))
  19614. (progn (outline-next-heading) (1- (point)))))
  19615. (goto-char beg)
  19616. (and
  19617. (or
  19618. (and (memq 'scheduled conditions)
  19619. (re-search-forward org-scheduled-time-regexp end t))
  19620. (and (memq 'notscheduled conditions)
  19621. (not (re-search-forward org-scheduled-time-regexp end t)))
  19622. (and (memq 'deadline conditions)
  19623. (re-search-forward org-deadline-time-regexp end t))
  19624. (and (memq 'notdeadline conditions)
  19625. (not (re-search-forward org-deadline-time-regexp end t)))
  19626. (and (setq m (memq 'regexp conditions))
  19627. (stringp (nth 1 m))
  19628. (re-search-forward (nth 1 m) end t))
  19629. (and (setq m (memq 'notregexp conditions))
  19630. (stringp (nth 1 m))
  19631. (not (re-search-forward (nth 1 m) end t))))
  19632. end)))
  19633. ;;;###autoload
  19634. (defun org-agenda-list-stuck-projects (&rest ignore)
  19635. "Create agenda view for projects that are stuck.
  19636. Stuck projects are project that have no next actions. For the definitions
  19637. of what a project is and how to check if it stuck, customize the variable
  19638. `org-stuck-projects'.
  19639. MATCH is being ignored."
  19640. (interactive)
  19641. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19642. ;; FIXME: we could have used org-agenda-skip-if here.
  19643. (org-agenda-overriding-header "List of stuck projects: ")
  19644. (matcher (nth 0 org-stuck-projects))
  19645. (todo (nth 1 org-stuck-projects))
  19646. (todo-wds (if (member "*" todo)
  19647. (progn
  19648. (org-prepare-agenda-buffers (org-agenda-files))
  19649. (org-delete-all
  19650. org-done-keywords-for-agenda
  19651. (copy-sequence org-todo-keywords-for-agenda)))
  19652. todo))
  19653. (todo-re (concat "^\\*+[ \t]+\\("
  19654. (mapconcat 'identity todo-wds "\\|")
  19655. "\\)\\>"))
  19656. (tags (nth 2 org-stuck-projects))
  19657. (tags-re (if (member "*" tags)
  19658. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19659. (concat "^\\*+ .*:\\("
  19660. (mapconcat 'identity tags "\\|")
  19661. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19662. (gen-re (nth 3 org-stuck-projects))
  19663. (re-list
  19664. (delq nil
  19665. (list
  19666. (if todo todo-re)
  19667. (if tags tags-re)
  19668. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19669. gen-re)))))
  19670. (setq org-agenda-skip-regexp
  19671. (if re-list
  19672. (mapconcat 'identity re-list "\\|")
  19673. (error "No information how to identify unstuck projects")))
  19674. (org-tags-view nil matcher)
  19675. (with-current-buffer org-agenda-buffer-name
  19676. (setq org-agenda-redo-command
  19677. '(org-agenda-list-stuck-projects
  19678. (or current-prefix-arg org-last-arg))))))
  19679. ;;; Diary integration
  19680. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19681. (defun org-get-entries-from-diary (date)
  19682. "Get the (Emacs Calendar) diary entries for DATE."
  19683. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19684. (diary-display-hook '(fancy-diary-display))
  19685. (pop-up-frames nil)
  19686. (list-diary-entries-hook
  19687. (cons 'org-diary-default-entry list-diary-entries-hook))
  19688. (diary-file-name-prefix-function nil) ; turn this feature off
  19689. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19690. entries
  19691. (org-disable-agenda-to-diary t))
  19692. (save-excursion
  19693. (save-window-excursion
  19694. (funcall (if (fboundp 'diary-list-entries)
  19695. 'diary-list-entries 'list-diary-entries)
  19696. date 1)))
  19697. (if (not (get-buffer fancy-diary-buffer))
  19698. (setq entries nil)
  19699. (with-current-buffer fancy-diary-buffer
  19700. (setq buffer-read-only nil)
  19701. (if (zerop (buffer-size))
  19702. ;; No entries
  19703. (setq entries nil)
  19704. ;; Omit the date and other unnecessary stuff
  19705. (org-agenda-cleanup-fancy-diary)
  19706. ;; Add prefix to each line and extend the text properties
  19707. (if (zerop (buffer-size))
  19708. (setq entries nil)
  19709. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19710. (set-buffer-modified-p nil)
  19711. (kill-buffer fancy-diary-buffer)))
  19712. (when entries
  19713. (setq entries (org-split-string entries "\n"))
  19714. (setq entries
  19715. (mapcar
  19716. (lambda (x)
  19717. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19718. ;; Extend the text properties to the beginning of the line
  19719. (org-add-props x (text-properties-at (1- (length x)) x)
  19720. 'type "diary" 'date date))
  19721. entries)))))
  19722. (defun org-agenda-cleanup-fancy-diary ()
  19723. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19724. This gets rid of the date, the underline under the date, and
  19725. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19726. date. It also removes lines that contain only whitespace."
  19727. (goto-char (point-min))
  19728. (if (looking-at ".*?:[ \t]*")
  19729. (progn
  19730. (replace-match "")
  19731. (re-search-forward "\n=+$" nil t)
  19732. (replace-match "")
  19733. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19734. (re-search-forward "\n=+$" nil t)
  19735. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19736. (goto-char (point-min))
  19737. (while (re-search-forward "^ +\n" nil t)
  19738. (replace-match ""))
  19739. (goto-char (point-min))
  19740. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19741. (replace-match "")))
  19742. ;; Make sure entries from the diary have the right text properties.
  19743. (eval-after-load "diary-lib"
  19744. '(if (boundp 'diary-modify-entry-list-string-function)
  19745. ;; We can rely on the hook, nothing to do
  19746. nil
  19747. ;; Hook not avaiable, must use advice to make this work
  19748. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19749. "Make the position visible."
  19750. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19751. (stringp string)
  19752. buffer-file-name)
  19753. (setq string (org-modify-diary-entry-string string))))))
  19754. (defun org-modify-diary-entry-string (string)
  19755. "Add text properties to string, allowing org-mode to act on it."
  19756. (org-add-props string nil
  19757. 'mouse-face 'highlight
  19758. 'keymap org-agenda-keymap
  19759. 'help-echo (if buffer-file-name
  19760. (format "mouse-2 or RET jump to diary file %s"
  19761. (abbreviate-file-name buffer-file-name))
  19762. "")
  19763. 'org-agenda-diary-link t
  19764. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19765. (defun org-diary-default-entry ()
  19766. "Add a dummy entry to the diary.
  19767. Needed to avoid empty dates which mess up holiday display."
  19768. ;; Catch the error if dealing with the new add-to-diary-alist
  19769. (when org-disable-agenda-to-diary
  19770. (condition-case nil
  19771. (add-to-diary-list original-date "Org-mode dummy" "")
  19772. (error
  19773. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19774. ;;;###autoload
  19775. (defun org-diary (&rest args)
  19776. "Return diary information from org-files.
  19777. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19778. It accesses org files and extracts information from those files to be
  19779. listed in the diary. The function accepts arguments specifying what
  19780. items should be listed. The following arguments are allowed:
  19781. :timestamp List the headlines of items containing a date stamp or
  19782. date range matching the selected date. Deadlines will
  19783. also be listed, on the expiration day.
  19784. :sexp List entries resulting from diary-like sexps.
  19785. :deadline List any deadlines past due, or due within
  19786. `org-deadline-warning-days'. The listing occurs only
  19787. in the diary for *today*, not at any other date. If
  19788. an entry is marked DONE, it is no longer listed.
  19789. :scheduled List all items which are scheduled for the given date.
  19790. The diary for *today* also contains items which were
  19791. scheduled earlier and are not yet marked DONE.
  19792. :todo List all TODO items from the org-file. This may be a
  19793. long list - so this is not turned on by default.
  19794. Like deadlines, these entries only show up in the
  19795. diary for *today*, not at any other date.
  19796. The call in the diary file should look like this:
  19797. &%%(org-diary) ~/path/to/some/orgfile.org
  19798. Use a separate line for each org file to check. Or, if you omit the file name,
  19799. all files listed in `org-agenda-files' will be checked automatically:
  19800. &%%(org-diary)
  19801. If you don't give any arguments (as in the example above), the default
  19802. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19803. So the example above may also be written as
  19804. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19805. The function expects the lisp variables `entry' and `date' to be provided
  19806. by the caller, because this is how the calendar works. Don't use this
  19807. function from a program - use `org-agenda-get-day-entries' instead."
  19808. (when (> (- (time-to-seconds (current-time))
  19809. org-agenda-last-marker-time)
  19810. 5)
  19811. (org-agenda-reset-markers))
  19812. (org-compile-prefix-format 'agenda)
  19813. (org-set-sorting-strategy 'agenda)
  19814. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19815. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19816. (list entry)
  19817. (org-agenda-files t)))
  19818. file rtn results)
  19819. (org-prepare-agenda-buffers files)
  19820. ;; If this is called during org-agenda, don't return any entries to
  19821. ;; the calendar. Org Agenda will list these entries itself.
  19822. (if org-disable-agenda-to-diary (setq files nil))
  19823. (while (setq file (pop files))
  19824. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19825. (setq results (append results rtn)))
  19826. (if results
  19827. (concat (org-finalize-agenda-entries results) "\n"))))
  19828. ;;; Agenda entry finders
  19829. (defun org-agenda-get-day-entries (file date &rest args)
  19830. "Does the work for `org-diary' and `org-agenda'.
  19831. FILE is the path to a file to be checked for entries. DATE is date like
  19832. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19833. which kind of entries should be extracted. For details about these, see
  19834. the documentation of `org-diary'."
  19835. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19836. (let* ((org-startup-folded nil)
  19837. (org-startup-align-all-tables nil)
  19838. (buffer (if (file-exists-p file)
  19839. (org-get-agenda-file-buffer file)
  19840. (error "No such file %s" file)))
  19841. arg results rtn)
  19842. (if (not buffer)
  19843. ;; If file does not exist, make sure an error message ends up in diary
  19844. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19845. (with-current-buffer buffer
  19846. (unless (org-mode-p)
  19847. (error "Agenda file %s is not in `org-mode'" file))
  19848. (let ((case-fold-search nil))
  19849. (save-excursion
  19850. (save-restriction
  19851. (if org-agenda-restrict
  19852. (narrow-to-region org-agenda-restrict-begin
  19853. org-agenda-restrict-end)
  19854. (widen))
  19855. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19856. (while (setq arg (pop args))
  19857. (cond
  19858. ((and (eq arg :todo)
  19859. (equal date (calendar-current-date)))
  19860. (setq rtn (org-agenda-get-todos))
  19861. (setq results (append results rtn)))
  19862. ((eq arg :timestamp)
  19863. (setq rtn (org-agenda-get-blocks))
  19864. (setq results (append results rtn))
  19865. (setq rtn (org-agenda-get-timestamps))
  19866. (setq results (append results rtn)))
  19867. ((eq arg :sexp)
  19868. (setq rtn (org-agenda-get-sexps))
  19869. (setq results (append results rtn)))
  19870. ((eq arg :scheduled)
  19871. (setq rtn (org-agenda-get-scheduled))
  19872. (setq results (append results rtn)))
  19873. ((eq arg :closed)
  19874. (setq rtn (org-agenda-get-closed))
  19875. (setq results (append results rtn)))
  19876. ((eq arg :deadline)
  19877. (setq rtn (org-agenda-get-deadlines))
  19878. (setq results (append results rtn))))))))
  19879. results))))
  19880. (defun org-entry-is-todo-p ()
  19881. (member (org-get-todo-state) org-not-done-keywords))
  19882. (defun org-entry-is-done-p ()
  19883. (member (org-get-todo-state) org-done-keywords))
  19884. (defun org-get-todo-state ()
  19885. (save-excursion
  19886. (org-back-to-heading t)
  19887. (and (looking-at org-todo-line-regexp)
  19888. (match-end 2)
  19889. (match-string 2))))
  19890. (defun org-at-date-range-p (&optional inactive-ok)
  19891. "Is the cursor inside a date range?"
  19892. (interactive)
  19893. (save-excursion
  19894. (catch 'exit
  19895. (let ((pos (point)))
  19896. (skip-chars-backward "^[<\r\n")
  19897. (skip-chars-backward "<[")
  19898. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19899. (>= (match-end 0) pos)
  19900. (throw 'exit t))
  19901. (skip-chars-backward "^<[\r\n")
  19902. (skip-chars-backward "<[")
  19903. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19904. (>= (match-end 0) pos)
  19905. (throw 'exit t)))
  19906. nil)))
  19907. (defun org-agenda-get-todos ()
  19908. "Return the TODO information for agenda display."
  19909. (let* ((props (list 'face nil
  19910. 'done-face 'org-done
  19911. 'org-not-done-regexp org-not-done-regexp
  19912. 'org-todo-regexp org-todo-regexp
  19913. 'mouse-face 'highlight
  19914. 'keymap org-agenda-keymap
  19915. 'help-echo
  19916. (format "mouse-2 or RET jump to org file %s"
  19917. (abbreviate-file-name buffer-file-name))))
  19918. ;; FIXME: get rid of the \n at some point but watch out
  19919. (regexp (concat "^\\*+[ \t]+\\("
  19920. (if org-select-this-todo-keyword
  19921. (if (equal org-select-this-todo-keyword "*")
  19922. org-todo-regexp
  19923. (concat "\\<\\("
  19924. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19925. "\\)\\>"))
  19926. org-not-done-regexp)
  19927. "[^\n\r]*\\)"))
  19928. marker priority category tags
  19929. ee txt beg end)
  19930. (goto-char (point-min))
  19931. (while (re-search-forward regexp nil t)
  19932. (catch :skip
  19933. (save-match-data
  19934. (beginning-of-line)
  19935. (setq beg (point) end (progn (outline-next-heading) (point)))
  19936. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19937. (re-search-forward org-ts-regexp end t))
  19938. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19939. (re-search-forward org-scheduled-time-regexp end t))
  19940. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19941. (re-search-forward org-deadline-time-regexp end t)
  19942. (org-deadline-close (match-string 1))))
  19943. (goto-char (1+ beg))
  19944. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19945. (throw :skip nil)))
  19946. (goto-char beg)
  19947. (org-agenda-skip)
  19948. (goto-char (match-beginning 1))
  19949. (setq marker (org-agenda-new-marker (match-beginning 0))
  19950. category (org-get-category)
  19951. tags (org-get-tags-at (point))
  19952. txt (org-format-agenda-item "" (match-string 1) category tags)
  19953. priority (1+ (org-get-priority txt)))
  19954. (org-add-props txt props
  19955. 'org-marker marker 'org-hd-marker marker
  19956. 'priority priority 'org-category category
  19957. 'type "todo")
  19958. (push txt ee)
  19959. (if org-agenda-todo-list-sublevels
  19960. (goto-char (match-end 1))
  19961. (org-end-of-subtree 'invisible))))
  19962. (nreverse ee)))
  19963. (defconst org-agenda-no-heading-message
  19964. "No heading for this item in buffer or region.")
  19965. (defun org-agenda-get-timestamps ()
  19966. "Return the date stamp information for agenda display."
  19967. (let* ((props (list 'face nil
  19968. 'org-not-done-regexp org-not-done-regexp
  19969. 'org-todo-regexp org-todo-regexp
  19970. 'mouse-face 'highlight
  19971. 'keymap org-agenda-keymap
  19972. 'help-echo
  19973. (format "mouse-2 or RET jump to org file %s"
  19974. (abbreviate-file-name buffer-file-name))))
  19975. (d1 (calendar-absolute-from-gregorian date))
  19976. (remove-re
  19977. (concat
  19978. (regexp-quote
  19979. (format-time-string
  19980. "<%Y-%m-%d"
  19981. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19982. ".*?>"))
  19983. (regexp
  19984. (concat
  19985. (regexp-quote
  19986. (substring
  19987. (format-time-string
  19988. (car org-time-stamp-formats)
  19989. (apply 'encode-time ; DATE bound by calendar
  19990. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19991. 0 11))
  19992. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19993. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19994. marker hdmarker deadlinep scheduledp donep tmp priority category
  19995. ee txt timestr tags b0 b3 e3 head)
  19996. (goto-char (point-min))
  19997. (while (re-search-forward regexp nil t)
  19998. (setq b0 (match-beginning 0)
  19999. b3 (match-beginning 3) e3 (match-end 3))
  20000. (catch :skip
  20001. (and (org-at-date-range-p) (throw :skip nil))
  20002. (org-agenda-skip)
  20003. (if (and (match-end 1)
  20004. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  20005. (throw :skip nil))
  20006. (if (and e3
  20007. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  20008. (throw :skip nil))
  20009. (setq marker (org-agenda-new-marker b0)
  20010. category (org-get-category b0)
  20011. tmp (buffer-substring (max (point-min)
  20012. (- b0 org-ds-keyword-length))
  20013. b0)
  20014. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  20015. deadlinep (string-match org-deadline-regexp tmp)
  20016. scheduledp (string-match org-scheduled-regexp tmp)
  20017. donep (org-entry-is-done-p))
  20018. (if (or scheduledp deadlinep) (throw :skip t))
  20019. (if (string-match ">" timestr)
  20020. ;; substring should only run to end of time stamp
  20021. (setq timestr (substring timestr 0 (match-end 0))))
  20022. (save-excursion
  20023. (if (re-search-backward "^\\*+ " nil t)
  20024. (progn
  20025. (goto-char (match-beginning 0))
  20026. (setq hdmarker (org-agenda-new-marker)
  20027. tags (org-get-tags-at))
  20028. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20029. (setq head (match-string 1))
  20030. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  20031. (setq txt (org-format-agenda-item
  20032. nil head category tags timestr nil
  20033. remove-re)))
  20034. (setq txt org-agenda-no-heading-message))
  20035. (setq priority (org-get-priority txt))
  20036. (org-add-props txt props
  20037. 'org-marker marker 'org-hd-marker hdmarker)
  20038. (org-add-props txt nil 'priority priority
  20039. 'org-category category 'date date
  20040. 'type "timestamp")
  20041. (push txt ee))
  20042. (outline-next-heading)))
  20043. (nreverse ee)))
  20044. (defun org-agenda-get-sexps ()
  20045. "Return the sexp information for agenda display."
  20046. (require 'diary-lib)
  20047. (let* ((props (list 'face nil
  20048. 'mouse-face 'highlight
  20049. 'keymap org-agenda-keymap
  20050. 'help-echo
  20051. (format "mouse-2 or RET jump to org file %s"
  20052. (abbreviate-file-name buffer-file-name))))
  20053. (regexp "^&?%%(")
  20054. marker category ee txt tags entry result beg b sexp sexp-entry)
  20055. (goto-char (point-min))
  20056. (while (re-search-forward regexp nil t)
  20057. (catch :skip
  20058. (org-agenda-skip)
  20059. (setq beg (match-beginning 0))
  20060. (goto-char (1- (match-end 0)))
  20061. (setq b (point))
  20062. (forward-sexp 1)
  20063. (setq sexp (buffer-substring b (point)))
  20064. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20065. (org-trim (match-string 1))
  20066. ""))
  20067. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20068. (when result
  20069. (setq marker (org-agenda-new-marker beg)
  20070. category (org-get-category beg))
  20071. (if (string-match "\\S-" result)
  20072. (setq txt result)
  20073. (setq txt "SEXP entry returned empty string"))
  20074. (setq txt (org-format-agenda-item
  20075. "" txt category tags 'time))
  20076. (org-add-props txt props 'org-marker marker)
  20077. (org-add-props txt nil
  20078. 'org-category category 'date date
  20079. 'type "sexp")
  20080. (push txt ee))))
  20081. (nreverse ee)))
  20082. (defun org-agenda-get-closed ()
  20083. "Return the logged TODO entries for agenda display."
  20084. (let* ((props (list 'mouse-face 'highlight
  20085. 'org-not-done-regexp org-not-done-regexp
  20086. 'org-todo-regexp org-todo-regexp
  20087. 'keymap org-agenda-keymap
  20088. 'help-echo
  20089. (format "mouse-2 or RET jump to org file %s"
  20090. (abbreviate-file-name buffer-file-name))))
  20091. (regexp (concat
  20092. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20093. (regexp-quote
  20094. (substring
  20095. (format-time-string
  20096. (car org-time-stamp-formats)
  20097. (apply 'encode-time ; DATE bound by calendar
  20098. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20099. 1 11))))
  20100. marker hdmarker priority category tags closedp
  20101. ee txt timestr)
  20102. (goto-char (point-min))
  20103. (while (re-search-forward regexp nil t)
  20104. (catch :skip
  20105. (org-agenda-skip)
  20106. (setq marker (org-agenda-new-marker (match-beginning 0))
  20107. closedp (equal (match-string 1) org-closed-string)
  20108. category (org-get-category (match-beginning 0))
  20109. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20110. ;; donep (org-entry-is-done-p)
  20111. )
  20112. (if (string-match "\\]" timestr)
  20113. ;; substring should only run to end of time stamp
  20114. (setq timestr (substring timestr 0 (match-end 0))))
  20115. (save-excursion
  20116. (if (re-search-backward "^\\*+ " nil t)
  20117. (progn
  20118. (goto-char (match-beginning 0))
  20119. (setq hdmarker (org-agenda-new-marker)
  20120. tags (org-get-tags-at))
  20121. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20122. (setq txt (org-format-agenda-item
  20123. (if closedp "Closed: " "Clocked: ")
  20124. (match-string 1) category tags timestr)))
  20125. (setq txt org-agenda-no-heading-message))
  20126. (setq priority 100000)
  20127. (org-add-props txt props
  20128. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20129. 'priority priority 'org-category category
  20130. 'type "closed" 'date date
  20131. 'undone-face 'org-warning 'done-face 'org-done)
  20132. (push txt ee))
  20133. (goto-char (point-at-eol))))
  20134. (nreverse ee)))
  20135. (defun org-agenda-get-deadlines ()
  20136. "Return the deadline information for agenda display."
  20137. (let* ((props (list 'mouse-face 'highlight
  20138. 'org-not-done-regexp org-not-done-regexp
  20139. 'org-todo-regexp org-todo-regexp
  20140. 'keymap org-agenda-keymap
  20141. 'help-echo
  20142. (format "mouse-2 or RET jump to org file %s"
  20143. (abbreviate-file-name buffer-file-name))))
  20144. (regexp org-deadline-time-regexp)
  20145. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20146. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20147. d2 diff dfrac wdays pos pos1 category tags
  20148. ee txt head face s upcomingp donep timestr)
  20149. (goto-char (point-min))
  20150. (while (re-search-forward regexp nil t)
  20151. (catch :skip
  20152. (org-agenda-skip)
  20153. (setq s (match-string 1)
  20154. pos (1- (match-beginning 1))
  20155. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20156. diff (- d2 d1)
  20157. wdays (org-get-wdays s)
  20158. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20159. upcomingp (and todayp (> diff 0)))
  20160. ;; When to show a deadline in the calendar:
  20161. ;; If the expiration is within wdays warning time.
  20162. ;; Past-due deadlines are only shown on the current date
  20163. (if (or (and (<= diff wdays)
  20164. (and todayp (not org-agenda-only-exact-dates)))
  20165. (= diff 0))
  20166. (save-excursion
  20167. (setq category (org-get-category))
  20168. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20169. (progn
  20170. (goto-char (match-end 0))
  20171. (setq pos1 (match-beginning 0))
  20172. (setq tags (org-get-tags-at pos1))
  20173. (setq head (buffer-substring-no-properties
  20174. (point)
  20175. (progn (skip-chars-forward "^\r\n")
  20176. (point))))
  20177. (setq donep (string-match org-looking-at-done-regexp head))
  20178. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20179. (setq timestr
  20180. (concat (substring s (match-beginning 1)) " "))
  20181. (setq timestr 'time))
  20182. (if (and donep
  20183. (or org-agenda-skip-deadline-if-done
  20184. (not (= diff 0))))
  20185. (setq txt nil)
  20186. (setq txt (org-format-agenda-item
  20187. (if (= diff 0)
  20188. (car org-agenda-deadline-leaders)
  20189. (format (nth 1 org-agenda-deadline-leaders)
  20190. diff))
  20191. head category tags timestr))))
  20192. (setq txt org-agenda-no-heading-message))
  20193. (when txt
  20194. (setq face (org-agenda-deadline-face dfrac wdays))
  20195. (org-add-props txt props
  20196. 'org-marker (org-agenda-new-marker pos)
  20197. 'org-hd-marker (org-agenda-new-marker pos1)
  20198. 'priority (+ (- diff)
  20199. (org-get-priority txt))
  20200. 'org-category category
  20201. 'type (if upcomingp "upcoming-deadline" "deadline")
  20202. 'date (if upcomingp date d2)
  20203. 'face (if donep 'org-done face)
  20204. 'undone-face face 'done-face 'org-done)
  20205. (push txt ee))))))
  20206. (nreverse ee)))
  20207. (defun org-agenda-deadline-face (fraction &optional wdays)
  20208. "Return the face to displaying a deadline item.
  20209. FRACTION is what fraction of the head-warning time has passed."
  20210. (if (equal wdays 0) (setq fraction 1.))
  20211. (let ((faces org-agenda-deadline-faces) f)
  20212. (catch 'exit
  20213. (while (setq f (pop faces))
  20214. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20215. (defun org-agenda-get-scheduled ()
  20216. "Return the scheduled information for agenda display."
  20217. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20218. 'org-todo-regexp org-todo-regexp
  20219. 'done-face 'org-done
  20220. 'mouse-face 'highlight
  20221. 'keymap org-agenda-keymap
  20222. 'help-echo
  20223. (format "mouse-2 or RET jump to org file %s"
  20224. (abbreviate-file-name buffer-file-name))))
  20225. (regexp org-scheduled-time-regexp)
  20226. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20227. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20228. d2 diff pos pos1 category tags
  20229. ee txt head pastschedp donep face timestr s)
  20230. (goto-char (point-min))
  20231. (while (re-search-forward regexp nil t)
  20232. (catch :skip
  20233. (org-agenda-skip)
  20234. (setq s (match-string 1)
  20235. pos (1- (match-beginning 1))
  20236. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20237. ;;; is this right?
  20238. ;;; do we need to do this for deadleine too????
  20239. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20240. diff (- d2 d1))
  20241. (setq pastschedp (and todayp (< diff 0)))
  20242. ;; When to show a scheduled item in the calendar:
  20243. ;; If it is on or past the date.
  20244. (if (or (and (< diff 0)
  20245. (and todayp (not org-agenda-only-exact-dates)))
  20246. (= diff 0))
  20247. (save-excursion
  20248. (setq category (org-get-category))
  20249. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20250. (progn
  20251. (goto-char (match-end 0))
  20252. (setq pos1 (match-beginning 0))
  20253. (setq tags (org-get-tags-at))
  20254. (setq head (buffer-substring-no-properties
  20255. (point)
  20256. (progn (skip-chars-forward "^\r\n") (point))))
  20257. (setq donep (string-match org-looking-at-done-regexp head))
  20258. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20259. (setq timestr
  20260. (concat (substring s (match-beginning 1)) " "))
  20261. (setq timestr 'time))
  20262. (if (and donep
  20263. (or org-agenda-skip-scheduled-if-done
  20264. (not (= diff 0))))
  20265. (setq txt nil)
  20266. (setq txt (org-format-agenda-item
  20267. (if (= diff 0)
  20268. (car org-agenda-scheduled-leaders)
  20269. (format (nth 1 org-agenda-scheduled-leaders)
  20270. (- 1 diff)))
  20271. head category tags timestr))))
  20272. (setq txt org-agenda-no-heading-message))
  20273. (when txt
  20274. (setq face (if pastschedp
  20275. 'org-scheduled-previously
  20276. 'org-scheduled-today))
  20277. (org-add-props txt props
  20278. 'undone-face face
  20279. 'face (if donep 'org-done face)
  20280. 'org-marker (org-agenda-new-marker pos)
  20281. 'org-hd-marker (org-agenda-new-marker pos1)
  20282. 'type (if pastschedp "past-scheduled" "scheduled")
  20283. 'date (if pastschedp d2 date)
  20284. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20285. 'org-category category)
  20286. (push txt ee))))))
  20287. (nreverse ee)))
  20288. (defun org-agenda-get-blocks ()
  20289. "Return the date-range information for agenda display."
  20290. (let* ((props (list 'face nil
  20291. 'org-not-done-regexp org-not-done-regexp
  20292. 'org-todo-regexp org-todo-regexp
  20293. 'mouse-face 'highlight
  20294. 'keymap org-agenda-keymap
  20295. 'help-echo
  20296. (format "mouse-2 or RET jump to org file %s"
  20297. (abbreviate-file-name buffer-file-name))))
  20298. (regexp org-tr-regexp)
  20299. (d0 (calendar-absolute-from-gregorian date))
  20300. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20301. donep head)
  20302. (goto-char (point-min))
  20303. (while (re-search-forward regexp nil t)
  20304. (catch :skip
  20305. (org-agenda-skip)
  20306. (setq pos (point))
  20307. (setq timestr (match-string 0)
  20308. s1 (match-string 1)
  20309. s2 (match-string 2)
  20310. d1 (time-to-days (org-time-string-to-time s1))
  20311. d2 (time-to-days (org-time-string-to-time s2)))
  20312. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20313. ;; Only allow days between the limits, because the normal
  20314. ;; date stamps will catch the limits.
  20315. (save-excursion
  20316. (setq marker (org-agenda-new-marker (point)))
  20317. (setq category (org-get-category))
  20318. (if (re-search-backward "^\\*+ " nil t)
  20319. (progn
  20320. (goto-char (match-beginning 0))
  20321. (setq hdmarker (org-agenda-new-marker (point)))
  20322. (setq tags (org-get-tags-at))
  20323. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20324. (setq head (match-string 1))
  20325. (and org-agenda-skip-timestamp-if-done
  20326. (org-entry-is-done-p)
  20327. (throw :skip t))
  20328. (setq txt (org-format-agenda-item
  20329. (format (if (= d1 d2) "" "(%d/%d): ")
  20330. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20331. head category tags
  20332. (if (= d0 d1) timestr))))
  20333. (setq txt org-agenda-no-heading-message))
  20334. (org-add-props txt props
  20335. 'org-marker marker 'org-hd-marker hdmarker
  20336. 'type "block" 'date date
  20337. 'priority (org-get-priority txt) 'org-category category)
  20338. (push txt ee)))
  20339. (goto-char pos)))
  20340. ;; Sort the entries by expiration date.
  20341. (nreverse ee)))
  20342. ;;; Agenda presentation and sorting
  20343. (defconst org-plain-time-of-day-regexp
  20344. (concat
  20345. "\\(\\<[012]?[0-9]"
  20346. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20347. "\\(--?"
  20348. "\\(\\<[012]?[0-9]"
  20349. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20350. "\\)?")
  20351. "Regular expression to match a plain time or time range.
  20352. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20353. groups carry important information:
  20354. 0 the full match
  20355. 1 the first time, range or not
  20356. 8 the second time, if it is a range.")
  20357. (defconst org-plain-time-extension-regexp
  20358. (concat
  20359. "\\(\\<[012]?[0-9]"
  20360. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20361. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20362. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20363. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20364. groups carry important information:
  20365. 0 the full match
  20366. 7 hours of duration
  20367. 9 minutes of duration")
  20368. (defconst org-stamp-time-of-day-regexp
  20369. (concat
  20370. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20371. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20372. "\\(--?"
  20373. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20374. "Regular expression to match a timestamp time or time range.
  20375. After a match, the following groups carry important information:
  20376. 0 the full match
  20377. 1 date plus weekday, for backreferencing to make sure both times on same day
  20378. 2 the first time, range or not
  20379. 4 the second time, if it is a range.")
  20380. (defvar org-prefix-has-time nil
  20381. "A flag, set by `org-compile-prefix-format'.
  20382. The flag is set if the currently compiled format contains a `%t'.")
  20383. (defvar org-prefix-has-tag nil
  20384. "A flag, set by `org-compile-prefix-format'.
  20385. The flag is set if the currently compiled format contains a `%T'.")
  20386. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20387. noprefix remove-re)
  20388. "Format TXT to be inserted into the agenda buffer.
  20389. In particular, it adds the prefix and corresponding text properties. EXTRA
  20390. must be a string and replaces the `%s' specifier in the prefix format.
  20391. CATEGORY (string, symbol or nil) may be used to overrule the default
  20392. category taken from local variable or file name. It will replace the `%c'
  20393. specifier in the format. DOTIME, when non-nil, indicates that a
  20394. time-of-day should be extracted from TXT for sorting of this entry, and for
  20395. the `%t' specifier in the format. When DOTIME is a string, this string is
  20396. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20397. only the correctly processes TXT should be returned - this is used by
  20398. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20399. Any match of REMOVE-RE will be removed from TXT."
  20400. (save-match-data
  20401. ;; Diary entries sometimes have extra whitespace at the beginning
  20402. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20403. (let* ((category (or category
  20404. org-category
  20405. (if buffer-file-name
  20406. (file-name-sans-extension
  20407. (file-name-nondirectory buffer-file-name))
  20408. "")))
  20409. (tag (if tags (nth (1- (length tags)) tags) ""))
  20410. time ; time and tag are needed for the eval of the prefix format
  20411. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20412. (time-of-day (and dotime (org-get-time-of-day ts)))
  20413. stamp plain s0 s1 s2 rtn srp)
  20414. (when (and dotime time-of-day org-prefix-has-time)
  20415. ;; Extract starting and ending time and move them to prefix
  20416. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20417. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20418. (setq s0 (match-string 0 ts)
  20419. srp (and stamp (match-end 3))
  20420. s1 (match-string (if plain 1 2) ts)
  20421. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20422. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20423. ;; them, we might want to remove them there to avoid duplication.
  20424. ;; The user can turn this off with a variable.
  20425. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20426. (string-match (concat (regexp-quote s0) " *") txt)
  20427. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20428. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20429. (= (match-beginning 0) 0)
  20430. t))
  20431. (setq txt (replace-match "" nil nil txt))))
  20432. ;; Normalize the time(s) to 24 hour
  20433. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20434. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20435. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20436. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20437. (let ((m (+ (string-to-number (match-string 2 s1))
  20438. (* 60 (string-to-number (match-string 1 s1)))
  20439. org-agenda-default-appointment-duration))
  20440. h)
  20441. (setq h (/ m 60) m (- m (* h 60)))
  20442. (setq s2 (format "%02d:%02d" h m))))
  20443. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20444. txt)
  20445. ;; Tags are in the string
  20446. (if (or (eq org-agenda-remove-tags t)
  20447. (and org-agenda-remove-tags
  20448. org-prefix-has-tag))
  20449. (setq txt (replace-match "" t t txt))
  20450. (setq txt (replace-match
  20451. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20452. (match-string 2 txt))
  20453. t t txt))))
  20454. (when remove-re
  20455. (while (string-match remove-re txt)
  20456. (setq txt (replace-match "" t t txt))))
  20457. ;; Create the final string
  20458. (if noprefix
  20459. (setq rtn txt)
  20460. ;; Prepare the variables needed in the eval of the compiled format
  20461. (setq time (cond (s2 (concat s1 "-" s2))
  20462. (s1 (concat s1 "......"))
  20463. (t ""))
  20464. extra (or extra "")
  20465. category (if (symbolp category) (symbol-name category) category))
  20466. ;; Evaluate the compiled format
  20467. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20468. ;; And finally add the text properties
  20469. (org-add-props rtn nil
  20470. 'org-category (downcase category) 'tags tags
  20471. 'org-highest-priority org-highest-priority
  20472. 'org-lowest-priority org-lowest-priority
  20473. 'prefix-length (- (length rtn) (length txt))
  20474. 'time-of-day time-of-day
  20475. 'txt txt
  20476. 'time time
  20477. 'extra extra
  20478. 'dotime dotime))))
  20479. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20480. (defvar org-agenda-sorting-strategy-selected nil)
  20481. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20482. (catch 'exit
  20483. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20484. ((and todayp (member 'today (car org-agenda-time-grid))))
  20485. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20486. ((member 'weekly (car org-agenda-time-grid)))
  20487. (t (throw 'exit list)))
  20488. (let* ((have (delq nil (mapcar
  20489. (lambda (x) (get-text-property 1 'time-of-day x))
  20490. list)))
  20491. (string (nth 1 org-agenda-time-grid))
  20492. (gridtimes (nth 2 org-agenda-time-grid))
  20493. (req (car org-agenda-time-grid))
  20494. (remove (member 'remove-match req))
  20495. new time)
  20496. (if (and (member 'require-timed req) (not have))
  20497. ;; don't show empty grid
  20498. (throw 'exit list))
  20499. (while (setq time (pop gridtimes))
  20500. (unless (and remove (member time have))
  20501. (setq time (int-to-string time))
  20502. (push (org-format-agenda-item
  20503. nil string "" nil
  20504. (concat (substring time 0 -2) ":" (substring time -2)))
  20505. new)
  20506. (put-text-property
  20507. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20508. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20509. (append new list)
  20510. (append list new)))))
  20511. (defun org-compile-prefix-format (key)
  20512. "Compile the prefix format into a Lisp form that can be evaluated.
  20513. The resulting form is returned and stored in the variable
  20514. `org-prefix-format-compiled'."
  20515. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20516. (let ((s (cond
  20517. ((stringp org-agenda-prefix-format)
  20518. org-agenda-prefix-format)
  20519. ((assq key org-agenda-prefix-format)
  20520. (cdr (assq key org-agenda-prefix-format)))
  20521. (t " %-12:c%?-12t% s")))
  20522. (start 0)
  20523. varform vars var e c f opt)
  20524. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20525. s start)
  20526. (setq var (cdr (assoc (match-string 4 s)
  20527. '(("c" . category) ("t" . time) ("s" . extra)
  20528. ("T" . tag))))
  20529. c (or (match-string 3 s) "")
  20530. opt (match-beginning 1)
  20531. start (1+ (match-beginning 0)))
  20532. (if (equal var 'time) (setq org-prefix-has-time t))
  20533. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20534. (setq f (concat "%" (match-string 2 s) "s"))
  20535. (if opt
  20536. (setq varform
  20537. `(if (equal "" ,var)
  20538. ""
  20539. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20540. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20541. (setq s (replace-match "%s" t nil s))
  20542. (push varform vars))
  20543. (setq vars (nreverse vars))
  20544. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20545. (defun org-set-sorting-strategy (key)
  20546. (if (symbolp (car org-agenda-sorting-strategy))
  20547. ;; the old format
  20548. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20549. (setq org-agenda-sorting-strategy-selected
  20550. (or (cdr (assq key org-agenda-sorting-strategy))
  20551. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20552. '(time-up category-keep priority-down)))))
  20553. (defun org-get-time-of-day (s &optional string mod24)
  20554. "Check string S for a time of day.
  20555. If found, return it as a military time number between 0 and 2400.
  20556. If not found, return nil.
  20557. The optional STRING argument forces conversion into a 5 character wide string
  20558. HH:MM."
  20559. (save-match-data
  20560. (when
  20561. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20562. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20563. (let* ((h (string-to-number (match-string 1 s)))
  20564. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20565. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20566. (am-p (equal ampm "am"))
  20567. (h1 (cond ((not ampm) h)
  20568. ((= h 12) (if am-p 0 12))
  20569. (t (+ h (if am-p 0 12)))))
  20570. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20571. (mod h1 24) h1))
  20572. (t0 (+ (* 100 h2) m))
  20573. (t1 (concat (if (>= h1 24) "+" " ")
  20574. (if (< t0 100) "0" "")
  20575. (if (< t0 10) "0" "")
  20576. (int-to-string t0))))
  20577. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20578. (defun org-finalize-agenda-entries (list &optional nosort)
  20579. "Sort and concatenate the agenda items."
  20580. (setq list (mapcar 'org-agenda-highlight-todo list))
  20581. (if nosort
  20582. list
  20583. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20584. (defun org-agenda-highlight-todo (x)
  20585. (let (re pl)
  20586. (if (eq x 'line)
  20587. (save-excursion
  20588. (beginning-of-line 1)
  20589. (setq re (get-text-property (point) 'org-todo-regexp))
  20590. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20591. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20592. (add-text-properties (match-beginning 0) (match-end 0)
  20593. (list 'face (org-get-todo-face 0)))
  20594. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20595. (delete-region (match-beginning 1) (1- (match-end 0)))
  20596. (goto-char (match-beginning 1))
  20597. (insert (format org-agenda-todo-keyword-format s)))))
  20598. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20599. pl (get-text-property 0 'prefix-length x))
  20600. (when (and re
  20601. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20602. x (or pl 0)) pl))
  20603. (add-text-properties
  20604. (or (match-end 1) (match-end 0)) (match-end 0)
  20605. (list 'face (org-get-todo-face (match-string 2 x)))
  20606. x)
  20607. (setq x (concat (substring x 0 (match-end 1))
  20608. (format org-agenda-todo-keyword-format
  20609. (match-string 2 x))
  20610. " "
  20611. (substring x (match-end 3)))))
  20612. x)))
  20613. (defsubst org-cmp-priority (a b)
  20614. "Compare the priorities of string A and B."
  20615. (let ((pa (or (get-text-property 1 'priority a) 0))
  20616. (pb (or (get-text-property 1 'priority b) 0)))
  20617. (cond ((> pa pb) +1)
  20618. ((< pa pb) -1)
  20619. (t nil))))
  20620. (defsubst org-cmp-category (a b)
  20621. "Compare the string values of categories of strings A and B."
  20622. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20623. (cb (or (get-text-property 1 'org-category b) "")))
  20624. (cond ((string-lessp ca cb) -1)
  20625. ((string-lessp cb ca) +1)
  20626. (t nil))))
  20627. (defsubst org-cmp-tag (a b)
  20628. "Compare the string values of categories of strings A and B."
  20629. (let ((ta (car (last (get-text-property 1 'tags a))))
  20630. (tb (car (last (get-text-property 1 'tags b)))))
  20631. (cond ((not ta) +1)
  20632. ((not tb) -1)
  20633. ((string-lessp ta tb) -1)
  20634. ((string-lessp tb ta) +1)
  20635. (t nil))))
  20636. (defsubst org-cmp-time (a b)
  20637. "Compare the time-of-day values of strings A and B."
  20638. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20639. (ta (or (get-text-property 1 'time-of-day a) def))
  20640. (tb (or (get-text-property 1 'time-of-day b) def)))
  20641. (cond ((< ta tb) -1)
  20642. ((< tb ta) +1)
  20643. (t nil))))
  20644. (defun org-entries-lessp (a b)
  20645. "Predicate for sorting agenda entries."
  20646. ;; The following variables will be used when the form is evaluated.
  20647. ;; So even though the compiler complains, keep them.
  20648. (let* ((time-up (org-cmp-time a b))
  20649. (time-down (if time-up (- time-up) nil))
  20650. (priority-up (org-cmp-priority a b))
  20651. (priority-down (if priority-up (- priority-up) nil))
  20652. (category-up (org-cmp-category a b))
  20653. (category-down (if category-up (- category-up) nil))
  20654. (category-keep (if category-up +1 nil))
  20655. (tag-up (org-cmp-tag a b))
  20656. (tag-down (if tag-up (- tag-up) nil)))
  20657. (cdr (assoc
  20658. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20659. '((-1 . t) (1 . nil) (nil . nil))))))
  20660. ;;; Agenda restriction lock
  20661. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20662. "Overlay to mark the headline to which arenda commands are restricted.")
  20663. (org-overlay-put org-agenda-restriction-lock-overlay
  20664. 'face 'org-agenda-restriction-lock)
  20665. (org-overlay-put org-agenda-restriction-lock-overlay
  20666. 'help-echo "Agendas are currently limited to this subtree.")
  20667. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20668. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20669. "Overlay marking the agenda restriction line in speedbar.")
  20670. (org-overlay-put org-speedbar-restriction-lock-overlay
  20671. 'face 'org-agenda-restriction-lock)
  20672. (org-overlay-put org-speedbar-restriction-lock-overlay
  20673. 'help-echo "Agendas are currently limited to this item.")
  20674. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20675. (defun org-agenda-set-restriction-lock (&optional type)
  20676. "Set restriction lock for agenda, to current subtree or file.
  20677. Restriction will be the file if TYPE is `file', or if type is the
  20678. universal prefix '(4), or if the cursor is before the first headline
  20679. in the file. Otherwise, restriction will be to the current subtree."
  20680. (interactive "P")
  20681. (and (equal type '(4)) (setq type 'file))
  20682. (setq type (cond
  20683. (type type)
  20684. ((org-at-heading-p) 'subtree)
  20685. ((condition-case nil (org-back-to-heading t) (error nil))
  20686. 'subtree)
  20687. (t 'file)))
  20688. (if (eq type 'subtree)
  20689. (progn
  20690. (setq org-agenda-restrict t)
  20691. (setq org-agenda-overriding-restriction 'subtree)
  20692. (put 'org-agenda-files 'org-restrict
  20693. (list (buffer-file-name (buffer-base-buffer))))
  20694. (org-back-to-heading t)
  20695. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20696. (move-marker org-agenda-restrict-begin (point))
  20697. (move-marker org-agenda-restrict-end
  20698. (save-excursion (org-end-of-subtree t)))
  20699. (message "Locking agenda restriction to subtree"))
  20700. (put 'org-agenda-files 'org-restrict
  20701. (list (buffer-file-name (buffer-base-buffer))))
  20702. (setq org-agenda-restrict nil)
  20703. (setq org-agenda-overriding-restriction 'file)
  20704. (move-marker org-agenda-restrict-begin nil)
  20705. (move-marker org-agenda-restrict-end nil)
  20706. (message "Locking agenda restriction to file"))
  20707. (setq current-prefix-arg nil)
  20708. (org-agenda-maybe-redo))
  20709. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20710. "Remove the agenda restriction lock."
  20711. (interactive "P")
  20712. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20713. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20714. (setq org-agenda-overriding-restriction nil)
  20715. (setq org-agenda-restrict nil)
  20716. (put 'org-agenda-files 'org-restrict nil)
  20717. (move-marker org-agenda-restrict-begin nil)
  20718. (move-marker org-agenda-restrict-end nil)
  20719. (setq current-prefix-arg nil)
  20720. (message "Agenda restriction lock removed")
  20721. (or noupdate (org-agenda-maybe-redo)))
  20722. (defun org-agenda-maybe-redo ()
  20723. "If there is any window showing the agenda view, update it."
  20724. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20725. (w0 (selected-window)))
  20726. (when w
  20727. (select-window w)
  20728. (org-agenda-redo)
  20729. (select-window w0)
  20730. (if org-agenda-overriding-restriction
  20731. (message "Agenda view shifted to new %s restriction"
  20732. org-agenda-overriding-restriction)
  20733. (message "Agenda restriction lock removed")))))
  20734. ;;; Agenda commands
  20735. (defun org-agenda-check-type (error &rest types)
  20736. "Check if agenda buffer is of allowed type.
  20737. If ERROR is non-nil, throw an error, otherwise just return nil."
  20738. (if (memq org-agenda-type types)
  20739. t
  20740. (if error
  20741. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20742. nil)))
  20743. (defun org-agenda-quit ()
  20744. "Exit agenda by removing the window or the buffer."
  20745. (interactive)
  20746. (let ((buf (current-buffer)))
  20747. (if (not (one-window-p)) (delete-window))
  20748. (kill-buffer buf)
  20749. (org-agenda-reset-markers)
  20750. (org-columns-remove-overlays))
  20751. ;; Maybe restore the pre-agenda window configuration.
  20752. (and org-agenda-restore-windows-after-quit
  20753. (not (eq org-agenda-window-setup 'other-frame))
  20754. org-pre-agenda-window-conf
  20755. (set-window-configuration org-pre-agenda-window-conf)))
  20756. (defun org-agenda-exit ()
  20757. "Exit agenda by removing the window or the buffer.
  20758. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20759. Org-mode buffers visited directly by the user will not be touched."
  20760. (interactive)
  20761. (org-release-buffers org-agenda-new-buffers)
  20762. (setq org-agenda-new-buffers nil)
  20763. (org-agenda-quit))
  20764. (defun org-agenda-execute (arg)
  20765. "Execute another agenda command, keeping same window.\\<global-map>
  20766. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20767. (interactive "P")
  20768. (let ((org-agenda-window-setup 'current-window))
  20769. (org-agenda arg)))
  20770. (defun org-save-all-org-buffers ()
  20771. "Save all Org-mode buffers without user confirmation."
  20772. (interactive)
  20773. (message "Saving all Org-mode buffers...")
  20774. (save-some-buffers t 'org-mode-p)
  20775. (message "Saving all Org-mode buffers... done"))
  20776. (defun org-agenda-redo ()
  20777. "Rebuild Agenda.
  20778. When this is the global TODO list, a prefix argument will be interpreted."
  20779. (interactive)
  20780. (let* ((org-agenda-keep-modes t)
  20781. (line (org-current-line))
  20782. (window-line (- line (org-current-line (window-start))))
  20783. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20784. (message "Rebuilding agenda buffer...")
  20785. (org-let lprops '(eval org-agenda-redo-command))
  20786. (setq org-agenda-undo-list nil
  20787. org-agenda-pending-undo-list nil)
  20788. (message "Rebuilding agenda buffer...done")
  20789. (goto-line line)
  20790. (recenter window-line)))
  20791. (defun org-agenda-manipulate-query-add ()
  20792. "Manipulate the query by adding a search term with positive selection.
  20793. Positive selection means, the term must be matched for selection of an entry."
  20794. (interactive)
  20795. (org-agenda-manipulate-query ?\[))
  20796. (defun org-agenda-manipulate-query-subtract ()
  20797. "Manipulate the query by adding a search term with negative selection.
  20798. Negative selection means, term must not be matched for selection of an entry."
  20799. (interactive)
  20800. (org-agenda-manipulate-query ?\]))
  20801. (defun org-agenda-manipulate-query-add-re ()
  20802. "Manipulate the query by adding a search regexp with positive selection.
  20803. Positive selection means, the regexp must match for selection of an entry."
  20804. (interactive)
  20805. (org-agenda-manipulate-query ?\{))
  20806. (defun org-agenda-manipulate-query-subtract-re ()
  20807. "Manipulate the query by adding a search regexp with negative selection.
  20808. Negative selection means, regexp must not match for selection of an entry."
  20809. (interactive)
  20810. (org-agenda-manipulate-query ?\}))
  20811. (defun org-agenda-manipulate-query (char)
  20812. (cond
  20813. ((eq org-agenda-type 'search)
  20814. (org-add-to-string
  20815. 'org-agenda-query-string
  20816. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20817. (?\{ . " +{}") (?\} . " -{}")))))
  20818. (setq org-agenda-redo-command
  20819. (list 'org-search-view
  20820. (+ (length org-agenda-query-string)
  20821. (if (member char '(?\{ ?\})) 0 1))
  20822. org-agenda-query-string))
  20823. (set-register org-agenda-query-register org-agenda-query-string)
  20824. (org-agenda-redo))
  20825. (t (error "Canot manipulate query for %s-type agenda buffers"
  20826. org-agenda-type))))
  20827. (defun org-add-to-string (var string)
  20828. (set var (concat (symbol-value var) string)))
  20829. (defun org-agenda-goto-date (date)
  20830. "Jump to DATE in agenda."
  20831. (interactive (list (org-read-date)))
  20832. (org-agenda-list nil date))
  20833. (defun org-agenda-goto-today ()
  20834. "Go to today."
  20835. (interactive)
  20836. (org-agenda-check-type t 'timeline 'agenda)
  20837. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20838. (cond
  20839. (tdpos (goto-char tdpos))
  20840. ((eq org-agenda-type 'agenda)
  20841. (let* ((sd (time-to-days
  20842. (time-subtract (current-time)
  20843. (list 0 (* 3600 org-extend-today-until) 0))))
  20844. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20845. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20846. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20847. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20848. (org-agenda-redo)
  20849. (org-agenda-find-same-or-today-or-agenda)))
  20850. (t (error "Cannot find today")))))
  20851. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20852. (goto-char
  20853. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20854. (text-property-any (point-min) (point-max) 'org-today t)
  20855. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20856. (point-min))))
  20857. (defun org-agenda-later (arg)
  20858. "Go forward in time by thee current span.
  20859. With prefix ARG, go forward that many times the current span."
  20860. (interactive "p")
  20861. (org-agenda-check-type t 'agenda)
  20862. (let* ((span org-agenda-span)
  20863. (sd org-starting-day)
  20864. (greg (calendar-gregorian-from-absolute sd))
  20865. (cnt (get-text-property (point) 'org-day-cnt))
  20866. greg2 nd)
  20867. (cond
  20868. ((eq span 'day)
  20869. (setq sd (+ arg sd) nd 1))
  20870. ((eq span 'week)
  20871. (setq sd (+ (* 7 arg) sd) nd 7))
  20872. ((eq span 'month)
  20873. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20874. sd (calendar-absolute-from-gregorian greg2))
  20875. (setcar greg2 (1+ (car greg2)))
  20876. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20877. ((eq span 'year)
  20878. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20879. sd (calendar-absolute-from-gregorian greg2))
  20880. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20881. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20882. (let ((org-agenda-overriding-arguments
  20883. (list (car org-agenda-last-arguments) sd nd t)))
  20884. (org-agenda-redo)
  20885. (org-agenda-find-same-or-today-or-agenda cnt))))
  20886. (defun org-agenda-earlier (arg)
  20887. "Go backward in time by the current span.
  20888. With prefix ARG, go backward that many times the current span."
  20889. (interactive "p")
  20890. (org-agenda-later (- arg)))
  20891. (defun org-agenda-day-view ()
  20892. "Switch to daily view for agenda."
  20893. (interactive)
  20894. (setq org-agenda-ndays 1)
  20895. (org-agenda-change-time-span 'day))
  20896. (defun org-agenda-week-view ()
  20897. "Switch to daily view for agenda."
  20898. (interactive)
  20899. (setq org-agenda-ndays 7)
  20900. (org-agenda-change-time-span 'week))
  20901. (defun org-agenda-month-view ()
  20902. "Switch to daily view for agenda."
  20903. (interactive)
  20904. (org-agenda-change-time-span 'month))
  20905. (defun org-agenda-year-view ()
  20906. "Switch to daily view for agenda."
  20907. (interactive)
  20908. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20909. (org-agenda-change-time-span 'year)
  20910. (error "Abort")))
  20911. (defun org-agenda-change-time-span (span)
  20912. "Change the agenda view to SPAN.
  20913. SPAN may be `day', `week', `month', `year'."
  20914. (org-agenda-check-type t 'agenda)
  20915. (if (equal org-agenda-span span)
  20916. (error "Viewing span is already \"%s\"" span))
  20917. (let* ((sd (or (get-text-property (point) 'day)
  20918. org-starting-day))
  20919. (computed (org-agenda-compute-time-span sd span))
  20920. (org-agenda-overriding-arguments
  20921. (list (car org-agenda-last-arguments)
  20922. (car computed) (cdr computed) t)))
  20923. (org-agenda-redo)
  20924. (org-agenda-find-same-or-today-or-agenda))
  20925. (org-agenda-set-mode-name)
  20926. (message "Switched to %s view" span))
  20927. (defun org-agenda-compute-time-span (sd span)
  20928. "Compute starting date and number of days for agenda.
  20929. SPAN may be `day', `week', `month', `year'. The return value
  20930. is a cons cell with the starting date and the number of days,
  20931. so that the date SD will be in that range."
  20932. (let* ((greg (calendar-gregorian-from-absolute sd))
  20933. nd)
  20934. (cond
  20935. ((eq span 'day)
  20936. (setq nd 1))
  20937. ((eq span 'week)
  20938. (let* ((nt (calendar-day-of-week
  20939. (calendar-gregorian-from-absolute sd)))
  20940. (d (if org-agenda-start-on-weekday
  20941. (- nt org-agenda-start-on-weekday)
  20942. 0)))
  20943. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20944. (setq nd 7)))
  20945. ((eq span 'month)
  20946. (setq sd (calendar-absolute-from-gregorian
  20947. (list (car greg) 1 (nth 2 greg)))
  20948. nd (- (calendar-absolute-from-gregorian
  20949. (list (1+ (car greg)) 1 (nth 2 greg)))
  20950. sd)))
  20951. ((eq span 'year)
  20952. (setq sd (calendar-absolute-from-gregorian
  20953. (list 1 1 (nth 2 greg)))
  20954. nd (- (calendar-absolute-from-gregorian
  20955. (list 1 1 (1+ (nth 2 greg))))
  20956. sd))))
  20957. (cons sd nd)))
  20958. ;; FIXME: does not work if user makes date format that starts with a blank
  20959. (defun org-agenda-next-date-line (&optional arg)
  20960. "Jump to the next line indicating a date in agenda buffer."
  20961. (interactive "p")
  20962. (org-agenda-check-type t 'agenda 'timeline)
  20963. (beginning-of-line 1)
  20964. (if (looking-at "^\\S-") (forward-char 1))
  20965. (if (not (re-search-forward "^\\S-" nil t arg))
  20966. (progn
  20967. (backward-char 1)
  20968. (error "No next date after this line in this buffer")))
  20969. (goto-char (match-beginning 0)))
  20970. (defun org-agenda-previous-date-line (&optional arg)
  20971. "Jump to the previous line indicating a date in agenda buffer."
  20972. (interactive "p")
  20973. (org-agenda-check-type t 'agenda 'timeline)
  20974. (beginning-of-line 1)
  20975. (if (not (re-search-backward "^\\S-" nil t arg))
  20976. (error "No previous date before this line in this buffer")))
  20977. ;; Initialize the highlight
  20978. (defvar org-hl (org-make-overlay 1 1))
  20979. (org-overlay-put org-hl 'face 'highlight)
  20980. (defun org-highlight (begin end &optional buffer)
  20981. "Highlight a region with overlay."
  20982. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20983. org-hl begin end (or buffer (current-buffer))))
  20984. (defun org-unhighlight ()
  20985. "Detach overlay INDEX."
  20986. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20987. ;; FIXME this is currently not used.
  20988. (defun org-highlight-until-next-command (beg end &optional buffer)
  20989. (org-highlight beg end buffer)
  20990. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20991. (defun org-unhighlight-once ()
  20992. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20993. (org-unhighlight))
  20994. (defun org-agenda-follow-mode ()
  20995. "Toggle follow mode in an agenda buffer."
  20996. (interactive)
  20997. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20998. (org-agenda-set-mode-name)
  20999. (message "Follow mode is %s"
  21000. (if org-agenda-follow-mode "on" "off")))
  21001. (defun org-agenda-log-mode ()
  21002. "Toggle log mode in an agenda buffer."
  21003. (interactive)
  21004. (org-agenda-check-type t 'agenda 'timeline)
  21005. (setq org-agenda-show-log (not org-agenda-show-log))
  21006. (org-agenda-set-mode-name)
  21007. (org-agenda-redo)
  21008. (message "Log mode is %s"
  21009. (if org-agenda-show-log "on" "off")))
  21010. (defun org-agenda-toggle-diary ()
  21011. "Toggle diary inclusion in an agenda buffer."
  21012. (interactive)
  21013. (org-agenda-check-type t 'agenda)
  21014. (setq org-agenda-include-diary (not org-agenda-include-diary))
  21015. (org-agenda-redo)
  21016. (org-agenda-set-mode-name)
  21017. (message "Diary inclusion turned %s"
  21018. (if org-agenda-include-diary "on" "off")))
  21019. (defun org-agenda-toggle-time-grid ()
  21020. "Toggle time grid in an agenda buffer."
  21021. (interactive)
  21022. (org-agenda-check-type t 'agenda)
  21023. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  21024. (org-agenda-redo)
  21025. (org-agenda-set-mode-name)
  21026. (message "Time-grid turned %s"
  21027. (if org-agenda-use-time-grid "on" "off")))
  21028. (defun org-agenda-set-mode-name ()
  21029. "Set the mode name to indicate all the small mode settings."
  21030. (setq mode-name
  21031. (concat "Org-Agenda"
  21032. (if (equal org-agenda-ndays 1) " Day" "")
  21033. (if (equal org-agenda-ndays 7) " Week" "")
  21034. (if org-agenda-follow-mode " Follow" "")
  21035. (if org-agenda-include-diary " Diary" "")
  21036. (if org-agenda-use-time-grid " Grid" "")
  21037. (if org-agenda-show-log " Log" "")))
  21038. (force-mode-line-update))
  21039. (defun org-agenda-post-command-hook ()
  21040. (and (eolp) (not (bolp)) (backward-char 1))
  21041. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21042. (if (and org-agenda-follow-mode
  21043. (get-text-property (point) 'org-marker))
  21044. (org-agenda-show)))
  21045. (defun org-agenda-show-priority ()
  21046. "Show the priority of the current item.
  21047. This priority is composed of the main priority given with the [#A] cookies,
  21048. and by additional input from the age of a schedules or deadline entry."
  21049. (interactive)
  21050. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21051. (message "Priority is %d" (if pri pri -1000))))
  21052. (defun org-agenda-show-tags ()
  21053. "Show the tags applicable to the current item."
  21054. (interactive)
  21055. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21056. (if tags
  21057. (message "Tags are :%s:"
  21058. (org-no-properties (mapconcat 'identity tags ":")))
  21059. (message "No tags associated with this line"))))
  21060. (defun org-agenda-goto (&optional highlight)
  21061. "Go to the Org-mode file which contains the item at point."
  21062. (interactive)
  21063. (let* ((marker (or (get-text-property (point) 'org-marker)
  21064. (org-agenda-error)))
  21065. (buffer (marker-buffer marker))
  21066. (pos (marker-position marker)))
  21067. (switch-to-buffer-other-window buffer)
  21068. (widen)
  21069. (goto-char pos)
  21070. (when (org-mode-p)
  21071. (org-show-context 'agenda)
  21072. (save-excursion
  21073. (and (outline-next-heading)
  21074. (org-flag-heading nil)))) ; show the next heading
  21075. (recenter (/ (window-height) 2))
  21076. (run-hooks 'org-agenda-after-show-hook)
  21077. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21078. (defvar org-agenda-after-show-hook nil
  21079. "Normal hook run after an item has been shown from the agenda.
  21080. Point is in the buffer where the item originated.")
  21081. (defun org-agenda-kill ()
  21082. "Kill the entry or subtree belonging to the current agenda entry."
  21083. (interactive)
  21084. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21085. (let* ((marker (or (get-text-property (point) 'org-marker)
  21086. (org-agenda-error)))
  21087. (buffer (marker-buffer marker))
  21088. (pos (marker-position marker))
  21089. (type (get-text-property (point) 'type))
  21090. dbeg dend (n 0) conf)
  21091. (org-with-remote-undo buffer
  21092. (with-current-buffer buffer
  21093. (save-excursion
  21094. (goto-char pos)
  21095. (if (and (org-mode-p) (not (member type '("sexp"))))
  21096. (setq dbeg (progn (org-back-to-heading t) (point))
  21097. dend (org-end-of-subtree t t))
  21098. (setq dbeg (point-at-bol)
  21099. dend (min (point-max) (1+ (point-at-eol)))))
  21100. (goto-char dbeg)
  21101. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21102. (setq conf (or (eq t org-agenda-confirm-kill)
  21103. (and (numberp org-agenda-confirm-kill)
  21104. (> n org-agenda-confirm-kill))))
  21105. (and conf
  21106. (not (y-or-n-p
  21107. (format "Delete entry with %d lines in buffer \"%s\"? "
  21108. n (buffer-name buffer))))
  21109. (error "Abort"))
  21110. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21111. (with-current-buffer buffer (delete-region dbeg dend))
  21112. (message "Agenda item and source killed"))))
  21113. (defun org-agenda-archive ()
  21114. "Kill the entry or subtree belonging to the current agenda entry."
  21115. (interactive)
  21116. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21117. (let* ((marker (or (get-text-property (point) 'org-marker)
  21118. (org-agenda-error)))
  21119. (buffer (marker-buffer marker))
  21120. (pos (marker-position marker)))
  21121. (org-with-remote-undo buffer
  21122. (with-current-buffer buffer
  21123. (if (org-mode-p)
  21124. (save-excursion
  21125. (goto-char pos)
  21126. (org-remove-subtree-entries-from-agenda)
  21127. (org-back-to-heading t)
  21128. (org-archive-subtree))
  21129. (error "Archiving works only in Org-mode files"))))))
  21130. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21131. "Remove all lines in the agenda that correspond to a given subtree.
  21132. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21133. If this information is not given, the function uses the tree at point."
  21134. (let ((buf (or buf (current-buffer))) m p)
  21135. (save-excursion
  21136. (unless (and beg end)
  21137. (org-back-to-heading t)
  21138. (setq beg (point))
  21139. (org-end-of-subtree t)
  21140. (setq end (point)))
  21141. (set-buffer (get-buffer org-agenda-buffer-name))
  21142. (save-excursion
  21143. (goto-char (point-max))
  21144. (beginning-of-line 1)
  21145. (while (not (bobp))
  21146. (when (and (setq m (get-text-property (point) 'org-marker))
  21147. (equal buf (marker-buffer m))
  21148. (setq p (marker-position m))
  21149. (>= p beg)
  21150. (<= p end))
  21151. (let ((inhibit-read-only t))
  21152. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21153. (beginning-of-line 0))))))
  21154. (defun org-agenda-open-link ()
  21155. "Follow the link in the current line, if any."
  21156. (interactive)
  21157. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21158. (save-excursion
  21159. (save-restriction
  21160. (narrow-to-region (point-at-bol) (point-at-eol))
  21161. (org-open-at-point))))
  21162. (defun org-agenda-copy-local-variable (var)
  21163. "Get a variable from a referenced buffer and install it here."
  21164. (let ((m (get-text-property (point) 'org-marker)))
  21165. (when (and m (buffer-live-p (marker-buffer m)))
  21166. (org-set-local var (with-current-buffer (marker-buffer m)
  21167. (symbol-value var))))))
  21168. (defun org-agenda-switch-to (&optional delete-other-windows)
  21169. "Go to the Org-mode file which contains the item at point."
  21170. (interactive)
  21171. (let* ((marker (or (get-text-property (point) 'org-marker)
  21172. (org-agenda-error)))
  21173. (buffer (marker-buffer marker))
  21174. (pos (marker-position marker)))
  21175. (switch-to-buffer buffer)
  21176. (and delete-other-windows (delete-other-windows))
  21177. (widen)
  21178. (goto-char pos)
  21179. (when (org-mode-p)
  21180. (org-show-context 'agenda)
  21181. (save-excursion
  21182. (and (outline-next-heading)
  21183. (org-flag-heading nil)))))) ; show the next heading
  21184. (defun org-agenda-goto-mouse (ev)
  21185. "Go to the Org-mode file which contains the item at the mouse click."
  21186. (interactive "e")
  21187. (mouse-set-point ev)
  21188. (org-agenda-goto))
  21189. (defun org-agenda-show ()
  21190. "Display the Org-mode file which contains the item at point."
  21191. (interactive)
  21192. (let ((win (selected-window)))
  21193. (org-agenda-goto t)
  21194. (select-window win)))
  21195. (defun org-agenda-recenter (arg)
  21196. "Display the Org-mode file which contains the item at point and recenter."
  21197. (interactive "P")
  21198. (let ((win (selected-window)))
  21199. (org-agenda-goto t)
  21200. (recenter arg)
  21201. (select-window win)))
  21202. (defun org-agenda-show-mouse (ev)
  21203. "Display the Org-mode file which contains the item at the mouse click."
  21204. (interactive "e")
  21205. (mouse-set-point ev)
  21206. (org-agenda-show))
  21207. (defun org-agenda-check-no-diary ()
  21208. "Check if the entry is a diary link and abort if yes."
  21209. (if (get-text-property (point) 'org-agenda-diary-link)
  21210. (org-agenda-error)))
  21211. (defun org-agenda-error ()
  21212. (error "Command not allowed in this line"))
  21213. (defun org-agenda-tree-to-indirect-buffer ()
  21214. "Show the subtree corresponding to the current entry in an indirect buffer.
  21215. This calls the command `org-tree-to-indirect-buffer' from the original
  21216. Org-mode buffer.
  21217. With numerical prefix arg ARG, go up to this level and then take that tree.
  21218. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21219. dedicated frame)."
  21220. (interactive)
  21221. (org-agenda-check-no-diary)
  21222. (let* ((marker (or (get-text-property (point) 'org-marker)
  21223. (org-agenda-error)))
  21224. (buffer (marker-buffer marker))
  21225. (pos (marker-position marker)))
  21226. (with-current-buffer buffer
  21227. (save-excursion
  21228. (goto-char pos)
  21229. (call-interactively 'org-tree-to-indirect-buffer)))))
  21230. (defvar org-last-heading-marker (make-marker)
  21231. "Marker pointing to the headline that last changed its TODO state
  21232. by a remote command from the agenda.")
  21233. (defun org-agenda-todo-nextset ()
  21234. "Switch TODO entry to next sequence."
  21235. (interactive)
  21236. (org-agenda-todo 'nextset))
  21237. (defun org-agenda-todo-previousset ()
  21238. "Switch TODO entry to previous sequence."
  21239. (interactive)
  21240. (org-agenda-todo 'previousset))
  21241. (defun org-agenda-todo (&optional arg)
  21242. "Cycle TODO state of line at point, also in Org-mode file.
  21243. This changes the line at point, all other lines in the agenda referring to
  21244. the same tree node, and the headline of the tree node in the Org-mode file."
  21245. (interactive "P")
  21246. (org-agenda-check-no-diary)
  21247. (let* ((col (current-column))
  21248. (marker (or (get-text-property (point) 'org-marker)
  21249. (org-agenda-error)))
  21250. (buffer (marker-buffer marker))
  21251. (pos (marker-position marker))
  21252. (hdmarker (get-text-property (point) 'org-hd-marker))
  21253. (inhibit-read-only t)
  21254. newhead)
  21255. (org-with-remote-undo buffer
  21256. (with-current-buffer buffer
  21257. (widen)
  21258. (goto-char pos)
  21259. (org-show-context 'agenda)
  21260. (save-excursion
  21261. (and (outline-next-heading)
  21262. (org-flag-heading nil))) ; show the next heading
  21263. (org-todo arg)
  21264. (and (bolp) (forward-char 1))
  21265. (setq newhead (org-get-heading))
  21266. (save-excursion
  21267. (org-back-to-heading)
  21268. (move-marker org-last-heading-marker (point))))
  21269. (beginning-of-line 1)
  21270. (save-excursion
  21271. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21272. (move-to-column col))))
  21273. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21274. "Change all lines in the agenda buffer which match HDMARKER.
  21275. The new content of the line will be NEWHEAD (as modified by
  21276. `org-format-agenda-item'). HDMARKER is checked with
  21277. `equal' against all `org-hd-marker' text properties in the file.
  21278. If FIXFACE is non-nil, the face of each item is modified acording to
  21279. the new TODO state."
  21280. (let* ((inhibit-read-only t)
  21281. props m pl undone-face done-face finish new dotime cat tags)
  21282. (save-excursion
  21283. (goto-char (point-max))
  21284. (beginning-of-line 1)
  21285. (while (not finish)
  21286. (setq finish (bobp))
  21287. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21288. (equal m hdmarker))
  21289. (setq props (text-properties-at (point))
  21290. dotime (get-text-property (point) 'dotime)
  21291. cat (get-text-property (point) 'org-category)
  21292. tags (get-text-property (point) 'tags)
  21293. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21294. pl (get-text-property (point) 'prefix-length)
  21295. undone-face (get-text-property (point) 'undone-face)
  21296. done-face (get-text-property (point) 'done-face))
  21297. (move-to-column pl)
  21298. (cond
  21299. ((equal new "")
  21300. (beginning-of-line 1)
  21301. (and (looking-at ".*\n?") (replace-match "")))
  21302. ((looking-at ".*")
  21303. (replace-match new t t)
  21304. (beginning-of-line 1)
  21305. (add-text-properties (point-at-bol) (point-at-eol) props)
  21306. (when fixface
  21307. (add-text-properties
  21308. (point-at-bol) (point-at-eol)
  21309. (list 'face
  21310. (if org-last-todo-state-is-todo
  21311. undone-face done-face))))
  21312. (org-agenda-highlight-todo 'line)
  21313. (beginning-of-line 1))
  21314. (t (error "Line update did not work"))))
  21315. (beginning-of-line 0)))
  21316. (org-finalize-agenda)))
  21317. (defun org-agenda-align-tags (&optional line)
  21318. "Align all tags in agenda items to `org-agenda-tags-column'."
  21319. (let ((inhibit-read-only t) l c)
  21320. (save-excursion
  21321. (goto-char (if line (point-at-bol) (point-min)))
  21322. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21323. (if line (point-at-eol) nil) t)
  21324. (add-text-properties
  21325. (match-beginning 2) (match-end 2)
  21326. (list 'face (delq nil (list 'org-tag (get-text-property
  21327. (match-beginning 2) 'face)))))
  21328. (setq l (- (match-end 2) (match-beginning 2))
  21329. c (if (< org-agenda-tags-column 0)
  21330. (- (abs org-agenda-tags-column) l)
  21331. org-agenda-tags-column))
  21332. (delete-region (match-beginning 1) (match-end 1))
  21333. (goto-char (match-beginning 1))
  21334. (insert (org-add-props
  21335. (make-string (max 1 (- c (current-column))) ?\ )
  21336. (text-properties-at (point))))))))
  21337. (defun org-agenda-priority-up ()
  21338. "Increase the priority of line at point, also in Org-mode file."
  21339. (interactive)
  21340. (org-agenda-priority 'up))
  21341. (defun org-agenda-priority-down ()
  21342. "Decrease the priority of line at point, also in Org-mode file."
  21343. (interactive)
  21344. (org-agenda-priority 'down))
  21345. (defun org-agenda-priority (&optional force-direction)
  21346. "Set the priority of line at point, also in Org-mode file.
  21347. This changes the line at point, all other lines in the agenda referring to
  21348. the same tree node, and the headline of the tree node in the Org-mode file."
  21349. (interactive)
  21350. (org-agenda-check-no-diary)
  21351. (let* ((marker (or (get-text-property (point) 'org-marker)
  21352. (org-agenda-error)))
  21353. (hdmarker (get-text-property (point) 'org-hd-marker))
  21354. (buffer (marker-buffer hdmarker))
  21355. (pos (marker-position hdmarker))
  21356. (inhibit-read-only t)
  21357. newhead)
  21358. (org-with-remote-undo buffer
  21359. (with-current-buffer buffer
  21360. (widen)
  21361. (goto-char pos)
  21362. (org-show-context 'agenda)
  21363. (save-excursion
  21364. (and (outline-next-heading)
  21365. (org-flag-heading nil))) ; show the next heading
  21366. (funcall 'org-priority force-direction)
  21367. (end-of-line 1)
  21368. (setq newhead (org-get-heading)))
  21369. (org-agenda-change-all-lines newhead hdmarker)
  21370. (beginning-of-line 1))))
  21371. (defun org-get-tags-at (&optional pos)
  21372. "Get a list of all headline tags applicable at POS.
  21373. POS defaults to point. If tags are inherited, the list contains
  21374. the targets in the same sequence as the headlines appear, i.e.
  21375. the tags of the current headline come last."
  21376. (interactive)
  21377. (let (tags lastpos)
  21378. (save-excursion
  21379. (save-restriction
  21380. (widen)
  21381. (goto-char (or pos (point)))
  21382. (save-match-data
  21383. (org-back-to-heading t)
  21384. (condition-case nil
  21385. (while (not (equal lastpos (point)))
  21386. (setq lastpos (point))
  21387. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21388. (setq tags (append (org-split-string
  21389. (org-match-string-no-properties 1) ":")
  21390. tags)))
  21391. (or org-use-tag-inheritance (error ""))
  21392. (org-up-heading-all 1))
  21393. (error nil))))
  21394. tags)))
  21395. ;; FIXME: should fix the tags property of the agenda line.
  21396. (defun org-agenda-set-tags ()
  21397. "Set tags for the current headline."
  21398. (interactive)
  21399. (org-agenda-check-no-diary)
  21400. (if (and (org-region-active-p) (interactive-p))
  21401. (call-interactively 'org-change-tag-in-region)
  21402. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21403. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21404. (org-agenda-error)))
  21405. (buffer (marker-buffer hdmarker))
  21406. (pos (marker-position hdmarker))
  21407. (inhibit-read-only t)
  21408. newhead)
  21409. (org-with-remote-undo buffer
  21410. (with-current-buffer buffer
  21411. (widen)
  21412. (goto-char pos)
  21413. (save-excursion
  21414. (org-show-context 'agenda))
  21415. (save-excursion
  21416. (and (outline-next-heading)
  21417. (org-flag-heading nil))) ; show the next heading
  21418. (goto-char pos)
  21419. (call-interactively 'org-set-tags)
  21420. (end-of-line 1)
  21421. (setq newhead (org-get-heading)))
  21422. (org-agenda-change-all-lines newhead hdmarker)
  21423. (beginning-of-line 1)))))
  21424. (defun org-agenda-toggle-archive-tag ()
  21425. "Toggle the archive tag for the current entry."
  21426. (interactive)
  21427. (org-agenda-check-no-diary)
  21428. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21429. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21430. (org-agenda-error)))
  21431. (buffer (marker-buffer hdmarker))
  21432. (pos (marker-position hdmarker))
  21433. (inhibit-read-only t)
  21434. newhead)
  21435. (org-with-remote-undo buffer
  21436. (with-current-buffer buffer
  21437. (widen)
  21438. (goto-char pos)
  21439. (org-show-context 'agenda)
  21440. (save-excursion
  21441. (and (outline-next-heading)
  21442. (org-flag-heading nil))) ; show the next heading
  21443. (call-interactively 'org-toggle-archive-tag)
  21444. (end-of-line 1)
  21445. (setq newhead (org-get-heading)))
  21446. (org-agenda-change-all-lines newhead hdmarker)
  21447. (beginning-of-line 1))))
  21448. (defun org-agenda-date-later (arg &optional what)
  21449. "Change the date of this item to one day later."
  21450. (interactive "p")
  21451. (org-agenda-check-type t 'agenda 'timeline)
  21452. (org-agenda-check-no-diary)
  21453. (let* ((marker (or (get-text-property (point) 'org-marker)
  21454. (org-agenda-error)))
  21455. (buffer (marker-buffer marker))
  21456. (pos (marker-position marker)))
  21457. (org-with-remote-undo buffer
  21458. (with-current-buffer buffer
  21459. (widen)
  21460. (goto-char pos)
  21461. (if (not (org-at-timestamp-p))
  21462. (error "Cannot find time stamp"))
  21463. (org-timestamp-change arg (or what 'day)))
  21464. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21465. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21466. (defun org-agenda-date-earlier (arg &optional what)
  21467. "Change the date of this item to one day earlier."
  21468. (interactive "p")
  21469. (org-agenda-date-later (- arg) what))
  21470. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21471. "Show new date stamp via text properties."
  21472. ;; We use text properties to make this undoable
  21473. (let ((inhibit-read-only t))
  21474. (setq stamp (concat " " prefix " => " stamp))
  21475. (save-excursion
  21476. (goto-char (point-max))
  21477. (while (not (bobp))
  21478. (when (equal marker (get-text-property (point) 'org-marker))
  21479. (move-to-column (- (window-width) (length stamp)) t)
  21480. (if (featurep 'xemacs)
  21481. ;; Use `duplicable' property to trigger undo recording
  21482. (let ((ex (make-extent nil nil))
  21483. (gl (make-glyph stamp)))
  21484. (set-glyph-face gl 'secondary-selection)
  21485. (set-extent-properties
  21486. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21487. (insert-extent ex (1- (point)) (point-at-eol)))
  21488. (add-text-properties
  21489. (1- (point)) (point-at-eol)
  21490. (list 'display (org-add-props stamp nil
  21491. 'face 'secondary-selection))))
  21492. (beginning-of-line 1))
  21493. (beginning-of-line 0)))))
  21494. (defun org-agenda-date-prompt (arg)
  21495. "Change the date of this item. Date is prompted for, with default today.
  21496. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21497. be used to request time specification in the time stamp."
  21498. (interactive "P")
  21499. (org-agenda-check-type t 'agenda 'timeline)
  21500. (org-agenda-check-no-diary)
  21501. (let* ((marker (or (get-text-property (point) 'org-marker)
  21502. (org-agenda-error)))
  21503. (buffer (marker-buffer marker))
  21504. (pos (marker-position marker)))
  21505. (org-with-remote-undo buffer
  21506. (with-current-buffer buffer
  21507. (widen)
  21508. (goto-char pos)
  21509. (if (not (org-at-timestamp-p))
  21510. (error "Cannot find time stamp"))
  21511. (org-time-stamp arg)
  21512. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21513. (defun org-agenda-schedule (arg)
  21514. "Schedule the item at point."
  21515. (interactive "P")
  21516. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21517. (org-agenda-check-no-diary)
  21518. (let* ((marker (or (get-text-property (point) 'org-marker)
  21519. (org-agenda-error)))
  21520. (buffer (marker-buffer marker))
  21521. (pos (marker-position marker))
  21522. (org-insert-labeled-timestamps-at-point nil)
  21523. ts)
  21524. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21525. (set-marker-insertion-type marker t)
  21526. (org-with-remote-undo buffer
  21527. (with-current-buffer buffer
  21528. (widen)
  21529. (goto-char pos)
  21530. (setq ts (org-schedule arg)))
  21531. (org-agenda-show-new-time marker ts "S"))
  21532. (message "Item scheduled for %s" ts)))
  21533. (defun org-agenda-deadline (arg)
  21534. "Schedule the item at point."
  21535. (interactive "P")
  21536. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21537. (org-agenda-check-no-diary)
  21538. (let* ((marker (or (get-text-property (point) 'org-marker)
  21539. (org-agenda-error)))
  21540. (buffer (marker-buffer marker))
  21541. (pos (marker-position marker))
  21542. (org-insert-labeled-timestamps-at-point nil)
  21543. ts)
  21544. (org-with-remote-undo buffer
  21545. (with-current-buffer buffer
  21546. (widen)
  21547. (goto-char pos)
  21548. (setq ts (org-deadline arg)))
  21549. (org-agenda-show-new-time marker ts "S"))
  21550. (message "Deadline for this item set to %s" ts)))
  21551. (defun org-get-heading (&optional no-tags)
  21552. "Return the heading of the current entry, without the stars."
  21553. (save-excursion
  21554. (org-back-to-heading t)
  21555. (if (looking-at
  21556. (if no-tags
  21557. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21558. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21559. (match-string 1) "")))
  21560. (defun org-agenda-clock-in (&optional arg)
  21561. "Start the clock on the currently selected item."
  21562. (interactive "P")
  21563. (org-agenda-check-no-diary)
  21564. (let* ((marker (or (get-text-property (point) 'org-marker)
  21565. (org-agenda-error)))
  21566. (pos (marker-position marker)))
  21567. (org-with-remote-undo (marker-buffer marker)
  21568. (with-current-buffer (marker-buffer marker)
  21569. (widen)
  21570. (goto-char pos)
  21571. (org-clock-in)))))
  21572. (defun org-agenda-clock-out (&optional arg)
  21573. "Stop the currently running clock."
  21574. (interactive "P")
  21575. (unless (marker-buffer org-clock-marker)
  21576. (error "No running clock"))
  21577. (org-with-remote-undo (marker-buffer org-clock-marker)
  21578. (org-clock-out)))
  21579. (defun org-agenda-clock-cancel (&optional arg)
  21580. "Cancel the currently running clock."
  21581. (interactive "P")
  21582. (unless (marker-buffer org-clock-marker)
  21583. (error "No running clock"))
  21584. (org-with-remote-undo (marker-buffer org-clock-marker)
  21585. (org-clock-cancel)))
  21586. (defun org-agenda-diary-entry ()
  21587. "Make a diary entry, like the `i' command from the calendar.
  21588. All the standard commands work: block, weekly etc."
  21589. (interactive)
  21590. (org-agenda-check-type t 'agenda 'timeline)
  21591. (require 'diary-lib)
  21592. (let* ((char (progn
  21593. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21594. (read-char-exclusive)))
  21595. (cmd (cdr (assoc char
  21596. '((?d . insert-diary-entry)
  21597. (?w . insert-weekly-diary-entry)
  21598. (?m . insert-monthly-diary-entry)
  21599. (?y . insert-yearly-diary-entry)
  21600. (?a . insert-anniversary-diary-entry)
  21601. (?b . insert-block-diary-entry)
  21602. (?c . insert-cyclic-diary-entry)))))
  21603. (oldf (symbol-function 'calendar-cursor-to-date))
  21604. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21605. (point (point))
  21606. (mark (or (mark t) (point))))
  21607. (unless cmd
  21608. (error "No command associated with <%c>" char))
  21609. (unless (and (get-text-property point 'day)
  21610. (or (not (equal ?b char))
  21611. (get-text-property mark 'day)))
  21612. (error "Don't know which date to use for diary entry"))
  21613. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21614. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21615. (let ((calendar-mark-ring
  21616. (list (calendar-gregorian-from-absolute
  21617. (or (get-text-property mark 'day)
  21618. (get-text-property point 'day))))))
  21619. (unwind-protect
  21620. (progn
  21621. (fset 'calendar-cursor-to-date
  21622. (lambda (&optional error)
  21623. (calendar-gregorian-from-absolute
  21624. (get-text-property point 'day))))
  21625. (call-interactively cmd))
  21626. (fset 'calendar-cursor-to-date oldf)))))
  21627. (defun org-agenda-execute-calendar-command (cmd)
  21628. "Execute a calendar command from the agenda, with the date associated to
  21629. the cursor position."
  21630. (org-agenda-check-type t 'agenda 'timeline)
  21631. (require 'diary-lib)
  21632. (unless (get-text-property (point) 'day)
  21633. (error "Don't know which date to use for calendar command"))
  21634. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21635. (point (point))
  21636. (date (calendar-gregorian-from-absolute
  21637. (get-text-property point 'day)))
  21638. ;; the following 3 vars are needed in the calendar
  21639. (displayed-day (extract-calendar-day date))
  21640. (displayed-month (extract-calendar-month date))
  21641. (displayed-year (extract-calendar-year date)))
  21642. (unwind-protect
  21643. (progn
  21644. (fset 'calendar-cursor-to-date
  21645. (lambda (&optional error)
  21646. (calendar-gregorian-from-absolute
  21647. (get-text-property point 'day))))
  21648. (call-interactively cmd))
  21649. (fset 'calendar-cursor-to-date oldf))))
  21650. (defun org-agenda-phases-of-moon ()
  21651. "Display the phases of the moon for the 3 months around the cursor date."
  21652. (interactive)
  21653. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21654. (defun org-agenda-holidays ()
  21655. "Display the holidays for the 3 months around the cursor date."
  21656. (interactive)
  21657. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21658. (defun org-agenda-sunrise-sunset (arg)
  21659. "Display sunrise and sunset for the cursor date.
  21660. Latitude and longitude can be specified with the variables
  21661. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21662. argument, latitude and longitude will be prompted for."
  21663. (interactive "P")
  21664. (let ((calendar-longitude (if arg nil calendar-longitude))
  21665. (calendar-latitude (if arg nil calendar-latitude))
  21666. (calendar-location-name
  21667. (if arg "the given coordinates" calendar-location-name)))
  21668. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21669. (defun org-agenda-goto-calendar ()
  21670. "Open the Emacs calendar with the date at the cursor."
  21671. (interactive)
  21672. (org-agenda-check-type t 'agenda 'timeline)
  21673. (let* ((day (or (get-text-property (point) 'day)
  21674. (error "Don't know which date to open in calendar")))
  21675. (date (calendar-gregorian-from-absolute day))
  21676. (calendar-move-hook nil)
  21677. (view-calendar-holidays-initially nil)
  21678. (view-diary-entries-initially nil))
  21679. (calendar)
  21680. (calendar-goto-date date)))
  21681. (defun org-calendar-goto-agenda ()
  21682. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21683. This is a command that has to be installed in `calendar-mode-map'."
  21684. (interactive)
  21685. (org-agenda-list nil (calendar-absolute-from-gregorian
  21686. (calendar-cursor-to-date))
  21687. nil))
  21688. (defun org-agenda-convert-date ()
  21689. (interactive)
  21690. (org-agenda-check-type t 'agenda 'timeline)
  21691. (let ((day (get-text-property (point) 'day))
  21692. date s)
  21693. (unless day
  21694. (error "Don't know which date to convert"))
  21695. (setq date (calendar-gregorian-from-absolute day))
  21696. (setq s (concat
  21697. "Gregorian: " (calendar-date-string date) "\n"
  21698. "ISO: " (calendar-iso-date-string date) "\n"
  21699. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21700. "Julian: " (calendar-julian-date-string date) "\n"
  21701. "Astron. JD: " (calendar-astro-date-string date)
  21702. " (Julian date number at noon UTC)\n"
  21703. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21704. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21705. "French: " (calendar-french-date-string date) "\n"
  21706. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21707. "Mayan: " (calendar-mayan-date-string date) "\n"
  21708. "Coptic: " (calendar-coptic-date-string date) "\n"
  21709. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21710. "Persian: " (calendar-persian-date-string date) "\n"
  21711. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21712. (with-output-to-temp-buffer "*Dates*"
  21713. (princ s))
  21714. (if (fboundp 'fit-window-to-buffer)
  21715. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21716. ;;;; Embedded LaTeX
  21717. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21718. "Keymap for the minor `org-cdlatex-mode'.")
  21719. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21720. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21721. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21722. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21723. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21724. (defvar org-cdlatex-texmathp-advice-is-done nil
  21725. "Flag remembering if we have applied the advice to texmathp already.")
  21726. (define-minor-mode org-cdlatex-mode
  21727. "Toggle the minor `org-cdlatex-mode'.
  21728. This mode supports entering LaTeX environment and math in LaTeX fragments
  21729. in Org-mode.
  21730. \\{org-cdlatex-mode-map}"
  21731. nil " OCDL" nil
  21732. (when org-cdlatex-mode (require 'cdlatex))
  21733. (unless org-cdlatex-texmathp-advice-is-done
  21734. (setq org-cdlatex-texmathp-advice-is-done t)
  21735. (defadvice texmathp (around org-math-always-on activate)
  21736. "Always return t in org-mode buffers.
  21737. This is because we want to insert math symbols without dollars even outside
  21738. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21739. en embedded LaTeX fragement, let texmathp do its job.
  21740. \\[org-cdlatex-mode-map]"
  21741. (interactive)
  21742. (let (p)
  21743. (cond
  21744. ((not (org-mode-p)) ad-do-it)
  21745. ((eq this-command 'cdlatex-math-symbol)
  21746. (setq ad-return-value t
  21747. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21748. (t
  21749. (let ((p (org-inside-LaTeX-fragment-p)))
  21750. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21751. (setq ad-return-value t
  21752. texmathp-why '("Org-mode embedded math" . 0))
  21753. (if p ad-do-it)))))))))
  21754. (defun turn-on-org-cdlatex ()
  21755. "Unconditionally turn on `org-cdlatex-mode'."
  21756. (org-cdlatex-mode 1))
  21757. (defun org-inside-LaTeX-fragment-p ()
  21758. "Test if point is inside a LaTeX fragment.
  21759. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21760. sequence appearing also before point.
  21761. Even though the matchers for math are configurable, this function assumes
  21762. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21763. delimiters are skipped when they have been removed by customization.
  21764. The return value is nil, or a cons cell with the delimiter and
  21765. and the position of this delimiter.
  21766. This function does a reasonably good job, but can locally be fooled by
  21767. for example currency specifications. For example it will assume being in
  21768. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21769. fragments that are properly closed, but during editing, we have to live
  21770. with the uncertainty caused by missing closing delimiters. This function
  21771. looks only before point, not after."
  21772. (catch 'exit
  21773. (let ((pos (point))
  21774. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21775. (lim (progn
  21776. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21777. (point)))
  21778. dd-on str (start 0) m re)
  21779. (goto-char pos)
  21780. (when dodollar
  21781. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21782. re (nth 1 (assoc "$" org-latex-regexps)))
  21783. (while (string-match re str start)
  21784. (cond
  21785. ((= (match-end 0) (length str))
  21786. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21787. ((= (match-end 0) (- (length str) 5))
  21788. (throw 'exit nil))
  21789. (t (setq start (match-end 0))))))
  21790. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21791. (goto-char pos)
  21792. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21793. (and (match-beginning 2) (throw 'exit nil))
  21794. ;; count $$
  21795. (while (re-search-backward "\\$\\$" lim t)
  21796. (setq dd-on (not dd-on)))
  21797. (goto-char pos)
  21798. (if dd-on (cons "$$" m))))))
  21799. (defun org-try-cdlatex-tab ()
  21800. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21801. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21802. - inside a LaTeX fragment, or
  21803. - after the first word in a line, where an abbreviation expansion could
  21804. insert a LaTeX environment."
  21805. (when org-cdlatex-mode
  21806. (cond
  21807. ((save-excursion
  21808. (skip-chars-backward "a-zA-Z0-9*")
  21809. (skip-chars-backward " \t")
  21810. (bolp))
  21811. (cdlatex-tab) t)
  21812. ((org-inside-LaTeX-fragment-p)
  21813. (cdlatex-tab) t)
  21814. (t nil))))
  21815. (defun org-cdlatex-underscore-caret (&optional arg)
  21816. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21817. Revert to the normal definition outside of these fragments."
  21818. (interactive "P")
  21819. (if (org-inside-LaTeX-fragment-p)
  21820. (call-interactively 'cdlatex-sub-superscript)
  21821. (let (org-cdlatex-mode)
  21822. (call-interactively (key-binding (vector last-input-event))))))
  21823. (defun org-cdlatex-math-modify (&optional arg)
  21824. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21825. Revert to the normal definition outside of these fragments."
  21826. (interactive "P")
  21827. (if (org-inside-LaTeX-fragment-p)
  21828. (call-interactively 'cdlatex-math-modify)
  21829. (let (org-cdlatex-mode)
  21830. (call-interactively (key-binding (vector last-input-event))))))
  21831. (defvar org-latex-fragment-image-overlays nil
  21832. "List of overlays carrying the images of latex fragments.")
  21833. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21834. (defun org-remove-latex-fragment-image-overlays ()
  21835. "Remove all overlays with LaTeX fragment images in current buffer."
  21836. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21837. (setq org-latex-fragment-image-overlays nil))
  21838. (defun org-preview-latex-fragment (&optional subtree)
  21839. "Preview the LaTeX fragment at point, or all locally or globally.
  21840. If the cursor is in a LaTeX fragment, create the image and overlay
  21841. it over the source code. If there is no fragment at point, display
  21842. all fragments in the current text, from one headline to the next. With
  21843. prefix SUBTREE, display all fragments in the current subtree. With a
  21844. double prefix `C-u C-u', or when the cursor is before the first headline,
  21845. display all fragments in the buffer.
  21846. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21847. (interactive "P")
  21848. (org-remove-latex-fragment-image-overlays)
  21849. (save-excursion
  21850. (save-restriction
  21851. (let (beg end at msg)
  21852. (cond
  21853. ((or (equal subtree '(16))
  21854. (not (save-excursion
  21855. (re-search-backward (concat "^" outline-regexp) nil t))))
  21856. (setq beg (point-min) end (point-max)
  21857. msg "Creating images for buffer...%s"))
  21858. ((equal subtree '(4))
  21859. (org-back-to-heading)
  21860. (setq beg (point) end (org-end-of-subtree t)
  21861. msg "Creating images for subtree...%s"))
  21862. (t
  21863. (if (setq at (org-inside-LaTeX-fragment-p))
  21864. (goto-char (max (point-min) (- (cdr at) 2)))
  21865. (org-back-to-heading))
  21866. (setq beg (point) end (progn (outline-next-heading) (point))
  21867. msg (if at "Creating image...%s"
  21868. "Creating images for entry...%s"))))
  21869. (message msg "")
  21870. (narrow-to-region beg end)
  21871. (goto-char beg)
  21872. (org-format-latex
  21873. (concat "ltxpng/" (file-name-sans-extension
  21874. (file-name-nondirectory
  21875. buffer-file-name)))
  21876. default-directory 'overlays msg at 'forbuffer)
  21877. (message msg "done. Use `C-c C-c' to remove images.")))))
  21878. (defvar org-latex-regexps
  21879. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21880. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21881. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21882. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21883. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21884. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21885. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21886. "Regular expressions for matching embedded LaTeX.")
  21887. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21888. "Replace LaTeX fragments with links to an image, and produce images."
  21889. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21890. (let* ((prefixnodir (file-name-nondirectory prefix))
  21891. (absprefix (expand-file-name prefix dir))
  21892. (todir (file-name-directory absprefix))
  21893. (opt org-format-latex-options)
  21894. (matchers (plist-get opt :matchers))
  21895. (re-list org-latex-regexps)
  21896. (cnt 0) txt link beg end re e checkdir
  21897. m n block linkfile movefile ov)
  21898. ;; Check if there are old images files with this prefix, and remove them
  21899. (when (file-directory-p todir)
  21900. (mapc 'delete-file
  21901. (directory-files
  21902. todir 'full
  21903. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21904. ;; Check the different regular expressions
  21905. (while (setq e (pop re-list))
  21906. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21907. block (if (nth 3 e) "\n\n" ""))
  21908. (when (member m matchers)
  21909. (goto-char (point-min))
  21910. (while (re-search-forward re nil t)
  21911. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21912. (setq txt (match-string n)
  21913. beg (match-beginning n) end (match-end n)
  21914. cnt (1+ cnt)
  21915. linkfile (format "%s_%04d.png" prefix cnt)
  21916. movefile (format "%s_%04d.png" absprefix cnt)
  21917. link (concat block "[[file:" linkfile "]]" block))
  21918. (if msg (message msg cnt))
  21919. (goto-char beg)
  21920. (unless checkdir ; make sure the directory exists
  21921. (setq checkdir t)
  21922. (or (file-directory-p todir) (make-directory todir)))
  21923. (org-create-formula-image
  21924. txt movefile opt forbuffer)
  21925. (if overlays
  21926. (progn
  21927. (setq ov (org-make-overlay beg end))
  21928. (if (featurep 'xemacs)
  21929. (progn
  21930. (org-overlay-put ov 'invisible t)
  21931. (org-overlay-put
  21932. ov 'end-glyph
  21933. (make-glyph (vector 'png :file movefile))))
  21934. (org-overlay-put
  21935. ov 'display
  21936. (list 'image :type 'png :file movefile :ascent 'center)))
  21937. (push ov org-latex-fragment-image-overlays)
  21938. (goto-char end))
  21939. (delete-region beg end)
  21940. (insert link))))))))
  21941. ;; This function borrows from Ganesh Swami's latex2png.el
  21942. (defun org-create-formula-image (string tofile options buffer)
  21943. (let* ((tmpdir (if (featurep 'xemacs)
  21944. (temp-directory)
  21945. temporary-file-directory))
  21946. (texfilebase (make-temp-name
  21947. (expand-file-name "orgtex" tmpdir)))
  21948. (texfile (concat texfilebase ".tex"))
  21949. (dvifile (concat texfilebase ".dvi"))
  21950. (pngfile (concat texfilebase ".png"))
  21951. (fnh (face-attribute 'default :height nil))
  21952. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21953. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21954. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21955. "Black"))
  21956. (bg (or (plist-get options (if buffer :background :html-background))
  21957. "Transparent")))
  21958. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21959. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21960. (with-temp-file texfile
  21961. (insert org-format-latex-header
  21962. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21963. (let ((dir default-directory))
  21964. (condition-case nil
  21965. (progn
  21966. (cd tmpdir)
  21967. (call-process "latex" nil nil nil texfile))
  21968. (error nil))
  21969. (cd dir))
  21970. (if (not (file-exists-p dvifile))
  21971. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21972. (call-process "dvipng" nil nil nil
  21973. "-E" "-fg" fg "-bg" bg
  21974. "-D" dpi
  21975. ;;"-x" scale "-y" scale
  21976. "-T" "tight"
  21977. "-o" pngfile
  21978. dvifile)
  21979. (if (not (file-exists-p pngfile))
  21980. (progn (message "Failed to create png file from %s" texfile) nil)
  21981. ;; Use the requested file name and clean up
  21982. (copy-file pngfile tofile 'replace)
  21983. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21984. (delete-file (concat texfilebase e)))
  21985. pngfile))))
  21986. (defun org-dvipng-color (attr)
  21987. "Return an rgb color specification for dvipng."
  21988. (apply 'format "rgb %s %s %s"
  21989. (mapcar 'org-normalize-color
  21990. (color-values (face-attribute 'default attr nil)))))
  21991. (defun org-normalize-color (value)
  21992. "Return string to be used as color value for an RGB component."
  21993. (format "%g" (/ value 65535.0)))
  21994. ;;;; Exporting
  21995. ;;; Variables, constants, and parameter plists
  21996. (defconst org-level-max 20)
  21997. (defvar org-export-html-preamble nil
  21998. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21999. (defvar org-export-html-postamble nil
  22000. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  22001. (defvar org-export-html-auto-preamble t
  22002. "Should default preamble be inserted? Set by publishing functions.")
  22003. (defvar org-export-html-auto-postamble t
  22004. "Should default postamble be inserted? Set by publishing functions.")
  22005. (defvar org-current-export-file nil) ; dynamically scoped parameter
  22006. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  22007. (defconst org-export-plist-vars
  22008. '((:language . org-export-default-language)
  22009. (:customtime . org-display-custom-times)
  22010. (:headline-levels . org-export-headline-levels)
  22011. (:section-numbers . org-export-with-section-numbers)
  22012. (:table-of-contents . org-export-with-toc)
  22013. (:preserve-breaks . org-export-preserve-breaks)
  22014. (:archived-trees . org-export-with-archived-trees)
  22015. (:emphasize . org-export-with-emphasize)
  22016. (:sub-superscript . org-export-with-sub-superscripts)
  22017. (:special-strings . org-export-with-special-strings)
  22018. (:footnotes . org-export-with-footnotes)
  22019. (:drawers . org-export-with-drawers)
  22020. (:tags . org-export-with-tags)
  22021. (:TeX-macros . org-export-with-TeX-macros)
  22022. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  22023. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  22024. (:fixed-width . org-export-with-fixed-width)
  22025. (:timestamps . org-export-with-timestamps)
  22026. (:author-info . org-export-author-info)
  22027. (:time-stamp-file . org-export-time-stamp-file)
  22028. (:tables . org-export-with-tables)
  22029. (:table-auto-headline . org-export-highlight-first-table-line)
  22030. (:style . org-export-html-style)
  22031. (:agenda-style . org-agenda-export-html-style)
  22032. (:convert-org-links . org-export-html-link-org-files-as-html)
  22033. (:inline-images . org-export-html-inline-images)
  22034. (:html-extension . org-export-html-extension)
  22035. (:html-table-tag . org-export-html-table-tag)
  22036. (:expand-quoted-html . org-export-html-expand)
  22037. (:timestamp . org-export-html-with-timestamp)
  22038. (:publishing-directory . org-export-publishing-directory)
  22039. (:preamble . org-export-html-preamble)
  22040. (:postamble . org-export-html-postamble)
  22041. (:auto-preamble . org-export-html-auto-preamble)
  22042. (:auto-postamble . org-export-html-auto-postamble)
  22043. (:author . user-full-name)
  22044. (:email . user-mail-address)))
  22045. (defun org-default-export-plist ()
  22046. "Return the property list with default settings for the export variables."
  22047. (let ((l org-export-plist-vars) rtn e)
  22048. (while (setq e (pop l))
  22049. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22050. rtn))
  22051. (defun org-infile-export-plist ()
  22052. "Return the property list with file-local settings for export."
  22053. (save-excursion
  22054. (save-restriction
  22055. (widen)
  22056. (goto-char 0)
  22057. (let ((re (org-make-options-regexp
  22058. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22059. p key val text options)
  22060. (while (re-search-forward re nil t)
  22061. (setq key (org-match-string-no-properties 1)
  22062. val (org-match-string-no-properties 2))
  22063. (cond
  22064. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22065. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22066. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22067. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22068. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22069. ((string-equal key "TEXT")
  22070. (setq text (if text (concat text "\n" val) val)))
  22071. ((string-equal key "OPTIONS") (setq options val))))
  22072. (setq p (plist-put p :text text))
  22073. (when options
  22074. (let ((op '(("H" . :headline-levels)
  22075. ("num" . :section-numbers)
  22076. ("toc" . :table-of-contents)
  22077. ("\\n" . :preserve-breaks)
  22078. ("@" . :expand-quoted-html)
  22079. (":" . :fixed-width)
  22080. ("|" . :tables)
  22081. ("^" . :sub-superscript)
  22082. ("-" . :special-strings)
  22083. ("f" . :footnotes)
  22084. ("d" . :drawers)
  22085. ("tags" . :tags)
  22086. ("*" . :emphasize)
  22087. ("TeX" . :TeX-macros)
  22088. ("LaTeX" . :LaTeX-fragments)
  22089. ("skip" . :skip-before-1st-heading)
  22090. ("author" . :author-info)
  22091. ("timestamp" . :time-stamp-file)))
  22092. o)
  22093. (while (setq o (pop op))
  22094. (if (string-match (concat (regexp-quote (car o))
  22095. ":\\([^ \t\n\r;,.]*\\)")
  22096. options)
  22097. (setq p (plist-put p (cdr o)
  22098. (car (read-from-string
  22099. (match-string 1 options)))))))))
  22100. p))))
  22101. (defun org-export-directory (type plist)
  22102. (let* ((val (plist-get plist :publishing-directory))
  22103. (dir (if (listp val)
  22104. (or (cdr (assoc type val)) ".")
  22105. val)))
  22106. dir))
  22107. (defun org-skip-comments (lines)
  22108. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22109. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22110. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22111. (case-fold-search nil)
  22112. rtn line level)
  22113. (while (setq line (pop lines))
  22114. (cond
  22115. ((and (string-match re1 line)
  22116. (setq level (- (match-end 1) (match-beginning 1))))
  22117. ;; Beginning of a COMMENT subtree. Skip it.
  22118. (while (and (setq line (pop lines))
  22119. (or (not (string-match re2 line))
  22120. (> (- (match-end 1) (match-beginning 1)) level))))
  22121. (setq lines (cons line lines)))
  22122. ((string-match "^#" line)
  22123. ;; an ordinary comment line
  22124. )
  22125. ((and org-export-table-remove-special-lines
  22126. (string-match "^[ \t]*|" line)
  22127. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22128. (and (string-match "| *<[0-9]+> *|" line)
  22129. (not (string-match "| *[^ <|]" line)))))
  22130. ;; a special table line that should be removed
  22131. )
  22132. (t (setq rtn (cons line rtn)))))
  22133. (nreverse rtn)))
  22134. (defun org-export (&optional arg)
  22135. (interactive)
  22136. (let ((help "[t] insert the export option template
  22137. \[v] limit export to visible part of outline tree
  22138. \[a] export as ASCII
  22139. \[h] export as HTML
  22140. \[H] export as HTML to temporary buffer
  22141. \[R] export region as HTML
  22142. \[b] export as HTML and browse immediately
  22143. \[x] export as XOXO
  22144. \[l] export as LaTeX
  22145. \[L] export as LaTeX to temporary buffer
  22146. \[i] export current file as iCalendar file
  22147. \[I] export all agenda files as iCalendar files
  22148. \[c] export agenda files into combined iCalendar file
  22149. \[F] publish current file
  22150. \[P] publish current project
  22151. \[X] publish... (project will be prompted for)
  22152. \[A] publish all projects")
  22153. (cmds
  22154. '((?t . org-insert-export-options-template)
  22155. (?v . org-export-visible)
  22156. (?a . org-export-as-ascii)
  22157. (?h . org-export-as-html)
  22158. (?b . org-export-as-html-and-open)
  22159. (?H . org-export-as-html-to-buffer)
  22160. (?R . org-export-region-as-html)
  22161. (?x . org-export-as-xoxo)
  22162. (?l . org-export-as-latex)
  22163. (?L . org-export-as-latex-to-buffer)
  22164. (?i . org-export-icalendar-this-file)
  22165. (?I . org-export-icalendar-all-agenda-files)
  22166. (?c . org-export-icalendar-combine-agenda-files)
  22167. (?F . org-publish-current-file)
  22168. (?P . org-publish-current-project)
  22169. (?X . org-publish)
  22170. (?A . org-publish-all)))
  22171. r1 r2 ass)
  22172. (save-window-excursion
  22173. (delete-other-windows)
  22174. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22175. (princ help))
  22176. (message "Select command: ")
  22177. (setq r1 (read-char-exclusive)))
  22178. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22179. (if (setq ass (assq r2 cmds))
  22180. (call-interactively (cdr ass))
  22181. (error "No command associated with key %c" r1))))
  22182. (defconst org-html-entities
  22183. '(("nbsp")
  22184. ("iexcl")
  22185. ("cent")
  22186. ("pound")
  22187. ("curren")
  22188. ("yen")
  22189. ("brvbar")
  22190. ("vert" . "&#124;")
  22191. ("sect")
  22192. ("uml")
  22193. ("copy")
  22194. ("ordf")
  22195. ("laquo")
  22196. ("not")
  22197. ("shy")
  22198. ("reg")
  22199. ("macr")
  22200. ("deg")
  22201. ("plusmn")
  22202. ("sup2")
  22203. ("sup3")
  22204. ("acute")
  22205. ("micro")
  22206. ("para")
  22207. ("middot")
  22208. ("odot"."o")
  22209. ("star"."*")
  22210. ("cedil")
  22211. ("sup1")
  22212. ("ordm")
  22213. ("raquo")
  22214. ("frac14")
  22215. ("frac12")
  22216. ("frac34")
  22217. ("iquest")
  22218. ("Agrave")
  22219. ("Aacute")
  22220. ("Acirc")
  22221. ("Atilde")
  22222. ("Auml")
  22223. ("Aring") ("AA"."&Aring;")
  22224. ("AElig")
  22225. ("Ccedil")
  22226. ("Egrave")
  22227. ("Eacute")
  22228. ("Ecirc")
  22229. ("Euml")
  22230. ("Igrave")
  22231. ("Iacute")
  22232. ("Icirc")
  22233. ("Iuml")
  22234. ("ETH")
  22235. ("Ntilde")
  22236. ("Ograve")
  22237. ("Oacute")
  22238. ("Ocirc")
  22239. ("Otilde")
  22240. ("Ouml")
  22241. ("times")
  22242. ("Oslash")
  22243. ("Ugrave")
  22244. ("Uacute")
  22245. ("Ucirc")
  22246. ("Uuml")
  22247. ("Yacute")
  22248. ("THORN")
  22249. ("szlig")
  22250. ("agrave")
  22251. ("aacute")
  22252. ("acirc")
  22253. ("atilde")
  22254. ("auml")
  22255. ("aring")
  22256. ("aelig")
  22257. ("ccedil")
  22258. ("egrave")
  22259. ("eacute")
  22260. ("ecirc")
  22261. ("euml")
  22262. ("igrave")
  22263. ("iacute")
  22264. ("icirc")
  22265. ("iuml")
  22266. ("eth")
  22267. ("ntilde")
  22268. ("ograve")
  22269. ("oacute")
  22270. ("ocirc")
  22271. ("otilde")
  22272. ("ouml")
  22273. ("divide")
  22274. ("oslash")
  22275. ("ugrave")
  22276. ("uacute")
  22277. ("ucirc")
  22278. ("uuml")
  22279. ("yacute")
  22280. ("thorn")
  22281. ("yuml")
  22282. ("fnof")
  22283. ("Alpha")
  22284. ("Beta")
  22285. ("Gamma")
  22286. ("Delta")
  22287. ("Epsilon")
  22288. ("Zeta")
  22289. ("Eta")
  22290. ("Theta")
  22291. ("Iota")
  22292. ("Kappa")
  22293. ("Lambda")
  22294. ("Mu")
  22295. ("Nu")
  22296. ("Xi")
  22297. ("Omicron")
  22298. ("Pi")
  22299. ("Rho")
  22300. ("Sigma")
  22301. ("Tau")
  22302. ("Upsilon")
  22303. ("Phi")
  22304. ("Chi")
  22305. ("Psi")
  22306. ("Omega")
  22307. ("alpha")
  22308. ("beta")
  22309. ("gamma")
  22310. ("delta")
  22311. ("epsilon")
  22312. ("varepsilon"."&epsilon;")
  22313. ("zeta")
  22314. ("eta")
  22315. ("theta")
  22316. ("iota")
  22317. ("kappa")
  22318. ("lambda")
  22319. ("mu")
  22320. ("nu")
  22321. ("xi")
  22322. ("omicron")
  22323. ("pi")
  22324. ("rho")
  22325. ("sigmaf") ("varsigma"."&sigmaf;")
  22326. ("sigma")
  22327. ("tau")
  22328. ("upsilon")
  22329. ("phi")
  22330. ("chi")
  22331. ("psi")
  22332. ("omega")
  22333. ("thetasym") ("vartheta"."&thetasym;")
  22334. ("upsih")
  22335. ("piv")
  22336. ("bull") ("bullet"."&bull;")
  22337. ("hellip") ("dots"."&hellip;")
  22338. ("prime")
  22339. ("Prime")
  22340. ("oline")
  22341. ("frasl")
  22342. ("weierp")
  22343. ("image")
  22344. ("real")
  22345. ("trade")
  22346. ("alefsym")
  22347. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22348. ("uarr") ("uparrow"."&uarr;")
  22349. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22350. ("darr")("downarrow"."&darr;")
  22351. ("harr") ("leftrightarrow"."&harr;")
  22352. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22353. ("lArr") ("Leftarrow"."&lArr;")
  22354. ("uArr") ("Uparrow"."&uArr;")
  22355. ("rArr") ("Rightarrow"."&rArr;")
  22356. ("dArr") ("Downarrow"."&dArr;")
  22357. ("hArr") ("Leftrightarrow"."&hArr;")
  22358. ("forall")
  22359. ("part") ("partial"."&part;")
  22360. ("exist") ("exists"."&exist;")
  22361. ("empty") ("emptyset"."&empty;")
  22362. ("nabla")
  22363. ("isin") ("in"."&isin;")
  22364. ("notin")
  22365. ("ni")
  22366. ("prod")
  22367. ("sum")
  22368. ("minus")
  22369. ("lowast") ("ast"."&lowast;")
  22370. ("radic")
  22371. ("prop") ("proptp"."&prop;")
  22372. ("infin") ("infty"."&infin;")
  22373. ("ang") ("angle"."&ang;")
  22374. ("and") ("wedge"."&and;")
  22375. ("or") ("vee"."&or;")
  22376. ("cap")
  22377. ("cup")
  22378. ("int")
  22379. ("there4")
  22380. ("sim")
  22381. ("cong") ("simeq"."&cong;")
  22382. ("asymp")("approx"."&asymp;")
  22383. ("ne") ("neq"."&ne;")
  22384. ("equiv")
  22385. ("le")
  22386. ("ge")
  22387. ("sub") ("subset"."&sub;")
  22388. ("sup") ("supset"."&sup;")
  22389. ("nsub")
  22390. ("sube")
  22391. ("supe")
  22392. ("oplus")
  22393. ("otimes")
  22394. ("perp")
  22395. ("sdot") ("cdot"."&sdot;")
  22396. ("lceil")
  22397. ("rceil")
  22398. ("lfloor")
  22399. ("rfloor")
  22400. ("lang")
  22401. ("rang")
  22402. ("loz") ("Diamond"."&loz;")
  22403. ("spades") ("spadesuit"."&spades;")
  22404. ("clubs") ("clubsuit"."&clubs;")
  22405. ("hearts") ("diamondsuit"."&hearts;")
  22406. ("diams") ("diamondsuit"."&diams;")
  22407. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22408. ("quot")
  22409. ("amp")
  22410. ("lt")
  22411. ("gt")
  22412. ("OElig")
  22413. ("oelig")
  22414. ("Scaron")
  22415. ("scaron")
  22416. ("Yuml")
  22417. ("circ")
  22418. ("tilde")
  22419. ("ensp")
  22420. ("emsp")
  22421. ("thinsp")
  22422. ("zwnj")
  22423. ("zwj")
  22424. ("lrm")
  22425. ("rlm")
  22426. ("ndash")
  22427. ("mdash")
  22428. ("lsquo")
  22429. ("rsquo")
  22430. ("sbquo")
  22431. ("ldquo")
  22432. ("rdquo")
  22433. ("bdquo")
  22434. ("dagger")
  22435. ("Dagger")
  22436. ("permil")
  22437. ("lsaquo")
  22438. ("rsaquo")
  22439. ("euro")
  22440. ("arccos"."arccos")
  22441. ("arcsin"."arcsin")
  22442. ("arctan"."arctan")
  22443. ("arg"."arg")
  22444. ("cos"."cos")
  22445. ("cosh"."cosh")
  22446. ("cot"."cot")
  22447. ("coth"."coth")
  22448. ("csc"."csc")
  22449. ("deg"."deg")
  22450. ("det"."det")
  22451. ("dim"."dim")
  22452. ("exp"."exp")
  22453. ("gcd"."gcd")
  22454. ("hom"."hom")
  22455. ("inf"."inf")
  22456. ("ker"."ker")
  22457. ("lg"."lg")
  22458. ("lim"."lim")
  22459. ("liminf"."liminf")
  22460. ("limsup"."limsup")
  22461. ("ln"."ln")
  22462. ("log"."log")
  22463. ("max"."max")
  22464. ("min"."min")
  22465. ("Pr"."Pr")
  22466. ("sec"."sec")
  22467. ("sin"."sin")
  22468. ("sinh"."sinh")
  22469. ("sup"."sup")
  22470. ("tan"."tan")
  22471. ("tanh"."tanh")
  22472. )
  22473. "Entities for TeX->HTML translation.
  22474. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22475. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22476. In that case, \"\\ent\" will be translated to \"&other;\".
  22477. The list contains HTML entities for Latin-1, Greek and other symbols.
  22478. It is supplemented by a number of commonly used TeX macros with appropriate
  22479. translations. There is currently no way for users to extend this.")
  22480. ;;; General functions for all backends
  22481. (defun org-cleaned-string-for-export (string &rest parameters)
  22482. "Cleanup a buffer STRING so that links can be created safely."
  22483. (interactive)
  22484. (let* ((re-radio (and org-target-link-regexp
  22485. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22486. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22487. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22488. (re-archive (concat ":" org-archive-tag ":"))
  22489. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22490. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22491. (htmlp (plist-get parameters :for-html))
  22492. (asciip (plist-get parameters :for-ascii))
  22493. (latexp (plist-get parameters :for-LaTeX))
  22494. (commentsp (plist-get parameters :comments))
  22495. (archived-trees (plist-get parameters :archived-trees))
  22496. (inhibit-read-only t)
  22497. (drawers org-drawers)
  22498. (exp-drawers (plist-get parameters :drawers))
  22499. (outline-regexp "\\*+ ")
  22500. a b xx
  22501. rtn p)
  22502. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22503. (erase-buffer)
  22504. (insert string)
  22505. ;; Remove license-to-kill stuff
  22506. (while (setq p (text-property-any (point-min) (point-max)
  22507. :org-license-to-kill t))
  22508. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22509. (let ((org-inhibit-startup t)) (org-mode))
  22510. (untabify (point-min) (point-max))
  22511. ;; Get rid of drawers
  22512. (unless (eq t exp-drawers)
  22513. (goto-char (point-min))
  22514. (let ((re (concat "^[ \t]*:\\("
  22515. (mapconcat
  22516. 'identity
  22517. (org-delete-all exp-drawers
  22518. (copy-sequence drawers))
  22519. "\\|")
  22520. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22521. (while (re-search-forward re nil t)
  22522. (replace-match ""))))
  22523. ;; Get the correct stuff before the first headline
  22524. (when (plist-get parameters :skip-before-1st-heading)
  22525. (goto-char (point-min))
  22526. (when (re-search-forward "^\\*+[ \t]" nil t)
  22527. (delete-region (point-min) (match-beginning 0))
  22528. (goto-char (point-min))
  22529. (insert "\n")))
  22530. (when (plist-get parameters :add-text)
  22531. (goto-char (point-min))
  22532. (insert (plist-get parameters :add-text) "\n"))
  22533. ;; Get rid of archived trees
  22534. (when (not (eq archived-trees t))
  22535. (goto-char (point-min))
  22536. (while (re-search-forward re-archive nil t)
  22537. (if (not (org-on-heading-p t))
  22538. (org-end-of-subtree t)
  22539. (beginning-of-line 1)
  22540. (setq a (if archived-trees
  22541. (1+ (point-at-eol)) (point))
  22542. b (org-end-of-subtree t))
  22543. (if (> b a) (delete-region a b)))))
  22544. ;; Find targets in comments and move them out of comments,
  22545. ;; but mark them as targets that should be invisible
  22546. (goto-char (point-min))
  22547. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22548. (replace-match "\\1(INVISIBLE)"))
  22549. ;; Protect backend specific stuff, throw away the others.
  22550. (let ((formatters
  22551. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22552. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22553. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22554. fmt)
  22555. (goto-char (point-min))
  22556. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22557. (goto-char (match-end 0))
  22558. (while (not (looking-at "#\\+END_EXAMPLE"))
  22559. (insert ": ")
  22560. (beginning-of-line 2)))
  22561. (goto-char (point-min))
  22562. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22563. (add-text-properties (match-beginning 0) (match-end 0)
  22564. '(org-protected t)))
  22565. (while formatters
  22566. (setq fmt (pop formatters))
  22567. (when (car fmt)
  22568. (goto-char (point-min))
  22569. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22570. ":[ \t]*\\(.*\\)") nil t)
  22571. (replace-match "\\1" t)
  22572. (add-text-properties
  22573. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22574. '(org-protected t))))
  22575. (goto-char (point-min))
  22576. (while (re-search-forward
  22577. (concat "^#\\+"
  22578. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22579. (cadddr fmt) "\\>.*\n?") nil t)
  22580. (if (car fmt)
  22581. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22582. '(org-protected t))
  22583. (delete-region (match-beginning 0) (match-end 0))))))
  22584. ;; Protect quoted subtrees
  22585. (goto-char (point-min))
  22586. (while (re-search-forward re-quote nil t)
  22587. (goto-char (match-beginning 0))
  22588. (end-of-line 1)
  22589. (add-text-properties (point) (org-end-of-subtree t)
  22590. '(org-protected t)))
  22591. ;; Protect verbatim elements
  22592. (goto-char (point-min))
  22593. (while (re-search-forward org-verbatim-re nil t)
  22594. (add-text-properties (match-beginning 4) (match-end 4)
  22595. '(org-protected t))
  22596. (goto-char (1+ (match-end 4))))
  22597. ;; Remove subtrees that are commented
  22598. (goto-char (point-min))
  22599. (while (re-search-forward re-commented nil t)
  22600. (goto-char (match-beginning 0))
  22601. (delete-region (point) (org-end-of-subtree t)))
  22602. ;; Remove special table lines
  22603. (when org-export-table-remove-special-lines
  22604. (goto-char (point-min))
  22605. (while (re-search-forward "^[ \t]*|" nil t)
  22606. (beginning-of-line 1)
  22607. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22608. (and (looking-at ".*?| *<[0-9]+> *|")
  22609. (not (looking-at ".*?| *[^ <|]"))))
  22610. (delete-region (max (point-min) (1- (point-at-bol)))
  22611. (point-at-eol))
  22612. (end-of-line 1))))
  22613. ;; Specific LaTeX stuff
  22614. (when latexp
  22615. (require 'org-export-latex nil)
  22616. (org-export-latex-cleaned-string))
  22617. (when asciip
  22618. (org-export-ascii-clean-string))
  22619. ;; Specific HTML stuff
  22620. (when htmlp
  22621. ;; Convert LaTeX fragments to images
  22622. (when (plist-get parameters :LaTeX-fragments)
  22623. (org-format-latex
  22624. (concat "ltxpng/" (file-name-sans-extension
  22625. (file-name-nondirectory
  22626. org-current-export-file)))
  22627. org-current-export-dir nil "Creating LaTeX image %s"))
  22628. (message "Exporting..."))
  22629. ;; Remove or replace comments
  22630. (goto-char (point-min))
  22631. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22632. (if commentsp
  22633. (progn (add-text-properties
  22634. (match-beginning 0) (match-end 0) '(org-protected t))
  22635. (replace-match (format commentsp (match-string 1)) t t))
  22636. (replace-match "")))
  22637. ;; Find matches for radio targets and turn them into internal links
  22638. (goto-char (point-min))
  22639. (when re-radio
  22640. (while (re-search-forward re-radio nil t)
  22641. (org-if-unprotected
  22642. (replace-match "\\1[[\\2]]"))))
  22643. ;; Find all links that contain a newline and put them into a single line
  22644. (goto-char (point-min))
  22645. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22646. (org-if-unprotected
  22647. (replace-match "\\1 \\3")
  22648. (goto-char (match-beginning 0))))
  22649. ;; Normalize links: Convert angle and plain links into bracket links
  22650. ;; Expand link abbreviations
  22651. (goto-char (point-min))
  22652. (while (re-search-forward re-plain-link nil t)
  22653. (goto-char (1- (match-end 0)))
  22654. (org-if-unprotected
  22655. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22656. ":" (match-string 3) "]]")))
  22657. ;; added 'org-link face to links
  22658. (put-text-property 0 (length s) 'face 'org-link s)
  22659. (replace-match s t t))))
  22660. (goto-char (point-min))
  22661. (while (re-search-forward re-angle-link nil t)
  22662. (goto-char (1- (match-end 0)))
  22663. (org-if-unprotected
  22664. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22665. ":" (match-string 3) "]]")))
  22666. (put-text-property 0 (length s) 'face 'org-link s)
  22667. (replace-match s t t))))
  22668. (goto-char (point-min))
  22669. (while (re-search-forward org-bracket-link-regexp nil t)
  22670. (org-if-unprotected
  22671. (let* ((s (concat "[[" (setq xx (save-match-data
  22672. (org-link-expand-abbrev (match-string 1))))
  22673. "]"
  22674. (if (match-end 3)
  22675. (match-string 2)
  22676. (concat "[" xx "]"))
  22677. "]")))
  22678. (put-text-property 0 (length s) 'face 'org-link s)
  22679. (replace-match s t t))))
  22680. ;; Find multiline emphasis and put them into single line
  22681. (when (plist-get parameters :emph-multiline)
  22682. (goto-char (point-min))
  22683. (while (re-search-forward org-emph-re nil t)
  22684. (if (not (= (char-after (match-beginning 3))
  22685. (char-after (match-beginning 4))))
  22686. (org-if-unprotected
  22687. (subst-char-in-region (match-beginning 0) (match-end 0)
  22688. ?\n ?\ t)
  22689. (goto-char (1- (match-end 0))))
  22690. (goto-char (1+ (match-beginning 0))))))
  22691. (setq rtn (buffer-string)))
  22692. (kill-buffer " org-mode-tmp")
  22693. rtn))
  22694. (defun org-export-grab-title-from-buffer ()
  22695. "Get a title for the current document, from looking at the buffer."
  22696. (let ((inhibit-read-only t))
  22697. (save-excursion
  22698. (goto-char (point-min))
  22699. (let ((end (save-excursion (outline-next-heading) (point))))
  22700. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22701. ;; Mark the line so that it will not be exported as normal text.
  22702. (org-unmodified
  22703. (add-text-properties (match-beginning 0) (match-end 0)
  22704. (list :org-license-to-kill t)))
  22705. ;; Return the title string
  22706. (org-trim (match-string 0)))))))
  22707. (defun org-export-get-title-from-subtree ()
  22708. "Return subtree title and exclude it from export."
  22709. (let (title (m (mark)))
  22710. (save-excursion
  22711. (goto-char (region-beginning))
  22712. (when (and (org-at-heading-p)
  22713. (>= (org-end-of-subtree t t) (region-end)))
  22714. ;; This is a subtree, we take the title from the first heading
  22715. (goto-char (region-beginning))
  22716. (looking-at org-todo-line-regexp)
  22717. (setq title (match-string 3))
  22718. (org-unmodified
  22719. (add-text-properties (point) (1+ (point-at-eol))
  22720. (list :org-license-to-kill t)))))
  22721. title))
  22722. (defun org-solidify-link-text (s &optional alist)
  22723. "Take link text and make a safe target out of it."
  22724. (save-match-data
  22725. (let* ((rtn
  22726. (mapconcat
  22727. 'identity
  22728. (org-split-string s "[ \t\r\n]+") "--"))
  22729. (a (assoc rtn alist)))
  22730. (or (cdr a) rtn))))
  22731. (defun org-get-min-level (lines)
  22732. "Get the minimum level in LINES."
  22733. (let ((re "^\\(\\*+\\) ") l min)
  22734. (catch 'exit
  22735. (while (setq l (pop lines))
  22736. (if (string-match re l)
  22737. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22738. 1)))
  22739. ;; Variable holding the vector with section numbers
  22740. (defvar org-section-numbers (make-vector org-level-max 0))
  22741. (defun org-init-section-numbers ()
  22742. "Initialize the vector for the section numbers."
  22743. (let* ((level -1)
  22744. (numbers (nreverse (org-split-string "" "\\.")))
  22745. (depth (1- (length org-section-numbers)))
  22746. (i depth) number-string)
  22747. (while (>= i 0)
  22748. (if (> i level)
  22749. (aset org-section-numbers i 0)
  22750. (setq number-string (or (car numbers) "0"))
  22751. (if (string-match "\\`[A-Z]\\'" number-string)
  22752. (aset org-section-numbers i
  22753. (- (string-to-char number-string) ?A -1))
  22754. (aset org-section-numbers i (string-to-number number-string)))
  22755. (pop numbers))
  22756. (setq i (1- i)))))
  22757. (defun org-section-number (&optional level)
  22758. "Return a string with the current section number.
  22759. When LEVEL is non-nil, increase section numbers on that level."
  22760. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22761. (when level
  22762. (when (> level -1)
  22763. (aset org-section-numbers
  22764. level (1+ (aref org-section-numbers level))))
  22765. (setq idx (1+ level))
  22766. (while (<= idx depth)
  22767. (if (not (= idx 1))
  22768. (aset org-section-numbers idx 0))
  22769. (setq idx (1+ idx))))
  22770. (setq idx 0)
  22771. (while (<= idx depth)
  22772. (setq n (aref org-section-numbers idx))
  22773. (setq string (concat string (if (not (string= string "")) "." "")
  22774. (int-to-string n)))
  22775. (setq idx (1+ idx)))
  22776. (save-match-data
  22777. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22778. (setq string (replace-match "" t nil string)))
  22779. (if (string-match "\\(\\.0\\)+\\'" string)
  22780. (setq string (replace-match "" t nil string))))
  22781. string))
  22782. ;;; ASCII export
  22783. (defvar org-last-level nil) ; dynamically scoped variable
  22784. (defvar org-min-level nil) ; dynamically scoped variable
  22785. (defvar org-levels-open nil) ; dynamically scoped parameter
  22786. (defvar org-ascii-current-indentation nil) ; For communication
  22787. (defun org-export-as-ascii (arg)
  22788. "Export the outline as a pretty ASCII file.
  22789. If there is an active region, export only the region.
  22790. The prefix ARG specifies how many levels of the outline should become
  22791. underlined headlines. The default is 3."
  22792. (interactive "P")
  22793. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22794. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22795. (org-infile-export-plist)))
  22796. (region-p (org-region-active-p))
  22797. (subtree-p
  22798. (when region-p
  22799. (save-excursion
  22800. (goto-char (region-beginning))
  22801. (and (org-at-heading-p)
  22802. (>= (org-end-of-subtree t t) (region-end))))))
  22803. (custom-times org-display-custom-times)
  22804. (org-ascii-current-indentation '(0 . 0))
  22805. (level 0) line txt
  22806. (umax nil)
  22807. (umax-toc nil)
  22808. (case-fold-search nil)
  22809. (filename (concat (file-name-as-directory
  22810. (org-export-directory :ascii opt-plist))
  22811. (file-name-sans-extension
  22812. (or (and subtree-p
  22813. (org-entry-get (region-beginning)
  22814. "EXPORT_FILE_NAME" t))
  22815. (file-name-nondirectory buffer-file-name)))
  22816. ".txt"))
  22817. (filename (if (equal (file-truename filename)
  22818. (file-truename buffer-file-name))
  22819. (concat filename ".txt")
  22820. filename))
  22821. (buffer (find-file-noselect filename))
  22822. (org-levels-open (make-vector org-level-max nil))
  22823. (odd org-odd-levels-only)
  22824. (date (plist-get opt-plist :date))
  22825. (author (plist-get opt-plist :author))
  22826. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22827. (plist-get opt-plist :title)
  22828. (and (not
  22829. (plist-get opt-plist :skip-before-1st-heading))
  22830. (org-export-grab-title-from-buffer))
  22831. (file-name-sans-extension
  22832. (file-name-nondirectory buffer-file-name))))
  22833. (email (plist-get opt-plist :email))
  22834. (language (plist-get opt-plist :language))
  22835. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22836. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22837. (todo nil)
  22838. (lang-words nil)
  22839. (region
  22840. (buffer-substring
  22841. (if (org-region-active-p) (region-beginning) (point-min))
  22842. (if (org-region-active-p) (region-end) (point-max))))
  22843. (lines (org-split-string
  22844. (org-cleaned-string-for-export
  22845. region
  22846. :for-ascii t
  22847. :skip-before-1st-heading
  22848. (plist-get opt-plist :skip-before-1st-heading)
  22849. :drawers (plist-get opt-plist :drawers)
  22850. :verbatim-multiline t
  22851. :archived-trees
  22852. (plist-get opt-plist :archived-trees)
  22853. :add-text (plist-get opt-plist :text))
  22854. "\n"))
  22855. thetoc have-headings first-heading-pos
  22856. table-open table-buffer)
  22857. (let ((inhibit-read-only t))
  22858. (org-unmodified
  22859. (remove-text-properties (point-min) (point-max)
  22860. '(:org-license-to-kill t))))
  22861. (setq org-min-level (org-get-min-level lines))
  22862. (setq org-last-level org-min-level)
  22863. (org-init-section-numbers)
  22864. (find-file-noselect filename)
  22865. (setq lang-words (or (assoc language org-export-language-setup)
  22866. (assoc "en" org-export-language-setup)))
  22867. (switch-to-buffer-other-window buffer)
  22868. (erase-buffer)
  22869. (fundamental-mode)
  22870. ;; create local variables for all options, to make sure all called
  22871. ;; functions get the correct information
  22872. (mapc (lambda (x)
  22873. (set (make-local-variable (cdr x))
  22874. (plist-get opt-plist (car x))))
  22875. org-export-plist-vars)
  22876. (org-set-local 'org-odd-levels-only odd)
  22877. (setq umax (if arg (prefix-numeric-value arg)
  22878. org-export-headline-levels))
  22879. (setq umax-toc (if (integerp org-export-with-toc)
  22880. (min org-export-with-toc umax)
  22881. umax))
  22882. ;; File header
  22883. (if title (org-insert-centered title ?=))
  22884. (insert "\n")
  22885. (if (and (or author email)
  22886. org-export-author-info)
  22887. (insert (concat (nth 1 lang-words) ": " (or author "")
  22888. (if email (concat " <" email ">") "")
  22889. "\n")))
  22890. (cond
  22891. ((and date (string-match "%" date))
  22892. (setq date (format-time-string date (current-time))))
  22893. (date)
  22894. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22895. (if (and date org-export-time-stamp-file)
  22896. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22897. (insert "\n\n")
  22898. (if org-export-with-toc
  22899. (progn
  22900. (push (concat (nth 3 lang-words) "\n") thetoc)
  22901. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22902. (mapc '(lambda (line)
  22903. (if (string-match org-todo-line-regexp
  22904. line)
  22905. ;; This is a headline
  22906. (progn
  22907. (setq have-headings t)
  22908. (setq level (- (match-end 1) (match-beginning 1))
  22909. level (org-tr-level level)
  22910. txt (match-string 3 line)
  22911. todo
  22912. (or (and org-export-mark-todo-in-toc
  22913. (match-beginning 2)
  22914. (not (member (match-string 2 line)
  22915. org-done-keywords)))
  22916. ; TODO, not DONE
  22917. (and org-export-mark-todo-in-toc
  22918. (= level umax-toc)
  22919. (org-search-todo-below
  22920. line lines level))))
  22921. (setq txt (org-html-expand-for-ascii txt))
  22922. (while (string-match org-bracket-link-regexp txt)
  22923. (setq txt
  22924. (replace-match
  22925. (match-string (if (match-end 2) 3 1) txt)
  22926. t t txt)))
  22927. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22928. (string-match
  22929. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22930. txt))
  22931. (setq txt (replace-match "" t t txt)))
  22932. (if (string-match quote-re0 txt)
  22933. (setq txt (replace-match "" t t txt)))
  22934. (if org-export-with-section-numbers
  22935. (setq txt (concat (org-section-number level)
  22936. " " txt)))
  22937. (if (<= level umax-toc)
  22938. (progn
  22939. (push
  22940. (concat
  22941. (make-string
  22942. (* (max 0 (- level org-min-level)) 4) ?\ )
  22943. (format (if todo "%s (*)\n" "%s\n") txt))
  22944. thetoc)
  22945. (setq org-last-level level))
  22946. ))))
  22947. lines)
  22948. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22949. (org-init-section-numbers)
  22950. (while (setq line (pop lines))
  22951. ;; Remove the quoted HTML tags.
  22952. (setq line (org-html-expand-for-ascii line))
  22953. ;; Remove targets
  22954. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22955. (setq line (replace-match "" t t line)))
  22956. ;; Replace internal links
  22957. (while (string-match org-bracket-link-regexp line)
  22958. (setq line (replace-match
  22959. (if (match-end 3) "[\\3]" "[\\1]")
  22960. t nil line)))
  22961. (when custom-times
  22962. (setq line (org-translate-time line)))
  22963. (cond
  22964. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22965. ;; a Headline
  22966. (setq first-heading-pos (or first-heading-pos (point)))
  22967. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22968. txt (match-string 2 line))
  22969. (org-ascii-level-start level txt umax lines))
  22970. ((and org-export-with-tables
  22971. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22972. (if (not table-open)
  22973. ;; New table starts
  22974. (setq table-open t table-buffer nil))
  22975. ;; Accumulate lines
  22976. (setq table-buffer (cons line table-buffer))
  22977. (when (or (not lines)
  22978. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22979. (car lines))))
  22980. (setq table-open nil
  22981. table-buffer (nreverse table-buffer))
  22982. (insert (mapconcat
  22983. (lambda (x)
  22984. (org-fix-indentation x org-ascii-current-indentation))
  22985. (org-format-table-ascii table-buffer)
  22986. "\n") "\n")))
  22987. (t
  22988. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22989. (if (and org-export-with-fixed-width
  22990. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22991. (setq line (replace-match "\\1" nil nil line)))
  22992. (insert line "\n"))))
  22993. (normal-mode)
  22994. ;; insert the table of contents
  22995. (when thetoc
  22996. (goto-char (point-min))
  22997. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22998. (progn
  22999. (goto-char (match-beginning 0))
  23000. (replace-match ""))
  23001. (goto-char first-heading-pos))
  23002. (mapc 'insert thetoc)
  23003. (or (looking-at "[ \t]*\n[ \t]*\n")
  23004. (insert "\n\n")))
  23005. ;; Convert whitespace place holders
  23006. (goto-char (point-min))
  23007. (let (beg end)
  23008. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23009. (setq end (next-single-property-change beg 'org-whitespace))
  23010. (goto-char beg)
  23011. (delete-region beg end)
  23012. (insert (make-string (- end beg) ?\ ))))
  23013. (save-buffer)
  23014. ;; remove display and invisible chars
  23015. (let (beg end)
  23016. (goto-char (point-min))
  23017. (while (setq beg (next-single-property-change (point) 'display))
  23018. (setq end (next-single-property-change beg 'display))
  23019. (delete-region beg end)
  23020. (goto-char beg)
  23021. (insert "=>"))
  23022. (goto-char (point-min))
  23023. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  23024. (setq end (next-single-property-change beg 'org-cwidth))
  23025. (delete-region beg end)
  23026. (goto-char beg)))
  23027. (goto-char (point-min))))
  23028. (defun org-export-ascii-clean-string ()
  23029. "Do extra work for ASCII export"
  23030. (goto-char (point-min))
  23031. (while (re-search-forward org-verbatim-re nil t)
  23032. (goto-char (match-end 2))
  23033. (backward-delete-char 1) (insert "'")
  23034. (goto-char (match-beginning 2))
  23035. (delete-char 1) (insert "`")
  23036. (goto-char (match-end 2))))
  23037. (defun org-search-todo-below (line lines level)
  23038. "Search the subtree below LINE for any TODO entries."
  23039. (let ((rest (cdr (memq line lines)))
  23040. (re org-todo-line-regexp)
  23041. line lv todo)
  23042. (catch 'exit
  23043. (while (setq line (pop rest))
  23044. (if (string-match re line)
  23045. (progn
  23046. (setq lv (- (match-end 1) (match-beginning 1))
  23047. todo (and (match-beginning 2)
  23048. (not (member (match-string 2 line)
  23049. org-done-keywords))))
  23050. ; TODO, not DONE
  23051. (if (<= lv level) (throw 'exit nil))
  23052. (if todo (throw 'exit t))))))))
  23053. (defun org-html-expand-for-ascii (line)
  23054. "Handle quoted HTML for ASCII export."
  23055. (if org-export-html-expand
  23056. (while (string-match "@<[^<>\n]*>" line)
  23057. ;; We just remove the tags for now.
  23058. (setq line (replace-match "" nil nil line))))
  23059. line)
  23060. (defun org-insert-centered (s &optional underline)
  23061. "Insert the string S centered and underline it with character UNDERLINE."
  23062. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23063. (insert (make-string ind ?\ ) s "\n")
  23064. (if underline
  23065. (insert (make-string ind ?\ )
  23066. (make-string (string-width s) underline)
  23067. "\n"))))
  23068. (defun org-ascii-level-start (level title umax &optional lines)
  23069. "Insert a new level in ASCII export."
  23070. (let (char (n (- level umax 1)) (ind 0))
  23071. (if (> level umax)
  23072. (progn
  23073. (insert (make-string (* 2 n) ?\ )
  23074. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23075. org-export-ascii-bullets))
  23076. " " title "\n")
  23077. ;; find the indentation of the next non-empty line
  23078. (catch 'stop
  23079. (while lines
  23080. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23081. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23082. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23083. (pop lines)))
  23084. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23085. (if (or (not (equal (char-before) ?\n))
  23086. (not (equal (char-before (1- (point))) ?\n)))
  23087. (insert "\n"))
  23088. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23089. (unless org-export-with-tags
  23090. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23091. (setq title (replace-match "" t t title))))
  23092. (if org-export-with-section-numbers
  23093. (setq title (concat (org-section-number level) " " title)))
  23094. (insert title "\n" (make-string (string-width title) char) "\n")
  23095. (setq org-ascii-current-indentation '(0 . 0)))))
  23096. (defun org-export-visible (type arg)
  23097. "Create a copy of the visible part of the current buffer, and export it.
  23098. The copy is created in a temporary buffer and removed after use.
  23099. TYPE is the final key (as a string) that also select the export command in
  23100. the `C-c C-e' export dispatcher.
  23101. As a special case, if the you type SPC at the prompt, the temporary
  23102. org-mode file will not be removed but presented to you so that you can
  23103. continue to use it. The prefix arg ARG is passed through to the exporting
  23104. command."
  23105. (interactive
  23106. (list (progn
  23107. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23108. (read-char-exclusive))
  23109. current-prefix-arg))
  23110. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23111. (error "Invalid export key"))
  23112. (let* ((binding (cdr (assoc type
  23113. '((?a . org-export-as-ascii)
  23114. (?\C-a . org-export-as-ascii)
  23115. (?b . org-export-as-html-and-open)
  23116. (?\C-b . org-export-as-html-and-open)
  23117. (?h . org-export-as-html)
  23118. (?H . org-export-as-html-to-buffer)
  23119. (?R . org-export-region-as-html)
  23120. (?x . org-export-as-xoxo)))))
  23121. (keepp (equal type ?\ ))
  23122. (file buffer-file-name)
  23123. (buffer (get-buffer-create "*Org Export Visible*"))
  23124. s e)
  23125. ;; Need to hack the drawers here.
  23126. (save-excursion
  23127. (goto-char (point-min))
  23128. (while (re-search-forward org-drawer-regexp nil t)
  23129. (goto-char (match-beginning 1))
  23130. (or (org-invisible-p) (org-flag-drawer nil))))
  23131. (with-current-buffer buffer (erase-buffer))
  23132. (save-excursion
  23133. (setq s (goto-char (point-min)))
  23134. (while (not (= (point) (point-max)))
  23135. (goto-char (org-find-invisible))
  23136. (append-to-buffer buffer s (point))
  23137. (setq s (goto-char (org-find-visible))))
  23138. (org-cycle-hide-drawers 'all)
  23139. (goto-char (point-min))
  23140. (unless keepp
  23141. ;; Copy all comment lines to the end, to make sure #+ settings are
  23142. ;; still available for the second export step. Kind of a hack, but
  23143. ;; does do the trick.
  23144. (if (looking-at "#[^\r\n]*")
  23145. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23146. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23147. (append-to-buffer buffer (1+ (match-beginning 0))
  23148. (min (point-max) (1+ (match-end 0))))))
  23149. (set-buffer buffer)
  23150. (let ((buffer-file-name file)
  23151. (org-inhibit-startup t))
  23152. (org-mode)
  23153. (show-all)
  23154. (unless keepp (funcall binding arg))))
  23155. (if (not keepp)
  23156. (kill-buffer buffer)
  23157. (switch-to-buffer-other-window buffer)
  23158. (goto-char (point-min)))))
  23159. (defun org-find-visible ()
  23160. (let ((s (point)))
  23161. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23162. (get-char-property s 'invisible)))
  23163. s))
  23164. (defun org-find-invisible ()
  23165. (let ((s (point)))
  23166. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23167. (not (get-char-property s 'invisible))))
  23168. s))
  23169. ;;; HTML export
  23170. (defun org-get-current-options ()
  23171. "Return a string with current options as keyword options.
  23172. Does include HTML export options as well as TODO and CATEGORY stuff."
  23173. (format
  23174. "#+TITLE: %s
  23175. #+AUTHOR: %s
  23176. #+EMAIL: %s
  23177. #+LANGUAGE: %s
  23178. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23179. #+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
  23180. #+CATEGORY: %s
  23181. #+SEQ_TODO: %s
  23182. #+TYP_TODO: %s
  23183. #+PRIORITIES: %c %c %c
  23184. #+DRAWERS: %s
  23185. #+STARTUP: %s %s %s %s %s
  23186. #+TAGS: %s
  23187. #+ARCHIVE: %s
  23188. #+LINK: %s
  23189. "
  23190. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23191. org-export-headline-levels
  23192. org-export-with-section-numbers
  23193. org-export-with-toc
  23194. org-export-preserve-breaks
  23195. org-export-html-expand
  23196. org-export-with-fixed-width
  23197. org-export-with-tables
  23198. org-export-with-sub-superscripts
  23199. org-export-with-special-strings
  23200. org-export-with-footnotes
  23201. org-export-with-emphasize
  23202. org-export-with-TeX-macros
  23203. org-export-with-LaTeX-fragments
  23204. org-export-skip-text-before-1st-heading
  23205. org-export-with-drawers
  23206. org-export-with-tags
  23207. (file-name-nondirectory buffer-file-name)
  23208. "TODO FEEDBACK VERIFY DONE"
  23209. "Me Jason Marie DONE"
  23210. org-highest-priority org-lowest-priority org-default-priority
  23211. (mapconcat 'identity org-drawers " ")
  23212. (cdr (assoc org-startup-folded
  23213. '((nil . "showall") (t . "overview") (content . "content"))))
  23214. (if org-odd-levels-only "odd" "oddeven")
  23215. (if org-hide-leading-stars "hidestars" "showstars")
  23216. (if org-startup-align-all-tables "align" "noalign")
  23217. (cond ((eq org-log-done t) "logdone")
  23218. ((equal org-log-done 'note) "lognotedone")
  23219. ((not org-log-done) "nologdone"))
  23220. (or (mapconcat (lambda (x)
  23221. (cond
  23222. ((equal '(:startgroup) x) "{")
  23223. ((equal '(:endgroup) x) "}")
  23224. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23225. (t (car x))))
  23226. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23227. org-archive-location
  23228. "org file:~/org/%s.org"
  23229. ))
  23230. (defun org-insert-export-options-template ()
  23231. "Insert into the buffer a template with information for exporting."
  23232. (interactive)
  23233. (if (not (bolp)) (newline))
  23234. (let ((s (org-get-current-options)))
  23235. (and (string-match "#\\+CATEGORY" s)
  23236. (setq s (substring s 0 (match-beginning 0))))
  23237. (insert s)))
  23238. (defun org-toggle-fixed-width-section (arg)
  23239. "Toggle the fixed-width export.
  23240. If there is no active region, the QUOTE keyword at the current headline is
  23241. inserted or removed. When present, it causes the text between this headline
  23242. and the next to be exported as fixed-width text, and unmodified.
  23243. If there is an active region, this command adds or removes a colon as the
  23244. first character of this line. If the first character of a line is a colon,
  23245. this line is also exported in fixed-width font."
  23246. (interactive "P")
  23247. (let* ((cc 0)
  23248. (regionp (org-region-active-p))
  23249. (beg (if regionp (region-beginning) (point)))
  23250. (end (if regionp (region-end)))
  23251. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23252. (case-fold-search nil)
  23253. (re "[ \t]*\\(:\\)")
  23254. off)
  23255. (if regionp
  23256. (save-excursion
  23257. (goto-char beg)
  23258. (setq cc (current-column))
  23259. (beginning-of-line 1)
  23260. (setq off (looking-at re))
  23261. (while (> nlines 0)
  23262. (setq nlines (1- nlines))
  23263. (beginning-of-line 1)
  23264. (cond
  23265. (arg
  23266. (move-to-column cc t)
  23267. (insert ":\n")
  23268. (forward-line -1))
  23269. ((and off (looking-at re))
  23270. (replace-match "" t t nil 1))
  23271. ((not off) (move-to-column cc t) (insert ":")))
  23272. (forward-line 1)))
  23273. (save-excursion
  23274. (org-back-to-heading)
  23275. (if (looking-at (concat outline-regexp
  23276. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23277. (replace-match "" t t nil 1)
  23278. (if (looking-at outline-regexp)
  23279. (progn
  23280. (goto-char (match-end 0))
  23281. (insert org-quote-string " "))))))))
  23282. (defun org-export-as-html-and-open (arg)
  23283. "Export the outline as HTML and immediately open it with a browser.
  23284. If there is an active region, export only the region.
  23285. The prefix ARG specifies how many levels of the outline should become
  23286. headlines. The default is 3. Lower levels will become bulleted lists."
  23287. (interactive "P")
  23288. (org-export-as-html arg 'hidden)
  23289. (org-open-file buffer-file-name))
  23290. (defun org-export-as-html-batch ()
  23291. "Call `org-export-as-html', may be used in batch processing as
  23292. emacs --batch
  23293. --load=$HOME/lib/emacs/org.el
  23294. --eval \"(setq org-export-headline-levels 2)\"
  23295. --visit=MyFile --funcall org-export-as-html-batch"
  23296. (org-export-as-html org-export-headline-levels 'hidden))
  23297. (defun org-export-as-html-to-buffer (arg)
  23298. "Call `org-exort-as-html` with output to a temporary buffer.
  23299. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23300. (interactive "P")
  23301. (org-export-as-html arg nil nil "*Org HTML Export*")
  23302. (switch-to-buffer-other-window "*Org HTML Export*"))
  23303. (defun org-replace-region-by-html (beg end)
  23304. "Assume the current region has org-mode syntax, and convert it to HTML.
  23305. This can be used in any buffer. For example, you could write an
  23306. itemized list in org-mode syntax in an HTML buffer and then use this
  23307. command to convert it."
  23308. (interactive "r")
  23309. (let (reg html buf pop-up-frames)
  23310. (save-window-excursion
  23311. (if (org-mode-p)
  23312. (setq html (org-export-region-as-html
  23313. beg end t 'string))
  23314. (setq reg (buffer-substring beg end)
  23315. buf (get-buffer-create "*Org tmp*"))
  23316. (with-current-buffer buf
  23317. (erase-buffer)
  23318. (insert reg)
  23319. (org-mode)
  23320. (setq html (org-export-region-as-html
  23321. (point-min) (point-max) t 'string)))
  23322. (kill-buffer buf)))
  23323. (delete-region beg end)
  23324. (insert html)))
  23325. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23326. "Convert region from BEG to END in org-mode buffer to HTML.
  23327. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23328. contents, and only produce the region of converted text, useful for
  23329. cut-and-paste operations.
  23330. If BUFFER is a buffer or a string, use/create that buffer as a target
  23331. of the converted HTML. If BUFFER is the symbol `string', return the
  23332. produced HTML as a string and leave not buffer behind. For example,
  23333. a Lisp program could call this function in the following way:
  23334. (setq html (org-export-region-as-html beg end t 'string))
  23335. When called interactively, the output buffer is selected, and shown
  23336. in a window. A non-interactive call will only retunr the buffer."
  23337. (interactive "r\nP")
  23338. (when (interactive-p)
  23339. (setq buffer "*Org HTML Export*"))
  23340. (let ((transient-mark-mode t) (zmacs-regions t)
  23341. rtn)
  23342. (goto-char end)
  23343. (set-mark (point)) ;; to activate the region
  23344. (goto-char beg)
  23345. (setq rtn (org-export-as-html
  23346. nil nil nil
  23347. buffer body-only))
  23348. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23349. (if (and (interactive-p) (bufferp rtn))
  23350. (switch-to-buffer-other-window rtn)
  23351. rtn)))
  23352. (defvar html-table-tag nil) ; dynamically scoped into this.
  23353. (defun org-export-as-html (arg &optional hidden ext-plist
  23354. to-buffer body-only pub-dir)
  23355. "Export the outline as a pretty HTML file.
  23356. If there is an active region, export only the region. The prefix
  23357. ARG specifies how many levels of the outline should become
  23358. headlines. The default is 3. Lower levels will become bulleted
  23359. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23360. EXT-PLIST is a property list with external parameters overriding
  23361. org-mode's default settings, but still inferior to file-local
  23362. settings. When TO-BUFFER is non-nil, create a buffer with that
  23363. name and export to that buffer. If TO-BUFFER is the symbol
  23364. `string', don't leave any buffer behind but just return the
  23365. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23366. the file header and footer, simply return the content of
  23367. <body>...</body>, without even the body tags themselves. When
  23368. PUB-DIR is set, use this as the publishing directory."
  23369. (interactive "P")
  23370. ;; Make sure we have a file name when we need it.
  23371. (when (and (not (or to-buffer body-only))
  23372. (not buffer-file-name))
  23373. (if (buffer-base-buffer)
  23374. (org-set-local 'buffer-file-name
  23375. (with-current-buffer (buffer-base-buffer)
  23376. buffer-file-name))
  23377. (error "Need a file name to be able to export.")))
  23378. (message "Exporting...")
  23379. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23380. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23381. (setq-default org-done-keywords org-done-keywords)
  23382. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23383. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23384. ext-plist
  23385. (org-infile-export-plist)))
  23386. (style (plist-get opt-plist :style))
  23387. (html-extension (plist-get opt-plist :html-extension))
  23388. (link-validate (plist-get opt-plist :link-validation-function))
  23389. valid thetoc have-headings first-heading-pos
  23390. (odd org-odd-levels-only)
  23391. (region-p (org-region-active-p))
  23392. (subtree-p
  23393. (when region-p
  23394. (save-excursion
  23395. (goto-char (region-beginning))
  23396. (and (org-at-heading-p)
  23397. (>= (org-end-of-subtree t t) (region-end))))))
  23398. ;; The following two are dynamically scoped into other
  23399. ;; routines below.
  23400. (org-current-export-dir
  23401. (or pub-dir (org-export-directory :html opt-plist)))
  23402. (org-current-export-file buffer-file-name)
  23403. (level 0) (line "") (origline "") txt todo
  23404. (umax nil)
  23405. (umax-toc nil)
  23406. (filename (if to-buffer nil
  23407. (expand-file-name
  23408. (concat
  23409. (file-name-sans-extension
  23410. (or (and subtree-p
  23411. (org-entry-get (region-beginning)
  23412. "EXPORT_FILE_NAME" t))
  23413. (file-name-nondirectory buffer-file-name)))
  23414. "." html-extension)
  23415. (file-name-as-directory
  23416. (or pub-dir (org-export-directory :html opt-plist))))))
  23417. (current-dir (if buffer-file-name
  23418. (file-name-directory buffer-file-name)
  23419. default-directory))
  23420. (buffer (if to-buffer
  23421. (cond
  23422. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23423. (t (get-buffer-create to-buffer)))
  23424. (find-file-noselect filename)))
  23425. (org-levels-open (make-vector org-level-max nil))
  23426. (date (plist-get opt-plist :date))
  23427. (author (plist-get opt-plist :author))
  23428. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23429. (plist-get opt-plist :title)
  23430. (and (not
  23431. (plist-get opt-plist :skip-before-1st-heading))
  23432. (org-export-grab-title-from-buffer))
  23433. (and buffer-file-name
  23434. (file-name-sans-extension
  23435. (file-name-nondirectory buffer-file-name)))
  23436. "UNTITLED"))
  23437. (html-table-tag (plist-get opt-plist :html-table-tag))
  23438. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23439. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23440. (inquote nil)
  23441. (infixed nil)
  23442. (in-local-list nil)
  23443. (local-list-num nil)
  23444. (local-list-indent nil)
  23445. (llt org-plain-list-ordered-item-terminator)
  23446. (email (plist-get opt-plist :email))
  23447. (language (plist-get opt-plist :language))
  23448. (lang-words nil)
  23449. (target-alist nil) tg
  23450. (head-count 0) cnt
  23451. (start 0)
  23452. (coding-system (and (boundp 'buffer-file-coding-system)
  23453. buffer-file-coding-system))
  23454. (coding-system-for-write (or org-export-html-coding-system
  23455. coding-system))
  23456. (save-buffer-coding-system (or org-export-html-coding-system
  23457. coding-system))
  23458. (charset (and coding-system-for-write
  23459. (fboundp 'coding-system-get)
  23460. (coding-system-get coding-system-for-write
  23461. 'mime-charset)))
  23462. (region
  23463. (buffer-substring
  23464. (if region-p (region-beginning) (point-min))
  23465. (if region-p (region-end) (point-max))))
  23466. (lines
  23467. (org-split-string
  23468. (org-cleaned-string-for-export
  23469. region
  23470. :emph-multiline t
  23471. :for-html t
  23472. :skip-before-1st-heading
  23473. (plist-get opt-plist :skip-before-1st-heading)
  23474. :drawers (plist-get opt-plist :drawers)
  23475. :archived-trees
  23476. (plist-get opt-plist :archived-trees)
  23477. :add-text
  23478. (plist-get opt-plist :text)
  23479. :LaTeX-fragments
  23480. (plist-get opt-plist :LaTeX-fragments))
  23481. "[\r\n]"))
  23482. table-open type
  23483. table-buffer table-orig-buffer
  23484. ind start-is-num starter didclose
  23485. rpl path desc descp desc1 desc2 link
  23486. )
  23487. (let ((inhibit-read-only t))
  23488. (org-unmodified
  23489. (remove-text-properties (point-min) (point-max)
  23490. '(:org-license-to-kill t))))
  23491. (message "Exporting...")
  23492. (setq org-min-level (org-get-min-level lines))
  23493. (setq org-last-level org-min-level)
  23494. (org-init-section-numbers)
  23495. (cond
  23496. ((and date (string-match "%" date))
  23497. (setq date (format-time-string date (current-time))))
  23498. (date)
  23499. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23500. ;; Get the language-dependent settings
  23501. (setq lang-words (or (assoc language org-export-language-setup)
  23502. (assoc "en" org-export-language-setup)))
  23503. ;; Switch to the output buffer
  23504. (set-buffer buffer)
  23505. (let ((inhibit-read-only t)) (erase-buffer))
  23506. (fundamental-mode)
  23507. (and (fboundp 'set-buffer-file-coding-system)
  23508. (set-buffer-file-coding-system coding-system-for-write))
  23509. (let ((case-fold-search nil)
  23510. (org-odd-levels-only odd))
  23511. ;; create local variables for all options, to make sure all called
  23512. ;; functions get the correct information
  23513. (mapc (lambda (x)
  23514. (set (make-local-variable (cdr x))
  23515. (plist-get opt-plist (car x))))
  23516. org-export-plist-vars)
  23517. (setq umax (if arg (prefix-numeric-value arg)
  23518. org-export-headline-levels))
  23519. (setq umax-toc (if (integerp org-export-with-toc)
  23520. (min org-export-with-toc umax)
  23521. umax))
  23522. (unless body-only
  23523. ;; File header
  23524. (insert (format
  23525. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23526. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23527. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23528. lang=\"%s\" xml:lang=\"%s\">
  23529. <head>
  23530. <title>%s</title>
  23531. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23532. <meta name=\"generator\" content=\"Org-mode\"/>
  23533. <meta name=\"generated\" content=\"%s\"/>
  23534. <meta name=\"author\" content=\"%s\"/>
  23535. %s
  23536. </head><body>
  23537. "
  23538. language language (org-html-expand title)
  23539. (or charset "iso-8859-1") date author style))
  23540. (insert (or (plist-get opt-plist :preamble) ""))
  23541. (when (plist-get opt-plist :auto-preamble)
  23542. (if title (insert (format org-export-html-title-format
  23543. (org-html-expand title))))))
  23544. (if (and org-export-with-toc (not body-only))
  23545. (progn
  23546. (push (format "<h%d>%s</h%d>\n"
  23547. org-export-html-toplevel-hlevel
  23548. (nth 3 lang-words)
  23549. org-export-html-toplevel-hlevel)
  23550. thetoc)
  23551. (push "<ul>\n<li>" thetoc)
  23552. (setq lines
  23553. (mapcar '(lambda (line)
  23554. (if (string-match org-todo-line-regexp line)
  23555. ;; This is a headline
  23556. (progn
  23557. (setq have-headings t)
  23558. (setq level (- (match-end 1) (match-beginning 1))
  23559. level (org-tr-level level)
  23560. txt (save-match-data
  23561. (org-html-expand
  23562. (org-export-cleanup-toc-line
  23563. (match-string 3 line))))
  23564. todo
  23565. (or (and org-export-mark-todo-in-toc
  23566. (match-beginning 2)
  23567. (not (member (match-string 2 line)
  23568. org-done-keywords)))
  23569. ; TODO, not DONE
  23570. (and org-export-mark-todo-in-toc
  23571. (= level umax-toc)
  23572. (org-search-todo-below
  23573. line lines level))))
  23574. (if (string-match
  23575. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23576. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23577. (if (string-match quote-re0 txt)
  23578. (setq txt (replace-match "" t t txt)))
  23579. (if org-export-with-section-numbers
  23580. (setq txt (concat (org-section-number level)
  23581. " " txt)))
  23582. (if (<= level (max umax umax-toc))
  23583. (setq head-count (+ head-count 1)))
  23584. (if (<= level umax-toc)
  23585. (progn
  23586. (if (> level org-last-level)
  23587. (progn
  23588. (setq cnt (- level org-last-level))
  23589. (while (>= (setq cnt (1- cnt)) 0)
  23590. (push "\n<ul>\n<li>" thetoc))
  23591. (push "\n" thetoc)))
  23592. (if (< level org-last-level)
  23593. (progn
  23594. (setq cnt (- org-last-level level))
  23595. (while (>= (setq cnt (1- cnt)) 0)
  23596. (push "</li>\n</ul>" thetoc))
  23597. (push "\n" thetoc)))
  23598. ;; Check for targets
  23599. (while (string-match org-target-regexp line)
  23600. (setq tg (match-string 1 line)
  23601. line (replace-match
  23602. (concat "@<span class=\"target\">" tg "@</span> ")
  23603. t t line))
  23604. (push (cons (org-solidify-link-text tg)
  23605. (format "sec-%d" head-count))
  23606. target-alist))
  23607. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23608. (setq txt (replace-match "" t t txt)))
  23609. (push
  23610. (format
  23611. (if todo
  23612. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23613. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23614. head-count txt) thetoc)
  23615. (setq org-last-level level))
  23616. )))
  23617. line)
  23618. lines))
  23619. (while (> org-last-level (1- org-min-level))
  23620. (setq org-last-level (1- org-last-level))
  23621. (push "</li>\n</ul>\n" thetoc))
  23622. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23623. (setq head-count 0)
  23624. (org-init-section-numbers)
  23625. (while (setq line (pop lines) origline line)
  23626. (catch 'nextline
  23627. ;; end of quote section?
  23628. (when (and inquote (string-match "^\\*+ " line))
  23629. (insert "</pre>\n")
  23630. (setq inquote nil))
  23631. ;; inside a quote section?
  23632. (when inquote
  23633. (insert (org-html-protect line) "\n")
  23634. (throw 'nextline nil))
  23635. ;; verbatim lines
  23636. (when (and org-export-with-fixed-width
  23637. (string-match "^[ \t]*:\\(.*\\)" line))
  23638. (when (not infixed)
  23639. (setq infixed t)
  23640. (insert "<pre>\n"))
  23641. (insert (org-html-protect (match-string 1 line)) "\n")
  23642. (when (and lines
  23643. (not (string-match "^[ \t]*\\(:.*\\)"
  23644. (car lines))))
  23645. (setq infixed nil)
  23646. (insert "</pre>\n"))
  23647. (throw 'nextline nil))
  23648. ;; Protected HTML
  23649. (when (get-text-property 0 'org-protected line)
  23650. (let (par)
  23651. (when (re-search-backward
  23652. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23653. (setq par (match-string 1))
  23654. (replace-match "\\2\n"))
  23655. (insert line "\n")
  23656. (while (and lines
  23657. (or (= (length (car lines)) 0)
  23658. (get-text-property 0 'org-protected (car lines))))
  23659. (insert (pop lines) "\n"))
  23660. (and par (insert "<p>\n")))
  23661. (throw 'nextline nil))
  23662. ;; Horizontal line
  23663. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23664. (insert "\n<hr/>\n")
  23665. (throw 'nextline nil))
  23666. ;; make targets to anchors
  23667. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23668. (cond
  23669. ((match-end 2)
  23670. (setq line (replace-match
  23671. (concat "@<a name=\""
  23672. (org-solidify-link-text (match-string 1 line))
  23673. "\">\\nbsp@</a>")
  23674. t t line)))
  23675. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23676. (setq line (replace-match
  23677. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23678. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23679. t t line)))
  23680. (t
  23681. (setq line (replace-match
  23682. (concat "@<a name=\""
  23683. (org-solidify-link-text (match-string 1 line))
  23684. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23685. t t line)))))
  23686. (setq line (org-html-handle-time-stamps line))
  23687. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23688. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23689. ;; Also handle sub_superscripts and checkboxes
  23690. (or (string-match org-table-hline-regexp line)
  23691. (setq line (org-html-expand line)))
  23692. ;; Format the links
  23693. (setq start 0)
  23694. (while (string-match org-bracket-link-analytic-regexp line start)
  23695. (setq start (match-beginning 0))
  23696. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23697. (setq path (match-string 3 line))
  23698. (setq desc1 (if (match-end 5) (match-string 5 line))
  23699. desc2 (if (match-end 2) (concat type ":" path) path)
  23700. descp (and desc1 (not (equal desc1 desc2)))
  23701. desc (or desc1 desc2))
  23702. ;; Make an image out of the description if that is so wanted
  23703. (when (and descp (org-file-image-p desc))
  23704. (save-match-data
  23705. (if (string-match "^file:" desc)
  23706. (setq desc (substring desc (match-end 0)))))
  23707. (setq desc (concat "<img src=\"" desc "\"/>")))
  23708. ;; FIXME: do we need to unescape here somewhere?
  23709. (cond
  23710. ((equal type "internal")
  23711. (setq rpl
  23712. (concat
  23713. "<a href=\"#"
  23714. (org-solidify-link-text
  23715. (save-match-data (org-link-unescape path)) target-alist)
  23716. "\">" desc "</a>")))
  23717. ((member type '("http" "https"))
  23718. ;; standard URL, just check if we need to inline an image
  23719. (if (and (or (eq t org-export-html-inline-images)
  23720. (and org-export-html-inline-images (not descp)))
  23721. (org-file-image-p path))
  23722. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23723. (setq link (concat type ":" path))
  23724. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23725. ((member type '("ftp" "mailto" "news"))
  23726. ;; standard URL
  23727. (setq link (concat type ":" path))
  23728. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23729. ((string= type "file")
  23730. ;; FILE link
  23731. (let* ((filename path)
  23732. (abs-p (file-name-absolute-p filename))
  23733. thefile file-is-image-p search)
  23734. (save-match-data
  23735. (if (string-match "::\\(.*\\)" filename)
  23736. (setq search (match-string 1 filename)
  23737. filename (replace-match "" t nil filename)))
  23738. (setq valid
  23739. (if (functionp link-validate)
  23740. (funcall link-validate filename current-dir)
  23741. t))
  23742. (setq file-is-image-p (org-file-image-p filename))
  23743. (setq thefile (if abs-p (expand-file-name filename) filename))
  23744. (when (and org-export-html-link-org-files-as-html
  23745. (string-match "\\.org$" thefile))
  23746. (setq thefile (concat (substring thefile 0
  23747. (match-beginning 0))
  23748. "." html-extension))
  23749. (if (and search
  23750. ;; make sure this is can be used as target search
  23751. (not (string-match "^[0-9]*$" search))
  23752. (not (string-match "^\\*" search))
  23753. (not (string-match "^/.*/$" search)))
  23754. (setq thefile (concat thefile "#"
  23755. (org-solidify-link-text
  23756. (org-link-unescape search)))))
  23757. (when (string-match "^file:" desc)
  23758. (setq desc (replace-match "" t t desc))
  23759. (if (string-match "\\.org$" desc)
  23760. (setq desc (replace-match "" t t desc))))))
  23761. (setq rpl (if (and file-is-image-p
  23762. (or (eq t org-export-html-inline-images)
  23763. (and org-export-html-inline-images
  23764. (not descp))))
  23765. (concat "<img src=\"" thefile "\"/>")
  23766. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23767. (if (not valid) (setq rpl desc))))
  23768. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23769. (setq rpl (concat "<i>&lt;" type ":"
  23770. (save-match-data (org-link-unescape path))
  23771. "&gt;</i>"))))
  23772. (setq line (replace-match rpl t t line)
  23773. start (+ start (length rpl))))
  23774. ;; TODO items
  23775. (if (and (string-match org-todo-line-regexp line)
  23776. (match-beginning 2))
  23777. (setq line
  23778. (concat (substring line 0 (match-beginning 2))
  23779. "<span class=\""
  23780. (if (member (match-string 2 line)
  23781. org-done-keywords)
  23782. "done" "todo")
  23783. "\">" (match-string 2 line)
  23784. "</span>" (substring line (match-end 2)))))
  23785. ;; Does this contain a reference to a footnote?
  23786. (when org-export-with-footnotes
  23787. (setq start 0)
  23788. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23789. (if (get-text-property (match-beginning 2) 'org-protected line)
  23790. (setq start (match-end 2))
  23791. (let ((n (match-string 2 line)))
  23792. (setq line
  23793. (replace-match
  23794. (format
  23795. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23796. (match-string 1 line) n n n)
  23797. t t line))))))
  23798. (cond
  23799. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23800. ;; This is a headline
  23801. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23802. txt (match-string 2 line))
  23803. (if (string-match quote-re0 txt)
  23804. (setq txt (replace-match "" t t txt)))
  23805. (if (<= level (max umax umax-toc))
  23806. (setq head-count (+ head-count 1)))
  23807. (when in-local-list
  23808. ;; Close any local lists before inserting a new header line
  23809. (while local-list-num
  23810. (org-close-li)
  23811. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23812. (pop local-list-num))
  23813. (setq local-list-indent nil
  23814. in-local-list nil))
  23815. (setq first-heading-pos (or first-heading-pos (point)))
  23816. (org-html-level-start level txt umax
  23817. (and org-export-with-toc (<= level umax))
  23818. head-count)
  23819. ;; QUOTES
  23820. (when (string-match quote-re line)
  23821. (insert "<pre>")
  23822. (setq inquote t)))
  23823. ((and org-export-with-tables
  23824. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23825. (if (not table-open)
  23826. ;; New table starts
  23827. (setq table-open t table-buffer nil table-orig-buffer nil))
  23828. ;; Accumulate lines
  23829. (setq table-buffer (cons line table-buffer)
  23830. table-orig-buffer (cons origline table-orig-buffer))
  23831. (when (or (not lines)
  23832. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23833. (car lines))))
  23834. (setq table-open nil
  23835. table-buffer (nreverse table-buffer)
  23836. table-orig-buffer (nreverse table-orig-buffer))
  23837. (org-close-par-maybe)
  23838. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23839. (t
  23840. ;; Normal lines
  23841. (when (string-match
  23842. (cond
  23843. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23844. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23845. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23846. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23847. line)
  23848. (setq ind (org-get-string-indentation line)
  23849. start-is-num (match-beginning 4)
  23850. starter (if (match-beginning 2)
  23851. (substring (match-string 2 line) 0 -1))
  23852. line (substring line (match-beginning 5)))
  23853. (unless (string-match "[^ \t]" line)
  23854. ;; empty line. Pretend indentation is large.
  23855. (setq ind (if org-empty-line-terminates-plain-lists
  23856. 0
  23857. (1+ (or (car local-list-indent) 1)))))
  23858. (setq didclose nil)
  23859. (while (and in-local-list
  23860. (or (and (= ind (car local-list-indent))
  23861. (not starter))
  23862. (< ind (car local-list-indent))))
  23863. (setq didclose t)
  23864. (org-close-li)
  23865. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23866. (pop local-list-num) (pop local-list-indent)
  23867. (setq in-local-list local-list-indent))
  23868. (cond
  23869. ((and starter
  23870. (or (not in-local-list)
  23871. (> ind (car local-list-indent))))
  23872. ;; Start new (level of) list
  23873. (org-close-par-maybe)
  23874. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23875. (push start-is-num local-list-num)
  23876. (push ind local-list-indent)
  23877. (setq in-local-list t))
  23878. (starter
  23879. ;; continue current list
  23880. (org-close-li)
  23881. (insert "<li>\n"))
  23882. (didclose
  23883. ;; we did close a list, normal text follows: need <p>
  23884. (org-open-par)))
  23885. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23886. (setq line
  23887. (replace-match
  23888. (if (equal (match-string 1 line) "X")
  23889. "<b>[X]</b>"
  23890. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23891. t t line))))
  23892. ;; Empty lines start a new paragraph. If hand-formatted lists
  23893. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23894. ;; also start a new paragraph.
  23895. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23896. ;; Is this the start of a footnote?
  23897. (when org-export-with-footnotes
  23898. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23899. (org-close-par-maybe)
  23900. (let ((n (match-string 1 line)))
  23901. (setq line (replace-match
  23902. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23903. ;; Check if the line break needs to be conserved
  23904. (cond
  23905. ((string-match "\\\\\\\\[ \t]*$" line)
  23906. (setq line (replace-match "<br/>" t t line)))
  23907. (org-export-preserve-breaks
  23908. (setq line (concat line "<br/>"))))
  23909. (insert line "\n")))))
  23910. ;; Properly close all local lists and other lists
  23911. (when inquote (insert "</pre>\n"))
  23912. (when in-local-list
  23913. ;; Close any local lists before inserting a new header line
  23914. (while local-list-num
  23915. (org-close-li)
  23916. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23917. (pop local-list-num))
  23918. (setq local-list-indent nil
  23919. in-local-list nil))
  23920. (org-html-level-start 1 nil umax
  23921. (and org-export-with-toc (<= level umax))
  23922. head-count)
  23923. (unless body-only
  23924. (when (plist-get opt-plist :auto-postamble)
  23925. (insert "<div id=\"postamble\">")
  23926. (when (and org-export-author-info author)
  23927. (insert "<p class=\"author\"> "
  23928. (nth 1 lang-words) ": " author "\n")
  23929. (when email
  23930. (if (listp (split-string email ",+ *"))
  23931. (mapc (lambda(e)
  23932. (insert "<a href=\"mailto:" e "\">&lt;"
  23933. e "&gt;</a>\n"))
  23934. (split-string email ",+ *"))
  23935. (insert "<a href=\"mailto:" email "\">&lt;"
  23936. email "&gt;</a>\n")))
  23937. (insert "</p>\n"))
  23938. (when (and date org-export-time-stamp-file)
  23939. (insert "<p class=\"date\"> "
  23940. (nth 2 lang-words) ": "
  23941. date "</p>\n"))
  23942. (insert "</div>"))
  23943. (if org-export-html-with-timestamp
  23944. (insert org-export-html-html-helper-timestamp))
  23945. (insert (or (plist-get opt-plist :postamble) ""))
  23946. (insert "</body>\n</html>\n"))
  23947. (normal-mode)
  23948. (if (eq major-mode default-major-mode) (html-mode))
  23949. ;; insert the table of contents
  23950. (goto-char (point-min))
  23951. (when thetoc
  23952. (if (or (re-search-forward
  23953. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23954. (re-search-forward
  23955. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23956. (progn
  23957. (goto-char (match-beginning 0))
  23958. (replace-match ""))
  23959. (goto-char first-heading-pos)
  23960. (when (looking-at "\\s-*</p>")
  23961. (goto-char (match-end 0))
  23962. (insert "\n")))
  23963. (insert "<div id=\"table-of-contents\">\n")
  23964. (mapc 'insert thetoc)
  23965. (insert "</div>\n"))
  23966. ;; remove empty paragraphs and lists
  23967. (goto-char (point-min))
  23968. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23969. (replace-match ""))
  23970. (goto-char (point-min))
  23971. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23972. (replace-match ""))
  23973. (goto-char (point-min))
  23974. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23975. (replace-match ""))
  23976. ;; Convert whitespace place holders
  23977. (goto-char (point-min))
  23978. (let (beg end n)
  23979. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23980. (setq n (get-text-property beg 'org-whitespace)
  23981. end (next-single-property-change beg 'org-whitespace))
  23982. (goto-char beg)
  23983. (delete-region beg end)
  23984. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23985. (make-string n ?x)))))
  23986. (or to-buffer (progn (save-buffer) (kill-buffer (current-buffer))))
  23987. (goto-char (point-min))
  23988. (message "Exporting... done")
  23989. (if (eq to-buffer 'string)
  23990. (prog1 (buffer-substring (point-min) (point-max))
  23991. (kill-buffer (current-buffer)))
  23992. (current-buffer)))))
  23993. (defvar org-table-colgroup-info nil)
  23994. (defun org-format-table-ascii (lines)
  23995. "Format a table for ascii export."
  23996. (if (stringp lines)
  23997. (setq lines (org-split-string lines "\n")))
  23998. (if (not (string-match "^[ \t]*|" (car lines)))
  23999. ;; Table made by table.el - test for spanning
  24000. lines
  24001. ;; A normal org table
  24002. ;; Get rid of hlines at beginning and end
  24003. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24004. (setq lines (nreverse lines))
  24005. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24006. (setq lines (nreverse lines))
  24007. (when org-export-table-remove-special-lines
  24008. ;; Check if the table has a marking column. If yes remove the
  24009. ;; column and the special lines
  24010. (setq lines (org-table-clean-before-export lines)))
  24011. ;; Get rid of the vertical lines except for grouping
  24012. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  24013. rtn line vl1 start)
  24014. (while (setq line (pop lines))
  24015. (if (string-match org-table-hline-regexp line)
  24016. (and (string-match "|\\(.*\\)|" line)
  24017. (setq line (replace-match " \\1" t nil line)))
  24018. (setq start 0 vl1 vl)
  24019. (while (string-match "|" line start)
  24020. (setq start (match-end 0))
  24021. (or (pop vl1) (setq line (replace-match " " t t line)))))
  24022. (push line rtn))
  24023. (nreverse rtn))))
  24024. (defun org-colgroup-info-to-vline-list (info)
  24025. (let (vl new last)
  24026. (while info
  24027. (setq last new new (pop info))
  24028. (if (or (memq last '(:end :startend))
  24029. (memq new '(:start :startend)))
  24030. (push t vl)
  24031. (push nil vl)))
  24032. (setq vl (nreverse vl))
  24033. (and vl (setcar vl nil))
  24034. vl))
  24035. (defun org-format-table-html (lines olines)
  24036. "Find out which HTML converter to use and return the HTML code."
  24037. (if (stringp lines)
  24038. (setq lines (org-split-string lines "\n")))
  24039. (if (string-match "^[ \t]*|" (car lines))
  24040. ;; A normal org table
  24041. (org-format-org-table-html lines)
  24042. ;; Table made by table.el - test for spanning
  24043. (let* ((hlines (delq nil (mapcar
  24044. (lambda (x)
  24045. (if (string-match "^[ \t]*\\+-" x) x
  24046. nil))
  24047. lines)))
  24048. (first (car hlines))
  24049. (ll (and (string-match "\\S-+" first)
  24050. (match-string 0 first)))
  24051. (re (concat "^[ \t]*" (regexp-quote ll)))
  24052. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24053. hlines))))
  24054. (if (and (not spanning)
  24055. (not org-export-prefer-native-exporter-for-tables))
  24056. ;; We can use my own converter with HTML conversions
  24057. (org-format-table-table-html lines)
  24058. ;; Need to use the code generator in table.el, with the original text.
  24059. (org-format-table-table-html-using-table-generate-source olines)))))
  24060. (defun org-format-org-table-html (lines &optional splice)
  24061. "Format a table into HTML."
  24062. ;; Get rid of hlines at beginning and end
  24063. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24064. (setq lines (nreverse lines))
  24065. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24066. (setq lines (nreverse lines))
  24067. (when org-export-table-remove-special-lines
  24068. ;; Check if the table has a marking column. If yes remove the
  24069. ;; column and the special lines
  24070. (setq lines (org-table-clean-before-export lines)))
  24071. (let ((head (and org-export-highlight-first-table-line
  24072. (delq nil (mapcar
  24073. (lambda (x) (string-match "^[ \t]*|-" x))
  24074. (cdr lines)))))
  24075. (nlines 0) fnum i
  24076. tbopen line fields html gr colgropen)
  24077. (if splice (setq head nil))
  24078. (unless splice (push (if head "<thead>" "<tbody>") html))
  24079. (setq tbopen t)
  24080. (while (setq line (pop lines))
  24081. (catch 'next-line
  24082. (if (string-match "^[ \t]*|-" line)
  24083. (progn
  24084. (unless splice
  24085. (push (if head "</thead>" "</tbody>") html)
  24086. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24087. (setq head nil) ;; head ends here, first time around
  24088. ;; ignore this line
  24089. (throw 'next-line t)))
  24090. ;; Break the line into fields
  24091. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24092. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24093. (setq nlines (1+ nlines) i -1)
  24094. (push (concat "<tr>"
  24095. (mapconcat
  24096. (lambda (x)
  24097. (setq i (1+ i))
  24098. (if (and (< i nlines)
  24099. (string-match org-table-number-regexp x))
  24100. (incf (aref fnum i)))
  24101. (if head
  24102. (concat (car org-export-table-header-tags) x
  24103. (cdr org-export-table-header-tags))
  24104. (concat (car org-export-table-data-tags) x
  24105. (cdr org-export-table-data-tags))))
  24106. fields "")
  24107. "</tr>")
  24108. html)))
  24109. (unless splice (if tbopen (push "</tbody>" html)))
  24110. (unless splice (push "</table>\n" html))
  24111. (setq html (nreverse html))
  24112. (unless splice
  24113. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24114. (push (mapconcat
  24115. (lambda (x)
  24116. (setq gr (pop org-table-colgroup-info))
  24117. (format "%s<col align=\"%s\"></col>%s"
  24118. (if (memq gr '(:start :startend))
  24119. (prog1
  24120. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24121. (setq colgropen t))
  24122. "")
  24123. (if (> (/ (float x) nlines) org-table-number-fraction)
  24124. "right" "left")
  24125. (if (memq gr '(:end :startend))
  24126. (progn (setq colgropen nil) "</colgroup>")
  24127. "")))
  24128. fnum "")
  24129. html)
  24130. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24131. (push html-table-tag html))
  24132. (concat (mapconcat 'identity html "\n") "\n")))
  24133. (defun org-table-clean-before-export (lines)
  24134. "Check if the table has a marking column.
  24135. If yes remove the column and the special lines."
  24136. (setq org-table-colgroup-info nil)
  24137. (if (memq nil
  24138. (mapcar
  24139. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24140. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24141. lines))
  24142. (progn
  24143. (setq org-table-clean-did-remove-column nil)
  24144. (delq nil
  24145. (mapcar
  24146. (lambda (x)
  24147. (cond
  24148. ((string-match "^[ \t]*| */ *|" x)
  24149. (setq org-table-colgroup-info
  24150. (mapcar (lambda (x)
  24151. (cond ((member x '("<" "&lt;")) :start)
  24152. ((member x '(">" "&gt;")) :end)
  24153. ((member x '("<>" "&lt;&gt;")) :startend)
  24154. (t nil)))
  24155. (org-split-string x "[ \t]*|[ \t]*")))
  24156. nil)
  24157. (t x)))
  24158. lines)))
  24159. (setq org-table-clean-did-remove-column t)
  24160. (delq nil
  24161. (mapcar
  24162. (lambda (x)
  24163. (cond
  24164. ((string-match "^[ \t]*| */ *|" x)
  24165. (setq org-table-colgroup-info
  24166. (mapcar (lambda (x)
  24167. (cond ((member x '("<" "&lt;")) :start)
  24168. ((member x '(">" "&gt;")) :end)
  24169. ((member x '("<>" "&lt;&gt;")) :startend)
  24170. (t nil)))
  24171. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24172. nil)
  24173. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24174. nil) ; ignore this line
  24175. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24176. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24177. ;; remove the first column
  24178. (replace-match "\\1|" t nil x))))
  24179. lines))))
  24180. (defun org-format-table-table-html (lines)
  24181. "Format a table generated by table.el into HTML.
  24182. This conversion does *not* use `table-generate-source' from table.el.
  24183. This has the advantage that Org-mode's HTML conversions can be used.
  24184. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24185. (let (line field-buffer
  24186. (head org-export-highlight-first-table-line)
  24187. fields html empty)
  24188. (setq html (concat html-table-tag "\n"))
  24189. (while (setq line (pop lines))
  24190. (setq empty "&nbsp;")
  24191. (catch 'next-line
  24192. (if (string-match "^[ \t]*\\+-" line)
  24193. (progn
  24194. (if field-buffer
  24195. (progn
  24196. (setq
  24197. html
  24198. (concat
  24199. html
  24200. "<tr>"
  24201. (mapconcat
  24202. (lambda (x)
  24203. (if (equal x "") (setq x empty))
  24204. (if head
  24205. (concat (car org-export-table-header-tags) x
  24206. (cdr org-export-table-header-tags))
  24207. (concat (car org-export-table-data-tags) x
  24208. (cdr org-export-table-data-tags))))
  24209. field-buffer "\n")
  24210. "</tr>\n"))
  24211. (setq head nil)
  24212. (setq field-buffer nil)))
  24213. ;; Ignore this line
  24214. (throw 'next-line t)))
  24215. ;; Break the line into fields and store the fields
  24216. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24217. (if field-buffer
  24218. (setq field-buffer (mapcar
  24219. (lambda (x)
  24220. (concat x "<br/>" (pop fields)))
  24221. field-buffer))
  24222. (setq field-buffer fields))))
  24223. (setq html (concat html "</table>\n"))
  24224. html))
  24225. (defun org-format-table-table-html-using-table-generate-source (lines)
  24226. "Format a table into html, using `table-generate-source' from table.el.
  24227. This has the advantage that cell- or row-spanning is allowed.
  24228. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24229. (require 'table)
  24230. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24231. (erase-buffer)
  24232. (insert (mapconcat 'identity lines "\n"))
  24233. (goto-char (point-min))
  24234. (if (not (re-search-forward "|[^+]" nil t))
  24235. (error "Error processing table"))
  24236. (table-recognize-table)
  24237. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24238. (table-generate-source 'html " org-tmp2 ")
  24239. (set-buffer " org-tmp2 ")
  24240. (buffer-substring (point-min) (point-max))))
  24241. (defun org-html-handle-time-stamps (s)
  24242. "Format time stamps in string S, or remove them."
  24243. (catch 'exit
  24244. (let (r b)
  24245. (while (string-match org-maybe-keyword-time-regexp s)
  24246. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24247. ;; never export CLOCK
  24248. (throw 'exit ""))
  24249. (or b (setq b (substring s 0 (match-beginning 0))))
  24250. (if (not org-export-with-timestamps)
  24251. (setq r (concat r (substring s 0 (match-beginning 0)))
  24252. s (substring s (match-end 0)))
  24253. (setq r (concat
  24254. r (substring s 0 (match-beginning 0))
  24255. (if (match-end 1)
  24256. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24257. (match-string 1 s)))
  24258. (format " @<span class=\"timestamp\">%s@</span>"
  24259. (substring
  24260. (org-translate-time (match-string 3 s)) 1 -1)))
  24261. s (substring s (match-end 0)))))
  24262. ;; Line break if line started and ended with time stamp stuff
  24263. (if (not r)
  24264. s
  24265. (setq r (concat r s))
  24266. (unless (string-match "\\S-" (concat b s))
  24267. (setq r (concat r "@<br/>")))
  24268. r))))
  24269. (defun org-html-protect (s)
  24270. ;; convert & to &amp;, < to &lt; and > to &gt;
  24271. (let ((start 0))
  24272. (while (string-match "&" s start)
  24273. (setq s (replace-match "&amp;" t t s)
  24274. start (1+ (match-beginning 0))))
  24275. (while (string-match "<" s)
  24276. (setq s (replace-match "&lt;" t t s)))
  24277. (while (string-match ">" s)
  24278. (setq s (replace-match "&gt;" t t s))))
  24279. s)
  24280. (defun org-export-cleanup-toc-line (s)
  24281. "Remove tags and time staps from lines going into the toc."
  24282. (when (memq org-export-with-tags '(not-in-toc nil))
  24283. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24284. (setq s (replace-match "" t t s))))
  24285. (when org-export-remove-timestamps-from-toc
  24286. (while (string-match org-maybe-keyword-time-regexp s)
  24287. (setq s (replace-match "" t t s))))
  24288. (while (string-match org-bracket-link-regexp s)
  24289. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24290. t t s)))
  24291. s)
  24292. (defun org-html-expand (string)
  24293. "Prepare STRING for HTML export. Applies all active conversions.
  24294. If there are links in the string, don't modify these."
  24295. (let* ((re (concat org-bracket-link-regexp "\\|"
  24296. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24297. m s l res)
  24298. (while (setq m (string-match re string))
  24299. (setq s (substring string 0 m)
  24300. l (match-string 0 string)
  24301. string (substring string (match-end 0)))
  24302. (push (org-html-do-expand s) res)
  24303. (push l res))
  24304. (push (org-html-do-expand string) res)
  24305. (apply 'concat (nreverse res))))
  24306. (defun org-html-do-expand (s)
  24307. "Apply all active conversions to translate special ASCII to HTML."
  24308. (setq s (org-html-protect s))
  24309. (if org-export-html-expand
  24310. (let ((start 0))
  24311. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24312. (setq s (replace-match "<\\1>" t nil s)))))
  24313. (if org-export-with-emphasize
  24314. (setq s (org-export-html-convert-emphasize s)))
  24315. (if org-export-with-special-strings
  24316. (setq s (org-export-html-convert-special-strings s)))
  24317. (if org-export-with-sub-superscripts
  24318. (setq s (org-export-html-convert-sub-super s)))
  24319. (if org-export-with-TeX-macros
  24320. (let ((start 0) wd ass)
  24321. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24322. (if (get-text-property (match-beginning 0) 'org-protected s)
  24323. (setq start (match-end 0))
  24324. (setq wd (match-string 1 s))
  24325. (if (setq ass (assoc wd org-html-entities))
  24326. (setq s (replace-match (or (cdr ass)
  24327. (concat "&" (car ass) ";"))
  24328. t t s))
  24329. (setq start (+ start (length wd))))))))
  24330. s)
  24331. (defun org-create-multibrace-regexp (left right n)
  24332. "Create a regular expression which will match a balanced sexp.
  24333. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24334. as single character strings.
  24335. The regexp returned will match the entire expression including the
  24336. delimiters. It will also define a single group which contains the
  24337. match except for the outermost delimiters. The maximum depth of
  24338. stacked delimiters is N. Escaping delimiters is not possible."
  24339. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24340. (or "\\|")
  24341. (re nothing)
  24342. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24343. (while (> n 1)
  24344. (setq n (1- n)
  24345. re (concat re or next)
  24346. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24347. (concat left "\\(" re "\\)" right)))
  24348. (defvar org-match-substring-regexp
  24349. (concat
  24350. "\\([^\\]\\)\\([_^]\\)\\("
  24351. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24352. "\\|"
  24353. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24354. "\\|"
  24355. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24356. "The regular expression matching a sub- or superscript.")
  24357. (defvar org-match-substring-with-braces-regexp
  24358. (concat
  24359. "\\([^\\]\\)\\([_^]\\)\\("
  24360. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24361. "\\)")
  24362. "The regular expression matching a sub- or superscript, forcing braces.")
  24363. (defconst org-export-html-special-string-regexps
  24364. '(("\\\\-" . "&shy;")
  24365. ("---\\([^-]\\)" . "&mdash;\\1")
  24366. ("--\\([^-]\\)" . "&ndash;\\1")
  24367. ("\\.\\.\\." . "&hellip;"))
  24368. "Regular expressions for special string conversion.")
  24369. (defun org-export-html-convert-special-strings (string)
  24370. "Convert special characters in STRING to HTML."
  24371. (let ((all org-export-html-special-string-regexps)
  24372. e a re rpl start)
  24373. (while (setq a (pop all))
  24374. (setq re (car a) rpl (cdr a) start 0)
  24375. (while (string-match re string start)
  24376. (if (get-text-property (match-beginning 0) 'org-protected string)
  24377. (setq start (match-end 0))
  24378. (setq string (replace-match rpl t nil string)))))
  24379. string))
  24380. (defun org-export-html-convert-sub-super (string)
  24381. "Convert sub- and superscripts in STRING to HTML."
  24382. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24383. (while (string-match org-match-substring-regexp string s)
  24384. (cond
  24385. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24386. ((get-text-property (match-beginning 2) 'org-protected string)
  24387. (setq s (match-end 2)))
  24388. (t
  24389. (setq s (match-end 1)
  24390. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24391. c (or (match-string 8 string)
  24392. (match-string 6 string)
  24393. (match-string 5 string))
  24394. string (replace-match
  24395. (concat (match-string 1 string)
  24396. "<" key ">" c "</" key ">")
  24397. t t string)))))
  24398. (while (string-match "\\\\\\([_^]\\)" string)
  24399. (setq string (replace-match (match-string 1 string) t t string)))
  24400. string))
  24401. (defun org-export-html-convert-emphasize (string)
  24402. "Apply emphasis."
  24403. (let ((s 0) rpl)
  24404. (while (string-match org-emph-re string s)
  24405. (if (not (equal
  24406. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24407. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24408. (setq s (match-beginning 0)
  24409. rpl
  24410. (concat
  24411. (match-string 1 string)
  24412. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24413. (match-string 4 string)
  24414. (nth 3 (assoc (match-string 3 string)
  24415. org-emphasis-alist))
  24416. (match-string 5 string))
  24417. string (replace-match rpl t t string)
  24418. s (+ s (- (length rpl) 2)))
  24419. (setq s (1+ s))))
  24420. string))
  24421. (defvar org-par-open nil)
  24422. (defun org-open-par ()
  24423. "Insert <p>, but first close previous paragraph if any."
  24424. (org-close-par-maybe)
  24425. (insert "\n<p>")
  24426. (setq org-par-open t))
  24427. (defun org-close-par-maybe ()
  24428. "Close paragraph if there is one open."
  24429. (when org-par-open
  24430. (insert "</p>")
  24431. (setq org-par-open nil)))
  24432. (defun org-close-li ()
  24433. "Close <li> if necessary."
  24434. (org-close-par-maybe)
  24435. (insert "</li>\n"))
  24436. (defvar body-only) ; dynamically scoped into this.
  24437. (defun org-html-level-start (level title umax with-toc head-count)
  24438. "Insert a new level in HTML export.
  24439. When TITLE is nil, just close all open levels."
  24440. (org-close-par-maybe)
  24441. (let ((l org-level-max))
  24442. (while (>= l level)
  24443. (if (aref org-levels-open (1- l))
  24444. (progn
  24445. (org-html-level-close l umax)
  24446. (aset org-levels-open (1- l) nil)))
  24447. (setq l (1- l)))
  24448. (when title
  24449. ;; If title is nil, this means this function is called to close
  24450. ;; all levels, so the rest is done only if title is given
  24451. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24452. (setq title (replace-match
  24453. (if org-export-with-tags
  24454. (save-match-data
  24455. (concat
  24456. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24457. (mapconcat 'identity (org-split-string
  24458. (match-string 1 title) ":")
  24459. "&nbsp;")
  24460. "</span>"))
  24461. "")
  24462. t t title)))
  24463. (if (> level umax)
  24464. (progn
  24465. (if (aref org-levels-open (1- level))
  24466. (progn
  24467. (org-close-li)
  24468. (insert "<li>" title "<br/>\n"))
  24469. (aset org-levels-open (1- level) t)
  24470. (org-close-par-maybe)
  24471. (insert "<ul>\n<li>" title "<br/>\n")))
  24472. (aset org-levels-open (1- level) t)
  24473. (if (and org-export-with-section-numbers (not body-only))
  24474. (setq title (concat (org-section-number level) " " title)))
  24475. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24476. (if with-toc
  24477. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24478. level level head-count title level))
  24479. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24480. (org-open-par)))))
  24481. (defun org-html-level-close (level max-outline-level)
  24482. "Terminate one level in HTML export."
  24483. (if (<= level max-outline-level)
  24484. (insert "</div>\n")
  24485. (org-close-li)
  24486. (insert "</ul>\n")))
  24487. ;;; iCalendar export
  24488. ;;;###autoload
  24489. (defun org-export-icalendar-this-file ()
  24490. "Export current file as an iCalendar file.
  24491. The iCalendar file will be located in the same directory as the Org-mode
  24492. file, but with extension `.ics'."
  24493. (interactive)
  24494. (org-export-icalendar nil buffer-file-name))
  24495. ;;;###autoload
  24496. (defun org-export-icalendar-all-agenda-files ()
  24497. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24498. Each iCalendar file will be located in the same directory as the Org-mode
  24499. file, but with extension `.ics'."
  24500. (interactive)
  24501. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24502. ;;;###autoload
  24503. (defun org-export-icalendar-combine-agenda-files ()
  24504. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24505. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24506. (interactive)
  24507. (apply 'org-export-icalendar t (org-agenda-files t)))
  24508. (defun org-export-icalendar (combine &rest files)
  24509. "Create iCalendar files for all elements of FILES.
  24510. If COMBINE is non-nil, combine all calendar entries into a single large
  24511. file and store it under the name `org-combined-agenda-icalendar-file'."
  24512. (save-excursion
  24513. (org-prepare-agenda-buffers files)
  24514. (let* ((dir (org-export-directory
  24515. :ical (list :publishing-directory
  24516. org-export-publishing-directory)))
  24517. file ical-file ical-buffer category started org-agenda-new-buffers)
  24518. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24519. (when combine
  24520. (setq ical-file
  24521. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24522. org-combined-agenda-icalendar-file
  24523. (expand-file-name org-combined-agenda-icalendar-file dir))
  24524. ical-buffer (org-get-agenda-file-buffer ical-file))
  24525. (set-buffer ical-buffer) (erase-buffer))
  24526. (while (setq file (pop files))
  24527. (catch 'nextfile
  24528. (org-check-agenda-file file)
  24529. (set-buffer (org-get-agenda-file-buffer file))
  24530. (unless combine
  24531. (setq ical-file (concat (file-name-as-directory dir)
  24532. (file-name-sans-extension
  24533. (file-name-nondirectory buffer-file-name))
  24534. ".ics"))
  24535. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24536. (with-current-buffer ical-buffer (erase-buffer)))
  24537. (setq category (or org-category
  24538. (file-name-sans-extension
  24539. (file-name-nondirectory buffer-file-name))))
  24540. (if (symbolp category) (setq category (symbol-name category)))
  24541. (let ((standard-output ical-buffer))
  24542. (if combine
  24543. (and (not started) (setq started t)
  24544. (org-start-icalendar-file org-icalendar-combined-name))
  24545. (org-start-icalendar-file category))
  24546. (org-print-icalendar-entries combine)
  24547. (when (or (and combine (not files)) (not combine))
  24548. (org-finish-icalendar-file)
  24549. (set-buffer ical-buffer)
  24550. (save-buffer)
  24551. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24552. (org-release-buffers org-agenda-new-buffers))))
  24553. (defvar org-after-save-iCalendar-file-hook nil
  24554. "Hook run after an iCalendar file has been saved.
  24555. The iCalendar buffer is still current when this hook is run.
  24556. A good way to use this is to tell a desktop calenndar application to re-read
  24557. the iCalendar file.")
  24558. (defun org-print-icalendar-entries (&optional combine)
  24559. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24560. When COMBINE is non nil, add the category to each line."
  24561. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24562. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24563. (dts (org-ical-ts-to-string
  24564. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24565. "DTSTART"))
  24566. hd ts ts2 state status (inc t) pos b sexp rrule
  24567. scheduledp deadlinep tmp pri category entry location summary desc
  24568. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24569. (org-refresh-category-properties)
  24570. (save-excursion
  24571. (goto-char (point-min))
  24572. (while (re-search-forward re1 nil t)
  24573. (catch :skip
  24574. (org-agenda-skip)
  24575. (setq pos (match-beginning 0)
  24576. ts (match-string 0)
  24577. inc t
  24578. hd (org-get-heading)
  24579. summary (org-icalendar-cleanup-string
  24580. (org-entry-get nil "SUMMARY"))
  24581. desc (org-icalendar-cleanup-string
  24582. (or (org-entry-get nil "DESCRIPTION")
  24583. (and org-icalendar-include-body (org-get-entry)))
  24584. t org-icalendar-include-body)
  24585. location (org-icalendar-cleanup-string
  24586. (org-entry-get nil "LOCATION"))
  24587. category (org-get-category))
  24588. (if (looking-at re2)
  24589. (progn
  24590. (goto-char (match-end 0))
  24591. (setq ts2 (match-string 1) inc nil))
  24592. (setq tmp (buffer-substring (max (point-min)
  24593. (- pos org-ds-keyword-length))
  24594. pos)
  24595. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24596. (progn
  24597. (setq inc nil)
  24598. (replace-match "\\1" t nil ts))
  24599. ts)
  24600. deadlinep (string-match org-deadline-regexp tmp)
  24601. scheduledp (string-match org-scheduled-regexp tmp)
  24602. ;; donep (org-entry-is-done-p)
  24603. ))
  24604. (if (or (string-match org-tr-regexp hd)
  24605. (string-match org-ts-regexp hd))
  24606. (setq hd (replace-match "" t t hd)))
  24607. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24608. (setq rrule
  24609. (concat "\nRRULE:FREQ="
  24610. (cdr (assoc
  24611. (match-string 2 ts)
  24612. '(("d" . "DAILY")("w" . "WEEKLY")
  24613. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24614. ";INTERVAL=" (match-string 1 ts)))
  24615. (setq rrule ""))
  24616. (setq summary (or summary hd))
  24617. (if (string-match org-bracket-link-regexp summary)
  24618. (setq summary
  24619. (replace-match (if (match-end 3)
  24620. (match-string 3 summary)
  24621. (match-string 1 summary))
  24622. t t summary)))
  24623. (if deadlinep (setq summary (concat "DL: " summary)))
  24624. (if scheduledp (setq summary (concat "S: " summary)))
  24625. (if (string-match "\\`<%%" ts)
  24626. (with-current-buffer sexp-buffer
  24627. (insert (substring ts 1 -1) " " summary "\n"))
  24628. (princ (format "BEGIN:VEVENT
  24629. %s
  24630. %s%s
  24631. SUMMARY:%s%s%s
  24632. CATEGORIES:%s
  24633. END:VEVENT\n"
  24634. (org-ical-ts-to-string ts "DTSTART")
  24635. (org-ical-ts-to-string ts2 "DTEND" inc)
  24636. rrule summary
  24637. (if (and desc (string-match "\\S-" desc))
  24638. (concat "\nDESCRIPTION: " desc) "")
  24639. (if (and location (string-match "\\S-" location))
  24640. (concat "\nLOCATION: " location) "")
  24641. category)))))
  24642. (when (and org-icalendar-include-sexps
  24643. (condition-case nil (require 'icalendar) (error nil))
  24644. (fboundp 'icalendar-export-region))
  24645. ;; Get all the literal sexps
  24646. (goto-char (point-min))
  24647. (while (re-search-forward "^&?%%(" nil t)
  24648. (catch :skip
  24649. (org-agenda-skip)
  24650. (setq b (match-beginning 0))
  24651. (goto-char (1- (match-end 0)))
  24652. (forward-sexp 1)
  24653. (end-of-line 1)
  24654. (setq sexp (buffer-substring b (point)))
  24655. (with-current-buffer sexp-buffer
  24656. (insert sexp "\n"))
  24657. (princ (org-diary-to-ical-string sexp-buffer)))))
  24658. (when org-icalendar-include-todo
  24659. (goto-char (point-min))
  24660. (while (re-search-forward org-todo-line-regexp nil t)
  24661. (catch :skip
  24662. (org-agenda-skip)
  24663. (setq state (match-string 2))
  24664. (setq status (if (member state org-done-keywords)
  24665. "COMPLETED" "NEEDS-ACTION"))
  24666. (when (and state
  24667. (or (not (member state org-done-keywords))
  24668. (eq org-icalendar-include-todo 'all))
  24669. (not (member org-archive-tag (org-get-tags-at)))
  24670. )
  24671. (setq hd (match-string 3)
  24672. summary (org-icalendar-cleanup-string
  24673. (org-entry-get nil "SUMMARY"))
  24674. desc (org-icalendar-cleanup-string
  24675. (or (org-entry-get nil "DESCRIPTION")
  24676. (and org-icalendar-include-body (org-get-entry)))
  24677. t org-icalendar-include-body)
  24678. location (org-icalendar-cleanup-string
  24679. (org-entry-get nil "LOCATION")))
  24680. (if (string-match org-bracket-link-regexp hd)
  24681. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24682. (match-string 1 hd))
  24683. t t hd)))
  24684. (if (string-match org-priority-regexp hd)
  24685. (setq pri (string-to-char (match-string 2 hd))
  24686. hd (concat (substring hd 0 (match-beginning 1))
  24687. (substring hd (match-end 1))))
  24688. (setq pri org-default-priority))
  24689. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24690. (- org-lowest-priority org-highest-priority))))))
  24691. (princ (format "BEGIN:VTODO
  24692. %s
  24693. SUMMARY:%s%s%s
  24694. CATEGORIES:%s
  24695. SEQUENCE:1
  24696. PRIORITY:%d
  24697. STATUS:%s
  24698. END:VTODO\n"
  24699. dts
  24700. (or summary hd)
  24701. (if (and location (string-match "\\S-" location))
  24702. (concat "\nLOCATION: " location) "")
  24703. (if (and desc (string-match "\\S-" desc))
  24704. (concat "\nDESCRIPTION: " desc) "")
  24705. category pri status)))))))))
  24706. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24707. "Take out stuff and quote what needs to be quoted.
  24708. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24709. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24710. characters."
  24711. (if (not s)
  24712. nil
  24713. (when is-body
  24714. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24715. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24716. (while (string-match re s) (setq s (replace-match "" t t s)))
  24717. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24718. (let ((start 0))
  24719. (while (string-match "\\([,;\\]\\)" s start)
  24720. (setq start (+ (match-beginning 0) 2)
  24721. s (replace-match "\\\\\\1" nil nil s))))
  24722. (when is-body
  24723. (while (string-match "[ \t]*\n[ \t]*" s)
  24724. (setq s (replace-match "\\n" t t s))))
  24725. (setq s (org-trim s))
  24726. (if is-body
  24727. (if maxlength
  24728. (if (and (numberp maxlength)
  24729. (> (length s) maxlength))
  24730. (setq s (substring s 0 maxlength)))))
  24731. s))
  24732. (defun org-get-entry ()
  24733. "Clean-up description string."
  24734. (save-excursion
  24735. (org-back-to-heading t)
  24736. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24737. (defun org-start-icalendar-file (name)
  24738. "Start an iCalendar file by inserting the header."
  24739. (let ((user user-full-name)
  24740. (name (or name "unknown"))
  24741. (timezone (cadr (current-time-zone))))
  24742. (princ
  24743. (format "BEGIN:VCALENDAR
  24744. VERSION:2.0
  24745. X-WR-CALNAME:%s
  24746. PRODID:-//%s//Emacs with Org-mode//EN
  24747. X-WR-TIMEZONE:%s
  24748. CALSCALE:GREGORIAN\n" name user timezone))))
  24749. (defun org-finish-icalendar-file ()
  24750. "Finish an iCalendar file by inserting the END statement."
  24751. (princ "END:VCALENDAR\n"))
  24752. (defun org-ical-ts-to-string (s keyword &optional inc)
  24753. "Take a time string S and convert it to iCalendar format.
  24754. KEYWORD is added in front, to make a complete line like DTSTART....
  24755. When INC is non-nil, increase the hour by two (if time string contains
  24756. a time), or the day by one (if it does not contain a time)."
  24757. (let ((t1 (org-parse-time-string s 'nodefault))
  24758. t2 fmt have-time time)
  24759. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24760. (setq t2 t1 have-time t)
  24761. (setq t2 (org-parse-time-string s)))
  24762. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24763. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24764. (when inc
  24765. (if have-time
  24766. (if org-agenda-default-appointment-duration
  24767. (setq mi (+ org-agenda-default-appointment-duration mi))
  24768. (setq h (+ 2 h)))
  24769. (setq d (1+ d))))
  24770. (setq time (encode-time s mi h d m y)))
  24771. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24772. (concat keyword (format-time-string fmt time))))
  24773. ;;; XOXO export
  24774. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24775. (with-current-buffer buffer
  24776. (apply 'insert output)))
  24777. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24778. (defun org-export-as-xoxo (&optional buffer)
  24779. "Export the org buffer as XOXO.
  24780. The XOXO buffer is named *xoxo-<source buffer name>*"
  24781. (interactive (list (current-buffer)))
  24782. ;; A quickie abstraction
  24783. ;; Output everything as XOXO
  24784. (with-current-buffer (get-buffer buffer)
  24785. (let* ((pos (point))
  24786. (opt-plist (org-combine-plists (org-default-export-plist)
  24787. (org-infile-export-plist)))
  24788. (filename (concat (file-name-as-directory
  24789. (org-export-directory :xoxo opt-plist))
  24790. (file-name-sans-extension
  24791. (file-name-nondirectory buffer-file-name))
  24792. ".html"))
  24793. (out (find-file-noselect filename))
  24794. (last-level 1)
  24795. (hanging-li nil))
  24796. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24797. ;; Check the output buffer is empty.
  24798. (with-current-buffer out (erase-buffer))
  24799. ;; Kick off the output
  24800. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24801. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24802. (let* ((hd (match-string-no-properties 1))
  24803. (level (length hd))
  24804. (text (concat
  24805. (match-string-no-properties 2)
  24806. (save-excursion
  24807. (goto-char (match-end 0))
  24808. (let ((str ""))
  24809. (catch 'loop
  24810. (while 't
  24811. (forward-line)
  24812. (if (looking-at "^[ \t]\\(.*\\)")
  24813. (setq str (concat str (match-string-no-properties 1)))
  24814. (throw 'loop str)))))))))
  24815. ;; Handle level rendering
  24816. (cond
  24817. ((> level last-level)
  24818. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24819. ((< level last-level)
  24820. (dotimes (- (- last-level level) 1)
  24821. (if hanging-li
  24822. (org-export-as-xoxo-insert-into out "</li>\n"))
  24823. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24824. (when hanging-li
  24825. (org-export-as-xoxo-insert-into out "</li>\n")
  24826. (setq hanging-li nil)))
  24827. ((equal level last-level)
  24828. (if hanging-li
  24829. (org-export-as-xoxo-insert-into out "</li>\n")))
  24830. )
  24831. (setq last-level level)
  24832. ;; And output the new li
  24833. (setq hanging-li 't)
  24834. (if (equal ?+ (elt text 0))
  24835. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24836. (org-export-as-xoxo-insert-into out "<li>" text))))
  24837. ;; Finally finish off the ol
  24838. (dotimes (- last-level 1)
  24839. (if hanging-li
  24840. (org-export-as-xoxo-insert-into out "</li>\n"))
  24841. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24842. (goto-char pos)
  24843. ;; Finish the buffer off and clean it up.
  24844. (switch-to-buffer-other-window out)
  24845. (indent-region (point-min) (point-max) nil)
  24846. (save-buffer)
  24847. (goto-char (point-min))
  24848. )))
  24849. ;;;; Key bindings
  24850. ;; Make `C-c C-x' a prefix key
  24851. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24852. ;; TAB key with modifiers
  24853. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24854. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24855. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24856. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24857. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24858. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24859. ;; The following line is necessary under Suse GNU/Linux
  24860. (unless (featurep 'xemacs)
  24861. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24862. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24863. (define-key org-mode-map [backtab] 'org-shifttab)
  24864. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24865. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24866. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24867. ;; Cursor keys with modifiers
  24868. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24869. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24870. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24871. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24872. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24873. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24874. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24875. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24876. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24877. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24878. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24879. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24880. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24881. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24882. ;;; Extra keys for tty access.
  24883. ;; We only set them when really needed because otherwise the
  24884. ;; menus don't show the simple keys
  24885. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24886. (not window-system))
  24887. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24888. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24889. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24890. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24891. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24892. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24893. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24894. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24895. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24896. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24897. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24898. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24899. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24900. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24901. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24902. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24903. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24904. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24905. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24906. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24907. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24908. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24909. ;; All the other keys
  24910. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24911. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24912. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24913. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24914. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24915. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24916. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24917. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24918. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24919. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24920. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24921. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24922. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24923. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24924. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24925. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24926. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24927. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24928. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24929. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24930. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24931. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24932. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24933. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24934. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24935. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24936. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24937. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24938. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24939. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24940. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24941. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24942. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24943. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24944. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24945. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24946. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24947. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24948. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24949. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  24950. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24951. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24952. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24953. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24954. (org-defkey org-mode-map "\C-m" 'org-return)
  24955. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24956. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24957. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24958. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24959. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24960. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24961. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24962. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24963. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24964. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24965. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24966. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24967. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24968. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24969. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24970. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24971. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24972. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24973. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24974. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24975. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24976. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24977. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24978. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24979. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24980. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24981. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24982. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24983. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24984. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24985. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24986. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24987. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24988. (when (featurep 'xemacs)
  24989. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24990. (defsubst org-table-p () (org-at-table-p))
  24991. (defun org-self-insert-command (N)
  24992. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24993. If the cursor is in a table looking at whitespace, the whitespace is
  24994. overwritten, and the table is not marked as requiring realignment."
  24995. (interactive "p")
  24996. (if (and (org-table-p)
  24997. (progn
  24998. ;; check if we blank the field, and if that triggers align
  24999. (and org-table-auto-blank-field
  25000. (member last-command
  25001. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  25002. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  25003. ;; got extra space, this field does not determine column width
  25004. (let (org-table-may-need-update) (org-table-blank-field))
  25005. ;; no extra space, this field may determine column width
  25006. (org-table-blank-field)))
  25007. t)
  25008. (eq N 1)
  25009. (looking-at "[^|\n]* |"))
  25010. (let (org-table-may-need-update)
  25011. (goto-char (1- (match-end 0)))
  25012. (delete-backward-char 1)
  25013. (goto-char (match-beginning 0))
  25014. (self-insert-command N))
  25015. (setq org-table-may-need-update t)
  25016. (self-insert-command N)
  25017. (org-fix-tags-on-the-fly)))
  25018. (defun org-fix-tags-on-the-fly ()
  25019. (when (and (equal (char-after (point-at-bol)) ?*)
  25020. (org-on-heading-p))
  25021. (org-align-tags-here org-tags-column)))
  25022. (defun org-delete-backward-char (N)
  25023. "Like `delete-backward-char', insert whitespace at field end in tables.
  25024. When deleting backwards, in tables this function will insert whitespace in
  25025. front of the next \"|\" separator, to keep the table aligned. The table will
  25026. still be marked for re-alignment if the field did fill the entire column,
  25027. because, in this case the deletion might narrow the column."
  25028. (interactive "p")
  25029. (if (and (org-table-p)
  25030. (eq N 1)
  25031. (string-match "|" (buffer-substring (point-at-bol) (point)))
  25032. (looking-at ".*?|"))
  25033. (let ((pos (point))
  25034. (noalign (looking-at "[^|\n\r]* |"))
  25035. (c org-table-may-need-update))
  25036. (backward-delete-char N)
  25037. (skip-chars-forward "^|")
  25038. (insert " ")
  25039. (goto-char (1- pos))
  25040. ;; noalign: if there were two spaces at the end, this field
  25041. ;; does not determine the width of the column.
  25042. (if noalign (setq org-table-may-need-update c)))
  25043. (backward-delete-char N)
  25044. (org-fix-tags-on-the-fly)))
  25045. (defun org-delete-char (N)
  25046. "Like `delete-char', but insert whitespace at field end in tables.
  25047. When deleting characters, in tables this function will insert whitespace in
  25048. front of the next \"|\" separator, to keep the table aligned. The table will
  25049. still be marked for re-alignment if the field did fill the entire column,
  25050. because, in this case the deletion might narrow the column."
  25051. (interactive "p")
  25052. (if (and (org-table-p)
  25053. (not (bolp))
  25054. (not (= (char-after) ?|))
  25055. (eq N 1))
  25056. (if (looking-at ".*?|")
  25057. (let ((pos (point))
  25058. (noalign (looking-at "[^|\n\r]* |"))
  25059. (c org-table-may-need-update))
  25060. (replace-match (concat
  25061. (substring (match-string 0) 1 -1)
  25062. " |"))
  25063. (goto-char pos)
  25064. ;; noalign: if there were two spaces at the end, this field
  25065. ;; does not determine the width of the column.
  25066. (if noalign (setq org-table-may-need-update c)))
  25067. (delete-char N))
  25068. (delete-char N)
  25069. (org-fix-tags-on-the-fly)))
  25070. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25071. (put 'org-self-insert-command 'delete-selection t)
  25072. (put 'orgtbl-self-insert-command 'delete-selection t)
  25073. (put 'org-delete-char 'delete-selection 'supersede)
  25074. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25075. ;; Make `flyspell-mode' delay after some commands
  25076. (put 'org-self-insert-command 'flyspell-delayed t)
  25077. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25078. (put 'org-delete-char 'flyspell-delayed t)
  25079. (put 'org-delete-backward-char 'flyspell-delayed t)
  25080. ;; Make pabbrev-mode expand after org-mode commands
  25081. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25082. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25083. ;; How to do this: Measure non-white length of current string
  25084. ;; If equal to column width, we should realign.
  25085. (defun org-remap (map &rest commands)
  25086. "In MAP, remap the functions given in COMMANDS.
  25087. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25088. (let (new old)
  25089. (while commands
  25090. (setq old (pop commands) new (pop commands))
  25091. (if (fboundp 'command-remapping)
  25092. (org-defkey map (vector 'remap old) new)
  25093. (substitute-key-definition old new map global-map)))))
  25094. (when (eq org-enable-table-editor 'optimized)
  25095. ;; If the user wants maximum table support, we need to hijack
  25096. ;; some standard editing functions
  25097. (org-remap org-mode-map
  25098. 'self-insert-command 'org-self-insert-command
  25099. 'delete-char 'org-delete-char
  25100. 'delete-backward-char 'org-delete-backward-char)
  25101. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25102. (defun org-shiftcursor-error ()
  25103. "Throw an error because Shift-Cursor command was applied in wrong context."
  25104. (error "This command is active in special context like tables, headlines or timestamps"))
  25105. (defun org-shifttab (&optional arg)
  25106. "Global visibility cycling or move to previous table field.
  25107. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25108. on context.
  25109. See the individual commands for more information."
  25110. (interactive "P")
  25111. (cond
  25112. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25113. (arg (message "Content view to level: ")
  25114. (org-content (prefix-numeric-value arg))
  25115. (setq org-cycle-global-status 'overview))
  25116. (t (call-interactively 'org-global-cycle))))
  25117. (defun org-shiftmetaleft ()
  25118. "Promote subtree or delete table column.
  25119. Calls `org-promote-subtree', `org-outdent-item',
  25120. or `org-table-delete-column', depending on context.
  25121. See the individual commands for more information."
  25122. (interactive)
  25123. (cond
  25124. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25125. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25126. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25127. (t (org-shiftcursor-error))))
  25128. (defun org-shiftmetaright ()
  25129. "Demote subtree or insert table column.
  25130. Calls `org-demote-subtree', `org-indent-item',
  25131. or `org-table-insert-column', depending on context.
  25132. See the individual commands for more information."
  25133. (interactive)
  25134. (cond
  25135. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25136. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25137. ((org-at-item-p) (call-interactively 'org-indent-item))
  25138. (t (org-shiftcursor-error))))
  25139. (defun org-shiftmetaup (&optional arg)
  25140. "Move subtree up or kill table row.
  25141. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25142. `org-move-item-up' depending on context. See the individual commands
  25143. for more information."
  25144. (interactive "P")
  25145. (cond
  25146. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25147. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25148. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25149. (t (org-shiftcursor-error))))
  25150. (defun org-shiftmetadown (&optional arg)
  25151. "Move subtree down or insert table row.
  25152. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25153. `org-move-item-down', depending on context. See the individual
  25154. commands for more information."
  25155. (interactive "P")
  25156. (cond
  25157. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25158. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25159. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25160. (t (org-shiftcursor-error))))
  25161. (defun org-metaleft (&optional arg)
  25162. "Promote heading or move table column to left.
  25163. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25164. With no specific context, calls the Emacs default `backward-word'.
  25165. See the individual commands for more information."
  25166. (interactive "P")
  25167. (cond
  25168. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25169. ((or (org-on-heading-p) (org-region-active-p))
  25170. (call-interactively 'org-do-promote))
  25171. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25172. (t (call-interactively 'backward-word))))
  25173. (defun org-metaright (&optional arg)
  25174. "Demote subtree or move table column to right.
  25175. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25176. With no specific context, calls the Emacs default `forward-word'.
  25177. See the individual commands for more information."
  25178. (interactive "P")
  25179. (cond
  25180. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25181. ((or (org-on-heading-p) (org-region-active-p))
  25182. (call-interactively 'org-do-demote))
  25183. ((org-at-item-p) (call-interactively 'org-indent-item))
  25184. (t (call-interactively 'forward-word))))
  25185. (defun org-metaup (&optional arg)
  25186. "Move subtree up or move table row up.
  25187. Calls `org-move-subtree-up' or `org-table-move-row' or
  25188. `org-move-item-up', depending on context. See the individual commands
  25189. for more information."
  25190. (interactive "P")
  25191. (cond
  25192. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25193. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25194. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25195. (t (transpose-lines 1) (beginning-of-line -1))))
  25196. (defun org-metadown (&optional arg)
  25197. "Move subtree down or move table row down.
  25198. Calls `org-move-subtree-down' or `org-table-move-row' or
  25199. `org-move-item-down', depending on context. See the individual
  25200. commands for more information."
  25201. (interactive "P")
  25202. (cond
  25203. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25204. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25205. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25206. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25207. (defun org-shiftup (&optional arg)
  25208. "Increase item in timestamp or increase priority of current headline.
  25209. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25210. depending on context. See the individual commands for more information."
  25211. (interactive "P")
  25212. (cond
  25213. ((org-at-timestamp-p t)
  25214. (call-interactively (if org-edit-timestamp-down-means-later
  25215. 'org-timestamp-down 'org-timestamp-up)))
  25216. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25217. ((org-at-item-p) (call-interactively 'org-previous-item))
  25218. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25219. (defun org-shiftdown (&optional arg)
  25220. "Decrease item in timestamp or decrease priority of current headline.
  25221. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25222. depending on context. See the individual commands for more information."
  25223. (interactive "P")
  25224. (cond
  25225. ((org-at-timestamp-p t)
  25226. (call-interactively (if org-edit-timestamp-down-means-later
  25227. 'org-timestamp-up 'org-timestamp-down)))
  25228. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25229. (t (call-interactively 'org-next-item))))
  25230. (defun org-shiftright ()
  25231. "Next TODO keyword or timestamp one day later, depending on context."
  25232. (interactive)
  25233. (cond
  25234. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25235. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25236. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25237. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25238. (t (org-shiftcursor-error))))
  25239. (defun org-shiftleft ()
  25240. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25241. (interactive)
  25242. (cond
  25243. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25244. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25245. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25246. ((org-at-property-p)
  25247. (call-interactively 'org-property-previous-allowed-value))
  25248. (t (org-shiftcursor-error))))
  25249. (defun org-shiftcontrolright ()
  25250. "Switch to next TODO set."
  25251. (interactive)
  25252. (cond
  25253. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25254. (t (org-shiftcursor-error))))
  25255. (defun org-shiftcontrolleft ()
  25256. "Switch to previous TODO set."
  25257. (interactive)
  25258. (cond
  25259. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25260. (t (org-shiftcursor-error))))
  25261. (defun org-ctrl-c-ret ()
  25262. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25263. (interactive)
  25264. (cond
  25265. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25266. (t (call-interactively 'org-insert-heading))))
  25267. (defun org-copy-special ()
  25268. "Copy region in table or copy current subtree.
  25269. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25270. See the individual commands for more information."
  25271. (interactive)
  25272. (call-interactively
  25273. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25274. (defun org-cut-special ()
  25275. "Cut region in table or cut current subtree.
  25276. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25277. See the individual commands for more information."
  25278. (interactive)
  25279. (call-interactively
  25280. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25281. (defun org-paste-special (arg)
  25282. "Paste rectangular region into table, or past subtree relative to level.
  25283. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25284. See the individual commands for more information."
  25285. (interactive "P")
  25286. (if (org-at-table-p)
  25287. (org-table-paste-rectangle)
  25288. (org-paste-subtree arg)))
  25289. (defun org-ctrl-c-ctrl-c (&optional arg)
  25290. "Set tags in headline, or update according to changed information at point.
  25291. This command does many different things, depending on context:
  25292. - If the cursor is in a headline, prompt for tags and insert them
  25293. into the current line, aligned to `org-tags-column'. When called
  25294. with prefix arg, realign all tags in the current buffer.
  25295. - If the cursor is in one of the special #+KEYWORD lines, this
  25296. triggers scanning the buffer for these lines and updating the
  25297. information.
  25298. - If the cursor is inside a table, realign the table. This command
  25299. works even if the automatic table editor has been turned off.
  25300. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25301. the entire table.
  25302. - If the cursor is a the beginning of a dynamic block, update it.
  25303. - If the cursor is inside a table created by the table.el package,
  25304. activate that table.
  25305. - If the current buffer is a remember buffer, close note and file it.
  25306. with a prefix argument, file it without further interaction to the default
  25307. location.
  25308. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25309. links in this buffer.
  25310. - If the cursor is on a numbered item in a plain list, renumber the
  25311. ordered list.
  25312. - If the cursor is on a checkbox, toggle it."
  25313. (interactive "P")
  25314. (let ((org-enable-table-editor t))
  25315. (cond
  25316. ((or org-clock-overlays
  25317. org-occur-highlights
  25318. org-latex-fragment-image-overlays)
  25319. (org-remove-clock-overlays)
  25320. (org-remove-occur-highlights)
  25321. (org-remove-latex-fragment-image-overlays)
  25322. (message "Temporary highlights/overlays removed from current buffer"))
  25323. ((and (local-variable-p 'org-finish-function (current-buffer))
  25324. (fboundp org-finish-function))
  25325. (funcall org-finish-function))
  25326. ((org-at-property-p)
  25327. (call-interactively 'org-property-action))
  25328. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25329. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25330. ((org-at-table.el-p)
  25331. (require 'table)
  25332. (beginning-of-line 1)
  25333. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25334. (call-interactively 'table-recognize-table))
  25335. ((org-at-table-p)
  25336. (org-table-maybe-eval-formula)
  25337. (if arg
  25338. (call-interactively 'org-table-recalculate)
  25339. (org-table-maybe-recalculate-line))
  25340. (call-interactively 'org-table-align))
  25341. ((org-at-item-checkbox-p)
  25342. (call-interactively 'org-toggle-checkbox))
  25343. ((org-at-item-p)
  25344. (call-interactively 'org-maybe-renumber-ordered-list))
  25345. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25346. ;; Dynamic block
  25347. (beginning-of-line 1)
  25348. (org-update-dblock))
  25349. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25350. (cond
  25351. ((equal (match-string 1) "TBLFM")
  25352. ;; Recalculate the table before this line
  25353. (save-excursion
  25354. (beginning-of-line 1)
  25355. (skip-chars-backward " \r\n\t")
  25356. (if (org-at-table-p)
  25357. (org-call-with-arg 'org-table-recalculate t))))
  25358. (t
  25359. (call-interactively 'org-mode-restart))))
  25360. (t (error "C-c C-c can do nothing useful at this location.")))))
  25361. (defun org-mode-restart ()
  25362. "Restart Org-mode, to scan again for special lines.
  25363. Also updates the keyword regular expressions."
  25364. (interactive)
  25365. (let ((org-inhibit-startup t)) (org-mode))
  25366. (message "Org-mode restarted to refresh keyword and special line setup"))
  25367. (defun org-kill-note-or-show-branches ()
  25368. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25369. (interactive)
  25370. (if (not org-finish-function)
  25371. (call-interactively 'show-branches)
  25372. (let ((org-note-abort t))
  25373. (funcall org-finish-function))))
  25374. (defun org-return (&optional indent)
  25375. "Goto next table row or insert a newline.
  25376. Calls `org-table-next-row' or `newline', depending on context.
  25377. See the individual commands for more information."
  25378. (interactive)
  25379. (cond
  25380. ((bobp) (if indent (newline-and-indent) (newline)))
  25381. ((and (org-at-heading-p)
  25382. (looking-at
  25383. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25384. (org-show-entry)
  25385. (end-of-line 1)
  25386. (newline))
  25387. ((org-at-table-p)
  25388. (org-table-justify-field-maybe)
  25389. (call-interactively 'org-table-next-row))
  25390. (t (if indent (newline-and-indent) (newline)))))
  25391. (defun org-return-indent ()
  25392. "Goto next table row or insert a newline and indent.
  25393. Calls `org-table-next-row' or `newline-and-indent', depending on
  25394. context. See the individual commands for more information."
  25395. (interactive)
  25396. (org-return t))
  25397. (defun org-ctrl-c-star ()
  25398. "Compute table, or change heading status of lines.
  25399. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25400. depending on context. This will also turn a plain list item or a normal
  25401. line into a subheading."
  25402. (interactive)
  25403. (cond
  25404. ((org-at-table-p)
  25405. (call-interactively 'org-table-recalculate))
  25406. ((org-region-active-p)
  25407. ;; Convert all lines in region to list items
  25408. (call-interactively 'org-toggle-region-headings))
  25409. ((org-on-heading-p)
  25410. (org-toggle-region-headings (point-at-bol)
  25411. (min (1+ (point-at-eol)) (point-max))))
  25412. ((org-at-item-p)
  25413. ;; Convert to heading
  25414. ;; FIXME: not yet implemented
  25415. )
  25416. (t (org-toggle-region-headings (point-at-bol)
  25417. (min (1+ (point-at-eol)) (point-max))))))
  25418. (defun org-ctrl-c-minus ()
  25419. "Insert separator line in table or modify bullet status of line.
  25420. Also turns a plain line or a region of lines into list items.
  25421. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25422. `org-cycle-list-bullet', depending on context."
  25423. (interactive)
  25424. (cond
  25425. ((org-at-table-p)
  25426. (call-interactively 'org-table-insert-hline))
  25427. ((org-on-heading-p)
  25428. ;; Convert to item
  25429. (save-excursion
  25430. (beginning-of-line 1)
  25431. (if (looking-at "\\*+ ")
  25432. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25433. ((org-region-active-p)
  25434. ;; Convert all lines in region to list items
  25435. (call-interactively 'org-toggle-region-items))
  25436. ((org-in-item-p)
  25437. (call-interactively 'org-cycle-list-bullet))
  25438. (t (org-toggle-region-items (point-at-bol)
  25439. (min (1+ (point-at-eol)) (point-max))))))
  25440. (defun org-toggle-region-items (beg end)
  25441. "Convert all lines in region to list items.
  25442. If the first line is already an item, convert all list items in the region
  25443. to normal lines."
  25444. (interactive "r")
  25445. (let (l2 l)
  25446. (save-excursion
  25447. (goto-char end)
  25448. (setq l2 (org-current-line))
  25449. (goto-char beg)
  25450. (beginning-of-line 1)
  25451. (setq l (1- (org-current-line)))
  25452. (if (org-at-item-p)
  25453. ;; We already have items, de-itemize
  25454. (while (< (setq l (1+ l)) l2)
  25455. (when (org-at-item-p)
  25456. (goto-char (match-beginning 2))
  25457. (delete-region (match-beginning 2) (match-end 2))
  25458. (and (looking-at "[ \t]+") (replace-match "")))
  25459. (beginning-of-line 2))
  25460. (while (< (setq l (1+ l)) l2)
  25461. (unless (org-at-item-p)
  25462. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25463. (replace-match "\\1- \\2")))
  25464. (beginning-of-line 2))))))
  25465. (defun org-toggle-region-headings (beg end)
  25466. "Convert all lines in region to list items.
  25467. If the first line is already an item, convert all list items in the region
  25468. to normal lines."
  25469. (interactive "r")
  25470. (let (l2 l)
  25471. (save-excursion
  25472. (goto-char end)
  25473. (setq l2 (org-current-line))
  25474. (goto-char beg)
  25475. (beginning-of-line 1)
  25476. (setq l (1- (org-current-line)))
  25477. (if (org-on-heading-p)
  25478. ;; We already have headlines, de-star them
  25479. (while (< (setq l (1+ l)) l2)
  25480. (when (org-on-heading-p t)
  25481. (and (looking-at outline-regexp) (replace-match "")))
  25482. (beginning-of-line 2))
  25483. (let* ((stars (save-excursion
  25484. (re-search-backward org-complex-heading-regexp nil t)
  25485. (or (match-string 1) "*")))
  25486. (add-stars (if org-odd-levels-only "**" "*"))
  25487. (rpl (concat stars add-stars " \\2")))
  25488. (while (< (setq l (1+ l)) l2)
  25489. (unless (org-on-heading-p)
  25490. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25491. (replace-match rpl)))
  25492. (beginning-of-line 2)))))))
  25493. (defun org-meta-return (&optional arg)
  25494. "Insert a new heading or wrap a region in a table.
  25495. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25496. See the individual commands for more information."
  25497. (interactive "P")
  25498. (cond
  25499. ((org-at-table-p)
  25500. (call-interactively 'org-table-wrap-region))
  25501. (t (call-interactively 'org-insert-heading))))
  25502. ;;; Menu entries
  25503. ;; Define the Org-mode menus
  25504. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25505. '("Tbl"
  25506. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25507. ["Next Field" org-cycle (org-at-table-p)]
  25508. ["Previous Field" org-shifttab (org-at-table-p)]
  25509. ["Next Row" org-return (org-at-table-p)]
  25510. "--"
  25511. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25512. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25513. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25514. "--"
  25515. ("Column"
  25516. ["Move Column Left" org-metaleft (org-at-table-p)]
  25517. ["Move Column Right" org-metaright (org-at-table-p)]
  25518. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25519. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25520. ("Row"
  25521. ["Move Row Up" org-metaup (org-at-table-p)]
  25522. ["Move Row Down" org-metadown (org-at-table-p)]
  25523. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25524. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25525. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25526. "--"
  25527. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25528. ("Rectangle"
  25529. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25530. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25531. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25532. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25533. "--"
  25534. ("Calculate"
  25535. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25536. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25537. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25538. "--"
  25539. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25540. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25541. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25542. "--"
  25543. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25544. "--"
  25545. ["Sum Column/Rectangle" org-table-sum
  25546. (or (org-at-table-p) (org-region-active-p))]
  25547. ["Which Column?" org-table-current-column (org-at-table-p)])
  25548. ["Debug Formulas"
  25549. org-table-toggle-formula-debugger
  25550. :style toggle :selected org-table-formula-debug]
  25551. ["Show Col/Row Numbers"
  25552. org-table-toggle-coordinate-overlays
  25553. :style toggle :selected org-table-overlay-coordinates]
  25554. "--"
  25555. ["Create" org-table-create (and (not (org-at-table-p))
  25556. org-enable-table-editor)]
  25557. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25558. ["Import from File" org-table-import (not (org-at-table-p))]
  25559. ["Export to File" org-table-export (org-at-table-p)]
  25560. "--"
  25561. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25562. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25563. '("Org"
  25564. ("Show/Hide"
  25565. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25566. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25567. ["Sparse Tree" org-occur t]
  25568. ["Reveal Context" org-reveal t]
  25569. ["Show All" show-all t]
  25570. "--"
  25571. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25572. "--"
  25573. ["New Heading" org-insert-heading t]
  25574. ("Navigate Headings"
  25575. ["Up" outline-up-heading t]
  25576. ["Next" outline-next-visible-heading t]
  25577. ["Previous" outline-previous-visible-heading t]
  25578. ["Next Same Level" outline-forward-same-level t]
  25579. ["Previous Same Level" outline-backward-same-level t]
  25580. "--"
  25581. ["Jump" org-goto t])
  25582. ("Edit Structure"
  25583. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25584. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25585. "--"
  25586. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25587. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25588. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25589. "--"
  25590. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25591. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25592. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25593. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25594. "--"
  25595. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25596. "--"
  25597. ["Convert to odd levels" org-convert-to-odd-levels t]
  25598. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25599. ("Editing"
  25600. ["Emphasis..." org-emphasize t])
  25601. ("Archive"
  25602. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25603. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25604. ; :active t :keys "C-u C-c C-x C-a"]
  25605. ["Sparse trees open ARCHIVE trees"
  25606. (setq org-sparse-tree-open-archived-trees
  25607. (not org-sparse-tree-open-archived-trees))
  25608. :style toggle :selected org-sparse-tree-open-archived-trees]
  25609. ["Cycling opens ARCHIVE trees"
  25610. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25611. :style toggle :selected org-cycle-open-archived-trees]
  25612. ["Agenda includes ARCHIVE trees"
  25613. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25614. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25615. "--"
  25616. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25617. ; ["Check and Move Children" (org-archive-subtree '(4))
  25618. ; :active t :keys "C-u C-c C-x C-s"]
  25619. )
  25620. "--"
  25621. ("TODO Lists"
  25622. ["TODO/DONE/-" org-todo t]
  25623. ("Select keyword"
  25624. ["Next keyword" org-shiftright (org-on-heading-p)]
  25625. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25626. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25627. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25628. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25629. ["Show TODO Tree" org-show-todo-tree t]
  25630. ["Global TODO list" org-todo-list t]
  25631. "--"
  25632. ["Set Priority" org-priority t]
  25633. ["Priority Up" org-shiftup t]
  25634. ["Priority Down" org-shiftdown t])
  25635. ("TAGS and Properties"
  25636. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25637. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25638. "--"
  25639. ["Set property" 'org-set-property t]
  25640. ["Column view of properties" org-columns t]
  25641. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25642. ("Dates and Scheduling"
  25643. ["Timestamp" org-time-stamp t]
  25644. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25645. ("Change Date"
  25646. ["1 Day Later" org-shiftright t]
  25647. ["1 Day Earlier" org-shiftleft t]
  25648. ["1 ... Later" org-shiftup t]
  25649. ["1 ... Earlier" org-shiftdown t])
  25650. ["Compute Time Range" org-evaluate-time-range t]
  25651. ["Schedule Item" org-schedule t]
  25652. ["Deadline" org-deadline t]
  25653. "--"
  25654. ["Custom time format" org-toggle-time-stamp-overlays
  25655. :style radio :selected org-display-custom-times]
  25656. "--"
  25657. ["Goto Calendar" org-goto-calendar t]
  25658. ["Date from Calendar" org-date-from-calendar t])
  25659. ("Logging work"
  25660. ["Clock in" org-clock-in t]
  25661. ["Clock out" org-clock-out t]
  25662. ["Clock cancel" org-clock-cancel t]
  25663. ["Goto running clock" org-clock-goto t]
  25664. ["Display times" org-clock-display t]
  25665. ["Create clock table" org-clock-report t]
  25666. "--"
  25667. ["Record DONE time"
  25668. (progn (setq org-log-done (not org-log-done))
  25669. (message "Switching to %s will %s record a timestamp"
  25670. (car org-done-keywords)
  25671. (if org-log-done "automatically" "not")))
  25672. :style toggle :selected org-log-done])
  25673. "--"
  25674. ["Agenda Command..." org-agenda t]
  25675. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25676. ("File List for Agenda")
  25677. ("Special views current file"
  25678. ["TODO Tree" org-show-todo-tree t]
  25679. ["Check Deadlines" org-check-deadlines t]
  25680. ["Timeline" org-timeline t]
  25681. ["Tags Tree" org-tags-sparse-tree t])
  25682. "--"
  25683. ("Hyperlinks"
  25684. ["Store Link (Global)" org-store-link t]
  25685. ["Insert Link" org-insert-link t]
  25686. ["Follow Link" org-open-at-point t]
  25687. "--"
  25688. ["Next link" org-next-link t]
  25689. ["Previous link" org-previous-link t]
  25690. "--"
  25691. ["Descriptive Links"
  25692. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25693. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25694. ["Literal Links"
  25695. (progn
  25696. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25697. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25698. "--"
  25699. ["Export/Publish..." org-export t]
  25700. ("LaTeX"
  25701. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25702. :selected org-cdlatex-mode]
  25703. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25704. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25705. ["Modify math symbol" org-cdlatex-math-modify
  25706. (org-inside-LaTeX-fragment-p)]
  25707. ["Export LaTeX fragments as images"
  25708. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25709. :style toggle :selected org-export-with-LaTeX-fragments])
  25710. "--"
  25711. ("Documentation"
  25712. ["Show Version" org-version t]
  25713. ["Info Documentation" org-info t])
  25714. ("Customize"
  25715. ["Browse Org Group" org-customize t]
  25716. "--"
  25717. ["Expand This Menu" org-create-customize-menu
  25718. (fboundp 'customize-menu-create)])
  25719. "--"
  25720. ["Refresh setup" org-mode-restart t]
  25721. ))
  25722. (defun org-info (&optional node)
  25723. "Read documentation for Org-mode in the info system.
  25724. With optional NODE, go directly to that node."
  25725. (interactive)
  25726. (require 'info)
  25727. (info (format "(org)%s" (or node ""))))
  25728. (defun org-install-agenda-files-menu ()
  25729. (let ((bl (buffer-list)))
  25730. (save-excursion
  25731. (while bl
  25732. (set-buffer (pop bl))
  25733. (if (org-mode-p) (setq bl nil)))
  25734. (when (org-mode-p)
  25735. (easy-menu-change
  25736. '("Org") "File List for Agenda"
  25737. (append
  25738. (list
  25739. ["Edit File List" (org-edit-agenda-file-list) t]
  25740. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25741. ["Remove Current File from List" org-remove-file t]
  25742. ["Cycle through agenda files" org-cycle-agenda-files t]
  25743. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25744. "--")
  25745. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25746. ;;;; Documentation
  25747. (defun org-customize ()
  25748. "Call the customize function with org as argument."
  25749. (interactive)
  25750. (customize-browse 'org))
  25751. (defun org-create-customize-menu ()
  25752. "Create a full customization menu for Org-mode, insert it into the menu."
  25753. (interactive)
  25754. (if (fboundp 'customize-menu-create)
  25755. (progn
  25756. (easy-menu-change
  25757. '("Org") "Customize"
  25758. `(["Browse Org group" org-customize t]
  25759. "--"
  25760. ,(customize-menu-create 'org)
  25761. ["Set" Custom-set t]
  25762. ["Save" Custom-save t]
  25763. ["Reset to Current" Custom-reset-current t]
  25764. ["Reset to Saved" Custom-reset-saved t]
  25765. ["Reset to Standard Settings" Custom-reset-standard t]))
  25766. (message "\"Org\"-menu now contains full customization menu"))
  25767. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25768. ;;;; Miscellaneous stuff
  25769. ;;; Generally useful functions
  25770. (defun org-context ()
  25771. "Return a list of contexts of the current cursor position.
  25772. If several contexts apply, all are returned.
  25773. Each context entry is a list with a symbol naming the context, and
  25774. two positions indicating start and end of the context. Possible
  25775. contexts are:
  25776. :headline anywhere in a headline
  25777. :headline-stars on the leading stars in a headline
  25778. :todo-keyword on a TODO keyword (including DONE) in a headline
  25779. :tags on the TAGS in a headline
  25780. :priority on the priority cookie in a headline
  25781. :item on the first line of a plain list item
  25782. :item-bullet on the bullet/number of a plain list item
  25783. :checkbox on the checkbox in a plain list item
  25784. :table in an org-mode table
  25785. :table-special on a special filed in a table
  25786. :table-table in a table.el table
  25787. :link on a hyperlink
  25788. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25789. :target on a <<target>>
  25790. :radio-target on a <<<radio-target>>>
  25791. :latex-fragment on a LaTeX fragment
  25792. :latex-preview on a LaTeX fragment with overlayed preview image
  25793. This function expects the position to be visible because it uses font-lock
  25794. faces as a help to recognize the following contexts: :table-special, :link,
  25795. and :keyword."
  25796. (let* ((f (get-text-property (point) 'face))
  25797. (faces (if (listp f) f (list f)))
  25798. (p (point)) clist o)
  25799. ;; First the large context
  25800. (cond
  25801. ((org-on-heading-p t)
  25802. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25803. (when (progn
  25804. (beginning-of-line 1)
  25805. (looking-at org-todo-line-tags-regexp))
  25806. (push (org-point-in-group p 1 :headline-stars) clist)
  25807. (push (org-point-in-group p 2 :todo-keyword) clist)
  25808. (push (org-point-in-group p 4 :tags) clist))
  25809. (goto-char p)
  25810. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25811. (if (looking-at "\\[#[A-Z0-9]\\]")
  25812. (push (org-point-in-group p 0 :priority) clist)))
  25813. ((org-at-item-p)
  25814. (push (org-point-in-group p 2 :item-bullet) clist)
  25815. (push (list :item (point-at-bol)
  25816. (save-excursion (org-end-of-item) (point)))
  25817. clist)
  25818. (and (org-at-item-checkbox-p)
  25819. (push (org-point-in-group p 0 :checkbox) clist)))
  25820. ((org-at-table-p)
  25821. (push (list :table (org-table-begin) (org-table-end)) clist)
  25822. (if (memq 'org-formula faces)
  25823. (push (list :table-special
  25824. (previous-single-property-change p 'face)
  25825. (next-single-property-change p 'face)) clist)))
  25826. ((org-at-table-p 'any)
  25827. (push (list :table-table) clist)))
  25828. (goto-char p)
  25829. ;; Now the small context
  25830. (cond
  25831. ((org-at-timestamp-p)
  25832. (push (org-point-in-group p 0 :timestamp) clist))
  25833. ((memq 'org-link faces)
  25834. (push (list :link
  25835. (previous-single-property-change p 'face)
  25836. (next-single-property-change p 'face)) clist))
  25837. ((memq 'org-special-keyword faces)
  25838. (push (list :keyword
  25839. (previous-single-property-change p 'face)
  25840. (next-single-property-change p 'face)) clist))
  25841. ((org-on-target-p)
  25842. (push (org-point-in-group p 0 :target) clist)
  25843. (goto-char (1- (match-beginning 0)))
  25844. (if (looking-at org-radio-target-regexp)
  25845. (push (org-point-in-group p 0 :radio-target) clist))
  25846. (goto-char p))
  25847. ((setq o (car (delq nil
  25848. (mapcar
  25849. (lambda (x)
  25850. (if (memq x org-latex-fragment-image-overlays) x))
  25851. (org-overlays-at (point))))))
  25852. (push (list :latex-fragment
  25853. (org-overlay-start o) (org-overlay-end o)) clist)
  25854. (push (list :latex-preview
  25855. (org-overlay-start o) (org-overlay-end o)) clist))
  25856. ((org-inside-LaTeX-fragment-p)
  25857. ;; FIXME: positions wrong.
  25858. (push (list :latex-fragment (point) (point)) clist)))
  25859. (setq clist (nreverse (delq nil clist)))
  25860. clist))
  25861. ;; FIXME: Compare with at-regexp-p Do we need both?
  25862. (defun org-in-regexp (re &optional nlines visually)
  25863. "Check if point is inside a match of regexp.
  25864. Normally only the current line is checked, but you can include NLINES extra
  25865. lines both before and after point into the search.
  25866. If VISUALLY is set, require that the cursor is not after the match but
  25867. really on, so that the block visually is on the match."
  25868. (catch 'exit
  25869. (let ((pos (point))
  25870. (eol (point-at-eol (+ 1 (or nlines 0))))
  25871. (inc (if visually 1 0)))
  25872. (save-excursion
  25873. (beginning-of-line (- 1 (or nlines 0)))
  25874. (while (re-search-forward re eol t)
  25875. (if (and (<= (match-beginning 0) pos)
  25876. (>= (+ inc (match-end 0)) pos))
  25877. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25878. (defun org-at-regexp-p (regexp)
  25879. "Is point inside a match of REGEXP in the current line?"
  25880. (catch 'exit
  25881. (save-excursion
  25882. (let ((pos (point)) (end (point-at-eol)))
  25883. (beginning-of-line 1)
  25884. (while (re-search-forward regexp end t)
  25885. (if (and (<= (match-beginning 0) pos)
  25886. (>= (match-end 0) pos))
  25887. (throw 'exit t)))
  25888. nil))))
  25889. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25890. "Call `multi-occur' with buffers for all agenda files."
  25891. (interactive "sOrg-files matching: \np")
  25892. (let* ((files (org-agenda-files))
  25893. (tnames (mapcar 'file-truename files))
  25894. (extra org-agenda-text-search-extra-files)
  25895. f)
  25896. (while (setq f (pop extra))
  25897. (unless (member (file-truename f) tnames)
  25898. (add-to-list 'files f 'append)
  25899. (add-to-list 'tnames (file-truename f) 'append)))
  25900. (multi-occur
  25901. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25902. regexp)))
  25903. (if (boundp 'occur-mode-find-occurrence-hook)
  25904. ;; Emacs 23
  25905. (add-hook 'occur-mode-find-occurrence-hook
  25906. (lambda ()
  25907. (when (org-mode-p)
  25908. (org-reveal))))
  25909. ;; Emacs 22
  25910. (defadvice occur-mode-goto-occurrence
  25911. (after org-occur-reveal activate)
  25912. (and (org-mode-p) (org-reveal)))
  25913. (defadvice occur-mode-goto-occurrence-other-window
  25914. (after org-occur-reveal activate)
  25915. (and (org-mode-p) (org-reveal)))
  25916. (defadvice occur-mode-display-occurrence
  25917. (after org-occur-reveal activate)
  25918. (when (org-mode-p)
  25919. (let ((pos (occur-mode-find-occurrence)))
  25920. (with-current-buffer (marker-buffer pos)
  25921. (save-excursion
  25922. (goto-char pos)
  25923. (org-reveal)))))))
  25924. (defun org-uniquify (list)
  25925. "Remove duplicate elements from LIST."
  25926. (let (res)
  25927. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25928. res))
  25929. (defun org-delete-all (elts list)
  25930. "Remove all elements in ELTS from LIST."
  25931. (while elts
  25932. (setq list (delete (pop elts) list)))
  25933. list)
  25934. (defun org-back-over-empty-lines ()
  25935. "Move backwards over witespace, to the beginning of the first empty line.
  25936. Returns the number o empty lines passed."
  25937. (let ((pos (point)))
  25938. (skip-chars-backward " \t\n\r")
  25939. (beginning-of-line 2)
  25940. (goto-char (min (point) pos))
  25941. (count-lines (point) pos)))
  25942. (defun org-skip-whitespace ()
  25943. (skip-chars-forward " \t\n\r"))
  25944. (defun org-point-in-group (point group &optional context)
  25945. "Check if POINT is in match-group GROUP.
  25946. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25947. match. If the match group does ot exist or point is not inside it,
  25948. return nil."
  25949. (and (match-beginning group)
  25950. (>= point (match-beginning group))
  25951. (<= point (match-end group))
  25952. (if context
  25953. (list context (match-beginning group) (match-end group))
  25954. t)))
  25955. (defun org-switch-to-buffer-other-window (&rest args)
  25956. "Switch to buffer in a second window on the current frame.
  25957. In particular, do not allow pop-up frames."
  25958. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25959. special-display-function)
  25960. (apply 'switch-to-buffer-other-window args)))
  25961. (defun org-combine-plists (&rest plists)
  25962. "Create a single property list from all plists in PLISTS.
  25963. The process starts by copying the first list, and then setting properties
  25964. from the other lists. Settings in the last list are the most significant
  25965. ones and overrule settings in the other lists."
  25966. (let ((rtn (copy-sequence (pop plists)))
  25967. p v ls)
  25968. (while plists
  25969. (setq ls (pop plists))
  25970. (while ls
  25971. (setq p (pop ls) v (pop ls))
  25972. (setq rtn (plist-put rtn p v))))
  25973. rtn))
  25974. (defun org-move-line-down (arg)
  25975. "Move the current line down. With prefix argument, move it past ARG lines."
  25976. (interactive "p")
  25977. (let ((col (current-column))
  25978. beg end pos)
  25979. (beginning-of-line 1) (setq beg (point))
  25980. (beginning-of-line 2) (setq end (point))
  25981. (beginning-of-line (+ 1 arg))
  25982. (setq pos (move-marker (make-marker) (point)))
  25983. (insert (delete-and-extract-region beg end))
  25984. (goto-char pos)
  25985. (move-to-column col)))
  25986. (defun org-move-line-up (arg)
  25987. "Move the current line up. With prefix argument, move it past ARG lines."
  25988. (interactive "p")
  25989. (let ((col (current-column))
  25990. beg end pos)
  25991. (beginning-of-line 1) (setq beg (point))
  25992. (beginning-of-line 2) (setq end (point))
  25993. (beginning-of-line (- arg))
  25994. (setq pos (move-marker (make-marker) (point)))
  25995. (insert (delete-and-extract-region beg end))
  25996. (goto-char pos)
  25997. (move-to-column col)))
  25998. (defun org-replace-escapes (string table)
  25999. "Replace %-escapes in STRING with values in TABLE.
  26000. TABLE is an association list with keys like \"%a\" and string values.
  26001. The sequences in STRING may contain normal field width and padding information,
  26002. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  26003. so values can contain further %-escapes if they are define later in TABLE."
  26004. (let ((case-fold-search nil)
  26005. e re rpl)
  26006. (while (setq e (pop table))
  26007. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  26008. (while (string-match re string)
  26009. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  26010. (cdr e)))
  26011. (setq string (replace-match rpl t t string))))
  26012. string))
  26013. (defun org-sublist (list start end)
  26014. "Return a section of LIST, from START to END.
  26015. Counting starts at 1."
  26016. (let (rtn (c start))
  26017. (setq list (nthcdr (1- start) list))
  26018. (while (and list (<= c end))
  26019. (push (pop list) rtn)
  26020. (setq c (1+ c)))
  26021. (nreverse rtn)))
  26022. (defun org-find-base-buffer-visiting (file)
  26023. "Like `find-buffer-visiting' but alway return the base buffer and
  26024. not an indirect buffer"
  26025. (let ((buf (find-buffer-visiting file)))
  26026. (if buf
  26027. (or (buffer-base-buffer buf) buf)
  26028. nil)))
  26029. (defun org-image-file-name-regexp ()
  26030. "Return regexp matching the file names of images."
  26031. (if (fboundp 'image-file-name-regexp)
  26032. (image-file-name-regexp)
  26033. (let ((image-file-name-extensions
  26034. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  26035. "xbm" "xpm" "pbm" "pgm" "ppm")))
  26036. (concat "\\."
  26037. (regexp-opt (nconc (mapcar 'upcase
  26038. image-file-name-extensions)
  26039. image-file-name-extensions)
  26040. t)
  26041. "\\'"))))
  26042. (defun org-file-image-p (file)
  26043. "Return non-nil if FILE is an image."
  26044. (save-match-data
  26045. (string-match (org-image-file-name-regexp) file)))
  26046. ;;; Paragraph filling stuff.
  26047. ;; We want this to be just right, so use the full arsenal.
  26048. (defun org-indent-line-function ()
  26049. "Indent line like previous, but further if previous was headline or item."
  26050. (interactive)
  26051. (let* ((pos (point))
  26052. (itemp (org-at-item-p))
  26053. column bpos bcol tpos tcol bullet btype bullet-type)
  26054. ;; Find the previous relevant line
  26055. (beginning-of-line 1)
  26056. (cond
  26057. ((looking-at "#") (setq column 0))
  26058. ((looking-at "\\*+ ") (setq column 0))
  26059. (t
  26060. (beginning-of-line 0)
  26061. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  26062. (beginning-of-line 0))
  26063. (cond
  26064. ((looking-at "\\*+[ \t]+")
  26065. (goto-char (match-end 0))
  26066. (setq column (current-column)))
  26067. ((org-in-item-p)
  26068. (org-beginning-of-item)
  26069. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26070. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26071. (setq bpos (match-beginning 1) tpos (match-end 0)
  26072. bcol (progn (goto-char bpos) (current-column))
  26073. tcol (progn (goto-char tpos) (current-column))
  26074. bullet (match-string 1)
  26075. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26076. (if (not itemp)
  26077. (setq column tcol)
  26078. (goto-char pos)
  26079. (beginning-of-line 1)
  26080. (if (looking-at "\\S-")
  26081. (progn
  26082. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26083. (setq bullet (match-string 1)
  26084. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26085. (setq column (if (equal btype bullet-type) bcol tcol)))
  26086. (setq column (org-get-indentation)))))
  26087. (t (setq column (org-get-indentation))))))
  26088. (goto-char pos)
  26089. (if (<= (current-column) (current-indentation))
  26090. (indent-line-to column)
  26091. (save-excursion (indent-line-to column)))
  26092. (setq column (current-column))
  26093. (beginning-of-line 1)
  26094. (if (looking-at
  26095. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26096. (replace-match (concat "\\1" (format org-property-format
  26097. (match-string 2) (match-string 3)))
  26098. t nil))
  26099. (move-to-column column)))
  26100. (defun org-set-autofill-regexps ()
  26101. (interactive)
  26102. ;; In the paragraph separator we include headlines, because filling
  26103. ;; text in a line directly attached to a headline would otherwise
  26104. ;; fill the headline as well.
  26105. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26106. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26107. ;; The paragraph starter includes hand-formatted lists.
  26108. (org-set-local 'paragraph-start
  26109. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26110. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26111. ;; But only if the user has not turned off tables or fixed-width regions
  26112. (org-set-local
  26113. 'auto-fill-inhibit-regexp
  26114. (concat "\\*+ \\|#\\+"
  26115. "\\|[ \t]*" org-keyword-time-regexp
  26116. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26117. (concat
  26118. "\\|[ \t]*["
  26119. (if org-enable-table-editor "|" "")
  26120. (if org-enable-fixed-width-editor ":" "")
  26121. "]"))))
  26122. ;; We use our own fill-paragraph function, to make sure that tables
  26123. ;; and fixed-width regions are not wrapped. That function will pass
  26124. ;; through to `fill-paragraph' when appropriate.
  26125. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26126. ; Adaptive filling: To get full control, first make sure that
  26127. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26128. (org-set-local 'adaptive-fill-regexp "\000")
  26129. (org-set-local 'adaptive-fill-function
  26130. 'org-adaptive-fill-function)
  26131. (org-set-local
  26132. 'align-mode-rules-list
  26133. '((org-in-buffer-settings
  26134. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26135. (modes . '(org-mode))))))
  26136. (defun org-fill-paragraph (&optional justify)
  26137. "Re-align a table, pass through to fill-paragraph if no table."
  26138. (let ((table-p (org-at-table-p))
  26139. (table.el-p (org-at-table.el-p)))
  26140. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26141. (save-excursion (goto-char (point-at-bol))
  26142. (looking-at outline-regexp)))
  26143. t) ; skip headlines
  26144. (table.el-p t) ; skip table.el tables
  26145. (table-p (org-table-align) t) ; align org-mode tables
  26146. (t nil)))) ; call paragraph-fill
  26147. ;; For reference, this is the default value of adaptive-fill-regexp
  26148. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26149. (defun org-adaptive-fill-function ()
  26150. "Return a fill prefix for org-mode files.
  26151. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26152. work correctly."
  26153. (cond ((looking-at "#[ \t]+")
  26154. (match-string 0))
  26155. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26156. (save-excursion
  26157. (goto-char (match-end 0))
  26158. (make-string (current-column) ?\ )))
  26159. (t nil)))
  26160. ;;;; Functions extending outline functionality
  26161. (defun org-beginning-of-line (&optional arg)
  26162. "Go to the beginning of the current line. If that is invisible, continue
  26163. to a visible line beginning. This makes the function of C-a more intuitive.
  26164. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26165. first attempt, and only move to after the tags when the cursor is already
  26166. beyond the end of the headline."
  26167. (interactive "P")
  26168. (let ((pos (point)))
  26169. (beginning-of-line 1)
  26170. (if (bobp)
  26171. nil
  26172. (backward-char 1)
  26173. (if (org-invisible-p)
  26174. (while (and (not (bobp)) (org-invisible-p))
  26175. (backward-char 1)
  26176. (beginning-of-line 1))
  26177. (forward-char 1)))
  26178. (when org-special-ctrl-a/e
  26179. (cond
  26180. ((and (looking-at org-todo-line-regexp)
  26181. (= (char-after (match-end 1)) ?\ ))
  26182. (goto-char
  26183. (if (eq org-special-ctrl-a/e t)
  26184. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26185. ((= pos (point)) (match-beginning 3))
  26186. (t (point)))
  26187. (cond ((> pos (point)) (point))
  26188. ((not (eq last-command this-command)) (point))
  26189. (t (match-beginning 3))))))
  26190. ((org-at-item-p)
  26191. (goto-char
  26192. (if (eq org-special-ctrl-a/e t)
  26193. (cond ((> pos (match-end 4)) (match-end 4))
  26194. ((= pos (point)) (match-end 4))
  26195. (t (point)))
  26196. (cond ((> pos (point)) (point))
  26197. ((not (eq last-command this-command)) (point))
  26198. (t (match-end 4))))))))))
  26199. (defun org-end-of-line (&optional arg)
  26200. "Go to the end of the line.
  26201. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26202. first attempt, and only move to after the tags when the cursor is already
  26203. beyond the end of the headline."
  26204. (interactive "P")
  26205. (if (or (not org-special-ctrl-a/e)
  26206. (not (org-on-heading-p)))
  26207. (end-of-line arg)
  26208. (let ((pos (point)))
  26209. (beginning-of-line 1)
  26210. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26211. (if (eq org-special-ctrl-a/e t)
  26212. (if (or (< pos (match-beginning 1))
  26213. (= pos (match-end 0)))
  26214. (goto-char (match-beginning 1))
  26215. (goto-char (match-end 0)))
  26216. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26217. (goto-char (match-end 0))
  26218. (goto-char (match-beginning 1))))
  26219. (end-of-line arg)))))
  26220. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26221. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26222. (defun org-kill-line (&optional arg)
  26223. "Kill line, to tags or end of line."
  26224. (interactive "P")
  26225. (cond
  26226. ((or (not org-special-ctrl-k)
  26227. (bolp)
  26228. (not (org-on-heading-p)))
  26229. (call-interactively 'kill-line))
  26230. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26231. (kill-region (point) (match-beginning 1))
  26232. (org-set-tags nil t))
  26233. (t (kill-region (point) (point-at-eol)))))
  26234. (define-key org-mode-map "\C-k" 'org-kill-line)
  26235. (defun org-invisible-p ()
  26236. "Check if point is at a character currently not visible."
  26237. ;; Early versions of noutline don't have `outline-invisible-p'.
  26238. (if (fboundp 'outline-invisible-p)
  26239. (outline-invisible-p)
  26240. (get-char-property (point) 'invisible)))
  26241. (defun org-invisible-p2 ()
  26242. "Check if point is at a character currently not visible."
  26243. (save-excursion
  26244. (if (and (eolp) (not (bobp))) (backward-char 1))
  26245. ;; Early versions of noutline don't have `outline-invisible-p'.
  26246. (if (fboundp 'outline-invisible-p)
  26247. (outline-invisible-p)
  26248. (get-char-property (point) 'invisible))))
  26249. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26250. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26251. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26252. (defun org-at-heading-or-item-p ()
  26253. (or (org-on-heading-p) (org-at-item-p)))
  26254. (defun org-on-target-p ()
  26255. (or (org-in-regexp org-radio-target-regexp)
  26256. (org-in-regexp org-target-regexp)))
  26257. (defun org-up-heading-all (arg)
  26258. "Move to the heading line of which the present line is a subheading.
  26259. This function considers both visible and invisible heading lines.
  26260. With argument, move up ARG levels."
  26261. (if (fboundp 'outline-up-heading-all)
  26262. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26263. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26264. (defun org-up-heading-safe ()
  26265. "Move to the heading line of which the present line is a subheading.
  26266. This version will not throw an error. It will return the level of the
  26267. headline found, or nil if no higher level is found."
  26268. (let ((pos (point)) start-level level
  26269. (re (concat "^" outline-regexp)))
  26270. (catch 'exit
  26271. (outline-back-to-heading t)
  26272. (setq start-level (funcall outline-level))
  26273. (if (equal start-level 1) (throw 'exit nil))
  26274. (while (re-search-backward re nil t)
  26275. (setq level (funcall outline-level))
  26276. (if (< level start-level) (throw 'exit level)))
  26277. nil)))
  26278. (defun org-first-sibling-p ()
  26279. "Is this heading the first child of its parents?"
  26280. (interactive)
  26281. (let ((re (concat "^" outline-regexp))
  26282. level l)
  26283. (unless (org-at-heading-p t)
  26284. (error "Not at a heading"))
  26285. (setq level (funcall outline-level))
  26286. (save-excursion
  26287. (if (not (re-search-backward re nil t))
  26288. t
  26289. (setq l (funcall outline-level))
  26290. (< l level)))))
  26291. (defun org-goto-sibling (&optional previous)
  26292. "Goto the next sibling, even if it is invisible.
  26293. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26294. when a sibling was found. When none is found, return nil and don't
  26295. move point."
  26296. (let ((fun (if previous 're-search-backward 're-search-forward))
  26297. (pos (point))
  26298. (re (concat "^" outline-regexp))
  26299. level l)
  26300. (when (condition-case nil (org-back-to-heading t) (error nil))
  26301. (setq level (funcall outline-level))
  26302. (catch 'exit
  26303. (or previous (forward-char 1))
  26304. (while (funcall fun re nil t)
  26305. (setq l (funcall outline-level))
  26306. (when (< l level) (goto-char pos) (throw 'exit nil))
  26307. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26308. (goto-char pos)
  26309. nil))))
  26310. (defun org-show-siblings ()
  26311. "Show all siblings of the current headline."
  26312. (save-excursion
  26313. (while (org-goto-sibling) (org-flag-heading nil)))
  26314. (save-excursion
  26315. (while (org-goto-sibling 'previous)
  26316. (org-flag-heading nil))))
  26317. (defun org-show-hidden-entry ()
  26318. "Show an entry where even the heading is hidden."
  26319. (save-excursion
  26320. (org-show-entry)))
  26321. (defun org-flag-heading (flag &optional entry)
  26322. "Flag the current heading. FLAG non-nil means make invisible.
  26323. When ENTRY is non-nil, show the entire entry."
  26324. (save-excursion
  26325. (org-back-to-heading t)
  26326. ;; Check if we should show the entire entry
  26327. (if entry
  26328. (progn
  26329. (org-show-entry)
  26330. (save-excursion
  26331. (and (outline-next-heading)
  26332. (org-flag-heading nil))))
  26333. (outline-flag-region (max (point-min) (1- (point)))
  26334. (save-excursion (outline-end-of-heading) (point))
  26335. flag))))
  26336. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26337. ;; This is an exact copy of the original function, but it uses
  26338. ;; `org-back-to-heading', to make it work also in invisible
  26339. ;; trees. And is uses an invisible-OK argument.
  26340. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26341. (org-back-to-heading invisible-OK)
  26342. (let ((first t)
  26343. (level (funcall outline-level)))
  26344. (while (and (not (eobp))
  26345. (or first (> (funcall outline-level) level)))
  26346. (setq first nil)
  26347. (outline-next-heading))
  26348. (unless to-heading
  26349. (if (memq (preceding-char) '(?\n ?\^M))
  26350. (progn
  26351. ;; Go to end of line before heading
  26352. (forward-char -1)
  26353. (if (memq (preceding-char) '(?\n ?\^M))
  26354. ;; leave blank line before heading
  26355. (forward-char -1))))))
  26356. (point))
  26357. (defun org-show-subtree ()
  26358. "Show everything after this heading at deeper levels."
  26359. (outline-flag-region
  26360. (point)
  26361. (save-excursion
  26362. (outline-end-of-subtree) (outline-next-heading) (point))
  26363. nil))
  26364. (defun org-show-entry ()
  26365. "Show the body directly following this heading.
  26366. Show the heading too, if it is currently invisible."
  26367. (interactive)
  26368. (save-excursion
  26369. (condition-case nil
  26370. (progn
  26371. (org-back-to-heading t)
  26372. (outline-flag-region
  26373. (max (point-min) (1- (point)))
  26374. (save-excursion
  26375. (re-search-forward
  26376. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26377. (or (match-beginning 1) (point-max)))
  26378. nil))
  26379. (error nil))))
  26380. (defun org-make-options-regexp (kwds)
  26381. "Make a regular expression for keyword lines."
  26382. (concat
  26383. "^"
  26384. "#?[ \t]*\\+\\("
  26385. (mapconcat 'regexp-quote kwds "\\|")
  26386. "\\):[ \t]*"
  26387. "\\(.+\\)"))
  26388. ;; Make isearch reveal the necessary context
  26389. (defun org-isearch-end ()
  26390. "Reveal context after isearch exits."
  26391. (when isearch-success ; only if search was successful
  26392. (if (featurep 'xemacs)
  26393. ;; Under XEmacs, the hook is run in the correct place,
  26394. ;; we directly show the context.
  26395. (org-show-context 'isearch)
  26396. ;; In Emacs the hook runs *before* restoring the overlays.
  26397. ;; So we have to use a one-time post-command-hook to do this.
  26398. ;; (Emacs 22 has a special variable, see function `org-mode')
  26399. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26400. isearch-mode-end-hook-quit)
  26401. ;; Only when the isearch was not quitted.
  26402. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26403. 'append 'local)))))
  26404. (defun org-isearch-post-command ()
  26405. "Remove self from hook, and show context."
  26406. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26407. (org-show-context 'isearch))
  26408. ;;;; Integration with and fixes for other packages
  26409. ;;; Imenu support
  26410. (defvar org-imenu-markers nil
  26411. "All markers currently used by Imenu.")
  26412. (make-variable-buffer-local 'org-imenu-markers)
  26413. (defun org-imenu-new-marker (&optional pos)
  26414. "Return a new marker for use by Imenu, and remember the marker."
  26415. (let ((m (make-marker)))
  26416. (move-marker m (or pos (point)))
  26417. (push m org-imenu-markers)
  26418. m))
  26419. (defun org-imenu-get-tree ()
  26420. "Produce the index for Imenu."
  26421. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26422. (setq org-imenu-markers nil)
  26423. (let* ((n org-imenu-depth)
  26424. (re (concat "^" outline-regexp))
  26425. (subs (make-vector (1+ n) nil))
  26426. (last-level 0)
  26427. m tree level head)
  26428. (save-excursion
  26429. (save-restriction
  26430. (widen)
  26431. (goto-char (point-max))
  26432. (while (re-search-backward re nil t)
  26433. (setq level (org-reduced-level (funcall outline-level)))
  26434. (when (<= level n)
  26435. (looking-at org-complex-heading-regexp)
  26436. (setq head (org-match-string-no-properties 4)
  26437. m (org-imenu-new-marker))
  26438. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26439. (if (>= level last-level)
  26440. (push (cons head m) (aref subs level))
  26441. (push (cons head (aref subs (1+ level))) (aref subs level))
  26442. (loop for i from (1+ level) to n do (aset subs i nil)))
  26443. (setq last-level level)))))
  26444. (aref subs 1)))
  26445. (eval-after-load "imenu"
  26446. '(progn
  26447. (add-hook 'imenu-after-jump-hook
  26448. (lambda () (org-show-context 'org-goto)))))
  26449. ;; Speedbar support
  26450. (defun org-speedbar-set-agenda-restriction ()
  26451. "Restrict future agenda commands to the location at point in speedbar.
  26452. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26453. (interactive)
  26454. (let (p m tp np dir txt w)
  26455. (cond
  26456. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26457. 'org-imenu t))
  26458. (setq m (get-text-property p 'org-imenu-marker))
  26459. (save-excursion
  26460. (save-restriction
  26461. (set-buffer (marker-buffer m))
  26462. (goto-char m)
  26463. (org-agenda-set-restriction-lock 'subtree))))
  26464. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26465. 'speedbar-function 'speedbar-find-file))
  26466. (setq tp (previous-single-property-change
  26467. (1+ p) 'speedbar-function)
  26468. np (next-single-property-change
  26469. tp 'speedbar-function)
  26470. dir (speedbar-line-directory)
  26471. txt (buffer-substring-no-properties (or tp (point-min))
  26472. (or np (point-max))))
  26473. (save-excursion
  26474. (save-restriction
  26475. (set-buffer (find-file-noselect
  26476. (let ((default-directory dir))
  26477. (expand-file-name txt))))
  26478. (unless (org-mode-p)
  26479. (error "Cannot restrict to non-Org-mode file"))
  26480. (org-agenda-set-restriction-lock 'file))))
  26481. (t (error "Don't know how to restrict Org-mode's agenda")))
  26482. (org-move-overlay org-speedbar-restriction-lock-overlay
  26483. (point-at-bol) (point-at-eol))
  26484. (setq current-prefix-arg nil)
  26485. (org-agenda-maybe-redo)))
  26486. (eval-after-load "speedbar"
  26487. '(progn
  26488. (speedbar-add-supported-extension ".org")
  26489. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26490. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26491. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26492. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26493. (add-hook 'speedbar-visiting-tag-hook
  26494. (lambda () (org-show-context 'org-goto)))))
  26495. ;;; Fixes and Hacks
  26496. ;; Make flyspell not check words in links, to not mess up our keymap
  26497. (defun org-mode-flyspell-verify ()
  26498. "Don't let flyspell put overlays at active buttons."
  26499. (not (get-text-property (point) 'keymap)))
  26500. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26501. (eval-after-load "bookmark"
  26502. '(if (boundp 'bookmark-after-jump-hook)
  26503. ;; We can use the hook
  26504. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26505. ;; Hook not available, use advice
  26506. (defadvice bookmark-jump (after org-make-visible activate)
  26507. "Make the position visible."
  26508. (org-bookmark-jump-unhide))))
  26509. (defun org-bookmark-jump-unhide ()
  26510. "Unhide the current position, to show the bookmark location."
  26511. (and (org-mode-p)
  26512. (or (org-invisible-p)
  26513. (save-excursion (goto-char (max (point-min) (1- (point))))
  26514. (org-invisible-p)))
  26515. (org-show-context 'bookmark-jump)))
  26516. ;; Fix a bug in htmlize where there are text properties (face nil)
  26517. (eval-after-load "htmlize"
  26518. '(progn
  26519. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26520. "Make sure there are no nil faces"
  26521. (setq ad-return-value (delq nil ad-return-value)))))
  26522. ;; Make session.el ignore our circular variable
  26523. (eval-after-load "session"
  26524. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26525. ;;;; Experimental code
  26526. (defun org-closed-in-range ()
  26527. "Sparse tree of items closed in a certain time range.
  26528. Still experimental, may disappear in the future."
  26529. (interactive)
  26530. ;; Get the time interval from the user.
  26531. (let* ((time1 (time-to-seconds
  26532. (org-read-date nil 'to-time nil "Starting date: ")))
  26533. (time2 (time-to-seconds
  26534. (org-read-date nil 'to-time nil "End date:")))
  26535. ;; callback function
  26536. (callback (lambda ()
  26537. (let ((time
  26538. (time-to-seconds
  26539. (apply 'encode-time
  26540. (org-parse-time-string
  26541. (match-string 1))))))
  26542. ;; check if time in interval
  26543. (and (>= time time1) (<= time time2))))))
  26544. ;; make tree, check each match with the callback
  26545. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26546. ;;;; Finish up
  26547. (provide 'org)
  26548. (run-hooks 'org-load-hook)
  26549. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26550. ;;; org.el ends here