org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691
  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. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.22a+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. (defcustom org-load-hook '(org-load-default-extensions)
  138. "Hook that is run after org.el has been loaded.
  139. This happens also after `org' has been provided, so
  140. requiring something in this hook that does a (require 'org) is ok."
  141. :group 'org
  142. :type 'hook)
  143. (defcustom org-default-extensions '(org-irc)
  144. "Extensions that should always be loaded together with org.el.
  145. If the description starts with <A>, this means the extension
  146. will be autoloaded when needed, preloading is not necessary."
  147. :group 'org
  148. :type
  149. '(set :greedy t
  150. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  151. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  152. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  153. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  154. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  155. (defun org-load-default-extensions ()
  156. "Load all extensions listed in `org-default-extensions'."
  157. (mapc (lambda (ext)
  158. (condition-case nil (require ext)
  159. (error (message "Feature `%s' is not known" ext))))
  160. org-default-extensions))
  161. ;; FIXME: Needs a separate group...
  162. (defcustom org-completion-fallback-command 'hippie-expand
  163. "The expansion command called by \\[org-complete] in normal context.
  164. Normal means, no org-mode-specific context."
  165. :group 'org
  166. :type 'function)
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  315. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  316. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  317. "Regular expression for specifying repeated events.
  318. After a match, group 1 contains the repeat expression.")
  319. (defgroup org-structure nil
  320. "Options concerning the general structure of Org-mode files."
  321. :tag "Org Structure"
  322. :group 'org)
  323. (defgroup org-reveal-location nil
  324. "Options about how to make context of a location visible."
  325. :tag "Org Reveal Location"
  326. :group 'org-structure)
  327. (defconst org-context-choice
  328. '(choice
  329. (const :tag "Always" t)
  330. (const :tag "Never" nil)
  331. (repeat :greedy t :tag "Individual contexts"
  332. (cons
  333. (choice :tag "Context"
  334. (const agenda)
  335. (const org-goto)
  336. (const occur-tree)
  337. (const tags-tree)
  338. (const link-search)
  339. (const mark-goto)
  340. (const bookmark-jump)
  341. (const isearch)
  342. (const default))
  343. (boolean))))
  344. "Contexts for the reveal options.")
  345. (defcustom org-show-hierarchy-above '((default . t))
  346. "Non-nil means, show full hierarchy when revealing a location.
  347. Org-mode often shows locations in an org-mode file which might have
  348. been invisible before. When this is set, the hierarchy of headings
  349. above the exposed location is shown.
  350. Turning this off for example for sparse trees makes them very compact.
  351. Instead of t, this can also be an alist specifying this option for different
  352. contexts. Valid contexts are
  353. agenda when exposing an entry from the agenda
  354. org-goto when using the command `org-goto' on key C-c C-j
  355. occur-tree when using the command `org-occur' on key C-c /
  356. tags-tree when constructing a sparse tree based on tags matches
  357. link-search when exposing search matches associated with a link
  358. mark-goto when exposing the jump goal of a mark
  359. bookmark-jump when exposing a bookmark location
  360. isearch when exiting from an incremental search
  361. default default for all contexts not set explicitly"
  362. :group 'org-reveal-location
  363. :type org-context-choice)
  364. (defcustom org-show-following-heading '((default . nil))
  365. "Non-nil means, show following heading when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the heading following the
  368. match is shown.
  369. Turning this off for example for sparse trees makes them very compact,
  370. but makes it harder to edit the location of the match. In such a case,
  371. use the command \\[org-reveal] to show more context.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defcustom org-show-siblings '((default . nil) (isearch t))
  377. "Non-nil means, show all sibling heading when revealing a location.
  378. Org-mode often shows locations in an org-mode file which might have
  379. been invisible before. When this is set, the sibling of the current entry
  380. heading are all made visible. If `org-show-hierarchy-above' is t,
  381. the same happens on each level of the hierarchy above the current entry.
  382. By default this is on for the isearch context, off for all other contexts.
  383. Turning this off for example for sparse trees makes them very compact,
  384. but makes it harder to edit the location of the match. In such a case,
  385. use the command \\[org-reveal] to show more context.
  386. Instead of t, this can also be an alist specifying this option for different
  387. contexts. See `org-show-hierarchy-above' for valid contexts."
  388. :group 'org-reveal-location
  389. :type org-context-choice)
  390. (defcustom org-show-entry-below '((default . nil))
  391. "Non-nil means, show the entry below a headline when revealing a location.
  392. Org-mode often shows locations in an org-mode file which might have
  393. been invisible before. When this is set, the text below the headline that is
  394. exposed is also shown.
  395. By default this is off for all contexts.
  396. Instead of t, this can also be an alist specifying this option for different
  397. contexts. See `org-show-hierarchy-above' for valid contexts."
  398. :group 'org-reveal-location
  399. :type org-context-choice)
  400. (defgroup org-cycle nil
  401. "Options concerning visibility cycling in Org-mode."
  402. :tag "Org Cycle"
  403. :group 'org-structure)
  404. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  405. "Names of drawers. Drawers are not opened by cycling on the headline above.
  406. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  407. this:
  408. :DRAWERNAME:
  409. .....
  410. :END:
  411. The drawer \"PROPERTIES\" is special for capturing properties through
  412. the property API.
  413. Drawers can be defined on the per-file basis with a line like:
  414. #+DRAWERS: HIDDEN STATE PROPERTIES"
  415. :group 'org-structure
  416. :type '(repeat (string :tag "Drawer Name")))
  417. (defcustom org-cycle-global-at-bob nil
  418. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  419. This makes it possible to do global cycling without having to use S-TAB or
  420. C-u TAB. For this special case to work, the first line of the buffer
  421. must not be a headline - it may be empty ot some other text. When used in
  422. this way, `org-cycle-hook' is disables temporarily, to make sure the
  423. cursor stays at the beginning of the buffer.
  424. When this option is nil, don't do anything special at the beginning
  425. of the buffer."
  426. :group 'org-cycle
  427. :type 'boolean)
  428. (defcustom org-cycle-emulate-tab t
  429. "Where should `org-cycle' emulate TAB.
  430. nil Never
  431. white Only in completely white lines
  432. whitestart Only at the beginning of lines, before the first non-white char
  433. t Everywhere except in headlines
  434. exc-hl-bol Everywhere except at the start of a headline
  435. If TAB is used in a place where it does not emulate TAB, the current subtree
  436. visibility is cycled."
  437. :group 'org-cycle
  438. :type '(choice (const :tag "Never" nil)
  439. (const :tag "Only in completely white lines" white)
  440. (const :tag "Before first char in a line" whitestart)
  441. (const :tag "Everywhere except in headlines" t)
  442. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  443. ))
  444. (defcustom org-cycle-separator-lines 2
  445. "Number of empty lines needed to keep an empty line between collapsed trees.
  446. If you leave an empty line between the end of a subtree and the following
  447. headline, this empty line is hidden when the subtree is folded.
  448. Org-mode will leave (exactly) one empty line visible if the number of
  449. empty lines is equal or larger to the number given in this variable.
  450. So the default 2 means, at least 2 empty lines after the end of a subtree
  451. are needed to produce free space between a collapsed subtree and the
  452. following headline.
  453. Special case: when 0, never leave empty lines in collapsed view."
  454. :group 'org-cycle
  455. :type 'integer)
  456. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  457. org-cycle-hide-drawers
  458. org-cycle-show-empty-lines
  459. org-optimize-window-after-visibility-change)
  460. "Hook that is run after `org-cycle' has changed the buffer visibility.
  461. The function(s) in this hook must accept a single argument which indicates
  462. the new state that was set by the most recent `org-cycle' command. The
  463. argument is a symbol. After a global state change, it can have the values
  464. `overview', `content', or `all'. After a local state change, it can have
  465. the values `folded', `children', or `subtree'."
  466. :group 'org-cycle
  467. :type 'hook)
  468. (defgroup org-edit-structure nil
  469. "Options concerning structure editing in Org-mode."
  470. :tag "Org Edit Structure"
  471. :group 'org-structure)
  472. (defcustom org-odd-levels-only nil
  473. "Non-nil means, skip even levels and only use odd levels for the outline.
  474. This has the effect that two stars are being added/taken away in
  475. promotion/demotion commands. It also influences how levels are
  476. handled by the exporters.
  477. Changing it requires restart of `font-lock-mode' to become effective
  478. for fontification also in regions already fontified.
  479. You may also set this on a per-file basis by adding one of the following
  480. lines to the buffer:
  481. #+STARTUP: odd
  482. #+STARTUP: oddeven"
  483. :group 'org-edit-structure
  484. :group 'org-font-lock
  485. :type 'boolean)
  486. (defcustom org-adapt-indentation t
  487. "Non-nil means, adapt indentation when promoting and demoting.
  488. When this is set and the *entire* text in an entry is indented, the
  489. indentation is increased by one space in a demotion command, and
  490. decreased by one in a promotion command. If any line in the entry
  491. body starts at column 0, indentation is not changed at all."
  492. :group 'org-edit-structure
  493. :type 'boolean)
  494. (defcustom org-special-ctrl-a/e nil
  495. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  496. When t, `C-a' will bring back the cursor to the beginning of the
  497. headline text, i.e. after the stars and after a possible TODO keyword.
  498. In an item, this will be the position after the bullet.
  499. When the cursor is already at that position, another `C-a' will bring
  500. it to the beginning of the line.
  501. `C-e' will jump to the end of the headline, ignoring the presence of tags
  502. in the headline. A second `C-e' will then jump to the true end of the
  503. line, after any tags.
  504. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  505. and only a directly following, identical keypress will bring the cursor
  506. to the special positions."
  507. :group 'org-edit-structure
  508. :type '(choice
  509. (const :tag "off" nil)
  510. (const :tag "after bullet first" t)
  511. (const :tag "border first" reversed)))
  512. (if (fboundp 'defvaralias)
  513. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  514. (defcustom org-special-ctrl-k nil
  515. "Non-nil means `C-k' will behave specially in headlines.
  516. When nil, `C-k' will call the default `kill-line' command.
  517. When t, the following will happen while the cursor is in the headline:
  518. - When the cursor is at the beginning of a headline, kill the entire
  519. line and possible the folded subtree below the line.
  520. - When in the middle of the headline text, kill the headline up to the tags.
  521. - When after the headline text, kill the tags."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defcustom org-M-RET-may-split-line '((default . t))
  525. "Non-nil means, M-RET will split the line at the cursor position.
  526. When nil, it will go to the end of the line before making a
  527. new line.
  528. You may also set this option in a different way for different
  529. contexts. Valid contexts are:
  530. headline when creating a new headline
  531. item when creating a new item
  532. table in a table field
  533. default the value to be used for all contexts not explicitly
  534. customized"
  535. :group 'org-structure
  536. :group 'org-table
  537. :type '(choice
  538. (const :tag "Always" t)
  539. (const :tag "Never" nil)
  540. (repeat :greedy t :tag "Individual contexts"
  541. (cons
  542. (choice :tag "Context"
  543. (const headline)
  544. (const item)
  545. (const table)
  546. (const default))
  547. (boolean)))))
  548. (defcustom org-blank-before-new-entry '((heading . nil)
  549. (plain-list-item . nil))
  550. "Should `org-insert-heading' leave a blank line before new heading/item?
  551. The value is an alist, with `heading' and `plain-list-item' as car,
  552. and a boolean flag as cdr."
  553. :group 'org-edit-structure
  554. :type '(list
  555. (cons (const heading) (boolean))
  556. (cons (const plain-list-item) (boolean))))
  557. (defcustom org-insert-heading-hook nil
  558. "Hook being run after inserting a new heading."
  559. :group 'org-edit-structure
  560. :type 'hook)
  561. (defcustom org-enable-fixed-width-editor t
  562. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  563. This currently only means, they are never auto-wrapped.
  564. When nil, such lines will be treated like ordinary lines.
  565. See also the QUOTE keyword."
  566. :group 'org-edit-structure
  567. :type 'boolean)
  568. (defcustom org-goto-auto-isearch t
  569. "Non-nil means, typing characters in org-goto starts incremental search."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defgroup org-sparse-trees nil
  573. "Options concerning sparse trees in Org-mode."
  574. :tag "Org Sparse Trees"
  575. :group 'org-structure)
  576. (defcustom org-highlight-sparse-tree-matches t
  577. "Non-nil means, highlight all matches that define a sparse tree.
  578. The highlights will automatically disappear the next time the buffer is
  579. changed by an edit command."
  580. :group 'org-sparse-trees
  581. :type 'boolean)
  582. (defcustom org-remove-highlights-with-change t
  583. "Non-nil means, any change to the buffer will remove temporary highlights.
  584. Such highlights are created by `org-occur' and `org-clock-display'.
  585. When nil, `C-c C-c needs to be used to get rid of the highlights.
  586. The highlights created by `org-preview-latex-fragment' always need
  587. `C-c C-c' to be removed."
  588. :group 'org-sparse-trees
  589. :group 'org-time
  590. :type 'boolean)
  591. (defcustom org-occur-hook '(org-first-headline-recenter)
  592. "Hook that is run after `org-occur' has constructed a sparse tree.
  593. This can be used to recenter the window to show as much of the structure
  594. as possible."
  595. :group 'org-sparse-trees
  596. :type 'hook)
  597. (defgroup org-plain-lists nil
  598. "Options concerning plain lists in Org-mode."
  599. :tag "Org Plain lists"
  600. :group 'org-structure)
  601. (defcustom org-cycle-include-plain-lists nil
  602. "Non-nil means, include plain lists into visibility cycling.
  603. This means that during cycling, plain list items will *temporarily* be
  604. interpreted as outline headlines with a level given by 1000+i where i is the
  605. indentation of the bullet. In all other operations, plain list items are
  606. not seen as headlines. For example, you cannot assign a TODO keyword to
  607. such an item."
  608. :group 'org-plain-lists
  609. :type 'boolean)
  610. (defcustom org-plain-list-ordered-item-terminator t
  611. "The character that makes a line with leading number an ordered list item.
  612. Valid values are ?. and ?\). To get both terminators, use t. While
  613. ?. may look nicer, it creates the danger that a line with leading
  614. number may be incorrectly interpreted as an item. ?\) therefore is
  615. the safe choice."
  616. :group 'org-plain-lists
  617. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  618. (const :tag "paren like in \"2)\"" ?\))
  619. (const :tab "both" t)))
  620. (defcustom org-auto-renumber-ordered-lists t
  621. "Non-nil means, automatically renumber ordered plain lists.
  622. Renumbering happens when the sequence have been changed with
  623. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  624. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  625. :group 'org-plain-lists
  626. :type 'boolean)
  627. (defcustom org-provide-checkbox-statistics t
  628. "Non-nil means, update checkbox statistics after insert and toggle.
  629. When this is set, checkbox statistics is updated each time you either insert
  630. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  631. with \\[org-ctrl-c-ctrl-c\\]."
  632. :group 'org-plain-lists
  633. :type 'boolean)
  634. (defgroup org-archive nil
  635. "Options concerning archiving in Org-mode."
  636. :tag "Org Archive"
  637. :group 'org-structure)
  638. (defcustom org-archive-tag "ARCHIVE"
  639. "The tag that marks a subtree as archived.
  640. An archived subtree does not open during visibility cycling, and does
  641. not contribute to the agenda listings.
  642. After changing this, font-lock must be restarted in the relevant buffers to
  643. get the proper fontification."
  644. :group 'org-archive
  645. :group 'org-keywords
  646. :type 'string)
  647. (defcustom org-agenda-skip-archived-trees t
  648. "Non-nil means, the agenda will skip any items located in archived trees.
  649. An archived tree is a tree marked with the tag ARCHIVE."
  650. :group 'org-archive
  651. :group 'org-agenda-skip
  652. :type 'boolean)
  653. (defcustom org-cycle-open-archived-trees nil
  654. "Non-nil means, `org-cycle' will open archived trees.
  655. An archived tree is a tree marked with the tag ARCHIVE.
  656. When nil, archived trees will stay folded. You can still open them with
  657. normal outline commands like `show-all', but not with the cycling commands."
  658. :group 'org-archive
  659. :group 'org-cycle
  660. :type 'boolean)
  661. (defcustom org-sparse-tree-open-archived-trees nil
  662. "Non-nil means sparse tree construction shows matches in archived trees.
  663. When nil, matches in these trees are highlighted, but the trees are kept in
  664. collapsed state."
  665. :group 'org-archive
  666. :group 'org-sparse-trees
  667. :type 'boolean)
  668. (defcustom org-archive-location "%s_archive::"
  669. "The location where subtrees should be archived.
  670. This string consists of two parts, separated by a double-colon.
  671. The first part is a file name - when omitted, archiving happens in the same
  672. file. %s will be replaced by the current file name (without directory part).
  673. Archiving to a different file is useful to keep archived entries from
  674. contributing to the Org-mode Agenda.
  675. The part after the double colon is a headline. The archived entries will be
  676. filed under that headline. When omitted, the subtrees are simply filed away
  677. at the end of the file, as top-level entries.
  678. Here are a few examples:
  679. \"%s_archive::\"
  680. If the current file is Projects.org, archive in file
  681. Projects.org_archive, as top-level trees. This is the default.
  682. \"::* Archived Tasks\"
  683. Archive in the current file, under the top-level headline
  684. \"* Archived Tasks\".
  685. \"~/org/archive.org::\"
  686. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  687. \"basement::** Finished Tasks\"
  688. Archive in file ./basement (relative path), as level 3 trees
  689. below the level 2 heading \"** Finished Tasks\".
  690. You may set this option on a per-file basis by adding to the buffer a
  691. line like
  692. #+ARCHIVE: basement::** Finished Tasks"
  693. :group 'org-archive
  694. :type 'string)
  695. (defcustom org-archive-mark-done t
  696. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  697. This can be a string to set the keyword to use. When t, Org-mode will
  698. use the first keyword in its list that means done."
  699. :group 'org-archive
  700. :type '(choice
  701. (const :tag "No" nil)
  702. (const :tag "Yes" t)
  703. (string :tag "Use this keyword")))
  704. (defcustom org-archive-stamp-time t
  705. "Non-nil means, add a time stamp to entries moved to an archive file.
  706. This variable is obsolete and has no effect anymore, instead add ot remove
  707. `time' from the variablle `org-archive-save-context-info'."
  708. :group 'org-archive
  709. :type 'boolean)
  710. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  711. "Parts of context info that should be stored as properties when archiving.
  712. When a subtree is moved to an archive file, it looses information given by
  713. context, like inherited tags, the category, and possibly also the TODO
  714. state (depending on the variable `org-archive-mark-done').
  715. This variable can be a list of any of the following symbols:
  716. time The time of archiving.
  717. file The file where the entry originates.
  718. itags The local tags, in the headline of the subtree.
  719. ltags The tags the subtree inherits from further up the hierarchy.
  720. todo The pre-archive TODO state.
  721. category The category, taken from file name or #+CATEGORY lines.
  722. olpath The outline path to the item. These are all headlines above
  723. the current item, separated by /, like a file path.
  724. For each symbol present in the list, a property will be created in
  725. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  726. information."
  727. :group 'org-archive
  728. :type '(set :greedy t
  729. (const :tag "Time" time)
  730. (const :tag "File" file)
  731. (const :tag "Category" category)
  732. (const :tag "TODO state" todo)
  733. (const :tag "TODO state" priority)
  734. (const :tag "Inherited tags" itags)
  735. (const :tag "Outline path" olpath)
  736. (const :tag "Local tags" ltags)))
  737. (defgroup org-imenu-and-speedbar nil
  738. "Options concerning imenu and speedbar in Org-mode."
  739. :tag "Org Imenu and Speedbar"
  740. :group 'org-structure)
  741. (defcustom org-imenu-depth 2
  742. "The maximum level for Imenu access to Org-mode headlines.
  743. This also applied for speedbar access."
  744. :group 'org-imenu-and-speedbar
  745. :type 'number)
  746. (defgroup org-table nil
  747. "Options concerning tables in Org-mode."
  748. :tag "Org Table"
  749. :group 'org)
  750. (defcustom org-enable-table-editor 'optimized
  751. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  752. When nil, such lines will be treated like ordinary lines.
  753. When equal to the symbol `optimized', the table editor will be optimized to
  754. do the following:
  755. - Automatic overwrite mode in front of whitespace in table fields.
  756. This makes the structure of the table stay in tact as long as the edited
  757. field does not exceed the column width.
  758. - Minimize the number of realigns. Normally, the table is aligned each time
  759. TAB or RET are pressed to move to another field. With optimization this
  760. happens only if changes to a field might have changed the column width.
  761. Optimization requires replacing the functions `self-insert-command',
  762. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  763. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  764. very good at guessing when a re-align will be necessary, but you can always
  765. force one with \\[org-ctrl-c-ctrl-c].
  766. If you would like to use the optimized version in Org-mode, but the
  767. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  768. This variable can be used to turn on and off the table editor during a session,
  769. but in order to toggle optimization, a restart is required.
  770. See also the variable `org-table-auto-blank-field'."
  771. :group 'org-table
  772. :type '(choice
  773. (const :tag "off" nil)
  774. (const :tag "on" t)
  775. (const :tag "on, optimized" optimized)))
  776. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  777. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  778. In the optimized version, the table editor takes over all simple keys that
  779. normally just insert a character. In tables, the characters are inserted
  780. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  781. for empty fields). Outside tables, the correct binding of the keys is
  782. restored.
  783. The default for this option is t if the optimized version is also used in
  784. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  785. this variable requires a restart of Emacs to become effective."
  786. :group 'org-table
  787. :type 'boolean)
  788. (defcustom orgtbl-radio-table-templates
  789. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  790. % END RECEIVE ORGTBL %n
  791. \\begin{comment}
  792. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  793. | | |
  794. \\end{comment}\n")
  795. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  796. @c END RECEIVE ORGTBL %n
  797. @ignore
  798. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  799. | | |
  800. @end ignore\n")
  801. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  802. <!-- END RECEIVE ORGTBL %n -->
  803. <!--
  804. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  805. | | |
  806. -->\n"))
  807. "Templates for radio tables in different major modes.
  808. All occurrences of %n in a template will be replaced with the name of the
  809. table, obtained by prompting the user."
  810. :group 'org-table
  811. :type '(repeat
  812. (list (symbol :tag "Major mode")
  813. (string :tag "Format"))))
  814. (defgroup org-table-settings nil
  815. "Settings for tables in Org-mode."
  816. :tag "Org Table Settings"
  817. :group 'org-table)
  818. (defcustom org-table-default-size "5x2"
  819. "The default size for newly created tables, Columns x Rows."
  820. :group 'org-table-settings
  821. :type 'string)
  822. (defcustom org-table-number-regexp
  823. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  824. "Regular expression for recognizing numbers in table columns.
  825. If a table column contains mostly numbers, it will be aligned to the
  826. right. If not, it will be aligned to the left.
  827. The default value of this option is a regular expression which allows
  828. anything which looks remotely like a number as used in scientific
  829. context. For example, all of the following will be considered a
  830. number:
  831. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  832. Other options offered by the customize interface are more restrictive."
  833. :group 'org-table-settings
  834. :type '(choice
  835. (const :tag "Positive Integers"
  836. "^[0-9]+$")
  837. (const :tag "Integers"
  838. "^[-+]?[0-9]+$")
  839. (const :tag "Floating Point Numbers"
  840. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  841. (const :tag "Floating Point Number or Integer"
  842. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  843. (const :tag "Exponential, Floating point, Integer"
  844. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  845. (const :tag "Very General Number-Like, including hex"
  846. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  847. (string :tag "Regexp:")))
  848. (defcustom org-table-number-fraction 0.5
  849. "Fraction of numbers in a column required to make the column align right.
  850. In a column all non-white fields are considered. If at least this
  851. fraction of fields is matched by `org-table-number-fraction',
  852. alignment to the right border applies."
  853. :group 'org-table-settings
  854. :type 'number)
  855. (defgroup org-table-editing nil
  856. "Behavior of tables during editing in Org-mode."
  857. :tag "Org Table Editing"
  858. :group 'org-table)
  859. (defcustom org-table-automatic-realign t
  860. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  861. When nil, aligning is only done with \\[org-table-align], or after column
  862. removal/insertion."
  863. :group 'org-table-editing
  864. :type 'boolean)
  865. (defcustom org-table-auto-blank-field t
  866. "Non-nil means, automatically blank table field when starting to type into it.
  867. This only happens when typing immediately after a field motion
  868. command (TAB, S-TAB or RET).
  869. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  870. :group 'org-table-editing
  871. :type 'boolean)
  872. (defcustom org-table-tab-jumps-over-hlines t
  873. "Non-nil means, tab in the last column of a table with jump over a hline.
  874. If a horizontal separator line is following the current line,
  875. `org-table-next-field' can either create a new row before that line, or jump
  876. over the line. When this option is nil, a new line will be created before
  877. this line."
  878. :group 'org-table-editing
  879. :type 'boolean)
  880. (defcustom org-table-tab-recognizes-table.el t
  881. "Non-nil means, TAB will automatically notice a table.el table.
  882. When it sees such a table, it moves point into it and - if necessary -
  883. calls `table-recognize-table'."
  884. :group 'org-table-editing
  885. :type 'boolean)
  886. (defgroup org-table-calculation nil
  887. "Options concerning tables in Org-mode."
  888. :tag "Org Table Calculation"
  889. :group 'org-table)
  890. (defcustom org-table-use-standard-references t
  891. "Should org-mode work with table refrences like B3 instead of @3$2?
  892. Possible values are:
  893. nil never use them
  894. from accept as input, do not present for editing
  895. t: accept as input and present for editing"
  896. :group 'org-table-calculation
  897. :type '(choice
  898. (const :tag "Never, don't even check unser input for them" nil)
  899. (const :tag "Always, both as user input, and when editing" t)
  900. (const :tag "Convert user input, don't offer during editing" 'from)))
  901. (defcustom org-table-copy-increment t
  902. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  903. :group 'org-table-calculation
  904. :type 'boolean)
  905. (defcustom org-calc-default-modes
  906. '(calc-internal-prec 12
  907. calc-float-format (float 5)
  908. calc-angle-mode deg
  909. calc-prefer-frac nil
  910. calc-symbolic-mode nil
  911. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  912. calc-display-working-message t
  913. )
  914. "List with Calc mode settings for use in calc-eval for table formulas.
  915. The list must contain alternating symbols (Calc modes variables and values).
  916. Don't remove any of the default settings, just change the values. Org-mode
  917. relies on the variables to be present in the list."
  918. :group 'org-table-calculation
  919. :type 'plist)
  920. (defcustom org-table-formula-evaluate-inline t
  921. "Non-nil means, TAB and RET evaluate a formula in current table field.
  922. If the current field starts with an equal sign, it is assumed to be a formula
  923. which should be evaluated as described in the manual and in the documentation
  924. string of the command `org-table-eval-formula'. This feature requires the
  925. Emacs calc package.
  926. When this variable is nil, formula calculation is only available through
  927. the command \\[org-table-eval-formula]."
  928. :group 'org-table-calculation
  929. :type 'boolean)
  930. (defcustom org-table-formula-use-constants t
  931. "Non-nil means, interpret constants in formulas in tables.
  932. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  933. by the value given in `org-table-formula-constants', or by a value obtained
  934. from the `constants.el' package."
  935. :group 'org-table-calculation
  936. :type 'boolean)
  937. (defcustom org-table-formula-constants nil
  938. "Alist with constant names and values, for use in table formulas.
  939. The car of each element is a name of a constant, without the `$' before it.
  940. The cdr is the value as a string. For example, if you'd like to use the
  941. speed of light in a formula, you would configure
  942. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  943. and then use it in an equation like `$1*$c'.
  944. Constants can also be defined on a per-file basis using a line like
  945. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  946. :group 'org-table-calculation
  947. :type '(repeat
  948. (cons (string :tag "name")
  949. (string :tag "value"))))
  950. (defvar org-table-formula-constants-local nil
  951. "Local version of `org-table-formula-constants'.")
  952. (make-variable-buffer-local 'org-table-formula-constants-local)
  953. (defcustom org-table-allow-automatic-line-recalculation t
  954. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  955. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  956. :group 'org-table-calculation
  957. :type 'boolean)
  958. (defgroup org-link nil
  959. "Options concerning links in Org-mode."
  960. :tag "Org Link"
  961. :group 'org)
  962. (defvar org-link-abbrev-alist-local nil
  963. "Buffer-local version of `org-link-abbrev-alist', which see.
  964. The value of this is taken from the #+LINK lines.")
  965. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  966. (defcustom org-link-abbrev-alist nil
  967. "Alist of link abbreviations.
  968. The car of each element is a string, to be replaced at the start of a link.
  969. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  970. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  971. [[linkkey:tag][description]]
  972. If REPLACE is a string, the tag will simply be appended to create the link.
  973. If the string contains \"%s\", the tag will be inserted there.
  974. REPLACE may also be a function that will be called with the tag as the
  975. only argument to create the link, which should be returned as a string.
  976. See the manual for examples."
  977. :group 'org-link
  978. :type 'alist)
  979. (defcustom org-descriptive-links t
  980. "Non-nil means, hide link part and only show description of bracket links.
  981. Bracket links are like [[link][descritpion]]. This variable sets the initial
  982. state in new org-mode buffers. The setting can then be toggled on a
  983. per-buffer basis from the Org->Hyperlinks menu."
  984. :group 'org-link
  985. :type 'boolean)
  986. (defcustom org-link-file-path-type 'adaptive
  987. "How the path name in file links should be stored.
  988. Valid values are:
  989. relative Relative to the current directory, i.e. the directory of the file
  990. into which the link is being inserted.
  991. absolute Absolute path, if possible with ~ for home directory.
  992. noabbrev Absolute path, no abbreviation of home directory.
  993. adaptive Use relative path for files in the current directory and sub-
  994. directories of it. For other files, use an absolute path."
  995. :group 'org-link
  996. :type '(choice
  997. (const relative)
  998. (const absolute)
  999. (const noabbrev)
  1000. (const adaptive)))
  1001. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1002. "Types of links that should be activated in Org-mode files.
  1003. This is a list of symbols, each leading to the activation of a certain link
  1004. type. In principle, it does not hurt to turn on most link types - there may
  1005. be a small gain when turning off unused link types. The types are:
  1006. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1007. angular Links in angular brackes that may contain whitespace like
  1008. <bbdb:Carsten Dominik>.
  1009. plain Plain links in normal text, no whitespace, like http://google.com.
  1010. radio Text that is matched by a radio target, see manual for details.
  1011. tag Tag settings in a headline (link to tag search).
  1012. date Time stamps (link to calendar).
  1013. Changing this variable requires a restart of Emacs to become effective."
  1014. :group 'org-link
  1015. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1016. (const :tag "Angular bracket links (old style)" angular)
  1017. (const :tag "Plain text links" plain)
  1018. (const :tag "Radio target matches" radio)
  1019. (const :tag "Tags" tag)
  1020. (const :tag "Timestamps" date)))
  1021. (defgroup org-link-store nil
  1022. "Options concerning storing links in Org-mode"
  1023. :tag "Org Store Link"
  1024. :group 'org-link)
  1025. (defcustom org-email-link-description-format "Email %c: %.30s"
  1026. "Format of the description part of a link to an email or usenet message.
  1027. The following %-excapes will be replaced by corresponding information:
  1028. %F full \"From\" field
  1029. %f name, taken from \"From\" field, address if no name
  1030. %T full \"To\" field
  1031. %t first name in \"To\" field, address if no name
  1032. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1033. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1034. %s subject
  1035. %m message-id.
  1036. You may use normal field width specification between the % and the letter.
  1037. This is for example useful to limit the length of the subject.
  1038. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1039. :group 'org-link-store
  1040. :type 'string)
  1041. (defcustom org-from-is-user-regexp
  1042. (let (r1 r2)
  1043. (when (and user-mail-address (not (string= user-mail-address "")))
  1044. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1045. (when (and user-full-name (not (string= user-full-name "")))
  1046. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1047. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1048. "Regexp mached against the \"From:\" header of an email or usenet message.
  1049. It should match if the message is from the user him/herself."
  1050. :group 'org-link-store
  1051. :type 'regexp)
  1052. (defcustom org-context-in-file-links t
  1053. "Non-nil means, file links from `org-store-link' contain context.
  1054. A search string will be added to the file name with :: as separator and
  1055. used to find the context when the link is activated by the command
  1056. `org-open-at-point'.
  1057. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1058. negates this setting for the duration of the command."
  1059. :group 'org-link-store
  1060. :type 'boolean)
  1061. (defcustom org-keep-stored-link-after-insertion nil
  1062. "Non-nil means, keep link in list for entire session.
  1063. The command `org-store-link' adds a link pointing to the current
  1064. location to an internal list. These links accumulate during a session.
  1065. The command `org-insert-link' can be used to insert links into any
  1066. Org-mode file (offering completion for all stored links). When this
  1067. option is nil, every link which has been inserted once using \\[org-insert-link]
  1068. will be removed from the list, to make completing the unused links
  1069. more efficient."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-usenet-links-prefer-google nil
  1073. "Non-nil means, `org-store-link' will create web links to Google groups.
  1074. When nil, Gnus will be used for such links.
  1075. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1076. negates this setting for the duration of the command."
  1077. :group 'org-link-store
  1078. :type 'boolean)
  1079. (defgroup org-link-follow nil
  1080. "Options concerning following links in Org-mode"
  1081. :tag "Org Follow Link"
  1082. :group 'org-link)
  1083. (defcustom org-tab-follows-link nil
  1084. "Non-nil means, on links TAB will follow the link.
  1085. Needs to be set before org.el is loaded."
  1086. :group 'org-link-follow
  1087. :type 'boolean)
  1088. (defcustom org-return-follows-link nil
  1089. "Non-nil means, on links RET will follow the link.
  1090. Needs to be set before org.el is loaded."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-mouse-1-follows-link
  1094. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1095. "Non-nil means, mouse-1 on a link will follow the link.
  1096. A longer mouse click will still set point. Does not work on XEmacs.
  1097. Needs to be set before org.el is loaded."
  1098. :group 'org-link-follow
  1099. :type 'boolean)
  1100. (defcustom org-mark-ring-length 4
  1101. "Number of different positions to be recorded in the ring
  1102. Changing this requires a restart of Emacs to work correctly."
  1103. :group 'org-link-follow
  1104. :type 'interger)
  1105. (defcustom org-link-frame-setup
  1106. '((vm . vm-visit-folder-other-frame)
  1107. (gnus . gnus-other-frame)
  1108. (file . find-file-other-window))
  1109. "Setup the frame configuration for following links.
  1110. When following a link with Emacs, it may often be useful to display
  1111. this link in another window or frame. This variable can be used to
  1112. set this up for the different types of links.
  1113. For VM, use any of
  1114. `vm-visit-folder'
  1115. `vm-visit-folder-other-frame'
  1116. For Gnus, use any of
  1117. `gnus'
  1118. `gnus-other-frame'
  1119. For FILE, use any of
  1120. `find-file'
  1121. `find-file-other-window'
  1122. `find-file-other-frame'
  1123. For the calendar, use the variable `calendar-setup'.
  1124. For BBDB, it is currently only possible to display the matches in
  1125. another window."
  1126. :group 'org-link-follow
  1127. :type '(list
  1128. (cons (const vm)
  1129. (choice
  1130. (const vm-visit-folder)
  1131. (const vm-visit-folder-other-window)
  1132. (const vm-visit-folder-other-frame)))
  1133. (cons (const gnus)
  1134. (choice
  1135. (const gnus)
  1136. (const gnus-other-frame)))
  1137. (cons (const file)
  1138. (choice
  1139. (const find-file)
  1140. (const find-file-other-window)
  1141. (const find-file-other-frame)))))
  1142. (defcustom org-display-internal-link-with-indirect-buffer nil
  1143. "Non-nil means, use indirect buffer to display infile links.
  1144. Activating internal links (from one location in a file to another location
  1145. in the same file) normally just jumps to the location. When the link is
  1146. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1147. another window. When this option is set, the other window actually displays
  1148. an indirect buffer clone of the current buffer, to avoid any visibility
  1149. changes to the current buffer."
  1150. :group 'org-link-follow
  1151. :type 'boolean)
  1152. (defcustom org-open-non-existing-files nil
  1153. "Non-nil means, `org-open-file' will open non-existing files.
  1154. When nil, an error will be generated."
  1155. :group 'org-link-follow
  1156. :type 'boolean)
  1157. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1158. "Function and arguments to call for following mailto links.
  1159. This is a list with the first element being a lisp function, and the
  1160. remaining elements being arguments to the function. In string arguments,
  1161. %a will be replaced by the address, and %s will be replaced by the subject
  1162. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1163. :group 'org-link-follow
  1164. :type '(choice
  1165. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1166. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1167. (const :tag "message-mail" (message-mail "%a" "%s"))
  1168. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1169. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1170. "Non-nil means, ask for confirmation before executing shell links.
  1171. Shell links can be dangerous: just think about a link
  1172. [[shell:rm -rf ~/*][Google Search]]
  1173. This link would show up in your Org-mode document as \"Google Search\",
  1174. but really it would remove your entire home directory.
  1175. Therefore we advise against setting this variable to nil.
  1176. Just change it to `y-or-n-p' of you want to confirm with a
  1177. single keystroke rather than having to type \"yes\"."
  1178. :group 'org-link-follow
  1179. :type '(choice
  1180. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1181. (const :tag "with y-or-n (faster)" y-or-n-p)
  1182. (const :tag "no confirmation (dangerous)" nil)))
  1183. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1184. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1185. Elisp links can be dangerous: just think about a link
  1186. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1187. This link would show up in your Org-mode document as \"Google Search\",
  1188. but really it would remove your entire home directory.
  1189. Therefore we advise against setting this variable to nil.
  1190. Just change it to `y-or-n-p' of you want to confirm with a
  1191. single keystroke rather than having to type \"yes\"."
  1192. :group 'org-link-follow
  1193. :type '(choice
  1194. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1195. (const :tag "with y-or-n (faster)" y-or-n-p)
  1196. (const :tag "no confirmation (dangerous)" nil)))
  1197. (defconst org-file-apps-defaults-gnu
  1198. '((remote . emacs)
  1199. (t . mailcap))
  1200. "Default file applications on a UNIX or GNU/Linux system.
  1201. See `org-file-apps'.")
  1202. (defconst org-file-apps-defaults-macosx
  1203. '((remote . emacs)
  1204. (t . "open %s")
  1205. ("ps" . "gv %s")
  1206. ("ps.gz" . "gv %s")
  1207. ("eps" . "gv %s")
  1208. ("eps.gz" . "gv %s")
  1209. ("dvi" . "xdvi %s")
  1210. ("fig" . "xfig %s"))
  1211. "Default file applications on a MacOS X system.
  1212. The system \"open\" is known as a default, but we use X11 applications
  1213. for some files for which the OS does not have a good default.
  1214. See `org-file-apps'.")
  1215. (defconst org-file-apps-defaults-windowsnt
  1216. (list
  1217. '(remote . emacs)
  1218. (cons t
  1219. (list (if (featurep 'xemacs)
  1220. 'mswindows-shell-execute
  1221. 'w32-shell-execute)
  1222. "open" 'file)))
  1223. "Default file applications on a Windows NT system.
  1224. The system \"open\" is used for most files.
  1225. See `org-file-apps'.")
  1226. (defcustom org-file-apps
  1227. '(
  1228. ("txt" . emacs)
  1229. ("tex" . emacs)
  1230. ("ltx" . emacs)
  1231. ("org" . emacs)
  1232. ("el" . emacs)
  1233. ("bib" . emacs)
  1234. )
  1235. "External applications for opening `file:path' items in a document.
  1236. Org-mode uses system defaults for different file types, but
  1237. you can use this variable to set the application for a given file
  1238. extension. The entries in this list are cons cells where the car identifies
  1239. files and the cdr the corresponding command. Possible values for the
  1240. file identifier are
  1241. \"ext\" A string identifying an extension
  1242. `directory' Matches a directory
  1243. `remote' Matches a remote file, accessible through tramp or efs.
  1244. Remote files most likely should be visited through Emacs
  1245. because external applications cannot handle such paths.
  1246. t Default for all remaining files
  1247. Possible values for the command are:
  1248. `emacs' The file will be visited by the current Emacs process.
  1249. `default' Use the default application for this file type.
  1250. string A command to be executed by a shell; %s will be replaced
  1251. by the path to the file.
  1252. sexp A Lisp form which will be evaluated. The file path will
  1253. be available in the Lisp variable `file'.
  1254. For more examples, see the system specific constants
  1255. `org-file-apps-defaults-macosx'
  1256. `org-file-apps-defaults-windowsnt'
  1257. `org-file-apps-defaults-gnu'."
  1258. :group 'org-link-follow
  1259. :type '(repeat
  1260. (cons (choice :value ""
  1261. (string :tag "Extension")
  1262. (const :tag "Default for unrecognized files" t)
  1263. (const :tag "Remote file" remote)
  1264. (const :tag "Links to a directory" directory))
  1265. (choice :value ""
  1266. (const :tag "Visit with Emacs" emacs)
  1267. (const :tag "Use system default" default)
  1268. (string :tag "Command")
  1269. (sexp :tag "Lisp form")))))
  1270. (defcustom org-mhe-search-all-folders nil
  1271. "Non-nil means, that the search for the mh-message will be extended to
  1272. all folders if the message cannot be found in the folder given in the link.
  1273. Searching all folders is very efficient with one of the search engines
  1274. supported by MH-E, but will be slow with pick."
  1275. :group 'org-link-follow
  1276. :type 'boolean)
  1277. (defgroup org-remember nil
  1278. "Options concerning interaction with remember.el."
  1279. :tag "Org Remember"
  1280. :group 'org)
  1281. (defcustom org-directory "~/org"
  1282. "Directory with org files.
  1283. This directory will be used as default to prompt for org files.
  1284. Used by the hooks for remember.el."
  1285. :group 'org-remember
  1286. :type 'directory)
  1287. (defcustom org-default-notes-file "~/.notes"
  1288. "Default target for storing notes.
  1289. Used by the hooks for remember.el. This can be a string, or nil to mean
  1290. the value of `remember-data-file'.
  1291. You can set this on a per-template basis with the variable
  1292. `org-remember-templates'."
  1293. :group 'org-remember
  1294. :type '(choice
  1295. (const :tag "Default from remember-data-file" nil)
  1296. file))
  1297. (defcustom org-remember-store-without-prompt t
  1298. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1299. In this case, you need `C-u C-c C-c' to get the prompts for
  1300. note file and headline.
  1301. When this variable is nil, `C-c C-c' give you the prompts, and
  1302. `C-u C-c C-c' trigger the fasttrack."
  1303. :group 'org-remember
  1304. :type 'boolean)
  1305. (defcustom org-remember-interactive-interface 'refile
  1306. "The interface to be used for interactive filing of remember notes.
  1307. This is only used when the interactive mode for selecting a filing
  1308. location is used (see the variable `org-remember-store-without-prompt').
  1309. Allowed vaues are:
  1310. outline The interface shows an outline of the relevant file
  1311. and the correct heading is found by moving through
  1312. the outline or by searching with incremental search.
  1313. outline-path-completion Headlines in the current buffer are offered via
  1314. completion.
  1315. refile Use the refile interface, and offer headlines,
  1316. possibly from different buffers."
  1317. :group 'org-remember
  1318. :type '(choice
  1319. (const :tag "Refile" refile)
  1320. (const :tag "Outline" outline)
  1321. (const :tag "Outline-path-completion" outline-path-completion)))
  1322. (defcustom org-goto-interface 'outline
  1323. "The default interface to be used for `org-goto'.
  1324. Allowed vaues are:
  1325. outline The interface shows an outline of the relevant file
  1326. and the correct heading is found by moving through
  1327. the outline or by searching with incremental search.
  1328. outline-path-completion Headlines in the current buffer are offered via
  1329. completion."
  1330. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1331. :type '(choice
  1332. (const :tag "Outline" outline)
  1333. (const :tag "Outline-path-completion" outline-path-completion)))
  1334. (defcustom org-remember-default-headline ""
  1335. "The headline that should be the default location in the notes file.
  1336. When filing remember notes, the cursor will start at that position.
  1337. You can set this on a per-template basis with the variable
  1338. `org-remember-templates'."
  1339. :group 'org-remember
  1340. :type 'string)
  1341. (defcustom org-remember-templates nil
  1342. "Templates for the creation of remember buffers.
  1343. When nil, just let remember make the buffer.
  1344. When not nil, this is a list of 5-element lists. In each entry, the first
  1345. element is the name of the template, which should be a single short word.
  1346. The second element is a character, a unique key to select this template.
  1347. The third element is the template. The fourth element is optional and can
  1348. specify a destination file for remember items created with this template.
  1349. The default file is given by `org-default-notes-file'. An optional fifth
  1350. element can specify the headline in that file that should be offered
  1351. first when the user is asked to file the entry. The default headline is
  1352. given in the variable `org-remember-default-headline'.
  1353. The template specifies the structure of the remember buffer. It should have
  1354. a first line starting with a star, to act as the org-mode headline.
  1355. Furthermore, the following %-escapes will be replaced with content:
  1356. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1357. A default value and a completion table ca be specified like this:
  1358. %^{prompt|default|completion2|completion3|...}
  1359. %t time stamp, date only
  1360. %T time stamp with date and time
  1361. %u, %U like the above, but inactive time stamps
  1362. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1363. You may define a prompt like %^{Please specify birthday}t
  1364. %n user name (taken from `user-full-name')
  1365. %a annotation, normally the link created with org-store-link
  1366. %i initial content, the region active. If %i is indented,
  1367. the entire inserted text will be indented as well.
  1368. %c content of the clipboard, or current kill ring head
  1369. %^g prompt for tags, with completion on tags in target file
  1370. %^G prompt for tags, with completion all tags in all agenda files
  1371. %:keyword specific information for certain link types, see below
  1372. %[pathname] insert the contents of the file given by `pathname'
  1373. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1374. %! Store this note immediately after filling the template
  1375. %? After completing the template, position cursor here.
  1376. Apart from these general escapes, you can access information specific to the
  1377. link type that is created. For example, calling `remember' in emails or gnus
  1378. will record the author and the subject of the message, which you can access
  1379. with %:author and %:subject, respectively. Here is a complete list of what
  1380. is recorded for each link type.
  1381. Link type | Available information
  1382. -------------------+------------------------------------------------------
  1383. bbdb | %:type %:name %:company
  1384. vm, wl, mh, rmail | %:type %:subject %:message-id
  1385. | %:from %:fromname %:fromaddress
  1386. | %:to %:toname %:toaddress
  1387. | %:fromto (either \"to NAME\" or \"from NAME\")
  1388. gnus | %:group, for messages also all email fields
  1389. w3, w3m | %:type %:url
  1390. info | %:type %:file %:node
  1391. calendar | %:type %:date"
  1392. :group 'org-remember
  1393. :get (lambda (var) ; Make sure all entries have 5 elements
  1394. (mapcar (lambda (x)
  1395. (if (not (stringp (car x))) (setq x (cons "" x)))
  1396. (cond ((= (length x) 4) (append x '("")))
  1397. ((= (length x) 3) (append x '("" "")))
  1398. (t x)))
  1399. (default-value var)))
  1400. :type '(repeat
  1401. :tag "enabled"
  1402. (list :value ("" ?a "\n" nil nil)
  1403. (string :tag "Name")
  1404. (character :tag "Selection Key")
  1405. (string :tag "Template")
  1406. (choice
  1407. (file :tag "Destination file")
  1408. (const :tag "Prompt for file" nil))
  1409. (choice
  1410. (string :tag "Destination headline")
  1411. (const :tag "Selection interface for heading")))))
  1412. (defcustom org-reverse-note-order nil
  1413. "Non-nil means, store new notes at the beginning of a file or entry.
  1414. When nil, new notes will be filed to the end of a file or entry.
  1415. This can also be a list with cons cells of regular expressions that
  1416. are matched against file names, and values."
  1417. :group 'org-remember
  1418. :type '(choice
  1419. (const :tag "Reverse always" t)
  1420. (const :tag "Reverse never" nil)
  1421. (repeat :tag "By file name regexp"
  1422. (cons regexp boolean))))
  1423. (defcustom org-refile-targets nil
  1424. "Targets for refiling entries with \\[org-refile].
  1425. This is list of cons cells. Each cell contains:
  1426. - a specification of the files to be considered, either a list of files,
  1427. or a symbol whose function or value fields will be used to retrieve
  1428. a file name or a list of file names. Nil means, refile to a different
  1429. heading in the current buffer.
  1430. - A specification of how to find candidate refile targets. This may be
  1431. any of
  1432. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1433. This tag has to be present in all target headlines, inheritance will
  1434. not be considered.
  1435. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1436. todo keyword.
  1437. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1438. headlines that are refiling targets.
  1439. - a cons cell (:level . N). Any headline of level N is considered a target.
  1440. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1441. ;; FIXME: what if there are a var and func with same name???
  1442. :group 'org-remember
  1443. :type '(repeat
  1444. (cons
  1445. (choice :value org-agenda-files
  1446. (const :tag "All agenda files" org-agenda-files)
  1447. (const :tag "Current buffer" nil)
  1448. (function) (variable) (file))
  1449. (choice :tag "Identify target headline by"
  1450. (cons :tag "Specific tag" (const :tag) (string))
  1451. (cons :tag "TODO keyword" (const :todo) (string))
  1452. (cons :tag "Regular expression" (const :regexp) (regexp))
  1453. (cons :tag "Level number" (const :level) (integer))
  1454. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1455. (defcustom org-refile-use-outline-path nil
  1456. "Non-nil means, provide refile targets as paths.
  1457. So a level 3 headline will be available as level1/level2/level3.
  1458. When the value is `file', also include the file name (without directory)
  1459. into the path. When `full-file-path', include the full file path."
  1460. :group 'org-remember
  1461. :type '(choice
  1462. (const :tag "Not" nil)
  1463. (const :tag "Yes" t)
  1464. (const :tag "Start with file name" file)
  1465. (const :tag "Start with full file path" full-file-path)))
  1466. (defgroup org-todo nil
  1467. "Options concerning TODO items in Org-mode."
  1468. :tag "Org TODO"
  1469. :group 'org)
  1470. (defgroup org-progress nil
  1471. "Options concerning Progress logging in Org-mode."
  1472. :tag "Org Progress"
  1473. :group 'org-time)
  1474. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1475. "List of TODO entry keyword sequences and their interpretation.
  1476. \\<org-mode-map>This is a list of sequences.
  1477. Each sequence starts with a symbol, either `sequence' or `type',
  1478. indicating if the keywords should be interpreted as a sequence of
  1479. action steps, or as different types of TODO items. The first
  1480. keywords are states requiring action - these states will select a headline
  1481. for inclusion into the global TODO list Org-mode produces. If one of
  1482. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1483. signify that no further action is necessary. If \"|\" is not found,
  1484. the last keyword is treated as the only DONE state of the sequence.
  1485. The command \\[org-todo] cycles an entry through these states, and one
  1486. additional state where no keyword is present. For details about this
  1487. cycling, see the manual.
  1488. TODO keywords and interpretation can also be set on a per-file basis with
  1489. the special #+SEQ_TODO and #+TYP_TODO lines.
  1490. Each keyword can optionally specify a character for fast state selection
  1491. \(in combination with the variable `org-use-fast-todo-selection')
  1492. and specifiers for state change logging, using the same syntax
  1493. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1494. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1495. indicates to record a time stamp each time this state is selected.
  1496. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1497. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1498. entering and when leaving this state.
  1499. For backward compatibility, this variable may also be just a list
  1500. of keywords - in this case the interptetation (sequence or type) will be
  1501. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1502. :group 'org-todo
  1503. :group 'org-keywords
  1504. :type '(choice
  1505. (repeat :tag "Old syntax, just keywords"
  1506. (string :tag "Keyword"))
  1507. (repeat :tag "New syntax"
  1508. (cons
  1509. (choice
  1510. :tag "Interpretation"
  1511. (const :tag "Sequence (cycling hits every state)" sequence)
  1512. (const :tag "Type (cycling directly to DONE)" type))
  1513. (repeat
  1514. (string :tag "Keyword"))))))
  1515. (defvar org-todo-keywords-1 nil
  1516. "All TODO and DONE keywords active in a buffer.")
  1517. (make-variable-buffer-local 'org-todo-keywords-1)
  1518. (defvar org-todo-keywords-for-agenda nil)
  1519. (defvar org-done-keywords-for-agenda nil)
  1520. (defvar org-not-done-keywords nil)
  1521. (make-variable-buffer-local 'org-not-done-keywords)
  1522. (defvar org-done-keywords nil)
  1523. (make-variable-buffer-local 'org-done-keywords)
  1524. (defvar org-todo-heads nil)
  1525. (make-variable-buffer-local 'org-todo-heads)
  1526. (defvar org-todo-sets nil)
  1527. (make-variable-buffer-local 'org-todo-sets)
  1528. (defvar org-todo-log-states nil)
  1529. (make-variable-buffer-local 'org-todo-log-states)
  1530. (defvar org-todo-kwd-alist nil)
  1531. (make-variable-buffer-local 'org-todo-kwd-alist)
  1532. (defvar org-todo-key-alist nil)
  1533. (make-variable-buffer-local 'org-todo-key-alist)
  1534. (defvar org-todo-key-trigger nil)
  1535. (make-variable-buffer-local 'org-todo-key-trigger)
  1536. (defcustom org-todo-interpretation 'sequence
  1537. "Controls how TODO keywords are interpreted.
  1538. This variable is in principle obsolete and is only used for
  1539. backward compatibility, if the interpretation of todo keywords is
  1540. not given already in `org-todo-keywords'. See that variable for
  1541. more information."
  1542. :group 'org-todo
  1543. :group 'org-keywords
  1544. :type '(choice (const sequence)
  1545. (const type)))
  1546. (defcustom org-use-fast-todo-selection 'prefix
  1547. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1548. This variable describes if and under what circumstances the cycling
  1549. mechanism for TODO keywords will be replaced by a single-key, direct
  1550. selection scheme.
  1551. When nil, fast selection is never used.
  1552. When the symbol `prefix', it will be used when `org-todo' is called with
  1553. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1554. in an agenda buffer.
  1555. When t, fast selection is used by default. In this case, the prefix
  1556. argument forces cycling instead.
  1557. In all cases, the special interface is only used if access keys have actually
  1558. been assigned by the user, i.e. if keywords in the configuration are followed
  1559. by a letter in parenthesis, like TODO(t)."
  1560. :group 'org-todo
  1561. :type '(choice
  1562. (const :tag "Never" nil)
  1563. (const :tag "By default" t)
  1564. (const :tag "Only with C-u C-c C-t" prefix)))
  1565. (defcustom org-after-todo-state-change-hook nil
  1566. "Hook which is run after the state of a TODO item was changed.
  1567. The new state (a string with a TODO keyword, or nil) is available in the
  1568. Lisp variable `state'."
  1569. :group 'org-todo
  1570. :type 'hook)
  1571. (defcustom org-log-done nil
  1572. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1573. When equal to the list (done), also prompt for a closing note.
  1574. This can also be configured on a per-file basis by adding one of
  1575. the following lines anywhere in the buffer:
  1576. #+STARTUP: logdone
  1577. #+STARTUP: lognotedone
  1578. #+STARTUP: nologdone"
  1579. :group 'org-todo
  1580. :group 'org-progress
  1581. :type '(choice
  1582. (const :tag "No logging" nil)
  1583. (const :tag "Record CLOSED timestamp" time)
  1584. (const :tag "Record CLOSED timestamp with closing note." note)))
  1585. ;; Normalize old uses of org-log-done.
  1586. (cond
  1587. ((eq org-log-done t) (setq org-log-done 'time))
  1588. ((and (listp org-log-done) (memq 'done org-log-done))
  1589. (setq org-log-done 'note)))
  1590. ;; FIXME: document
  1591. (defcustom org-log-note-clock-out nil
  1592. "Non-nil means, recored a note when clocking out of an item.
  1593. This can also be configured on a per-file basis by adding one of
  1594. the following lines anywhere in the buffer:
  1595. #+STARTUP: lognoteclock-out
  1596. #+STARTUP: nolognoteclock-out"
  1597. :group 'org-todo
  1598. :group 'org-progress
  1599. :type 'boolean)
  1600. (defcustom org-log-done-with-time t
  1601. "Non-nil means, the CLOSED time stamp will contain date and time.
  1602. When nil, only the date will be recorded."
  1603. :group 'org-progress
  1604. :type 'boolean)
  1605. (defcustom org-log-note-headings
  1606. '((done . "CLOSING NOTE %t")
  1607. (state . "State %-12s %t")
  1608. (clock-out . ""))
  1609. "Headings for notes added when clocking out or closing TODO items.
  1610. The value is an alist, with the car being a symbol indicating the note
  1611. context, and the cdr is the heading to be used. The heading may also be the
  1612. empty string.
  1613. %t in the heading will be replaced by a time stamp.
  1614. %s will be replaced by the new TODO state, in double quotes.
  1615. %u will be replaced by the user name.
  1616. %U will be replaced by the full user name."
  1617. :group 'org-todo
  1618. :group 'org-progress
  1619. :type '(list :greedy t
  1620. (cons (const :tag "Heading when closing an item" done) string)
  1621. (cons (const :tag
  1622. "Heading when changing todo state (todo sequence only)"
  1623. state) string)
  1624. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1625. (defcustom org-log-states-order-reversed t
  1626. "Non-nil means, the latest state change note will be directly after heading.
  1627. When nil, the notes will be orderer according to time."
  1628. :group 'org-todo
  1629. :group 'org-progress
  1630. :type 'boolean)
  1631. (defcustom org-log-repeat 'time
  1632. "Non-nil means, record moving through the DONE state when triggering repeat.
  1633. An auto-repeating tasks is immediately switched back to TODO when marked
  1634. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1635. the TODO keyword definition, or recording a cloing note by setting
  1636. `org-log-done', there will be no record of the task moving trhough DONE.
  1637. This variable forces taking a note anyway. Possible values are:
  1638. nil Don't force a record
  1639. time Record a time stamp
  1640. note Record a note
  1641. This option can also be set with on a per-file-basis with
  1642. #+STARTUP: logrepeat
  1643. #+STARTUP: lognoterepeat
  1644. #+STARTUP: nologrepeat
  1645. You can have local logging settings for a subtree by setting the LOGGING
  1646. property to one or more of these keywords."
  1647. :group 'org-todo
  1648. :group 'org-progress
  1649. :type '(choice
  1650. (const :tag "Don't force a record" nil)
  1651. (const :tag "Force recording the DONE state" time)
  1652. (const :tag "Force recording a note with the DONE state" note)))
  1653. (defcustom org-clock-into-drawer 2
  1654. "Should clocking info be wrapped into a drawer?
  1655. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1656. If necessary, the drawer will be created.
  1657. When nil, the drawer will not be created, but used when present.
  1658. When an integer and the number of clocking entries in an item
  1659. reaches or exceeds this number, a drawer will be created."
  1660. :group 'org-todo
  1661. :group 'org-progress
  1662. :type '(choice
  1663. (const :tag "Always" t)
  1664. (const :tag "Only when drawer exists" nil)
  1665. (integer :tag "When at least N clock entries")))
  1666. (defcustom org-clock-out-when-done t
  1667. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1668. Nil means, clock will keep running until stopped explicitly with
  1669. `C-c C-x C-o', or until the clock is started in a different item."
  1670. :group 'org-progress
  1671. :type 'boolean)
  1672. (defcustom org-clock-in-switch-to-state nil
  1673. "Set task to a special todo state while clocking it.
  1674. The value should be the state to which the entry should be switched."
  1675. :group 'org-progress
  1676. :group 'org-todo
  1677. :type '(choice
  1678. (const :tag "Don't force a state" nil)
  1679. (string :tag "State")))
  1680. (defgroup org-priorities nil
  1681. "Priorities in Org-mode."
  1682. :tag "Org Priorities"
  1683. :group 'org-todo)
  1684. (defcustom org-highest-priority ?A
  1685. "The highest priority of TODO items. A character like ?A, ?B etc.
  1686. Must have a smaller ASCII number than `org-lowest-priority'."
  1687. :group 'org-priorities
  1688. :type 'character)
  1689. (defcustom org-lowest-priority ?C
  1690. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1691. Must have a larger ASCII number than `org-highest-priority'."
  1692. :group 'org-priorities
  1693. :type 'character)
  1694. (defcustom org-default-priority ?B
  1695. "The default priority of TODO items.
  1696. This is the priority an item get if no explicit priority is given."
  1697. :group 'org-priorities
  1698. :type 'character)
  1699. (defcustom org-priority-start-cycle-with-default t
  1700. "Non-nil means, start with default priority when starting to cycle.
  1701. When this is nil, the first step in the cycle will be (depending on the
  1702. command used) one higher or lower that the default priority."
  1703. :group 'org-priorities
  1704. :type 'boolean)
  1705. (defgroup org-time nil
  1706. "Options concerning time stamps and deadlines in Org-mode."
  1707. :tag "Org Time"
  1708. :group 'org)
  1709. (defcustom org-insert-labeled-timestamps-at-point nil
  1710. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1711. When nil, these labeled time stamps are forces into the second line of an
  1712. entry, just after the headline. When scheduling from the global TODO list,
  1713. the time stamp will always be forced into the second line."
  1714. :group 'org-time
  1715. :type 'boolean)
  1716. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1717. "Formats for `format-time-string' which are used for time stamps.
  1718. It is not recommended to change this constant.")
  1719. (defcustom org-time-stamp-rounding-minutes 0
  1720. "Number of minutes to round time stamps to upon insertion.
  1721. When zero, insert the time unmodified. Useful rounding numbers
  1722. should be factors of 60, so for example 5, 10, 15.
  1723. When this is not zero, you can still force an exact time-stamp by using
  1724. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1725. :group 'org-time
  1726. :type 'integer)
  1727. (defcustom org-display-custom-times nil
  1728. "Non-nil means, overlay custom formats over all time stamps.
  1729. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1730. To turn this on on a per-file basis, insert anywhere in the file:
  1731. #+STARTUP: customtime"
  1732. :group 'org-time
  1733. :set 'set-default
  1734. :type 'sexp)
  1735. (make-variable-buffer-local 'org-display-custom-times)
  1736. (defcustom org-time-stamp-custom-formats
  1737. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1738. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1739. These are overlayed over the default ISO format if the variable
  1740. `org-display-custom-times' is set. Time like %H:%M should be at the
  1741. end of the second format."
  1742. :group 'org-time
  1743. :type 'sexp)
  1744. (defun org-time-stamp-format (&optional long inactive)
  1745. "Get the right format for a time string."
  1746. (let ((f (if long (cdr org-time-stamp-formats)
  1747. (car org-time-stamp-formats))))
  1748. (if inactive
  1749. (concat "[" (substring f 1 -1) "]")
  1750. f)))
  1751. (defcustom org-read-date-prefer-future t
  1752. "Non-nil means, assume future for incomplete date input from user.
  1753. This affects the following situations:
  1754. 1. The user gives a day, but no month.
  1755. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1756. read this as the third of *next* month. However, if you enter \"17\",
  1757. it will be considered as *this* month.
  1758. 2. The user gives a month but not a year.
  1759. For example, if it is april and you enter \"feb 2\", this will be read
  1760. as feb 2, *next* year. \"May 5\", however, will be this year.
  1761. When this option is nil, the current month and year will always be used
  1762. as defaults."
  1763. :group 'org-time
  1764. :type 'boolean)
  1765. (defcustom org-read-date-display-live t
  1766. "Non-nil means, display current interpretation of date prompt live.
  1767. This display will be in an overlay, in the minibuffer."
  1768. :group 'org-time
  1769. :type 'boolean)
  1770. (defcustom org-read-date-popup-calendar t
  1771. "Non-nil means, pop up a calendar when prompting for a date.
  1772. In the calendar, the date can be selected with mouse-1. However, the
  1773. minibuffer will also be active, and you can simply enter the date as well.
  1774. When nil, only the minibuffer will be available."
  1775. :group 'org-time
  1776. :type 'boolean)
  1777. (if (fboundp 'defvaralias)
  1778. (defvaralias 'org-popup-calendar-for-date-prompt
  1779. 'org-read-date-popup-calendar))
  1780. (defcustom org-extend-today-until 0
  1781. "The hour when your day really ends.
  1782. This has influence for the following applications:
  1783. - When switching the agenda to \"today\". It it is still earlier than
  1784. the time given here, the day recognized as TODAY is actually yesterday.
  1785. - When a date is read from the user and it is still before the time given
  1786. here, the current date and time will be assumed to be yesterday, 23:59.
  1787. FIXME:
  1788. IMPORTANT: This is still a very experimental feature, it may disappear
  1789. again or it may be extended to mean more things."
  1790. :group 'org-time
  1791. :type 'number)
  1792. (defcustom org-edit-timestamp-down-means-later nil
  1793. "Non-nil means, S-down will increase the time in a time stamp.
  1794. When nil, S-up will increase."
  1795. :group 'org-time
  1796. :type 'boolean)
  1797. (defcustom org-calendar-follow-timestamp-change t
  1798. "Non-nil means, make the calendar window follow timestamp changes.
  1799. When a timestamp is modified and the calendar window is visible, it will be
  1800. moved to the new date."
  1801. :group 'org-time
  1802. :type 'boolean)
  1803. (defcustom org-clock-heading-function nil
  1804. "When non-nil, should be a function to create `org-clock-heading'.
  1805. This is the string shown in the mode line when a clock is running.
  1806. The function is called with point at the beginning of the headline."
  1807. :group 'org-time ; FIXME: Should we have a separate group????
  1808. :type 'function)
  1809. (defgroup org-tags nil
  1810. "Options concerning tags in Org-mode."
  1811. :tag "Org Tags"
  1812. :group 'org)
  1813. (defcustom org-tag-alist nil
  1814. "List of tags allowed in Org-mode files.
  1815. When this list is nil, Org-mode will base TAG input on what is already in the
  1816. buffer.
  1817. The value of this variable is an alist, the car of each entry must be a
  1818. keyword as a string, the cdr may be a character that is used to select
  1819. that tag through the fast-tag-selection interface.
  1820. See the manual for details."
  1821. :group 'org-tags
  1822. :type '(repeat
  1823. (choice
  1824. (cons (string :tag "Tag name")
  1825. (character :tag "Access char"))
  1826. (const :tag "Start radio group" (:startgroup))
  1827. (const :tag "End radio group" (:endgroup)))))
  1828. (defcustom org-use-fast-tag-selection 'auto
  1829. "Non-nil means, use fast tag selection scheme.
  1830. This is a special interface to select and deselect tags with single keys.
  1831. When nil, fast selection is never used.
  1832. When the symbol `auto', fast selection is used if and only if selection
  1833. characters for tags have been configured, either through the variable
  1834. `org-tag-alist' or through a #+TAGS line in the buffer.
  1835. When t, fast selection is always used and selection keys are assigned
  1836. automatically if necessary."
  1837. :group 'org-tags
  1838. :type '(choice
  1839. (const :tag "Always" t)
  1840. (const :tag "Never" nil)
  1841. (const :tag "When selection characters are configured" 'auto)))
  1842. (defcustom org-fast-tag-selection-single-key nil
  1843. "Non-nil means, fast tag selection exits after first change.
  1844. When nil, you have to press RET to exit it.
  1845. During fast tag selection, you can toggle this flag with `C-c'.
  1846. This variable can also have the value `expert'. In this case, the window
  1847. displaying the tags menu is not even shown, until you press C-c again."
  1848. :group 'org-tags
  1849. :type '(choice
  1850. (const :tag "No" nil)
  1851. (const :tag "Yes" t)
  1852. (const :tag "Expert" expert)))
  1853. (defvar org-fast-tag-selection-include-todo nil
  1854. "Non-nil means, fast tags selection interface will also offer TODO states.
  1855. This is an undocumented feature, you should not rely on it.")
  1856. (defcustom org-tags-column -80
  1857. "The column to which tags should be indented in a headline.
  1858. If this number is positive, it specifies the column. If it is negative,
  1859. it means that the tags should be flushright to that column. For example,
  1860. -80 works well for a normal 80 character screen."
  1861. :group 'org-tags
  1862. :type 'integer)
  1863. (defcustom org-auto-align-tags t
  1864. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1865. These operations change the length of a headline and therefore shift
  1866. the tags around. With this options turned on, after each such operation
  1867. the tags are again aligned to `org-tags-column'."
  1868. :group 'org-tags
  1869. :type 'boolean)
  1870. (defcustom org-use-tag-inheritance t
  1871. "Non-nil means, tags in levels apply also for sublevels.
  1872. When nil, only the tags directly given in a specific line apply there.
  1873. If you turn off this option, you very likely want to turn on the
  1874. companion option `org-tags-match-list-sublevels'."
  1875. :group 'org-tags
  1876. :type 'boolean)
  1877. (defcustom org-tags-match-list-sublevels nil
  1878. "Non-nil means list also sublevels of headlines matching tag search.
  1879. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1880. the sublevels of a headline matching a tag search often also match
  1881. the same search. Listing all of them can create very long lists.
  1882. Setting this variable to nil causes subtrees of a match to be skipped.
  1883. This option is off by default, because inheritance in on. If you turn
  1884. inheritance off, you very likely want to turn this option on.
  1885. As a special case, if the tag search is restricted to TODO items, the
  1886. value of this variable is ignored and sublevels are always checked, to
  1887. make sure all corresponding TODO items find their way into the list."
  1888. :group 'org-tags
  1889. :type 'boolean)
  1890. (defvar org-tags-history nil
  1891. "History of minibuffer reads for tags.")
  1892. (defvar org-last-tags-completion-table nil
  1893. "The last used completion table for tags.")
  1894. (defvar org-after-tags-change-hook nil
  1895. "Hook that is run after the tags in a line have changed.")
  1896. (defgroup org-properties nil
  1897. "Options concerning properties in Org-mode."
  1898. :tag "Org Properties"
  1899. :group 'org)
  1900. (defcustom org-property-format "%-10s %s"
  1901. "How property key/value pairs should be formatted by `indent-line'.
  1902. When `indent-line' hits a property definition, it will format the line
  1903. according to this format, mainly to make sure that the values are
  1904. lined-up with respect to each other."
  1905. :group 'org-properties
  1906. :type 'string)
  1907. (defcustom org-use-property-inheritance nil
  1908. "Non-nil means, properties apply also for sublevels.
  1909. This setting is only relevant during property searches, not when querying
  1910. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1911. you need to call `org-entry-get' with the inheritance flag.
  1912. Turning this on can cause significant overhead when doing a search, so
  1913. this is turned off by default.
  1914. When nil, only the properties directly given in the current entry count.
  1915. The value may also be a list of properties that shouldhave inheritance.
  1916. However, note that some special properties use inheritance under special
  1917. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1918. and the properties ending in \"_ALL\" when they are used as descriptor
  1919. for valid values of a property."
  1920. :group 'org-properties
  1921. :type '(choice
  1922. (const :tag "Not" nil)
  1923. (const :tag "Always" nil)
  1924. (repeat :tag "Specific properties" (string :tag "Property"))))
  1925. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1926. "The default column format, if no other format has been defined.
  1927. This variable can be set on the per-file basis by inserting a line
  1928. #+COLUMNS: %25ITEM ....."
  1929. :group 'org-properties
  1930. :type 'string)
  1931. (defcustom org-global-properties nil
  1932. "List of property/value pairs that can be inherited by any entry.
  1933. You can set buffer-local values for this by adding lines like
  1934. #+PROPERTY: NAME VALUE"
  1935. :group 'org-properties
  1936. :type '(repeat
  1937. (cons (string :tag "Property")
  1938. (string :tag "Value"))))
  1939. (defvar org-local-properties nil
  1940. "List of property/value pairs that can be inherited by any entry.
  1941. Valid for the current buffer.
  1942. This variable is populated from #+PROPERTY lines.")
  1943. (defgroup org-agenda nil
  1944. "Options concerning agenda views in Org-mode."
  1945. :tag "Org Agenda"
  1946. :group 'org)
  1947. (defvar org-category nil
  1948. "Variable used by org files to set a category for agenda display.
  1949. Such files should use a file variable to set it, for example
  1950. # -*- mode: org; org-category: \"ELisp\"
  1951. or contain a special line
  1952. #+CATEGORY: ELisp
  1953. If the file does not specify a category, then file's base name
  1954. is used instead.")
  1955. (make-variable-buffer-local 'org-category)
  1956. (defcustom org-agenda-files nil
  1957. "The files to be used for agenda display.
  1958. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1959. \\[org-remove-file]. You can also use customize to edit the list.
  1960. If an entry is a directory, all files in that directory that are matched by
  1961. `org-agenda-file-regexp' will be part of the file list.
  1962. If the value of the variable is not a list but a single file name, then
  1963. the list of agenda files is actually stored and maintained in that file, one
  1964. agenda file per line."
  1965. :group 'org-agenda
  1966. :type '(choice
  1967. (repeat :tag "List of files and directories" file)
  1968. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1969. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1970. "Regular expression to match files for `org-agenda-files'.
  1971. If any element in the list in that variable contains a directory instead
  1972. of a normal file, all files in that directory that are matched by this
  1973. regular expression will be included."
  1974. :group 'org-agenda
  1975. :type 'regexp)
  1976. (defcustom org-agenda-skip-unavailable-files nil
  1977. "t means to just skip non-reachable files in `org-agenda-files'.
  1978. Nil means to remove them, after a query, from the list."
  1979. :group 'org-agenda
  1980. :type 'boolean)
  1981. (defcustom org-agenda-text-search-extra-files nil
  1982. "List of extra files to be searched by text search commands.
  1983. These files will be search in addition to the agenda files bu the
  1984. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1985. Note that these files will only be searched for text search commands,
  1986. not for the other agenda views like todo lists, tag earches or the weekly
  1987. agenda. This variable is intended to list notes and possibly archive files
  1988. that should also be searched by these two commands."
  1989. :group 'org-agenda
  1990. :type '(repeat file))
  1991. (if (fboundp 'defvaralias)
  1992. (defvaralias 'org-agenda-multi-occur-extra-files
  1993. 'org-agenda-text-search-extra-files))
  1994. (defcustom org-agenda-confirm-kill 1
  1995. "When set, remote killing from the agenda buffer needs confirmation.
  1996. When t, a confirmation is always needed. When a number N, confirmation is
  1997. only needed when the text to be killed contains more than N non-white lines."
  1998. :group 'org-agenda
  1999. :type '(choice
  2000. (const :tag "Never" nil)
  2001. (const :tag "Always" t)
  2002. (number :tag "When more than N lines")))
  2003. (defcustom org-calendar-to-agenda-key [?c]
  2004. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2005. The command `org-calendar-goto-agenda' will be bound to this key. The
  2006. default is the character `c' because then `c' can be used to switch back and
  2007. forth between agenda and calendar."
  2008. :group 'org-agenda
  2009. :type 'sexp)
  2010. (defcustom org-agenda-compact-blocks nil
  2011. "Non-nil means, make the block agenda more compact.
  2012. This is done by leaving out unnecessary lines."
  2013. :group 'org-agenda
  2014. :type nil)
  2015. (defgroup org-agenda-export nil
  2016. "Options concerning exporting agenda views in Org-mode."
  2017. :tag "Org Agenda Export"
  2018. :group 'org-agenda)
  2019. (defcustom org-agenda-with-colors t
  2020. "Non-nil means, use colors in agenda views."
  2021. :group 'org-agenda-export
  2022. :type 'boolean)
  2023. (defcustom org-agenda-exporter-settings nil
  2024. "Alist of variable/value pairs that should be active during agenda export.
  2025. This is a good place to set uptions for ps-print and for htmlize."
  2026. :group 'org-agenda-export
  2027. :type '(repeat
  2028. (list
  2029. (variable)
  2030. (sexp :tag "Value"))))
  2031. (defcustom org-agenda-export-html-style ""
  2032. "The style specification for exported HTML Agenda files.
  2033. If this variable contains a string, it will replace the default <style>
  2034. section as produced by `htmlize'.
  2035. Since there are different ways of setting style information, this variable
  2036. needs to contain the full HTML structure to provide a style, including the
  2037. surrounding HTML tags. The style specifications should include definitions
  2038. the fonts used by the agenda, here is an example:
  2039. <style type=\"text/css\">
  2040. p { font-weight: normal; color: gray; }
  2041. .org-agenda-structure {
  2042. font-size: 110%;
  2043. color: #003399;
  2044. font-weight: 600;
  2045. }
  2046. .org-todo {
  2047. color: #cc6666;Week-agenda:
  2048. font-weight: bold;
  2049. }
  2050. .org-done {
  2051. color: #339933;
  2052. }
  2053. .title { text-align: center; }
  2054. .todo, .deadline { color: red; }
  2055. .done { color: green; }
  2056. </style>
  2057. or, if you want to keep the style in a file,
  2058. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2059. As the value of this option simply gets inserted into the HTML <head> header,
  2060. you can \"misuse\" it to also add other text to the header. However,
  2061. <style>...</style> is required, if not present the variable will be ignored."
  2062. :group 'org-agenda-export
  2063. :group 'org-export-html
  2064. :type 'string)
  2065. (defgroup org-agenda-custom-commands nil
  2066. "Options concerning agenda views in Org-mode."
  2067. :tag "Org Agenda Custom Commands"
  2068. :group 'org-agenda)
  2069. (defcustom org-agenda-custom-commands nil
  2070. "Custom commands for the agenda.
  2071. These commands will be offered on the splash screen displayed by the
  2072. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2073. (key desc type match options files)
  2074. key The key (one or more characters as a string) to be associated
  2075. with the command.
  2076. desc A description of the commend, when omitted or nil, a default
  2077. description is built using MATCH.
  2078. type The command type, any of the following symbols:
  2079. agenda The daily/weekly agenda.
  2080. todo Entries with a specific TODO keyword, in all agenda files.
  2081. search Entries containing search words entry or headline.
  2082. tags Tags/Property/TODO match in all agenda files.
  2083. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2084. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2085. tags-tree Sparse tree with all tags matches in *current* file.
  2086. occur-tree Occur sparse tree for *current* file.
  2087. ... A user-defined function.
  2088. match What to search for:
  2089. - a single keyword for TODO keyword searches
  2090. - a tags match expression for tags searches
  2091. - a regular expression for occur searches
  2092. options A list of option settings, similar to that in a let form, so like
  2093. this: ((opt1 val1) (opt2 val2) ...)
  2094. files A list of files file to write the produced agenda buffer to
  2095. with the command `org-store-agenda-views'.
  2096. If a file name ends in \".html\", an HTML version of the buffer
  2097. is written out. If it ends in \".ps\", a postscript version is
  2098. produced. Otherwide, only the plain text is written to the file.
  2099. You can also define a set of commands, to create a composite agenda buffer.
  2100. In this case, an entry looks like this:
  2101. (key desc (cmd1 cmd2 ...) general-options file)
  2102. where
  2103. desc A description string to be displayed in the dispatcher menu.
  2104. cmd An agenda command, similar to the above. However, tree commands
  2105. are no allowed, but instead you can get agenda and global todo list.
  2106. So valid commands for a set are:
  2107. (agenda)
  2108. (alltodo)
  2109. (stuck)
  2110. (todo \"match\" options files)
  2111. (search \"match\" options files)
  2112. (tags \"match\" options files)
  2113. (tags-todo \"match\" options files)
  2114. Each command can carry a list of options, and another set of options can be
  2115. given for the whole set of commands. Individual command options take
  2116. precedence over the general options.
  2117. When using several characters as key to a command, the first characters
  2118. are prefix commands. For the dispatcher to display useful information, you
  2119. should provide a description for the prefix, like
  2120. (setq org-agenda-custom-commands
  2121. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2122. (\"hl\" tags \"+HOME+Lisa\")
  2123. (\"hp\" tags \"+HOME+Peter\")
  2124. (\"hk\" tags \"+HOME+Kim\")))"
  2125. :group 'org-agenda-custom-commands
  2126. :type '(repeat
  2127. (choice :value ("a" "" tags "" nil)
  2128. (list :tag "Single command"
  2129. (string :tag "Access Key(s) ")
  2130. (option (string :tag "Description"))
  2131. (choice
  2132. (const :tag "Agenda" agenda)
  2133. (const :tag "TODO list" alltodo)
  2134. (const :tag "Search words" search)
  2135. (const :tag "Stuck projects" stuck)
  2136. (const :tag "Tags search (all agenda files)" tags)
  2137. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2138. (const :tag "TODO keyword search (all agenda files)" todo)
  2139. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2140. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2141. (const :tag "Occur tree (current buffer)" occur-tree)
  2142. (sexp :tag "Other, user-defined function"))
  2143. (string :tag "Match")
  2144. (repeat :tag "Local options"
  2145. (list (variable :tag "Option") (sexp :tag "Value")))
  2146. (option (repeat :tag "Export" (file :tag "Export to"))))
  2147. (list :tag "Command series, all agenda files"
  2148. (string :tag "Access Key(s)")
  2149. (string :tag "Description ")
  2150. (repeat
  2151. (choice
  2152. (const :tag "Agenda" (agenda))
  2153. (const :tag "TODO list" (alltodo))
  2154. (list :tag "Search words"
  2155. (const :format "" search)
  2156. (string :tag "Match")
  2157. (repeat :tag "Local options"
  2158. (list (variable :tag "Option")
  2159. (sexp :tag "Value"))))
  2160. (const :tag "Stuck projects" (stuck))
  2161. (list :tag "Tags search"
  2162. (const :format "" tags)
  2163. (string :tag "Match")
  2164. (repeat :tag "Local options"
  2165. (list (variable :tag "Option")
  2166. (sexp :tag "Value"))))
  2167. (list :tag "Tags search, TODO entries only"
  2168. (const :format "" tags-todo)
  2169. (string :tag "Match")
  2170. (repeat :tag "Local options"
  2171. (list (variable :tag "Option")
  2172. (sexp :tag "Value"))))
  2173. (list :tag "TODO keyword search"
  2174. (const :format "" todo)
  2175. (string :tag "Match")
  2176. (repeat :tag "Local options"
  2177. (list (variable :tag "Option")
  2178. (sexp :tag "Value"))))
  2179. (list :tag "Other, user-defined function"
  2180. (symbol :tag "function")
  2181. (string :tag "Match")
  2182. (repeat :tag "Local options"
  2183. (list (variable :tag "Option")
  2184. (sexp :tag "Value"))))))
  2185. (repeat :tag "General options"
  2186. (list (variable :tag "Option")
  2187. (sexp :tag "Value")))
  2188. (option (repeat :tag "Export" (file :tag "Export to"))))
  2189. (cons :tag "Prefix key documentation"
  2190. (string :tag "Access Key(s)")
  2191. (string :tag "Description ")))))
  2192. (defcustom org-agenda-query-register ?o
  2193. "The register holding the current query string.
  2194. The prupose of this is that if you construct a query string interactively,
  2195. you can then use it to define a custom command."
  2196. :group 'org-agenda-custom-commands
  2197. :type 'character)
  2198. (defcustom org-stuck-projects
  2199. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2200. "How to identify stuck projects.
  2201. This is a list of four items:
  2202. 1. A tags/todo matcher string that is used to identify a project.
  2203. The entire tree below a headline matched by this is considered one project.
  2204. 2. A list of TODO keywords identifying non-stuck projects.
  2205. If the project subtree contains any headline with one of these todo
  2206. keywords, the project is considered to be not stuck. If you specify
  2207. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2208. 3. A list of tags identifying non-stuck projects.
  2209. If the project subtree contains any headline with one of these tags,
  2210. the project is considered to be not stuck. If you specify \"*\" as
  2211. a tag, any tag will mark the project unstuck.
  2212. 4. An arbitrary regular expression matching non-stuck projects.
  2213. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2214. or `C-c a #' to produce the list."
  2215. :group 'org-agenda-custom-commands
  2216. :type '(list
  2217. (string :tag "Tags/TODO match to identify a project")
  2218. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2219. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2220. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2221. (defgroup org-agenda-skip nil
  2222. "Options concerning skipping parts of agenda files."
  2223. :tag "Org Agenda Skip"
  2224. :group 'org-agenda)
  2225. (defcustom org-agenda-todo-list-sublevels t
  2226. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2227. When nil, the sublevels of a TODO entry are not checked, resulting in
  2228. potentially much shorter TODO lists."
  2229. :group 'org-agenda-skip
  2230. :group 'org-todo
  2231. :type 'boolean)
  2232. (defcustom org-agenda-todo-ignore-with-date nil
  2233. "Non-nil means, don't show entries with a date in the global todo list.
  2234. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2235. but don't want them to show up in the TODO list.
  2236. When this is set, it also covers deadlines and scheduled items, the settings
  2237. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2238. will be ignored."
  2239. :group 'org-agenda-skip
  2240. :group 'org-todo
  2241. :type 'boolean)
  2242. (defcustom org-agenda-todo-ignore-scheduled nil
  2243. "Non-nil means, don't show scheduled entries in the global todo list.
  2244. The idea behind this is that by scheduling it, you have already taken care
  2245. of this item.
  2246. See also `org-agenda-todo-ignore-with-date'."
  2247. :group 'org-agenda-skip
  2248. :group 'org-todo
  2249. :type 'boolean)
  2250. (defcustom org-agenda-todo-ignore-deadlines nil
  2251. "Non-nil means, don't show near deadline entries in the global todo list.
  2252. Near means closer than `org-deadline-warning-days' days.
  2253. The idea behind this is that such items will appear in the agenda anyway.
  2254. See also `org-agenda-todo-ignore-with-date'."
  2255. :group 'org-agenda-skip
  2256. :group 'org-todo
  2257. :type 'boolean)
  2258. (defcustom org-agenda-skip-scheduled-if-done nil
  2259. "Non-nil means don't show scheduled items in agenda when they are done.
  2260. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2261. it applies only to the actual date of the scheduling. Warnings about
  2262. an item with a past scheduling dates are always turned off when the item
  2263. is DONE."
  2264. :group 'org-agenda-skip
  2265. :type 'boolean)
  2266. (defcustom org-agenda-skip-deadline-if-done nil
  2267. "Non-nil means don't show deadines when the corresponding item is done.
  2268. When nil, the deadline is still shown and should give you a happy feeling.
  2269. This is relevant for the daily/weekly agenda. And it applied only to the
  2270. actualy date of the deadline. Warnings about approching and past-due
  2271. deadlines are always turned off when the item is DONE."
  2272. :group 'org-agenda-skip
  2273. :type 'boolean)
  2274. (defcustom org-agenda-skip-timestamp-if-done nil
  2275. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2276. :group 'org-agenda-skip
  2277. :type 'boolean)
  2278. (defcustom org-timeline-show-empty-dates 3
  2279. "Non-nil means, `org-timeline' also shows dates without an entry.
  2280. When nil, only the days which actually have entries are shown.
  2281. When t, all days between the first and the last date are shown.
  2282. When an integer, show also empty dates, but if there is a gap of more than
  2283. N days, just insert a special line indicating the size of the gap."
  2284. :group 'org-agenda-skip
  2285. :type '(choice
  2286. (const :tag "None" nil)
  2287. (const :tag "All" t)
  2288. (number :tag "at most")))
  2289. (defgroup org-agenda-startup nil
  2290. "Options concerning initial settings in the Agenda in Org Mode."
  2291. :tag "Org Agenda Startup"
  2292. :group 'org-agenda)
  2293. (defcustom org-finalize-agenda-hook nil
  2294. "Hook run just before displaying an agenda buffer."
  2295. :group 'org-agenda-startup
  2296. :type 'hook)
  2297. (defcustom org-agenda-mouse-1-follows-link nil
  2298. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2299. A longer mouse click will still set point. Does not work on XEmacs.
  2300. Needs to be set before org.el is loaded."
  2301. :group 'org-agenda-startup
  2302. :type 'boolean)
  2303. (defcustom org-agenda-start-with-follow-mode nil
  2304. "The initial value of follow-mode in a newly created agenda window."
  2305. :group 'org-agenda-startup
  2306. :type 'boolean)
  2307. (defgroup org-agenda-windows nil
  2308. "Options concerning the windows used by the Agenda in Org Mode."
  2309. :tag "Org Agenda Windows"
  2310. :group 'org-agenda)
  2311. (defcustom org-agenda-window-setup 'reorganize-frame
  2312. "How the agenda buffer should be displayed.
  2313. Possible values for this option are:
  2314. current-window Show agenda in the current window, keeping all other windows.
  2315. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2316. other-window Use `switch-to-buffer-other-window' to display agenda.
  2317. reorganize-frame Show only two windows on the current frame, the current
  2318. window and the agenda.
  2319. See also the variable `org-agenda-restore-windows-after-quit'."
  2320. :group 'org-agenda-windows
  2321. :type '(choice
  2322. (const current-window)
  2323. (const other-frame)
  2324. (const other-window)
  2325. (const reorganize-frame)))
  2326. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2327. "The min and max height of the agenda window as a fraction of frame height.
  2328. The value of the variable is a cons cell with two numbers between 0 and 1.
  2329. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2330. :group 'org-agenda-windows
  2331. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2332. (defcustom org-agenda-restore-windows-after-quit nil
  2333. "Non-nil means, restore window configuration open exiting agenda.
  2334. Before the window configuration is changed for displaying the agenda,
  2335. the current status is recorded. When the agenda is exited with
  2336. `q' or `x' and this option is set, the old state is restored. If
  2337. `org-agenda-window-setup' is `other-frame', the value of this
  2338. option will be ignored.."
  2339. :group 'org-agenda-windows
  2340. :type 'boolean)
  2341. (defcustom org-indirect-buffer-display 'other-window
  2342. "How should indirect tree buffers be displayed?
  2343. This applies to indirect buffers created with the commands
  2344. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2345. Valid values are:
  2346. current-window Display in the current window
  2347. other-window Just display in another window.
  2348. dedicated-frame Create one new frame, and re-use it each time.
  2349. new-frame Make a new frame each time. Note that in this case
  2350. previously-made indirect buffers are kept, and you need to
  2351. kill these buffers yourself."
  2352. :group 'org-structure
  2353. :group 'org-agenda-windows
  2354. :type '(choice
  2355. (const :tag "In current window" current-window)
  2356. (const :tag "In current frame, other window" other-window)
  2357. (const :tag "Each time a new frame" new-frame)
  2358. (const :tag "One dedicated frame" dedicated-frame)))
  2359. (defgroup org-agenda-daily/weekly nil
  2360. "Options concerning the daily/weekly agenda."
  2361. :tag "Org Agenda Daily/Weekly"
  2362. :group 'org-agenda)
  2363. (defcustom org-agenda-ndays 7
  2364. "Number of days to include in overview display.
  2365. Should be 1 or 7."
  2366. :group 'org-agenda-daily/weekly
  2367. :type 'number)
  2368. (defcustom org-agenda-start-on-weekday 1
  2369. "Non-nil means, start the overview always on the specified weekday.
  2370. 0 denotes Sunday, 1 denotes Monday etc.
  2371. When nil, always start on the current day."
  2372. :group 'org-agenda-daily/weekly
  2373. :type '(choice (const :tag "Today" nil)
  2374. (number :tag "Weekday No.")))
  2375. (defcustom org-agenda-show-all-dates t
  2376. "Non-nil means, `org-agenda' shows every day in the selected range.
  2377. When nil, only the days which actually have entries are shown."
  2378. :group 'org-agenda-daily/weekly
  2379. :type 'boolean)
  2380. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2381. "Format string for displaying dates in the agenda.
  2382. Used by the daily/weekly agenda and by the timeline. This should be
  2383. a format string understood by `format-time-string', or a function returning
  2384. the formatted date as a string. The function must take a single argument,
  2385. a calendar-style date list like (month day year)."
  2386. :group 'org-agenda-daily/weekly
  2387. :type '(choice
  2388. (string :tag "Format string")
  2389. (function :tag "Function")))
  2390. (defun org-agenda-format-date-aligned (date)
  2391. "Format a date string for display in the daily/weekly agenda, or timeline.
  2392. This function makes sure that dates are aligned for easy reading."
  2393. (format "%-9s %2d %s %4d"
  2394. (calendar-day-name date)
  2395. (extract-calendar-day date)
  2396. (calendar-month-name (extract-calendar-month date))
  2397. (extract-calendar-year date)))
  2398. (defcustom org-agenda-include-diary nil
  2399. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2400. :group 'org-agenda-daily/weekly
  2401. :type 'boolean)
  2402. (defcustom org-agenda-include-all-todo nil
  2403. "Set means weekly/daily agenda will always contain all TODO entries.
  2404. The TODO entries will be listed at the top of the agenda, before
  2405. the entries for specific days."
  2406. :group 'org-agenda-daily/weekly
  2407. :type 'boolean)
  2408. (defcustom org-agenda-repeating-timestamp-show-all t
  2409. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2410. When nil, only one occurence is shown, either today or the
  2411. nearest into the future."
  2412. :group 'org-agenda-daily/weekly
  2413. :type 'boolean)
  2414. (defcustom org-deadline-warning-days 14
  2415. "No. of days before expiration during which a deadline becomes active.
  2416. This variable governs the display in sparse trees and in the agenda.
  2417. When 0 or negative, it means use this number (the absolute value of it)
  2418. even if a deadline has a different individual lead time specified."
  2419. :group 'org-time
  2420. :group 'org-agenda-daily/weekly
  2421. :type 'number)
  2422. (defcustom org-scheduled-past-days 10000
  2423. "No. of days to continue listing scheduled items that are not marked DONE.
  2424. When an item is scheduled on a date, it shows up in the agenda on this
  2425. day and will be listed until it is marked done for the number of days
  2426. given here."
  2427. :group 'org-agenda-daily/weekly
  2428. :type 'number)
  2429. (defgroup org-agenda-time-grid nil
  2430. "Options concerning the time grid in the Org-mode Agenda."
  2431. :tag "Org Agenda Time Grid"
  2432. :group 'org-agenda)
  2433. (defcustom org-agenda-use-time-grid t
  2434. "Non-nil means, show a time grid in the agenda schedule.
  2435. A time grid is a set of lines for specific times (like every two hours between
  2436. 8:00 and 20:00). The items scheduled for a day at specific times are
  2437. sorted in between these lines.
  2438. For details about when the grid will be shown, and what it will look like, see
  2439. the variable `org-agenda-time-grid'."
  2440. :group 'org-agenda-time-grid
  2441. :type 'boolean)
  2442. (defcustom org-agenda-time-grid
  2443. '((daily today require-timed)
  2444. "----------------"
  2445. (800 1000 1200 1400 1600 1800 2000))
  2446. "The settings for time grid for agenda display.
  2447. This is a list of three items. The first item is again a list. It contains
  2448. symbols specifying conditions when the grid should be displayed:
  2449. daily if the agenda shows a single day
  2450. weekly if the agenda shows an entire week
  2451. today show grid on current date, independent of daily/weekly display
  2452. require-timed show grid only if at least one item has a time specification
  2453. The second item is a string which will be places behing the grid time.
  2454. The third item is a list of integers, indicating the times that should have
  2455. a grid line."
  2456. :group 'org-agenda-time-grid
  2457. :type
  2458. '(list
  2459. (set :greedy t :tag "Grid Display Options"
  2460. (const :tag "Show grid in single day agenda display" daily)
  2461. (const :tag "Show grid in weekly agenda display" weekly)
  2462. (const :tag "Always show grid for today" today)
  2463. (const :tag "Show grid only if any timed entries are present"
  2464. require-timed)
  2465. (const :tag "Skip grid times already present in an entry"
  2466. remove-match))
  2467. (string :tag "Grid String")
  2468. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2469. (defgroup org-agenda-sorting nil
  2470. "Options concerning sorting in the Org-mode Agenda."
  2471. :tag "Org Agenda Sorting"
  2472. :group 'org-agenda)
  2473. (defconst org-sorting-choice
  2474. '(choice
  2475. (const time-up) (const time-down)
  2476. (const category-keep) (const category-up) (const category-down)
  2477. (const tag-down) (const tag-up)
  2478. (const priority-up) (const priority-down))
  2479. "Sorting choices.")
  2480. (defcustom org-agenda-sorting-strategy
  2481. '((agenda time-up category-keep priority-down)
  2482. (todo category-keep priority-down)
  2483. (tags category-keep priority-down)
  2484. (search category-keep))
  2485. "Sorting structure for the agenda items of a single day.
  2486. This is a list of symbols which will be used in sequence to determine
  2487. if an entry should be listed before another entry. The following
  2488. symbols are recognized:
  2489. time-up Put entries with time-of-day indications first, early first
  2490. time-down Put entries with time-of-day indications first, late first
  2491. category-keep Keep the default order of categories, corresponding to the
  2492. sequence in `org-agenda-files'.
  2493. category-up Sort alphabetically by category, A-Z.
  2494. category-down Sort alphabetically by category, Z-A.
  2495. tag-up Sort alphabetically by last tag, A-Z.
  2496. tag-down Sort alphabetically by last tag, Z-A.
  2497. priority-up Sort numerically by priority, high priority last.
  2498. priority-down Sort numerically by priority, high priority first.
  2499. The different possibilities will be tried in sequence, and testing stops
  2500. if one comparison returns a \"not-equal\". For example, the default
  2501. '(time-up category-keep priority-down)
  2502. means: Pull out all entries having a specified time of day and sort them,
  2503. in order to make a time schedule for the current day the first thing in the
  2504. agenda listing for the day. Of the entries without a time indication, keep
  2505. the grouped in categories, don't sort the categories, but keep them in
  2506. the sequence given in `org-agenda-files'. Within each category sort by
  2507. priority.
  2508. Leaving out `category-keep' would mean that items will be sorted across
  2509. categories by priority.
  2510. Instead of a single list, this can also be a set of list for specific
  2511. contents, with a context symbol in the car of the list, any of
  2512. `agenda', `todo', `tags' for the corresponding agenda views."
  2513. :group 'org-agenda-sorting
  2514. :type `(choice
  2515. (repeat :tag "General" ,org-sorting-choice)
  2516. (list :tag "Individually"
  2517. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2518. (repeat ,org-sorting-choice))
  2519. (cons (const :tag "Strategy for TODO lists" todo)
  2520. (repeat ,org-sorting-choice))
  2521. (cons (const :tag "Strategy for Tags matches" tags)
  2522. (repeat ,org-sorting-choice)))))
  2523. (defcustom org-sort-agenda-notime-is-late t
  2524. "Non-nil means, items without time are considered late.
  2525. This is only relevant for sorting. When t, items which have no explicit
  2526. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2527. do have a time. When nil, the default time is before 0:00. You can use this
  2528. option to decide if the schedule for today should come before or after timeless
  2529. agenda entries."
  2530. :group 'org-agenda-sorting
  2531. :type 'boolean)
  2532. (defgroup org-agenda-line-format nil
  2533. "Options concerning the entry prefix in the Org-mode agenda display."
  2534. :tag "Org Agenda Line Format"
  2535. :group 'org-agenda)
  2536. (defcustom org-agenda-prefix-format
  2537. '((agenda . " %-12:c%?-12t% s")
  2538. (timeline . " % s")
  2539. (todo . " %-12:c")
  2540. (tags . " %-12:c")
  2541. (search . " %-12:c"))
  2542. "Format specifications for the prefix of items in the agenda views.
  2543. An alist with four entries, for the different agenda types. The keys to the
  2544. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2545. are format strings.
  2546. This format works similar to a printf format, with the following meaning:
  2547. %c the category of the item, \"Diary\" for entries from the diary, or
  2548. as given by the CATEGORY keyword or derived from the file name.
  2549. %T the *last* tag of the item. Last because inherited tags come
  2550. first in the list.
  2551. %t the time-of-day specification if one applies to the entry, in the
  2552. format HH:MM
  2553. %s Scheduling/Deadline information, a short string
  2554. All specifiers work basically like the standard `%s' of printf, but may
  2555. contain two additional characters: A question mark just after the `%' and
  2556. a whitespace/punctuation character just before the final letter.
  2557. If the first character after `%' is a question mark, the entire field
  2558. will only be included if the corresponding value applies to the
  2559. current entry. This is useful for fields which should have fixed
  2560. width when present, but zero width when absent. For example,
  2561. \"%?-12t\" will result in a 12 character time field if a time of the
  2562. day is specified, but will completely disappear in entries which do
  2563. not contain a time.
  2564. If there is punctuation or whitespace character just before the final
  2565. format letter, this character will be appended to the field value if
  2566. the value is not empty. For example, the format \"%-12:c\" leads to
  2567. \"Diary: \" if the category is \"Diary\". If the category were be
  2568. empty, no additional colon would be interted.
  2569. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2570. - Indent the line with two space characters
  2571. - Give the category in a 12 chars wide field, padded with whitespace on
  2572. the right (because of `-'). Append a colon if there is a category
  2573. (because of `:').
  2574. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2575. time, don't put in an empty field, just skip it (because of '?').
  2576. - Finally, put the scheduling information and append a whitespace.
  2577. As another example, if you don't want the time-of-day of entries in
  2578. the prefix, you could use:
  2579. (setq org-agenda-prefix-format \" %-11:c% s\")
  2580. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2581. `org-agenda-remove-tags'."
  2582. :type '(choice
  2583. (string :tag "General format")
  2584. (list :greedy t :tag "View dependent"
  2585. (cons (const agenda) (string :tag "Format"))
  2586. (cons (const timeline) (string :tag "Format"))
  2587. (cons (const todo) (string :tag "Format"))
  2588. (cons (const tags) (string :tag "Format"))
  2589. (cons (const search) (string :tag "Format"))))
  2590. :group 'org-agenda-line-format)
  2591. (defvar org-prefix-format-compiled nil
  2592. "The compiled version of the most recently used prefix format.
  2593. See the variable `org-agenda-prefix-format'.")
  2594. (defcustom org-agenda-todo-keyword-format "%-1s"
  2595. "Format for the TODO keyword in agenda lines.
  2596. Set this to something like \"%-12s\" if you want all TODO keywords
  2597. to occupy a fixed space in the agenda display."
  2598. :group 'org-agenda-line-format
  2599. :type 'string)
  2600. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2601. "Text preceeding scheduled items in the agenda view.
  2602. This is a list with two strings. The first applies when the item is
  2603. scheduled on the current day. The second applies when it has been scheduled
  2604. previously, it may contain a %d to capture how many days ago the item was
  2605. scheduled."
  2606. :group 'org-agenda-line-format
  2607. :type '(list
  2608. (string :tag "Scheduled today ")
  2609. (string :tag "Scheduled previously")))
  2610. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2611. "Text preceeding deadline items in the agenda view.
  2612. This is a list with two strings. The first applies when the item has its
  2613. deadline on the current day. The second applies when it is in the past or
  2614. in the future, it may contain %d to capture how many days away the deadline
  2615. is (was)."
  2616. :group 'org-agenda-line-format
  2617. :type '(list
  2618. (string :tag "Deadline today ")
  2619. (string :tag "Deadline relative")))
  2620. (defcustom org-agenda-remove-times-when-in-prefix t
  2621. "Non-nil means, remove duplicate time specifications in agenda items.
  2622. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2623. time-of-day specification in a headline or diary entry is extracted and
  2624. placed into the prefix. If this option is non-nil, the original specification
  2625. \(a timestamp or -range, or just a plain time(range) specification like
  2626. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2627. cluttered.
  2628. The option can be t or nil. It may also be the symbol `beg', indicating
  2629. that the time should only be removed what it is located at the beginning of
  2630. the headline/diary entry."
  2631. :group 'org-agenda-line-format
  2632. :type '(choice
  2633. (const :tag "Always" t)
  2634. (const :tag "Never" nil)
  2635. (const :tag "When at beginning of entry" beg)))
  2636. (defcustom org-agenda-default-appointment-duration nil
  2637. "Default duration for appointments that only have a starting time.
  2638. When nil, no duration is specified in such cases.
  2639. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2640. :group 'org-agenda-line-format
  2641. :type '(choice
  2642. (integer :tag "Minutes")
  2643. (const :tag "No default duration")))
  2644. (defcustom org-agenda-remove-tags nil
  2645. "Non-nil means, remove the tags from the headline copy in the agenda.
  2646. When this is the symbol `prefix', only remove tags when
  2647. `org-agenda-prefix-format' contains a `%T' specifier."
  2648. :group 'org-agenda-line-format
  2649. :type '(choice
  2650. (const :tag "Always" t)
  2651. (const :tag "Never" nil)
  2652. (const :tag "When prefix format contains %T" prefix)))
  2653. (if (fboundp 'defvaralias)
  2654. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2655. 'org-agenda-remove-tags))
  2656. (defcustom org-agenda-tags-column -80
  2657. "Shift tags in agenda items to this column.
  2658. If this number is positive, it specifies the column. If it is negative,
  2659. it means that the tags should be flushright to that column. For example,
  2660. -80 works well for a normal 80 character screen."
  2661. :group 'org-agenda-line-format
  2662. :type 'integer)
  2663. (if (fboundp 'defvaralias)
  2664. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2665. (defcustom org-agenda-fontify-priorities t
  2666. "Non-nil means, highlight low and high priorities in agenda.
  2667. When t, the highest priority entries are bold, lowest priority italic.
  2668. This may also be an association list of priority faces. The face may be
  2669. a names face, or a list like `(:background \"Red\")'."
  2670. :group 'org-agenda-line-format
  2671. :type '(choice
  2672. (const :tag "Never" nil)
  2673. (const :tag "Defaults" t)
  2674. (repeat :tag "Specify"
  2675. (list (character :tag "Priority" :value ?A)
  2676. (sexp :tag "face")))))
  2677. (defgroup org-latex nil
  2678. "Options for embedding LaTeX code into Org-mode"
  2679. :tag "Org LaTeX"
  2680. :group 'org)
  2681. (defcustom org-format-latex-options
  2682. '(:foreground default :background default :scale 1.0
  2683. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2684. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2685. "Options for creating images from LaTeX fragments.
  2686. This is a property list with the following properties:
  2687. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2688. `default' means use the forground of the default face.
  2689. :background the background color, or \"Transparent\".
  2690. `default' means use the background of the default face.
  2691. :scale a scaling factor for the size of the images
  2692. :html-foreground, :html-background, :html-scale
  2693. The same numbers for HTML export.
  2694. :matchers a list indicating which matchers should be used to
  2695. find LaTeX fragments. Valid members of this list are:
  2696. \"begin\" find environments
  2697. \"$\" find math expressions surrounded by $...$
  2698. \"$$\" find math expressions surrounded by $$....$$
  2699. \"\\(\" find math expressions surrounded by \\(...\\)
  2700. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2701. :group 'org-latex
  2702. :type 'plist)
  2703. (defcustom org-format-latex-header "\\documentclass{article}
  2704. \\usepackage{fullpage} % do not remove
  2705. \\usepackage{amssymb}
  2706. \\usepackage[usenames]{color}
  2707. \\usepackage{amsmath}
  2708. \\usepackage{latexsym}
  2709. \\usepackage[mathscr]{eucal}
  2710. \\pagestyle{empty} % do not remove"
  2711. "The document header used for processing LaTeX fragments."
  2712. :group 'org-latex
  2713. :type 'string)
  2714. (defgroup org-export nil
  2715. "Options for exporting org-listings."
  2716. :tag "Org Export"
  2717. :group 'org)
  2718. (defgroup org-export-general nil
  2719. "General options for exporting Org-mode files."
  2720. :tag "Org Export General"
  2721. :group 'org-export)
  2722. ;; FIXME
  2723. (defvar org-export-publishing-directory nil)
  2724. (defcustom org-export-with-special-strings t
  2725. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2726. When this option is turned on, these strings will be exported as:
  2727. Org HTML LaTeX
  2728. -----+----------+--------
  2729. \\- &shy; \\-
  2730. -- &ndash; --
  2731. --- &mdash; ---
  2732. ... &hellip; \ldots
  2733. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2734. :group 'org-export-translation
  2735. :type 'boolean)
  2736. (defcustom org-export-language-setup
  2737. '(("en" "Author" "Date" "Table of Contents")
  2738. ("cs" "Autor" "Datum" "Obsah")
  2739. ("da" "Ophavsmand" "Dato" "Indhold")
  2740. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2741. ("es" "Autor" "Fecha" "\xcdndice")
  2742. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2743. ("it" "Autore" "Data" "Indice")
  2744. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2745. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2746. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2747. "Terms used in export text, translated to different languages.
  2748. Use the variable `org-export-default-language' to set the language,
  2749. or use the +OPTION lines for a per-file setting."
  2750. :group 'org-export-general
  2751. :type '(repeat
  2752. (list
  2753. (string :tag "HTML language tag")
  2754. (string :tag "Author")
  2755. (string :tag "Date")
  2756. (string :tag "Table of Contents"))))
  2757. (defcustom org-export-default-language "en"
  2758. "The default language of HTML export, as a string.
  2759. This should have an association in `org-export-language-setup'."
  2760. :group 'org-export-general
  2761. :type 'string)
  2762. (defcustom org-export-skip-text-before-1st-heading t
  2763. "Non-nil means, skip all text before the first headline when exporting.
  2764. When nil, that text is exported as well."
  2765. :group 'org-export-general
  2766. :type 'boolean)
  2767. (defcustom org-export-headline-levels 3
  2768. "The last level which is still exported as a headline.
  2769. Inferior levels will produce itemize lists when exported.
  2770. Note that a numeric prefix argument to an exporter function overrides
  2771. this setting.
  2772. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2773. :group 'org-export-general
  2774. :type 'number)
  2775. (defcustom org-export-with-section-numbers t
  2776. "Non-nil means, add section numbers to headlines when exporting.
  2777. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2778. :group 'org-export-general
  2779. :type 'boolean)
  2780. (defcustom org-export-with-toc t
  2781. "Non-nil means, create a table of contents in exported files.
  2782. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2783. When an integer, include levels up to N in the toc, this may then be
  2784. different from `org-export-headline-levels', but it will not be allowed
  2785. to be larger than the number of headline levels.
  2786. When nil, no table of contents is made.
  2787. Headlines which contain any TODO items will be marked with \"(*)\" in
  2788. ASCII export, and with red color in HTML output, if the option
  2789. `org-export-mark-todo-in-toc' is set.
  2790. In HTML output, the TOC will be clickable.
  2791. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2792. or \"toc:3\"."
  2793. :group 'org-export-general
  2794. :type '(choice
  2795. (const :tag "No Table of Contents" nil)
  2796. (const :tag "Full Table of Contents" t)
  2797. (integer :tag "TOC to level")))
  2798. (defcustom org-export-mark-todo-in-toc nil
  2799. "Non-nil means, mark TOC lines that contain any open TODO items."
  2800. :group 'org-export-general
  2801. :type 'boolean)
  2802. (defcustom org-export-preserve-breaks nil
  2803. "Non-nil means, preserve all line breaks when exporting.
  2804. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2805. export, line breaks will always be preserved, regardless of this variable.
  2806. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2807. :group 'org-export-general
  2808. :type 'boolean)
  2809. (defcustom org-export-with-archived-trees 'headline
  2810. "Whether subtrees with the ARCHIVE tag should be exported.
  2811. This can have three different values
  2812. nil Do not export, pretend this tree is not present
  2813. t Do export the entire tree
  2814. headline Only export the headline, but skip the tree below it."
  2815. :group 'org-export-general
  2816. :group 'org-archive
  2817. :type '(choice
  2818. (const :tag "not at all" nil)
  2819. (const :tag "headline only" 'headline)
  2820. (const :tag "entirely" t)))
  2821. (defcustom org-export-author-info t
  2822. "Non-nil means, insert author name and email into the exported file.
  2823. This option can also be set with the +OPTIONS line,
  2824. e.g. \"author-info:nil\"."
  2825. :group 'org-export-general
  2826. :type 'boolean)
  2827. (defcustom org-export-time-stamp-file t
  2828. "Non-nil means, insert a time stamp into the exported file.
  2829. The time stamp shows when the file was created.
  2830. This option can also be set with the +OPTIONS line,
  2831. e.g. \"timestamp:nil\"."
  2832. :group 'org-export-general
  2833. :type 'boolean)
  2834. (defcustom org-export-with-timestamps t
  2835. "If nil, do not export time stamps and associated keywords."
  2836. :group 'org-export-general
  2837. :type 'boolean)
  2838. (defcustom org-export-remove-timestamps-from-toc t
  2839. "If nil, remove timestamps from the table of contents entries."
  2840. :group 'org-export-general
  2841. :type 'boolean)
  2842. (defcustom org-export-with-tags 'not-in-toc
  2843. "If nil, do not export tags, just remove them from headlines.
  2844. If this is the symbol `not-in-toc', tags will be removed from table of
  2845. contents entries, but still be shown in the headlines of the document.
  2846. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2847. :group 'org-export-general
  2848. :type '(choice
  2849. (const :tag "Off" nil)
  2850. (const :tag "Not in TOC" not-in-toc)
  2851. (const :tag "On" t)))
  2852. (defcustom org-export-with-drawers nil
  2853. "Non-nil means, export with drawers like the property drawer.
  2854. When t, all drawers are exported. This may also be a list of
  2855. drawer names to export."
  2856. :group 'org-export-general
  2857. :type '(choice
  2858. (const :tag "All drawers" t)
  2859. (const :tag "None" nil)
  2860. (repeat :tag "Selected drawers"
  2861. (string :tag "Drawer name"))))
  2862. (defgroup org-export-translation nil
  2863. "Options for translating special ascii sequences for the export backends."
  2864. :tag "Org Export Translation"
  2865. :group 'org-export)
  2866. (defcustom org-export-with-emphasize t
  2867. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2868. If the export target supports emphasizing text, the word will be
  2869. typeset in bold, italic, or underlined, respectively. Works only for
  2870. single words, but you can say: I *really* *mean* *this*.
  2871. Not all export backends support this.
  2872. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2873. :group 'org-export-translation
  2874. :type 'boolean)
  2875. (defcustom org-export-with-footnotes t
  2876. "If nil, export [1] as a footnote marker.
  2877. Lines starting with [1] will be formatted as footnotes.
  2878. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2879. :group 'org-export-translation
  2880. :type 'boolean)
  2881. (defcustom org-export-with-sub-superscripts t
  2882. "Non-nil means, interpret \"_\" and \"^\" for export.
  2883. When this option is turned on, you can use TeX-like syntax for sub- and
  2884. superscripts. Several characters after \"_\" or \"^\" will be
  2885. considered as a single item - so grouping with {} is normally not
  2886. needed. For example, the following things will be parsed as single
  2887. sub- or superscripts.
  2888. 10^24 or 10^tau several digits will be considered 1 item.
  2889. 10^-12 or 10^-tau a leading sign with digits or a word
  2890. x^2-y^3 will be read as x^2 - y^3, because items are
  2891. terminated by almost any nonword/nondigit char.
  2892. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2893. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2894. sub/superscript. If you set this variable to the symbol `{}',
  2895. the braces are *required* in order to trigger interpretations as
  2896. sub/superscript. This can be helpful in documents that need \"_\"
  2897. frequently in plain text.
  2898. Not all export backends support this, but HTML does.
  2899. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2900. :group 'org-export-translation
  2901. :type '(choice
  2902. (const :tag "Always interpret" t)
  2903. (const :tag "Only with braces" {})
  2904. (const :tag "Never interpret" nil)))
  2905. (defcustom org-export-with-special-strings t
  2906. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2907. When this option is turned on, these strings will be exported as:
  2908. \\- : &shy;
  2909. -- : &ndash;
  2910. --- : &mdash;
  2911. Not all export backends support this, but HTML does.
  2912. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2913. :group 'org-export-translation
  2914. :type 'boolean)
  2915. (defcustom org-export-with-TeX-macros t
  2916. "Non-nil means, interpret simple TeX-like macros when exporting.
  2917. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2918. No only real TeX macros will work here, but the standard HTML entities
  2919. for math can be used as macro names as well. For a list of supported
  2920. names in HTML export, see the constant `org-html-entities'.
  2921. Not all export backends support this.
  2922. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2923. :group 'org-export-translation
  2924. :group 'org-export-latex
  2925. :type 'boolean)
  2926. (defcustom org-export-with-LaTeX-fragments nil
  2927. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2928. When set, the exporter will find LaTeX environments if the \\begin line is
  2929. the first non-white thing on a line. It will also find the math delimiters
  2930. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2931. display math.
  2932. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2933. :group 'org-export-translation
  2934. :group 'org-export-latex
  2935. :type 'boolean)
  2936. (defcustom org-export-with-fixed-width t
  2937. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2938. This can be used to have pre-formatted text, fragments of code etc. For
  2939. example:
  2940. : ;; Some Lisp examples
  2941. : (while (defc cnt)
  2942. : (ding))
  2943. will be looking just like this in also HTML. See also the QUOTE keyword.
  2944. Not all export backends support this.
  2945. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2946. :group 'org-export-translation
  2947. :type 'boolean)
  2948. (defcustom org-match-sexp-depth 3
  2949. "Number of stacked braces for sub/superscript matching.
  2950. This has to be set before loading org.el to be effective."
  2951. :group 'org-export-translation
  2952. :type 'integer)
  2953. (defgroup org-export-tables nil
  2954. "Options for exporting tables in Org-mode."
  2955. :tag "Org Export Tables"
  2956. :group 'org-export)
  2957. (defcustom org-export-with-tables t
  2958. "If non-nil, lines starting with \"|\" define a table.
  2959. For example:
  2960. | Name | Address | Birthday |
  2961. |-------------+----------+-----------|
  2962. | Arthur Dent | England | 29.2.2100 |
  2963. Not all export backends support this.
  2964. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2965. :group 'org-export-tables
  2966. :type 'boolean)
  2967. (defcustom org-export-highlight-first-table-line t
  2968. "Non-nil means, highlight the first table line.
  2969. In HTML export, this means use <th> instead of <td>.
  2970. In tables created with table.el, this applies to the first table line.
  2971. In Org-mode tables, all lines before the first horizontal separator
  2972. line will be formatted with <th> tags."
  2973. :group 'org-export-tables
  2974. :type 'boolean)
  2975. (defcustom org-export-table-remove-special-lines t
  2976. "Remove special lines and marking characters in calculating tables.
  2977. This removes the special marking character column from tables that are set
  2978. up for spreadsheet calculations. It also removes the entire lines
  2979. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2980. the values of constants may be useful to have."
  2981. :group 'org-export-tables
  2982. :type 'boolean)
  2983. (defcustom org-export-prefer-native-exporter-for-tables nil
  2984. "Non-nil means, always export tables created with table.el natively.
  2985. Natively means, use the HTML code generator in table.el.
  2986. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2987. the table does not use row- or column-spanning). This has the
  2988. advantage, that the automatic HTML conversions for math symbols and
  2989. sub/superscripts can be applied. Org-mode's HTML generator is also
  2990. much faster."
  2991. :group 'org-export-tables
  2992. :type 'boolean)
  2993. (defgroup org-export-ascii nil
  2994. "Options specific for ASCII export of Org-mode files."
  2995. :tag "Org Export ASCII"
  2996. :group 'org-export)
  2997. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2998. "Characters for underlining headings in ASCII export.
  2999. In the given sequence, these characters will be used for level 1, 2, ..."
  3000. :group 'org-export-ascii
  3001. :type '(repeat character))
  3002. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3003. "Bullet characters for headlines converted to lists in ASCII export.
  3004. The first character is used for the first lest level generated in this
  3005. way, and so on. If there are more levels than characters given here,
  3006. the list will be repeated.
  3007. Note that plain lists will keep the same bullets as the have in the
  3008. Org-mode file."
  3009. :group 'org-export-ascii
  3010. :type '(repeat character))
  3011. (defgroup org-export-xml nil
  3012. "Options specific for XML export of Org-mode files."
  3013. :tag "Org Export XML"
  3014. :group 'org-export)
  3015. (defgroup org-export-html nil
  3016. "Options specific for HTML export of Org-mode files."
  3017. :tag "Org Export HTML"
  3018. :group 'org-export)
  3019. (defcustom org-export-html-coding-system nil
  3020. ""
  3021. :group 'org-export-html
  3022. :type 'coding-system)
  3023. (defcustom org-export-html-extension "html"
  3024. "The extension for exported HTML files."
  3025. :group 'org-export-html
  3026. :type 'string)
  3027. (defcustom org-export-html-style
  3028. "<style type=\"text/css\">
  3029. html {
  3030. font-family: Times, serif;
  3031. font-size: 12pt;
  3032. }
  3033. .title { text-align: center; }
  3034. .todo { color: red; }
  3035. .done { color: green; }
  3036. .timestamp { color: grey }
  3037. .timestamp-kwd { color: CadetBlue }
  3038. .tag { background-color:lightblue; font-weight:normal }
  3039. .target { background-color: lavender; }
  3040. pre {
  3041. border: 1pt solid #AEBDCC;
  3042. background-color: #F3F5F7;
  3043. padding: 5pt;
  3044. font-family: courier, monospace;
  3045. }
  3046. table { border-collapse: collapse; }
  3047. td, th {
  3048. vertical-align: top;
  3049. <!--border: 1pt solid #ADB9CC;-->
  3050. }
  3051. </style>"
  3052. "The default style specification for exported HTML files.
  3053. Since there are different ways of setting style information, this variable
  3054. needs to contain the full HTML structure to provide a style, including the
  3055. surrounding HTML tags. The style specifications should include definitions
  3056. for new classes todo, done, title, and deadline. For example, legal values
  3057. would be:
  3058. <style type=\"text/css\">
  3059. p { font-weight: normal; color: gray; }
  3060. h1 { color: black; }
  3061. .title { text-align: center; }
  3062. .todo, .deadline { color: red; }
  3063. .done { color: green; }
  3064. </style>
  3065. or, if you want to keep the style in a file,
  3066. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3067. As the value of this option simply gets inserted into the HTML <head> header,
  3068. you can \"misuse\" it to add arbitrary text to the header."
  3069. :group 'org-export-html
  3070. :type 'string)
  3071. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3072. "Format for typesetting the document title in HTML export."
  3073. :group 'org-export-html
  3074. :type 'string)
  3075. (defcustom org-export-html-toplevel-hlevel 2
  3076. "The <H> level for level 1 headings in HTML export."
  3077. :group 'org-export-html
  3078. :type 'string)
  3079. (defcustom org-export-html-link-org-files-as-html t
  3080. "Non-nil means, make file links to `file.org' point to `file.html'.
  3081. When org-mode is exporting an org-mode file to HTML, links to
  3082. non-html files are directly put into a href tag in HTML.
  3083. However, links to other Org-mode files (recognized by the
  3084. extension `.org.) should become links to the corresponding html
  3085. file, assuming that the linked org-mode file will also be
  3086. converted to HTML.
  3087. When nil, the links still point to the plain `.org' file."
  3088. :group 'org-export-html
  3089. :type 'boolean)
  3090. (defcustom org-export-html-inline-images 'maybe
  3091. "Non-nil means, inline images into exported HTML pages.
  3092. This is done using an <img> tag. When nil, an anchor with href is used to
  3093. link to the image. If this option is `maybe', then images in links with
  3094. an empty description will be inlined, while images with a description will
  3095. be linked only."
  3096. :group 'org-export-html
  3097. :type '(choice (const :tag "Never" nil)
  3098. (const :tag "Always" t)
  3099. (const :tag "When there is no description" maybe)))
  3100. ;; FIXME: rename
  3101. (defcustom org-export-html-expand t
  3102. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3103. When nil, these tags will be exported as plain text and therefore
  3104. not be interpreted by a browser.
  3105. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3106. :group 'org-export-html
  3107. :type 'boolean)
  3108. (defcustom org-export-html-table-tag
  3109. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3110. "The HTML tag that is used to start a table.
  3111. This must be a <table> tag, but you may change the options like
  3112. borders and spacing."
  3113. :group 'org-export-html
  3114. :type 'string)
  3115. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3116. "The opening tag for table header fields.
  3117. This is customizable so that alignment options can be specified."
  3118. :group 'org-export-tables
  3119. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3120. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3121. "The opening tag for table data fields.
  3122. This is customizable so that alignment options can be specified."
  3123. :group 'org-export-tables
  3124. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3125. (defcustom org-export-html-with-timestamp nil
  3126. "If non-nil, write `org-export-html-html-helper-timestamp'
  3127. into the exported HTML text. Otherwise, the buffer will just be saved
  3128. to a file."
  3129. :group 'org-export-html
  3130. :type 'boolean)
  3131. (defcustom org-export-html-html-helper-timestamp
  3132. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3133. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3134. :group 'org-export-html
  3135. :type 'string)
  3136. (defgroup org-export-icalendar nil
  3137. "Options specific for iCalendar export of Org-mode files."
  3138. :tag "Org Export iCalendar"
  3139. :group 'org-export)
  3140. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3141. "The file name for the iCalendar file covering all agenda files.
  3142. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3143. The file name should be absolute, the file will be overwritten without warning."
  3144. :group 'org-export-icalendar
  3145. :type 'file)
  3146. (defcustom org-icalendar-include-todo nil
  3147. "Non-nil means, export to iCalendar files should also cover TODO items."
  3148. :group 'org-export-icalendar
  3149. :type '(choice
  3150. (const :tag "None" nil)
  3151. (const :tag "Unfinished" t)
  3152. (const :tag "All" all)))
  3153. (defcustom org-icalendar-include-sexps t
  3154. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3155. These are entries like in the diary, but directly in an Org-mode file."
  3156. :group 'org-export-icalendar
  3157. :type 'boolean)
  3158. (defcustom org-icalendar-include-body 100
  3159. "Amount of text below headline to be included in iCalendar export.
  3160. This is a number of characters that should maximally be included.
  3161. Properties, scheduling and clocking lines will always be removed.
  3162. The text will be inserted into the DESCRIPTION field."
  3163. :group 'org-export-icalendar
  3164. :type '(choice
  3165. (const :tag "Nothing" nil)
  3166. (const :tag "Everything" t)
  3167. (integer :tag "Max characters")))
  3168. (defcustom org-icalendar-combined-name "OrgMode"
  3169. "Calendar name for the combined iCalendar representing all agenda files."
  3170. :group 'org-export-icalendar
  3171. :type 'string)
  3172. (defgroup org-font-lock nil
  3173. "Font-lock settings for highlighting in Org-mode."
  3174. :tag "Org Font Lock"
  3175. :group 'org)
  3176. (defcustom org-level-color-stars-only nil
  3177. "Non-nil means fontify only the stars in each headline.
  3178. When nil, the entire headline is fontified.
  3179. Changing it requires restart of `font-lock-mode' to become effective
  3180. also in regions already fontified."
  3181. :group 'org-font-lock
  3182. :type 'boolean)
  3183. (defcustom org-hide-leading-stars nil
  3184. "Non-nil means, hide the first N-1 stars in a headline.
  3185. This works by using the face `org-hide' for these stars. This
  3186. face is white for a light background, and black for a dark
  3187. background. You may have to customize the face `org-hide' to
  3188. make this work.
  3189. Changing it requires restart of `font-lock-mode' to become effective
  3190. also in regions already fontified.
  3191. You may also set this on a per-file basis by adding one of the following
  3192. lines to the buffer:
  3193. #+STARTUP: hidestars
  3194. #+STARTUP: showstars"
  3195. :group 'org-font-lock
  3196. :type 'boolean)
  3197. (defcustom org-fontify-done-headline nil
  3198. "Non-nil means, change the face of a headline if it is marked DONE.
  3199. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3200. When this is non-nil, the headline after the keyword is set to the
  3201. `org-headline-done' as an additional indication."
  3202. :group 'org-font-lock
  3203. :type 'boolean)
  3204. (defcustom org-fontify-emphasized-text t
  3205. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3206. Changing this variable requires a restart of Emacs to take effect."
  3207. :group 'org-font-lock
  3208. :type 'boolean)
  3209. (defcustom org-highlight-latex-fragments-and-specials nil
  3210. "Non-nil means, fontify what is treated specially by the exporters."
  3211. :group 'org-font-lock
  3212. :type 'boolean)
  3213. (defcustom org-hide-emphasis-markers nil
  3214. "Non-nil mean font-lock should hide the emphasis marker characters."
  3215. :group 'org-font-lock
  3216. :type 'boolean)
  3217. (defvar org-emph-re nil
  3218. "Regular expression for matching emphasis.")
  3219. (defvar org-verbatim-re nil
  3220. "Regular expression for matching verbatim text.")
  3221. (defvar org-emphasis-regexp-components) ; defined just below
  3222. (defvar org-emphasis-alist) ; defined just below
  3223. (defun org-set-emph-re (var val)
  3224. "Set variable and compute the emphasis regular expression."
  3225. (set var val)
  3226. (when (and (boundp 'org-emphasis-alist)
  3227. (boundp 'org-emphasis-regexp-components)
  3228. org-emphasis-alist org-emphasis-regexp-components)
  3229. (let* ((e org-emphasis-regexp-components)
  3230. (pre (car e))
  3231. (post (nth 1 e))
  3232. (border (nth 2 e))
  3233. (body (nth 3 e))
  3234. (nl (nth 4 e))
  3235. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3236. (body1 (concat body "*?"))
  3237. (markers (mapconcat 'car org-emphasis-alist ""))
  3238. (vmarkers (mapconcat
  3239. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3240. org-emphasis-alist "")))
  3241. ;; make sure special characters appear at the right position in the class
  3242. (if (string-match "\\^" markers)
  3243. (setq markers (concat (replace-match "" t t markers) "^")))
  3244. (if (string-match "-" markers)
  3245. (setq markers (concat (replace-match "" t t markers) "-")))
  3246. (if (string-match "\\^" vmarkers)
  3247. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3248. (if (string-match "-" vmarkers)
  3249. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3250. (if (> nl 0)
  3251. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3252. (int-to-string nl) "\\}")))
  3253. ;; Make the regexp
  3254. (setq org-emph-re
  3255. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3256. "\\("
  3257. "\\([" markers "]\\)"
  3258. "\\("
  3259. "[^" border "]\\|"
  3260. "[^" border (if (and nil stacked) markers) "]"
  3261. body1
  3262. "[^" border (if (and nil stacked) markers) "]"
  3263. "\\)"
  3264. "\\3\\)"
  3265. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3266. (setq org-verbatim-re
  3267. (concat "\\([" pre "]\\|^\\)"
  3268. "\\("
  3269. "\\([" vmarkers "]\\)"
  3270. "\\("
  3271. "[^" border "]\\|"
  3272. "[^" border "]"
  3273. body1
  3274. "[^" border "]"
  3275. "\\)"
  3276. "\\3\\)"
  3277. "\\([" post "]\\|$\\)")))))
  3278. (defcustom org-emphasis-regexp-components
  3279. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3280. "Components used to build the regular expression for emphasis.
  3281. This is a list with 6 entries. Terminology: In an emphasis string
  3282. like \" *strong word* \", we call the initial space PREMATCH, the final
  3283. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3284. and \"trong wor\" is the body. The different components in this variable
  3285. specify what is allowed/forbidden in each part:
  3286. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3287. post Chars allowed as postmatch. End of line will be allowed too.
  3288. border The chars *forbidden* as border characters.
  3289. body-regexp A regexp like \".\" to match a body character. Don't use
  3290. non-shy groups here, and don't allow newline here.
  3291. newline The maximum number of newlines allowed in an emphasis exp.
  3292. Use customize to modify this, or restart Emacs after changing it."
  3293. :group 'org-font-lock
  3294. :set 'org-set-emph-re
  3295. :type '(list
  3296. (sexp :tag "Allowed chars in pre ")
  3297. (sexp :tag "Allowed chars in post ")
  3298. (sexp :tag "Forbidden chars in border ")
  3299. (sexp :tag "Regexp for body ")
  3300. (integer :tag "number of newlines allowed")
  3301. (option (boolean :tag "Stacking (DISABLED) "))))
  3302. (defcustom org-emphasis-alist
  3303. '(("*" bold "<b>" "</b>")
  3304. ("/" italic "<i>" "</i>")
  3305. ("_" underline "<u>" "</u>")
  3306. ("=" org-code "<code>" "</code>" verbatim)
  3307. ("~" org-verbatim "" "" verbatim)
  3308. ("+" (:strike-through t) "<del>" "</del>")
  3309. )
  3310. "Special syntax for emphasized text.
  3311. Text starting and ending with a special character will be emphasized, for
  3312. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3313. characters, the face to be used by font-lock for highlighting in Org-mode
  3314. Emacs buffers, and the HTML tags to be used for this.
  3315. Use customize to modify this, or restart Emacs after changing it."
  3316. :group 'org-font-lock
  3317. :set 'org-set-emph-re
  3318. :type '(repeat
  3319. (list
  3320. (string :tag "Marker character")
  3321. (choice
  3322. (face :tag "Font-lock-face")
  3323. (plist :tag "Face property list"))
  3324. (string :tag "HTML start tag")
  3325. (string :tag "HTML end tag")
  3326. (option (const verbatim)))))
  3327. ;;; The faces
  3328. (defgroup org-faces nil
  3329. "Faces in Org-mode."
  3330. :tag "Org Faces"
  3331. :group 'org-font-lock)
  3332. (defun org-compatible-face (inherits specs)
  3333. "Make a compatible face specification.
  3334. If INHERITS is an existing face and if the Emacs version supports it,
  3335. just inherit the face. If not, use SPECS to define the face.
  3336. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3337. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3338. to the top of the list. The `min-colors' attribute will be removed from
  3339. any other entries, and any resulting duplicates will be removed entirely."
  3340. (cond
  3341. ((and inherits (facep inherits)
  3342. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3343. ;; In Emacs 23, we use inheritance where possible.
  3344. ;; We only do this in Emacs 23, because only there the outline
  3345. ;; faces have been changed to the original org-mode-level-faces.
  3346. (list (list t :inherit inherits)))
  3347. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3348. ;; These do not understand the `min-colors' attribute.
  3349. (let (r e a)
  3350. (while (setq e (pop specs))
  3351. (cond
  3352. ((memq (car e) '(t default)) (push e r))
  3353. ((setq a (member '(min-colors 8) (car e)))
  3354. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3355. (cdr e)))))
  3356. ((setq a (assq 'min-colors (car e)))
  3357. (setq e (cons (delq a (car e)) (cdr e)))
  3358. (or (assoc (car e) r) (push e r)))
  3359. (t (or (assoc (car e) r) (push e r)))))
  3360. (nreverse r)))
  3361. (t specs)))
  3362. (put 'org-compatible-face 'lisp-indent-function 1)
  3363. (defface org-hide
  3364. '((((background light)) (:foreground "white"))
  3365. (((background dark)) (:foreground "black")))
  3366. "Face used to hide leading stars in headlines.
  3367. The forground color of this face should be equal to the background
  3368. color of the frame."
  3369. :group 'org-faces)
  3370. (defface org-level-1 ;; font-lock-function-name-face
  3371. (org-compatible-face 'outline-1
  3372. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3373. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3374. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3375. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3376. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3377. (t (:bold t))))
  3378. "Face used for level 1 headlines."
  3379. :group 'org-faces)
  3380. (defface org-level-2 ;; font-lock-variable-name-face
  3381. (org-compatible-face 'outline-2
  3382. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3383. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3384. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3385. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3386. (t (:bold t))))
  3387. "Face used for level 2 headlines."
  3388. :group 'org-faces)
  3389. (defface org-level-3 ;; font-lock-keyword-face
  3390. (org-compatible-face 'outline-3
  3391. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3392. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3393. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3394. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3395. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3396. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3397. (t (:bold t))))
  3398. "Face used for level 3 headlines."
  3399. :group 'org-faces)
  3400. (defface org-level-4 ;; font-lock-comment-face
  3401. (org-compatible-face 'outline-4
  3402. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3403. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3404. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3405. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3406. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3407. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3408. (t (:bold t))))
  3409. "Face used for level 4 headlines."
  3410. :group 'org-faces)
  3411. (defface org-level-5 ;; font-lock-type-face
  3412. (org-compatible-face 'outline-5
  3413. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3414. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3415. (((class color) (min-colors 8)) (:foreground "green"))))
  3416. "Face used for level 5 headlines."
  3417. :group 'org-faces)
  3418. (defface org-level-6 ;; font-lock-constant-face
  3419. (org-compatible-face 'outline-6
  3420. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3421. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3422. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3423. "Face used for level 6 headlines."
  3424. :group 'org-faces)
  3425. (defface org-level-7 ;; font-lock-builtin-face
  3426. (org-compatible-face 'outline-7
  3427. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3428. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3429. (((class color) (min-colors 8)) (:foreground "blue"))))
  3430. "Face used for level 7 headlines."
  3431. :group 'org-faces)
  3432. (defface org-level-8 ;; font-lock-string-face
  3433. (org-compatible-face 'outline-8
  3434. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3435. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3436. (((class color) (min-colors 8)) (:foreground "green"))))
  3437. "Face used for level 8 headlines."
  3438. :group 'org-faces)
  3439. (defface org-special-keyword ;; font-lock-string-face
  3440. (org-compatible-face nil
  3441. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3442. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3443. (t (:italic t))))
  3444. "Face used for special keywords."
  3445. :group 'org-faces)
  3446. (defface org-drawer ;; font-lock-function-name-face
  3447. (org-compatible-face nil
  3448. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3449. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3450. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3451. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3452. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3453. (t (:bold t))))
  3454. "Face used for drawers."
  3455. :group 'org-faces)
  3456. (defface org-property-value nil
  3457. "Face used for the value of a property."
  3458. :group 'org-faces)
  3459. (defface org-column
  3460. (org-compatible-face nil
  3461. '((((class color) (min-colors 16) (background light))
  3462. (:background "grey90"))
  3463. (((class color) (min-colors 16) (background dark))
  3464. (:background "grey30"))
  3465. (((class color) (min-colors 8))
  3466. (:background "cyan" :foreground "black"))
  3467. (t (:inverse-video t))))
  3468. "Face for column display of entry properties."
  3469. :group 'org-faces)
  3470. (when (fboundp 'set-face-attribute)
  3471. ;; Make sure that a fixed-width face is used when we have a column table.
  3472. (set-face-attribute 'org-column nil
  3473. :height (face-attribute 'default :height)
  3474. :family (face-attribute 'default :family)))
  3475. (defface org-warning
  3476. (org-compatible-face 'font-lock-warning-face
  3477. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3478. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3479. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3480. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3481. (t (:bold t))))
  3482. "Face for deadlines and TODO keywords."
  3483. :group 'org-faces)
  3484. (defface org-archived ; similar to shadow
  3485. (org-compatible-face 'shadow
  3486. '((((class color grayscale) (min-colors 88) (background light))
  3487. (:foreground "grey50"))
  3488. (((class color grayscale) (min-colors 88) (background dark))
  3489. (:foreground "grey70"))
  3490. (((class color) (min-colors 8) (background light))
  3491. (:foreground "green"))
  3492. (((class color) (min-colors 8) (background dark))
  3493. (:foreground "yellow"))))
  3494. "Face for headline with the ARCHIVE tag."
  3495. :group 'org-faces)
  3496. (defface org-link
  3497. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3498. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3499. (t (:underline t)))
  3500. "Face for links."
  3501. :group 'org-faces)
  3502. (defface org-ellipsis
  3503. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3504. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3505. (t (:strike-through t)))
  3506. "Face for the ellipsis in folded text."
  3507. :group 'org-faces)
  3508. (defface org-target
  3509. '((((class color) (background light)) (:underline t))
  3510. (((class color) (background dark)) (:underline t))
  3511. (t (:underline t)))
  3512. "Face for links."
  3513. :group 'org-faces)
  3514. (defface org-date
  3515. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3516. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3517. (t (:underline t)))
  3518. "Face for links."
  3519. :group 'org-faces)
  3520. (defface org-sexp-date
  3521. '((((class color) (background light)) (:foreground "Purple"))
  3522. (((class color) (background dark)) (:foreground "Cyan"))
  3523. (t (:underline t)))
  3524. "Face for links."
  3525. :group 'org-faces)
  3526. (defface org-tag
  3527. '((t (:bold t)))
  3528. "Face for tags."
  3529. :group 'org-faces)
  3530. (defface org-todo ; font-lock-warning-face
  3531. (org-compatible-face nil
  3532. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3533. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3534. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3535. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3536. (t (:inverse-video t :bold t))))
  3537. "Face for TODO keywords."
  3538. :group 'org-faces)
  3539. (defface org-done ;; font-lock-type-face
  3540. (org-compatible-face nil
  3541. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3542. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3543. (((class color) (min-colors 8)) (:foreground "green"))
  3544. (t (:bold t))))
  3545. "Face used for todo keywords that indicate DONE items."
  3546. :group 'org-faces)
  3547. (defface org-headline-done ;; font-lock-string-face
  3548. (org-compatible-face nil
  3549. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3550. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3551. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3552. "Face used to indicate that a headline is DONE.
  3553. This face is only used if `org-fontify-done-headline' is set. If applies
  3554. to the part of the headline after the DONE keyword."
  3555. :group 'org-faces)
  3556. (defcustom org-todo-keyword-faces nil
  3557. "Faces for specific TODO keywords.
  3558. This is a list of cons cells, with TODO keywords in the car
  3559. and faces in the cdr. The face can be a symbol, or a property
  3560. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3561. :group 'org-faces
  3562. :group 'org-todo
  3563. :type '(repeat
  3564. (cons
  3565. (string :tag "keyword")
  3566. (sexp :tag "face"))))
  3567. (defface org-table ;; font-lock-function-name-face
  3568. (org-compatible-face nil
  3569. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3570. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3571. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3572. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3573. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3574. (((class color) (min-colors 8) (background dark)))))
  3575. "Face used for tables."
  3576. :group 'org-faces)
  3577. (defface org-formula
  3578. (org-compatible-face nil
  3579. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3580. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3581. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3582. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3583. (t (:bold t :italic t))))
  3584. "Face for formulas."
  3585. :group 'org-faces)
  3586. (defface org-code
  3587. (org-compatible-face nil
  3588. '((((class color grayscale) (min-colors 88) (background light))
  3589. (:foreground "grey50"))
  3590. (((class color grayscale) (min-colors 88) (background dark))
  3591. (:foreground "grey70"))
  3592. (((class color) (min-colors 8) (background light))
  3593. (:foreground "green"))
  3594. (((class color) (min-colors 8) (background dark))
  3595. (:foreground "yellow"))))
  3596. "Face for fixed-with text like code snippets."
  3597. :group 'org-faces
  3598. :version "22.1")
  3599. (defface org-verbatim
  3600. (org-compatible-face nil
  3601. '((((class color grayscale) (min-colors 88) (background light))
  3602. (:foreground "grey50" :underline t))
  3603. (((class color grayscale) (min-colors 88) (background dark))
  3604. (:foreground "grey70" :underline t))
  3605. (((class color) (min-colors 8) (background light))
  3606. (:foreground "green" :underline t))
  3607. (((class color) (min-colors 8) (background dark))
  3608. (:foreground "yellow" :underline t))))
  3609. "Face for fixed-with text like code snippets."
  3610. :group 'org-faces
  3611. :version "22.1")
  3612. (defface org-agenda-structure ;; font-lock-function-name-face
  3613. (org-compatible-face nil
  3614. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3615. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3616. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3617. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3618. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3619. (t (:bold t))))
  3620. "Face used in agenda for captions and dates."
  3621. :group 'org-faces)
  3622. (defface org-scheduled-today
  3623. (org-compatible-face nil
  3624. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3625. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3626. (((class color) (min-colors 8)) (:foreground "green"))
  3627. (t (:bold t :italic t))))
  3628. "Face for items scheduled for a certain day."
  3629. :group 'org-faces)
  3630. (defface org-scheduled-previously
  3631. (org-compatible-face nil
  3632. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3633. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3634. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3635. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3636. (t (:bold t))))
  3637. "Face for items scheduled previously, and not yet done."
  3638. :group 'org-faces)
  3639. (defface org-upcoming-deadline
  3640. (org-compatible-face nil
  3641. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3642. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3643. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3644. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3645. (t (:bold t))))
  3646. "Face for items scheduled previously, and not yet done."
  3647. :group 'org-faces)
  3648. (defcustom org-agenda-deadline-faces
  3649. '((1.0 . org-warning)
  3650. (0.5 . org-upcoming-deadline)
  3651. (0.0 . default))
  3652. "Faces for showing deadlines in the agenda.
  3653. This is a list of cons cells. The cdr of each cell is a face to be used,
  3654. and it can also just be like '(:foreground \"yellow\").
  3655. Each car is a fraction of the head-warning time that must have passed for
  3656. this the face in the cdr to be used for display. The numbers must be
  3657. given in descending order. The head-warning time is normally taken
  3658. from `org-deadline-warning-days', but can also be specified in the deadline
  3659. timestamp itself, like this:
  3660. DEADLINE: <2007-08-13 Mon -8d>
  3661. You may use d for days, w for weeks, m for months and y for years. Months
  3662. and years will only be treated in an approximate fashion (30.4 days for a
  3663. month and 365.24 days for a year)."
  3664. :group 'org-faces
  3665. :group 'org-agenda-daily/weekly
  3666. :type '(repeat
  3667. (cons
  3668. (number :tag "Fraction of head-warning time passed")
  3669. (sexp :tag "Face"))))
  3670. ;; FIXME: this is not a good face yet.
  3671. (defface org-agenda-restriction-lock
  3672. (org-compatible-face nil
  3673. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3674. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3675. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3676. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3677. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3678. (t (:inverse-video t))))
  3679. "Face for showing the agenda restriction lock."
  3680. :group 'org-faces)
  3681. (defface org-time-grid ;; font-lock-variable-name-face
  3682. (org-compatible-face nil
  3683. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3684. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3685. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3686. "Face used for time grids."
  3687. :group 'org-faces)
  3688. (defconst org-level-faces
  3689. '(org-level-1 org-level-2 org-level-3 org-level-4
  3690. org-level-5 org-level-6 org-level-7 org-level-8
  3691. ))
  3692. (defcustom org-n-level-faces (length org-level-faces)
  3693. "The number of different faces to be used for headlines.
  3694. Org-mode defines 8 different headline faces, so this can be at most 8.
  3695. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3696. :type 'number
  3697. :group 'org-faces)
  3698. ;;; Functions and variables from ther packages
  3699. ;; Declared here to avoid compiler warnings
  3700. (eval-and-compile
  3701. (unless (fboundp 'declare-function)
  3702. (defmacro declare-function (fn file &optional arglist fileonly))))
  3703. ;; XEmacs only
  3704. (defvar outline-mode-menu-heading)
  3705. (defvar outline-mode-menu-show)
  3706. (defvar outline-mode-menu-hide)
  3707. (defvar zmacs-regions) ; XEmacs regions
  3708. ;; Emacs only
  3709. (defvar mark-active)
  3710. ;; Various packages
  3711. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3712. (declare-function add-to-diary-list "diary-lib"
  3713. (date string specifier &optional marker globcolor literal))
  3714. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3715. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3716. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3717. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3718. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3719. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3720. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3721. (declare-function bbdb-record-name "ext:bbdb" (record))
  3722. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3723. (declare-function bibtex-generate-autokey "bibtex" ())
  3724. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3725. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3726. (defvar calc-embedded-close-formula)
  3727. (defvar calc-embedded-open-formula)
  3728. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3729. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3730. (declare-function calendar-check-holidays "holidays" (date))
  3731. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3732. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3733. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3734. (declare-function calendar-forward-day "cal-move" (arg))
  3735. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3736. (declare-function calendar-goto-date "cal-move" (date))
  3737. (declare-function calendar-goto-today "cal-move" ())
  3738. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3739. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3740. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3741. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3742. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3743. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3744. (defvar calendar-mode-map)
  3745. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3746. (declare-function cdlatex-tab "ext:cdlatex" ())
  3747. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3748. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3749. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3750. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3751. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3752. (defvar font-lock-unfontify-region-function)
  3753. (declare-function gnus-article-show-summary "gnus-art" ())
  3754. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3755. (defvar gnus-other-frame-object)
  3756. (defvar gnus-group-name)
  3757. (defvar gnus-article-current)
  3758. (defvar Info-current-file)
  3759. (defvar Info-current-node)
  3760. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3761. (declare-function mh-find-path "mh-utils" ())
  3762. (declare-function mh-get-header-field "mh-utils" (field))
  3763. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3764. (declare-function mh-header-display "mh-show" ())
  3765. (declare-function mh-index-previous-folder "mh-search" ())
  3766. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3767. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3768. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3769. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3770. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3771. (declare-function mh-show-header-display "mh-show" t t)
  3772. (declare-function mh-show-msg "mh-show" (msg))
  3773. (declare-function mh-show-show "mh-show" t t)
  3774. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3775. (defvar mh-progs)
  3776. (defvar mh-current-folder)
  3777. (defvar mh-show-folder-buffer)
  3778. (defvar mh-index-folder)
  3779. (defvar mh-searcher)
  3780. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3781. (declare-function parse-time-string "parse-time" (string))
  3782. (declare-function remember "remember" (&optional initial))
  3783. (declare-function remember-buffer-desc "remember" ())
  3784. (declare-function remember-finalize "remember" ())
  3785. (defvar remember-save-after-remembering)
  3786. (defvar remember-data-file)
  3787. (defvar remember-register)
  3788. (defvar remember-buffer)
  3789. (defvar remember-handler-functions)
  3790. (defvar remember-annotation-functions)
  3791. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3792. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3793. (declare-function rmail-what-message "rmail" ())
  3794. (defvar rmail-current-message)
  3795. (defvar texmathp-why)
  3796. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3797. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3798. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3799. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3800. (declare-function vm-isearch-update "ext:vm-search" ())
  3801. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3802. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3803. (declare-function vm-su-subject "ext:vm-summary" (m))
  3804. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3805. (defvar vm-message-pointer)
  3806. (defvar vm-folder-directory)
  3807. (defvar w3m-current-url)
  3808. (defvar w3m-current-title)
  3809. ;; backward compatibility to old version of wl
  3810. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3811. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3812. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3813. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3814. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3815. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3816. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3817. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3818. (defvar wl-summary-buffer-elmo-folder)
  3819. (defvar wl-summary-buffer-folder-name)
  3820. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3821. (defvar org-latex-regexps)
  3822. (defvar constants-unit-system)
  3823. ;;; Variables for pre-computed regular expressions, all buffer local
  3824. (defvar org-drawer-regexp nil
  3825. "Matches first line of a hidden block.")
  3826. (make-variable-buffer-local 'org-drawer-regexp)
  3827. (defvar org-todo-regexp nil
  3828. "Matches any of the TODO state keywords.")
  3829. (make-variable-buffer-local 'org-todo-regexp)
  3830. (defvar org-not-done-regexp nil
  3831. "Matches any of the TODO state keywords except the last one.")
  3832. (make-variable-buffer-local 'org-not-done-regexp)
  3833. (defvar org-todo-line-regexp nil
  3834. "Matches a headline and puts TODO state into group 2 if present.")
  3835. (make-variable-buffer-local 'org-todo-line-regexp)
  3836. (defvar org-complex-heading-regexp nil
  3837. "Matches a headline and puts everything into groups:
  3838. group 1: the stars
  3839. group 2: The todo keyword, maybe
  3840. group 3: Priority cookie
  3841. group 4: True headline
  3842. group 5: Tags")
  3843. (make-variable-buffer-local 'org-complex-heading-regexp)
  3844. (defvar org-todo-line-tags-regexp nil
  3845. "Matches a headline and puts TODO state into group 2 if present.
  3846. Also put tags into group 4 if tags are present.")
  3847. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3848. (defvar org-nl-done-regexp nil
  3849. "Matches newline followed by a headline with the DONE keyword.")
  3850. (make-variable-buffer-local 'org-nl-done-regexp)
  3851. (defvar org-looking-at-done-regexp nil
  3852. "Matches the DONE keyword a point.")
  3853. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3854. (defvar org-ds-keyword-length 12
  3855. "Maximum length of the Deadline and SCHEDULED keywords.")
  3856. (make-variable-buffer-local 'org-ds-keyword-length)
  3857. (defvar org-deadline-regexp nil
  3858. "Matches the DEADLINE keyword.")
  3859. (make-variable-buffer-local 'org-deadline-regexp)
  3860. (defvar org-deadline-time-regexp nil
  3861. "Matches the DEADLINE keyword together with a time stamp.")
  3862. (make-variable-buffer-local 'org-deadline-time-regexp)
  3863. (defvar org-deadline-line-regexp nil
  3864. "Matches the DEADLINE keyword and the rest of the line.")
  3865. (make-variable-buffer-local 'org-deadline-line-regexp)
  3866. (defvar org-scheduled-regexp nil
  3867. "Matches the SCHEDULED keyword.")
  3868. (make-variable-buffer-local 'org-scheduled-regexp)
  3869. (defvar org-scheduled-time-regexp nil
  3870. "Matches the SCHEDULED keyword together with a time stamp.")
  3871. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3872. (defvar org-closed-time-regexp nil
  3873. "Matches the CLOSED keyword together with a time stamp.")
  3874. (make-variable-buffer-local 'org-closed-time-regexp)
  3875. (defvar org-keyword-time-regexp nil
  3876. "Matches any of the 4 keywords, together with the time stamp.")
  3877. (make-variable-buffer-local 'org-keyword-time-regexp)
  3878. (defvar org-keyword-time-not-clock-regexp nil
  3879. "Matches any of the 3 keywords, together with the time stamp.")
  3880. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3881. (defvar org-maybe-keyword-time-regexp nil
  3882. "Matches a timestamp, possibly preceeded by a keyword.")
  3883. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3884. (defvar org-planning-or-clock-line-re nil
  3885. "Matches a line with planning or clock info.")
  3886. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3887. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3888. rear-nonsticky t mouse-map t fontified t)
  3889. "Properties to remove when a string without properties is wanted.")
  3890. (defsubst org-match-string-no-properties (num &optional string)
  3891. (if (featurep 'xemacs)
  3892. (let ((s (match-string num string)))
  3893. (remove-text-properties 0 (length s) org-rm-props s)
  3894. s)
  3895. (match-string-no-properties num string)))
  3896. (defsubst org-no-properties (s)
  3897. (if (fboundp 'set-text-properties)
  3898. (set-text-properties 0 (length s) nil s)
  3899. (remove-text-properties 0 (length s) org-rm-props s))
  3900. s)
  3901. (defsubst org-get-alist-option (option key)
  3902. (cond ((eq key t) t)
  3903. ((eq option t) t)
  3904. ((assoc key option) (cdr (assoc key option)))
  3905. (t (cdr (assq 'default option)))))
  3906. (defsubst org-inhibit-invisibility ()
  3907. "Modified `buffer-invisibility-spec' for Emacs 21.
  3908. Some ops with invisible text do not work correctly on Emacs 21. For these
  3909. we turn off invisibility temporarily. Use this in a `let' form."
  3910. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3911. (defsubst org-set-local (var value)
  3912. "Make VAR local in current buffer and set it to VALUE."
  3913. (set (make-variable-buffer-local var) value))
  3914. (defsubst org-mode-p ()
  3915. "Check if the current buffer is in Org-mode."
  3916. (eq major-mode 'org-mode))
  3917. (defsubst org-last (list)
  3918. "Return the last element of LIST."
  3919. (car (last list)))
  3920. (defun org-let (list &rest body)
  3921. (eval (cons 'let (cons list body))))
  3922. (put 'org-let 'lisp-indent-function 1)
  3923. (defun org-let2 (list1 list2 &rest body)
  3924. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3925. (put 'org-let2 'lisp-indent-function 2)
  3926. (defconst org-startup-options
  3927. '(("fold" org-startup-folded t)
  3928. ("overview" org-startup-folded t)
  3929. ("nofold" org-startup-folded nil)
  3930. ("showall" org-startup-folded nil)
  3931. ("content" org-startup-folded content)
  3932. ("hidestars" org-hide-leading-stars t)
  3933. ("showstars" org-hide-leading-stars nil)
  3934. ("odd" org-odd-levels-only t)
  3935. ("oddeven" org-odd-levels-only nil)
  3936. ("align" org-startup-align-all-tables t)
  3937. ("noalign" org-startup-align-all-tables nil)
  3938. ("customtime" org-display-custom-times t)
  3939. ("logdone" org-log-done time)
  3940. ("lognotedone" org-log-done note)
  3941. ("nologdone" org-log-done nil)
  3942. ("lognoteclock-out" org-log-note-clock-out t)
  3943. ("nolognoteclock-out" org-log-note-clock-out nil)
  3944. ("logrepeat" org-log-repeat state)
  3945. ("lognoterepeat" org-log-repeat note)
  3946. ("nologrepeat" org-log-repeat nil)
  3947. ("constcgs" constants-unit-system cgs)
  3948. ("constSI" constants-unit-system SI))
  3949. "Variable associated with STARTUP options for org-mode.
  3950. Each element is a list of three items: The startup options as written
  3951. in the #+STARTUP line, the corresponding variable, and the value to
  3952. set this variable to if the option is found. An optional forth element PUSH
  3953. means to push this value onto the list in the variable.")
  3954. (defun org-set-regexps-and-options ()
  3955. "Precompute regular expressions for current buffer."
  3956. (when (org-mode-p)
  3957. (org-set-local 'org-todo-kwd-alist nil)
  3958. (org-set-local 'org-todo-key-alist nil)
  3959. (org-set-local 'org-todo-key-trigger nil)
  3960. (org-set-local 'org-todo-keywords-1 nil)
  3961. (org-set-local 'org-done-keywords nil)
  3962. (org-set-local 'org-todo-heads nil)
  3963. (org-set-local 'org-todo-sets nil)
  3964. (org-set-local 'org-todo-log-states nil)
  3965. (let ((re (org-make-options-regexp
  3966. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3967. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3968. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3969. (splitre "[ \t]+")
  3970. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3971. tail sep kws1 prio props drawers)
  3972. (save-excursion
  3973. (save-restriction
  3974. (widen)
  3975. (goto-char (point-min))
  3976. (while (re-search-forward re nil t)
  3977. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3978. (cond
  3979. ((equal key "CATEGORY")
  3980. (if (string-match "[ \t]+$" value)
  3981. (setq value (replace-match "" t t value)))
  3982. (setq cat value))
  3983. ((member key '("SEQ_TODO" "TODO"))
  3984. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3985. ((equal key "TYP_TODO")
  3986. (push (cons 'type (org-split-string value splitre)) kwds))
  3987. ((equal key "TAGS")
  3988. (setq tags (append tags (org-split-string value splitre))))
  3989. ((equal key "COLUMNS")
  3990. (org-set-local 'org-columns-default-format value))
  3991. ((equal key "LINK")
  3992. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3993. (push (cons (match-string 1 value)
  3994. (org-trim (match-string 2 value)))
  3995. links)))
  3996. ((equal key "PRIORITIES")
  3997. (setq prio (org-split-string value " +")))
  3998. ((equal key "PROPERTY")
  3999. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4000. (push (cons (match-string 1 value) (match-string 2 value))
  4001. props)))
  4002. ((equal key "DRAWERS")
  4003. (setq drawers (org-split-string value splitre)))
  4004. ((equal key "CONSTANTS")
  4005. (setq const (append const (org-split-string value splitre))))
  4006. ((equal key "STARTUP")
  4007. (let ((opts (org-split-string value splitre))
  4008. l var val)
  4009. (while (setq l (pop opts))
  4010. (when (setq l (assoc l org-startup-options))
  4011. (setq var (nth 1 l) val (nth 2 l))
  4012. (if (not (nth 3 l))
  4013. (set (make-local-variable var) val)
  4014. (if (not (listp (symbol-value var)))
  4015. (set (make-local-variable var) nil))
  4016. (set (make-local-variable var) (symbol-value var))
  4017. (add-to-list var val))))))
  4018. ((equal key "ARCHIVE")
  4019. (string-match " *$" value)
  4020. (setq arch (replace-match "" t t value))
  4021. (remove-text-properties 0 (length arch)
  4022. '(face t fontified t) arch)))
  4023. )))
  4024. (when cat
  4025. (org-set-local 'org-category (intern cat))
  4026. (push (cons "CATEGORY" cat) props))
  4027. (when prio
  4028. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4029. (setq prio (mapcar 'string-to-char prio))
  4030. (org-set-local 'org-highest-priority (nth 0 prio))
  4031. (org-set-local 'org-lowest-priority (nth 1 prio))
  4032. (org-set-local 'org-default-priority (nth 2 prio)))
  4033. (and props (org-set-local 'org-local-properties (nreverse props)))
  4034. (and drawers (org-set-local 'org-drawers drawers))
  4035. (and arch (org-set-local 'org-archive-location arch))
  4036. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4037. ;; Process the TODO keywords
  4038. (unless kwds
  4039. ;; Use the global values as if they had been given locally.
  4040. (setq kwds (default-value 'org-todo-keywords))
  4041. (if (stringp (car kwds))
  4042. (setq kwds (list (cons org-todo-interpretation
  4043. (default-value 'org-todo-keywords)))))
  4044. (setq kwds (reverse kwds)))
  4045. (setq kwds (nreverse kwds))
  4046. (let (inter kws kw)
  4047. (while (setq kws (pop kwds))
  4048. (setq inter (pop kws) sep (member "|" kws)
  4049. kws0 (delete "|" (copy-sequence kws))
  4050. kwsa nil
  4051. kws1 (mapcar
  4052. (lambda (x)
  4053. ;; 1 2
  4054. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4055. (progn
  4056. (setq kw (match-string 1 x)
  4057. key (and (match-end 2) (match-string 2 x))
  4058. log (org-extract-log-state-settings x))
  4059. (push (cons kw (and key (string-to-char key))) kwsa)
  4060. (and log (push log org-todo-log-states))
  4061. kw)
  4062. (error "Invalid TODO keyword %s" x)))
  4063. kws0)
  4064. kwsa (if kwsa (append '((:startgroup))
  4065. (nreverse kwsa)
  4066. '((:endgroup))))
  4067. hw (car kws1)
  4068. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4069. tail (list inter hw (car dws) (org-last dws)))
  4070. (add-to-list 'org-todo-heads hw 'append)
  4071. (push kws1 org-todo-sets)
  4072. (setq org-done-keywords (append org-done-keywords dws nil))
  4073. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4074. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4075. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4076. (setq org-todo-sets (nreverse org-todo-sets)
  4077. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4078. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4079. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4080. ;; Process the constants
  4081. (when const
  4082. (let (e cst)
  4083. (while (setq e (pop const))
  4084. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4085. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4086. (setq org-table-formula-constants-local cst)))
  4087. ;; Process the tags.
  4088. (when tags
  4089. (let (e tgs)
  4090. (while (setq e (pop tags))
  4091. (cond
  4092. ((equal e "{") (push '(:startgroup) tgs))
  4093. ((equal e "}") (push '(:endgroup) tgs))
  4094. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4095. (push (cons (match-string 1 e)
  4096. (string-to-char (match-string 2 e)))
  4097. tgs))
  4098. (t (push (list e) tgs))))
  4099. (org-set-local 'org-tag-alist nil)
  4100. (while (setq e (pop tgs))
  4101. (or (and (stringp (car e))
  4102. (assoc (car e) org-tag-alist))
  4103. (push e org-tag-alist))))))
  4104. ;; Compute the regular expressions and other local variables
  4105. (if (not org-done-keywords)
  4106. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4107. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4108. (length org-scheduled-string)))
  4109. org-drawer-regexp
  4110. (concat "^[ \t]*:\\("
  4111. (mapconcat 'regexp-quote org-drawers "\\|")
  4112. "\\):[ \t]*$")
  4113. org-not-done-keywords
  4114. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4115. org-todo-regexp
  4116. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4117. "\\|") "\\)\\>")
  4118. org-not-done-regexp
  4119. (concat "\\<\\("
  4120. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4121. "\\)\\>")
  4122. org-todo-line-regexp
  4123. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4124. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4125. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4126. org-complex-heading-regexp
  4127. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4128. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4129. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4130. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4131. org-nl-done-regexp
  4132. (concat "\n\\*+[ \t]+"
  4133. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4134. "\\)" "\\>")
  4135. org-todo-line-tags-regexp
  4136. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4137. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4138. (org-re
  4139. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4140. org-looking-at-done-regexp
  4141. (concat "^" "\\(?:"
  4142. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4143. "\\>")
  4144. org-deadline-regexp (concat "\\<" org-deadline-string)
  4145. org-deadline-time-regexp
  4146. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4147. org-deadline-line-regexp
  4148. (concat "\\<\\(" org-deadline-string "\\).*")
  4149. org-scheduled-regexp
  4150. (concat "\\<" org-scheduled-string)
  4151. org-scheduled-time-regexp
  4152. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4153. org-closed-time-regexp
  4154. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4155. org-keyword-time-regexp
  4156. (concat "\\<\\(" org-scheduled-string
  4157. "\\|" org-deadline-string
  4158. "\\|" org-closed-string
  4159. "\\|" org-clock-string "\\)"
  4160. " *[[<]\\([^]>]+\\)[]>]")
  4161. org-keyword-time-not-clock-regexp
  4162. (concat "\\<\\(" org-scheduled-string
  4163. "\\|" org-deadline-string
  4164. "\\|" org-closed-string
  4165. "\\)"
  4166. " *[[<]\\([^]>]+\\)[]>]")
  4167. org-maybe-keyword-time-regexp
  4168. (concat "\\(\\<\\(" org-scheduled-string
  4169. "\\|" org-deadline-string
  4170. "\\|" org-closed-string
  4171. "\\|" org-clock-string "\\)\\)?"
  4172. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4173. org-planning-or-clock-line-re
  4174. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4175. "\\|" org-deadline-string
  4176. "\\|" org-closed-string "\\|" org-clock-string
  4177. "\\)\\>\\)")
  4178. )
  4179. (org-compute-latex-and-specials-regexp)
  4180. (org-set-font-lock-defaults)))
  4181. (defun org-extract-log-state-settings (x)
  4182. "Extract the log state setting from a TODO keyword string.
  4183. This will extract info from a string like \"WAIT(w@/!)\"."
  4184. (let (kw key log1 log2)
  4185. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4186. (setq kw (match-string 1 x)
  4187. key (and (match-end 2) (match-string 2 x))
  4188. log1 (and (match-end 3) (match-string 3 x))
  4189. log2 (and (match-end 4) (match-string 4 x)))
  4190. (and (or log1 log2)
  4191. (list kw
  4192. (and log1 (if (equal log1 "!") 'time 'note))
  4193. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4194. (defun org-remove-keyword-keys (list)
  4195. "Remove a pair of parenthesis at the end of each string in LIST."
  4196. (mapcar (lambda (x)
  4197. (if (string-match "(.*)$" x)
  4198. (substring x 0 (match-beginning 0))
  4199. x))
  4200. list))
  4201. ;; FIXME: this could be done much better, using second characters etc.
  4202. (defun org-assign-fast-keys (alist)
  4203. "Assign fast keys to a keyword-key alist.
  4204. Respect keys that are already there."
  4205. (let (new e k c c1 c2 (char ?a))
  4206. (while (setq e (pop alist))
  4207. (cond
  4208. ((equal e '(:startgroup)) (push e new))
  4209. ((equal e '(:endgroup)) (push e new))
  4210. (t
  4211. (setq k (car e) c2 nil)
  4212. (if (cdr e)
  4213. (setq c (cdr e))
  4214. ;; automatically assign a character.
  4215. (setq c1 (string-to-char
  4216. (downcase (substring
  4217. k (if (= (string-to-char k) ?@) 1 0)))))
  4218. (if (or (rassoc c1 new) (rassoc c1 alist))
  4219. (while (or (rassoc char new) (rassoc char alist))
  4220. (setq char (1+ char)))
  4221. (setq c2 c1))
  4222. (setq c (or c2 char)))
  4223. (push (cons k c) new))))
  4224. (nreverse new)))
  4225. ;;; Some variables ujsed in various places
  4226. (defvar org-window-configuration nil
  4227. "Used in various places to store a window configuration.")
  4228. (defvar org-finish-function nil
  4229. "Function to be called when `C-c C-c' is used.
  4230. This is for getting out of special buffers like remember.")
  4231. ;; FIXME: Occasionally check by commenting these, to make sure
  4232. ;; no other functions uses these, forgetting to let-bind them.
  4233. (defvar entry)
  4234. (defvar state)
  4235. (defvar last-state)
  4236. (defvar date)
  4237. (defvar description)
  4238. ;; Defined somewhere in this file, but used before definition.
  4239. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4240. (defvar org-agenda-buffer-name)
  4241. (defvar org-agenda-undo-list)
  4242. (defvar org-agenda-pending-undo-list)
  4243. (defvar org-agenda-overriding-header)
  4244. (defvar orgtbl-mode)
  4245. (defvar org-html-entities)
  4246. (defvar org-struct-menu)
  4247. (defvar org-org-menu)
  4248. (defvar org-tbl-menu)
  4249. (defvar org-agenda-keymap)
  4250. ;;;; Emacs/XEmacs compatibility
  4251. ;; Overlay compatibility functions
  4252. (defun org-make-overlay (beg end &optional buffer)
  4253. (if (featurep 'xemacs)
  4254. (make-extent beg end buffer)
  4255. (make-overlay beg end buffer)))
  4256. (defun org-delete-overlay (ovl)
  4257. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4258. (defun org-detach-overlay (ovl)
  4259. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4260. (defun org-move-overlay (ovl beg end &optional buffer)
  4261. (if (featurep 'xemacs)
  4262. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4263. (move-overlay ovl beg end buffer)))
  4264. (defun org-overlay-put (ovl prop value)
  4265. (if (featurep 'xemacs)
  4266. (set-extent-property ovl prop value)
  4267. (overlay-put ovl prop value)))
  4268. (defun org-overlay-display (ovl text &optional face evap)
  4269. "Make overlay OVL display TEXT with face FACE."
  4270. (if (featurep 'xemacs)
  4271. (let ((gl (make-glyph text)))
  4272. (and face (set-glyph-face gl face))
  4273. (set-extent-property ovl 'invisible t)
  4274. (set-extent-property ovl 'end-glyph gl))
  4275. (overlay-put ovl 'display text)
  4276. (if face (overlay-put ovl 'face face))
  4277. (if evap (overlay-put ovl 'evaporate t))))
  4278. (defun org-overlay-before-string (ovl text &optional face evap)
  4279. "Make overlay OVL display TEXT with face FACE."
  4280. (if (featurep 'xemacs)
  4281. (let ((gl (make-glyph text)))
  4282. (and face (set-glyph-face gl face))
  4283. (set-extent-property ovl 'begin-glyph gl))
  4284. (if face (org-add-props text nil 'face face))
  4285. (overlay-put ovl 'before-string text)
  4286. (if evap (overlay-put ovl 'evaporate t))))
  4287. (defun org-overlay-get (ovl prop)
  4288. (if (featurep 'xemacs)
  4289. (extent-property ovl prop)
  4290. (overlay-get ovl prop)))
  4291. (defun org-overlays-at (pos)
  4292. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4293. (defun org-overlays-in (&optional start end)
  4294. (if (featurep 'xemacs)
  4295. (extent-list nil start end)
  4296. (overlays-in start end)))
  4297. (defun org-overlay-start (o)
  4298. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4299. (defun org-overlay-end (o)
  4300. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4301. (defun org-find-overlays (prop &optional pos delete)
  4302. "Find all overlays specifying PROP at POS or point.
  4303. If DELETE is non-nil, delete all those overlays."
  4304. (let ((overlays (org-overlays-at (or pos (point))))
  4305. ov found)
  4306. (while (setq ov (pop overlays))
  4307. (if (org-overlay-get ov prop)
  4308. (if delete (org-delete-overlay ov) (push ov found))))
  4309. found))
  4310. ;; Region compatibility
  4311. (defun org-add-hook (hook function &optional append local)
  4312. "Add-hook, compatible with both Emacsen."
  4313. (if (and local (featurep 'xemacs))
  4314. (add-local-hook hook function append)
  4315. (add-hook hook function append local)))
  4316. (defvar org-ignore-region nil
  4317. "To temporarily disable the active region.")
  4318. (defun org-region-active-p ()
  4319. "Is `transient-mark-mode' on and the region active?
  4320. Works on both Emacs and XEmacs."
  4321. (if org-ignore-region
  4322. nil
  4323. (if (featurep 'xemacs)
  4324. (and zmacs-regions (region-active-p))
  4325. (if (fboundp 'use-region-p)
  4326. (use-region-p)
  4327. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4328. ;; Invisibility compatibility
  4329. (defun org-add-to-invisibility-spec (arg)
  4330. "Add elements to `buffer-invisibility-spec'.
  4331. See documentation for `buffer-invisibility-spec' for the kind of elements
  4332. that can be added."
  4333. (cond
  4334. ((fboundp 'add-to-invisibility-spec)
  4335. (add-to-invisibility-spec arg))
  4336. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4337. (setq buffer-invisibility-spec (list arg)))
  4338. (t
  4339. (setq buffer-invisibility-spec
  4340. (cons arg buffer-invisibility-spec)))))
  4341. (defun org-remove-from-invisibility-spec (arg)
  4342. "Remove elements from `buffer-invisibility-spec'."
  4343. (if (fboundp 'remove-from-invisibility-spec)
  4344. (remove-from-invisibility-spec arg)
  4345. (if (consp buffer-invisibility-spec)
  4346. (setq buffer-invisibility-spec
  4347. (delete arg buffer-invisibility-spec)))))
  4348. (defun org-in-invisibility-spec-p (arg)
  4349. "Is ARG a member of `buffer-invisibility-spec'?"
  4350. (if (consp buffer-invisibility-spec)
  4351. (member arg buffer-invisibility-spec)
  4352. nil))
  4353. ;;;; Define the Org-mode
  4354. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4355. (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."))
  4356. ;; We use a before-change function to check if a table might need
  4357. ;; an update.
  4358. (defvar org-table-may-need-update t
  4359. "Indicates that a table might need an update.
  4360. This variable is set by `org-before-change-function'.
  4361. `org-table-align' sets it back to nil.")
  4362. (defvar org-mode-map)
  4363. (defvar org-mode-hook nil)
  4364. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4365. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4366. (defvar org-table-buffer-is-an nil)
  4367. (defconst org-outline-regexp "\\*+ ")
  4368. ;;;###autoload
  4369. (define-derived-mode org-mode outline-mode "Org"
  4370. "Outline-based notes management and organizer, alias
  4371. \"Carsten's outline-mode for keeping track of everything.\"
  4372. Org-mode develops organizational tasks around a NOTES file which
  4373. contains information about projects as plain text. Org-mode is
  4374. implemented on top of outline-mode, which is ideal to keep the content
  4375. of large files well structured. It supports ToDo items, deadlines and
  4376. time stamps, which magically appear in the diary listing of the Emacs
  4377. calendar. Tables are easily created with a built-in table editor.
  4378. Plain text URL-like links connect to websites, emails (VM), Usenet
  4379. messages (Gnus), BBDB entries, and any files related to the project.
  4380. For printing and sharing of notes, an Org-mode file (or a part of it)
  4381. can be exported as a structured ASCII or HTML file.
  4382. The following commands are available:
  4383. \\{org-mode-map}"
  4384. ;; Get rid of Outline menus, they are not needed
  4385. ;; Need to do this here because define-derived-mode sets up
  4386. ;; the keymap so late. Still, it is a waste to call this each time
  4387. ;; we switch another buffer into org-mode.
  4388. (if (featurep 'xemacs)
  4389. (when (boundp 'outline-mode-menu-heading)
  4390. ;; Assume this is Greg's port, it used easymenu
  4391. (easy-menu-remove outline-mode-menu-heading)
  4392. (easy-menu-remove outline-mode-menu-show)
  4393. (easy-menu-remove outline-mode-menu-hide))
  4394. (define-key org-mode-map [menu-bar headings] 'undefined)
  4395. (define-key org-mode-map [menu-bar hide] 'undefined)
  4396. (define-key org-mode-map [menu-bar show] 'undefined))
  4397. (easy-menu-add org-org-menu)
  4398. (easy-menu-add org-tbl-menu)
  4399. (org-install-agenda-files-menu)
  4400. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4401. (org-add-to-invisibility-spec '(org-cwidth))
  4402. (when (featurep 'xemacs)
  4403. (org-set-local 'line-move-ignore-invisible t))
  4404. (org-set-local 'outline-regexp org-outline-regexp)
  4405. (org-set-local 'outline-level 'org-outline-level)
  4406. (when (and org-ellipsis
  4407. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4408. (fboundp 'make-glyph-code))
  4409. (unless org-display-table
  4410. (setq org-display-table (make-display-table)))
  4411. (set-display-table-slot
  4412. org-display-table 4
  4413. (vconcat (mapcar
  4414. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4415. org-ellipsis)))
  4416. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4417. (setq buffer-display-table org-display-table))
  4418. (org-set-regexps-and-options)
  4419. ;; Calc embedded
  4420. (org-set-local 'calc-embedded-open-mode "# ")
  4421. (modify-syntax-entry ?# "<")
  4422. (modify-syntax-entry ?@ "w")
  4423. (if org-startup-truncated (setq truncate-lines t))
  4424. (org-set-local 'font-lock-unfontify-region-function
  4425. 'org-unfontify-region)
  4426. ;; Activate before-change-function
  4427. (org-set-local 'org-table-may-need-update t)
  4428. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4429. 'local)
  4430. ;; Check for running clock before killing a buffer
  4431. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4432. ;; Paragraphs and auto-filling
  4433. (org-set-autofill-regexps)
  4434. (setq indent-line-function 'org-indent-line-function)
  4435. (org-update-radio-target-regexp)
  4436. ;; Comment characters
  4437. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4438. (org-set-local 'comment-padding " ")
  4439. ;; Align options lines
  4440. (org-set-local
  4441. 'align-mode-rules-list
  4442. '((org-in-buffer-settings
  4443. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4444. (modes . '(org-mode)))))
  4445. ;; Imenu
  4446. (org-set-local 'imenu-create-index-function
  4447. 'org-imenu-get-tree)
  4448. ;; Make isearch reveal context
  4449. (if (or (featurep 'xemacs)
  4450. (not (boundp 'outline-isearch-open-invisible-function)))
  4451. ;; Emacs 21 and XEmacs make use of the hook
  4452. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4453. ;; Emacs 22 deals with this through a special variable
  4454. (org-set-local 'outline-isearch-open-invisible-function
  4455. (lambda (&rest ignore) (org-show-context 'isearch))))
  4456. ;; If empty file that did not turn on org-mode automatically, make it to.
  4457. (if (and org-insert-mode-line-in-empty-file
  4458. (interactive-p)
  4459. (= (point-min) (point-max)))
  4460. (insert "# -*- mode: org -*-\n\n"))
  4461. (unless org-inhibit-startup
  4462. (when org-startup-align-all-tables
  4463. (let ((bmp (buffer-modified-p)))
  4464. (org-table-map-tables 'org-table-align)
  4465. (set-buffer-modified-p bmp)))
  4466. (org-cycle-hide-drawers 'all)
  4467. (cond
  4468. ((eq org-startup-folded t)
  4469. (org-cycle '(4)))
  4470. ((eq org-startup-folded 'content)
  4471. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4472. (org-cycle '(4)) (org-cycle '(4)))))))
  4473. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4474. (defsubst org-call-with-arg (command arg)
  4475. "Call COMMAND interactively, but pretend prefix are was ARG."
  4476. (let ((current-prefix-arg arg)) (call-interactively command)))
  4477. (defsubst org-current-line (&optional pos)
  4478. (save-excursion
  4479. (and pos (goto-char pos))
  4480. ;; works also in narrowed buffer, because we start at 1, not point-min
  4481. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4482. (defun org-current-time ()
  4483. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4484. (if (> org-time-stamp-rounding-minutes 0)
  4485. (let ((r org-time-stamp-rounding-minutes)
  4486. (time (decode-time)))
  4487. (apply 'encode-time
  4488. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4489. (nthcdr 2 time))))
  4490. (current-time)))
  4491. (defun org-add-props (string plist &rest props)
  4492. "Add text properties to entire string, from beginning to end.
  4493. PLIST may be a list of properties, PROPS are individual properties and values
  4494. that will be added to PLIST. Returns the string that was modified."
  4495. (add-text-properties
  4496. 0 (length string) (if props (append plist props) plist) string)
  4497. string)
  4498. (put 'org-add-props 'lisp-indent-function 2)
  4499. ;;;; Font-Lock stuff, including the activators
  4500. (defvar org-mouse-map (make-sparse-keymap))
  4501. (org-defkey org-mouse-map
  4502. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4503. (org-defkey org-mouse-map
  4504. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4505. (when org-mouse-1-follows-link
  4506. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4507. (when org-tab-follows-link
  4508. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4509. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4510. (when org-return-follows-link
  4511. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4512. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4513. (require 'font-lock)
  4514. (defconst org-non-link-chars "]\t\n\r<>")
  4515. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4516. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4517. (defvar org-link-re-with-space nil
  4518. "Matches a link with spaces, optional angular brackets around it.")
  4519. (defvar org-link-re-with-space2 nil
  4520. "Matches a link with spaces, optional angular brackets around it.")
  4521. (defvar org-angle-link-re nil
  4522. "Matches link with angular brackets, spaces are allowed.")
  4523. (defvar org-plain-link-re nil
  4524. "Matches plain link, without spaces.")
  4525. (defvar org-bracket-link-regexp nil
  4526. "Matches a link in double brackets.")
  4527. (defvar org-bracket-link-analytic-regexp nil
  4528. "Regular expression used to analyze links.
  4529. Here is what the match groups contain after a match:
  4530. 1: http:
  4531. 2: http
  4532. 3: path
  4533. 4: [desc]
  4534. 5: desc")
  4535. (defvar org-any-link-re nil
  4536. "Regular expression matching any link.")
  4537. (defun org-make-link-regexps ()
  4538. "Update the link regular expressions.
  4539. This should be called after the variable `org-link-types' has changed."
  4540. (setq org-link-re-with-space
  4541. (concat
  4542. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4543. "\\([^" org-non-link-chars " ]"
  4544. "[^" org-non-link-chars "]*"
  4545. "[^" org-non-link-chars " ]\\)>?")
  4546. org-link-re-with-space2
  4547. (concat
  4548. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4549. "\\([^" org-non-link-chars " ]"
  4550. "[^]\t\n\r]*"
  4551. "[^" org-non-link-chars " ]\\)>?")
  4552. org-angle-link-re
  4553. (concat
  4554. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4555. "\\([^" org-non-link-chars " ]"
  4556. "[^" org-non-link-chars "]*"
  4557. "\\)>")
  4558. org-plain-link-re
  4559. (concat
  4560. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4561. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4562. org-bracket-link-regexp
  4563. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4564. org-bracket-link-analytic-regexp
  4565. (concat
  4566. "\\[\\["
  4567. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4568. "\\([^]]+\\)"
  4569. "\\]"
  4570. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4571. "\\]")
  4572. org-any-link-re
  4573. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4574. org-angle-link-re "\\)\\|\\("
  4575. org-plain-link-re "\\)")))
  4576. (org-make-link-regexps)
  4577. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4578. "Regular expression for fast time stamp matching.")
  4579. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4580. "Regular expression for fast time stamp matching.")
  4581. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4582. "Regular expression matching time strings for analysis.
  4583. This one does not require the space after the date.")
  4584. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4585. "Regular expression matching time strings for analysis.")
  4586. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4587. "Regular expression matching time stamps, with groups.")
  4588. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4589. "Regular expression matching time stamps (also [..]), with groups.")
  4590. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4591. "Regular expression matching a time stamp range.")
  4592. (defconst org-tr-regexp-both
  4593. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4594. "Regular expression matching a time stamp range.")
  4595. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4596. org-ts-regexp "\\)?")
  4597. "Regular expression matching a time stamp or time stamp range.")
  4598. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4599. org-ts-regexp-both "\\)?")
  4600. "Regular expression matching a time stamp or time stamp range.
  4601. The time stamps may be either active or inactive.")
  4602. (defvar org-emph-face nil)
  4603. (defun org-do-emphasis-faces (limit)
  4604. "Run through the buffer and add overlays to links."
  4605. (let (rtn)
  4606. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4607. (if (not (= (char-after (match-beginning 3))
  4608. (char-after (match-beginning 4))))
  4609. (progn
  4610. (setq rtn t)
  4611. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4612. 'face
  4613. (nth 1 (assoc (match-string 3)
  4614. org-emphasis-alist)))
  4615. (add-text-properties (match-beginning 2) (match-end 2)
  4616. '(font-lock-multiline t))
  4617. (when org-hide-emphasis-markers
  4618. (add-text-properties (match-end 4) (match-beginning 5)
  4619. '(invisible org-link))
  4620. (add-text-properties (match-beginning 3) (match-end 3)
  4621. '(invisible org-link)))))
  4622. (backward-char 1))
  4623. rtn))
  4624. (defun org-emphasize (&optional char)
  4625. "Insert or change an emphasis, i.e. a font like bold or italic.
  4626. If there is an active region, change that region to a new emphasis.
  4627. If there is no region, just insert the marker characters and position
  4628. the cursor between them.
  4629. CHAR should be either the marker character, or the first character of the
  4630. HTML tag associated with that emphasis. If CHAR is a space, the means
  4631. to remove the emphasis of the selected region.
  4632. If char is not given (for example in an interactive call) it
  4633. will be prompted for."
  4634. (interactive)
  4635. (let ((eal org-emphasis-alist) e det
  4636. (erc org-emphasis-regexp-components)
  4637. (prompt "")
  4638. (string "") beg end move tag c s)
  4639. (if (org-region-active-p)
  4640. (setq beg (region-beginning) end (region-end)
  4641. string (buffer-substring beg end))
  4642. (setq move t))
  4643. (while (setq e (pop eal))
  4644. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4645. c (aref tag 0))
  4646. (push (cons c (string-to-char (car e))) det)
  4647. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4648. (substring tag 1)))))
  4649. (unless char
  4650. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4651. (setq char (read-char-exclusive)))
  4652. (setq char (or (cdr (assoc char det)) char))
  4653. (if (equal char ?\ )
  4654. (setq s "" move nil)
  4655. (unless (assoc (char-to-string char) org-emphasis-alist)
  4656. (error "No such emphasis marker: \"%c\"" char))
  4657. (setq s (char-to-string char)))
  4658. (while (and (> (length string) 1)
  4659. (equal (substring string 0 1) (substring string -1))
  4660. (assoc (substring string 0 1) org-emphasis-alist))
  4661. (setq string (substring string 1 -1)))
  4662. (setq string (concat s string s))
  4663. (if beg (delete-region beg end))
  4664. (unless (or (bolp)
  4665. (string-match (concat "[" (nth 0 erc) "\n]")
  4666. (char-to-string (char-before (point)))))
  4667. (insert " "))
  4668. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4669. (char-to-string (char-after (point))))
  4670. (insert " ") (backward-char 1))
  4671. (insert string)
  4672. (and move (backward-char 1))))
  4673. (defconst org-nonsticky-props
  4674. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4675. (defun org-activate-plain-links (limit)
  4676. "Run through the buffer and add overlays to links."
  4677. (catch 'exit
  4678. (let (f)
  4679. (while (re-search-forward org-plain-link-re limit t)
  4680. (setq f (get-text-property (match-beginning 0) 'face))
  4681. (if (or (eq f 'org-tag)
  4682. (and (listp f) (memq 'org-tag f)))
  4683. nil
  4684. (add-text-properties (match-beginning 0) (match-end 0)
  4685. (list 'mouse-face 'highlight
  4686. 'rear-nonsticky org-nonsticky-props
  4687. 'keymap org-mouse-map
  4688. ))
  4689. (throw 'exit t))))))
  4690. (defun org-activate-code (limit)
  4691. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4692. (unless (get-text-property (match-beginning 1) 'face)
  4693. (remove-text-properties (match-beginning 0) (match-end 0)
  4694. '(display t invisible t intangible t))
  4695. t)))
  4696. (defun org-activate-angle-links (limit)
  4697. "Run through the buffer and add overlays to links."
  4698. (if (re-search-forward org-angle-link-re limit t)
  4699. (progn
  4700. (add-text-properties (match-beginning 0) (match-end 0)
  4701. (list 'mouse-face 'highlight
  4702. 'rear-nonsticky org-nonsticky-props
  4703. 'keymap org-mouse-map
  4704. ))
  4705. t)))
  4706. (defmacro org-maybe-intangible (props)
  4707. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4708. In emacs 21, invisible text is not avoided by the command loop, so the
  4709. intangible property is needed to make sure point skips this text.
  4710. In Emacs 22, this is not necessary. The intangible text property has
  4711. led to problems with flyspell. These problems are fixed in flyspell.el,
  4712. but we still avoid setting the property in Emacs 22 and later.
  4713. We use a macro so that the test can happen at compilation time."
  4714. (if (< emacs-major-version 22)
  4715. `(append '(intangible t) ,props)
  4716. props))
  4717. (defun org-activate-bracket-links (limit)
  4718. "Run through the buffer and add overlays to bracketed links."
  4719. (if (re-search-forward org-bracket-link-regexp limit t)
  4720. (let* ((help (concat "LINK: "
  4721. (org-match-string-no-properties 1)))
  4722. ;; FIXME: above we should remove the escapes.
  4723. ;; but that requires another match, protecting match data,
  4724. ;; a lot of overhead for font-lock.
  4725. (ip (org-maybe-intangible
  4726. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4727. 'keymap org-mouse-map 'mouse-face 'highlight
  4728. 'font-lock-multiline t 'help-echo help)))
  4729. (vp (list 'rear-nonsticky org-nonsticky-props
  4730. 'keymap org-mouse-map 'mouse-face 'highlight
  4731. ' font-lock-multiline t 'help-echo help)))
  4732. ;; We need to remove the invisible property here. Table narrowing
  4733. ;; may have made some of this invisible.
  4734. (remove-text-properties (match-beginning 0) (match-end 0)
  4735. '(invisible nil))
  4736. (if (match-end 3)
  4737. (progn
  4738. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4739. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4740. (add-text-properties (match-end 3) (match-end 0) ip))
  4741. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4742. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4743. (add-text-properties (match-end 1) (match-end 0) ip))
  4744. t)))
  4745. (defun org-activate-dates (limit)
  4746. "Run through the buffer and add overlays to dates."
  4747. (if (re-search-forward org-tsr-regexp-both limit t)
  4748. (progn
  4749. (add-text-properties (match-beginning 0) (match-end 0)
  4750. (list 'mouse-face 'highlight
  4751. 'rear-nonsticky org-nonsticky-props
  4752. 'keymap org-mouse-map))
  4753. (when org-display-custom-times
  4754. (if (match-end 3)
  4755. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4756. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4757. t)))
  4758. (defvar org-target-link-regexp nil
  4759. "Regular expression matching radio targets in plain text.")
  4760. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4761. "Regular expression matching a link target.")
  4762. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4763. "Regular expression matching a radio target.")
  4764. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4765. "Regular expression matching any target.")
  4766. (defun org-activate-target-links (limit)
  4767. "Run through the buffer and add overlays to target matches."
  4768. (when org-target-link-regexp
  4769. (let ((case-fold-search t))
  4770. (if (re-search-forward org-target-link-regexp limit t)
  4771. (progn
  4772. (add-text-properties (match-beginning 0) (match-end 0)
  4773. (list 'mouse-face 'highlight
  4774. 'rear-nonsticky org-nonsticky-props
  4775. 'keymap org-mouse-map
  4776. 'help-echo "Radio target link"
  4777. 'org-linked-text t))
  4778. t)))))
  4779. (defun org-update-radio-target-regexp ()
  4780. "Find all radio targets in this file and update the regular expression."
  4781. (interactive)
  4782. (when (memq 'radio org-activate-links)
  4783. (setq org-target-link-regexp
  4784. (org-make-target-link-regexp (org-all-targets 'radio)))
  4785. (org-restart-font-lock)))
  4786. (defun org-hide-wide-columns (limit)
  4787. (let (s e)
  4788. (setq s (text-property-any (point) (or limit (point-max))
  4789. 'org-cwidth t))
  4790. (when s
  4791. (setq e (next-single-property-change s 'org-cwidth))
  4792. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4793. (goto-char e)
  4794. t)))
  4795. (defvar org-latex-and-specials-regexp nil
  4796. "Regular expression for highlighting export special stuff.")
  4797. (defvar org-match-substring-regexp)
  4798. (defvar org-match-substring-with-braces-regexp)
  4799. (defvar org-export-html-special-string-regexps)
  4800. (defun org-compute-latex-and-specials-regexp ()
  4801. "Compute regular expression for stuff treated specially by exporters."
  4802. (if (not org-highlight-latex-fragments-and-specials)
  4803. (org-set-local 'org-latex-and-specials-regexp nil)
  4804. (let*
  4805. ((matchers (plist-get org-format-latex-options :matchers))
  4806. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4807. org-latex-regexps)))
  4808. (options (org-combine-plists (org-default-export-plist)
  4809. (org-infile-export-plist)))
  4810. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4811. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4812. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4813. (org-export-html-expand (plist-get options :expand-quoted-html))
  4814. (org-export-with-special-strings (plist-get options :special-strings))
  4815. (re-sub
  4816. (cond
  4817. ((equal org-export-with-sub-superscripts '{})
  4818. (list org-match-substring-with-braces-regexp))
  4819. (org-export-with-sub-superscripts
  4820. (list org-match-substring-regexp))
  4821. (t nil)))
  4822. (re-latex
  4823. (if org-export-with-LaTeX-fragments
  4824. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4825. (re-macros
  4826. (if org-export-with-TeX-macros
  4827. (list (concat "\\\\"
  4828. (regexp-opt
  4829. (append (mapcar 'car org-html-entities)
  4830. (if (boundp 'org-latex-entities)
  4831. org-latex-entities nil))
  4832. 'words))) ; FIXME
  4833. ))
  4834. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4835. (re-special (if org-export-with-special-strings
  4836. (mapcar (lambda (x) (car x))
  4837. org-export-html-special-string-regexps)))
  4838. (re-rest
  4839. (delq nil
  4840. (list
  4841. (if org-export-html-expand "@<[^>\n]+>")
  4842. ))))
  4843. (org-set-local
  4844. 'org-latex-and-specials-regexp
  4845. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4846. re-rest) "\\|")))))
  4847. (defface org-latex-and-export-specials
  4848. (let ((font (cond ((assq :inherit custom-face-attributes)
  4849. '(:inherit underline))
  4850. (t '(:underline t)))))
  4851. `((((class grayscale) (background light))
  4852. (:foreground "DimGray" ,@font))
  4853. (((class grayscale) (background dark))
  4854. (:foreground "LightGray" ,@font))
  4855. (((class color) (background light))
  4856. (:foreground "SaddleBrown"))
  4857. (((class color) (background dark))
  4858. (:foreground "burlywood"))
  4859. (t (,@font))))
  4860. "Face used to highlight math latex and other special exporter stuff."
  4861. :group 'org-faces)
  4862. (defun org-do-latex-and-special-faces (limit)
  4863. "Run through the buffer and add overlays to links."
  4864. (when org-latex-and-specials-regexp
  4865. (let (rtn d)
  4866. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4867. limit t))
  4868. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4869. 'face))
  4870. '(org-code org-verbatim underline)))
  4871. (progn
  4872. (setq rtn t
  4873. d (cond ((member (char-after (1+ (match-beginning 0)))
  4874. '(?_ ?^)) 1)
  4875. (t 0)))
  4876. (font-lock-prepend-text-property
  4877. (+ d (match-beginning 0)) (match-end 0)
  4878. 'face 'org-latex-and-export-specials)
  4879. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4880. '(font-lock-multiline t)))))
  4881. rtn)))
  4882. (defun org-restart-font-lock ()
  4883. "Restart font-lock-mode, to force refontification."
  4884. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4885. (font-lock-mode -1)
  4886. (font-lock-mode 1)))
  4887. (defun org-all-targets (&optional radio)
  4888. "Return a list of all targets in this file.
  4889. With optional argument RADIO, only find radio targets."
  4890. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4891. rtn)
  4892. (save-excursion
  4893. (goto-char (point-min))
  4894. (while (re-search-forward re nil t)
  4895. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4896. rtn)))
  4897. (defun org-make-target-link-regexp (targets)
  4898. "Make regular expression matching all strings in TARGETS.
  4899. The regular expression finds the targets also if there is a line break
  4900. between words."
  4901. (and targets
  4902. (concat
  4903. "\\<\\("
  4904. (mapconcat
  4905. (lambda (x)
  4906. (while (string-match " +" x)
  4907. (setq x (replace-match "\\s-+" t t x)))
  4908. x)
  4909. targets
  4910. "\\|")
  4911. "\\)\\>")))
  4912. (defun org-activate-tags (limit)
  4913. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4914. (progn
  4915. (add-text-properties (match-beginning 1) (match-end 1)
  4916. (list 'mouse-face 'highlight
  4917. 'rear-nonsticky org-nonsticky-props
  4918. 'keymap org-mouse-map))
  4919. t)))
  4920. (defun org-outline-level ()
  4921. (save-excursion
  4922. (looking-at outline-regexp)
  4923. (if (match-beginning 1)
  4924. (+ (org-get-string-indentation (match-string 1)) 1000)
  4925. (1- (- (match-end 0) (match-beginning 0))))))
  4926. (defvar org-font-lock-keywords nil)
  4927. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4928. "Regular expression matching a property line.")
  4929. (defun org-set-font-lock-defaults ()
  4930. (let* ((em org-fontify-emphasized-text)
  4931. (lk org-activate-links)
  4932. (org-font-lock-extra-keywords
  4933. (list
  4934. ;; Headlines
  4935. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4936. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4937. ;; Table lines
  4938. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4939. (1 'org-table t))
  4940. ;; Table internals
  4941. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4942. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4943. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4944. ;; Drawers
  4945. (list org-drawer-regexp '(0 'org-special-keyword t))
  4946. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4947. ;; Properties
  4948. (list org-property-re
  4949. '(1 'org-special-keyword t)
  4950. '(3 'org-property-value t))
  4951. (if org-format-transports-properties-p
  4952. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4953. ;; Links
  4954. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4955. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4956. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4957. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4958. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4959. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4960. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4961. '(org-hide-wide-columns (0 nil append))
  4962. ;; TODO lines
  4963. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4964. '(1 (org-get-todo-face 1) t))
  4965. ;; DONE
  4966. (if org-fontify-done-headline
  4967. (list (concat "^[*]+ +\\<\\("
  4968. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4969. "\\)\\(.*\\)")
  4970. '(2 'org-headline-done t))
  4971. nil)
  4972. ;; Priorities
  4973. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4974. ;; Special keywords
  4975. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4976. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4977. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4978. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4979. ;; Emphasis
  4980. (if em
  4981. (if (featurep 'xemacs)
  4982. '(org-do-emphasis-faces (0 nil append))
  4983. '(org-do-emphasis-faces)))
  4984. ;; Checkboxes
  4985. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4986. 2 'bold prepend)
  4987. (if org-provide-checkbox-statistics
  4988. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4989. (0 (org-get-checkbox-statistics-face) t)))
  4990. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4991. '(1 'org-archived prepend))
  4992. ;; Specials
  4993. '(org-do-latex-and-special-faces)
  4994. ;; Code
  4995. '(org-activate-code (1 'org-code t))
  4996. ;; COMMENT
  4997. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4998. "\\|" org-quote-string "\\)\\>")
  4999. '(1 'org-special-keyword t))
  5000. '("^#.*" (0 'font-lock-comment-face t))
  5001. )))
  5002. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5003. ;; Now set the full font-lock-keywords
  5004. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5005. (org-set-local 'font-lock-defaults
  5006. '(org-font-lock-keywords t nil nil backward-paragraph))
  5007. (kill-local-variable 'font-lock-keywords) nil))
  5008. (defvar org-m nil)
  5009. (defvar org-l nil)
  5010. (defvar org-f nil)
  5011. (defun org-get-level-face (n)
  5012. "Get the right face for match N in font-lock matching of healdines."
  5013. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5014. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5015. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5016. (cond
  5017. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5018. ((eq n 2) org-f)
  5019. (t (if org-level-color-stars-only nil org-f))))
  5020. (defun org-get-todo-face (kwd)
  5021. "Get the right face for a TODO keyword KWD.
  5022. If KWD is a number, get the corresponding match group."
  5023. (if (numberp kwd) (setq kwd (match-string kwd)))
  5024. (or (cdr (assoc kwd org-todo-keyword-faces))
  5025. (and (member kwd org-done-keywords) 'org-done)
  5026. 'org-todo))
  5027. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5028. "Remove fontification and activation overlays from links."
  5029. (font-lock-default-unfontify-region beg end)
  5030. (let* ((buffer-undo-list t)
  5031. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5032. (inhibit-modification-hooks t)
  5033. deactivate-mark buffer-file-name buffer-file-truename)
  5034. (remove-text-properties beg end
  5035. '(mouse-face t keymap t org-linked-text t
  5036. invisible t intangible t))))
  5037. ;;;; Visibility cycling, including org-goto and indirect buffer
  5038. ;;; Cycling
  5039. (defvar org-cycle-global-status nil)
  5040. (make-variable-buffer-local 'org-cycle-global-status)
  5041. (defvar org-cycle-subtree-status nil)
  5042. (make-variable-buffer-local 'org-cycle-subtree-status)
  5043. ;;;###autoload
  5044. (defun org-cycle (&optional arg)
  5045. "Visibility cycling for Org-mode.
  5046. - When this function is called with a prefix argument, rotate the entire
  5047. buffer through 3 states (global cycling)
  5048. 1. OVERVIEW: Show only top-level headlines.
  5049. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5050. 3. SHOW ALL: Show everything.
  5051. - When point is at the beginning of a headline, rotate the subtree started
  5052. by this line through 3 different states (local cycling)
  5053. 1. FOLDED: Only the main headline is shown.
  5054. 2. CHILDREN: The main headline and the direct children are shown.
  5055. From this state, you can move to one of the children
  5056. and zoom in further.
  5057. 3. SUBTREE: Show the entire subtree, including body text.
  5058. - When there is a numeric prefix, go up to a heading with level ARG, do
  5059. a `show-subtree' and return to the previous cursor position. If ARG
  5060. is negative, go up that many levels.
  5061. - When point is not at the beginning of a headline, execute
  5062. `indent-relative', like TAB normally does. See the option
  5063. `org-cycle-emulate-tab' for details.
  5064. - Special case: if point is at the beginning of the buffer and there is
  5065. no headline in line 1, this function will act as if called with prefix arg.
  5066. But only if also the variable `org-cycle-global-at-bob' is t."
  5067. (interactive "P")
  5068. (let* ((outline-regexp
  5069. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5070. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5071. outline-regexp))
  5072. (bob-special (and org-cycle-global-at-bob (bobp)
  5073. (not (looking-at outline-regexp))))
  5074. (org-cycle-hook
  5075. (if bob-special
  5076. (delq 'org-optimize-window-after-visibility-change
  5077. (copy-sequence org-cycle-hook))
  5078. org-cycle-hook))
  5079. (pos (point)))
  5080. (if (or bob-special (equal arg '(4)))
  5081. ;; special case: use global cycling
  5082. (setq arg t))
  5083. (cond
  5084. ((org-at-table-p 'any)
  5085. ;; Enter the table or move to the next field in the table
  5086. (or (org-table-recognize-table.el)
  5087. (progn
  5088. (if arg (org-table-edit-field t)
  5089. (org-table-justify-field-maybe)
  5090. (call-interactively 'org-table-next-field)))))
  5091. ((eq arg t) ;; Global cycling
  5092. (cond
  5093. ((and (eq last-command this-command)
  5094. (eq org-cycle-global-status 'overview))
  5095. ;; We just created the overview - now do table of contents
  5096. ;; This can be slow in very large buffers, so indicate action
  5097. (message "CONTENTS...")
  5098. (org-content)
  5099. (message "CONTENTS...done")
  5100. (setq org-cycle-global-status 'contents)
  5101. (run-hook-with-args 'org-cycle-hook 'contents))
  5102. ((and (eq last-command this-command)
  5103. (eq org-cycle-global-status 'contents))
  5104. ;; We just showed the table of contents - now show everything
  5105. (show-all)
  5106. (message "SHOW ALL")
  5107. (setq org-cycle-global-status 'all)
  5108. (run-hook-with-args 'org-cycle-hook 'all))
  5109. (t
  5110. ;; Default action: go to overview
  5111. (org-overview)
  5112. (message "OVERVIEW")
  5113. (setq org-cycle-global-status 'overview)
  5114. (run-hook-with-args 'org-cycle-hook 'overview))))
  5115. ((and org-drawers org-drawer-regexp
  5116. (save-excursion
  5117. (beginning-of-line 1)
  5118. (looking-at org-drawer-regexp)))
  5119. ;; Toggle block visibility
  5120. (org-flag-drawer
  5121. (not (get-char-property (match-end 0) 'invisible))))
  5122. ((integerp arg)
  5123. ;; Show-subtree, ARG levels up from here.
  5124. (save-excursion
  5125. (org-back-to-heading)
  5126. (outline-up-heading (if (< arg 0) (- arg)
  5127. (- (funcall outline-level) arg)))
  5128. (org-show-subtree)))
  5129. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5130. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5131. ;; At a heading: rotate between three different views
  5132. (org-back-to-heading)
  5133. (let ((goal-column 0) eoh eol eos)
  5134. ;; First, some boundaries
  5135. (save-excursion
  5136. (org-back-to-heading)
  5137. (save-excursion
  5138. (beginning-of-line 2)
  5139. (while (and (not (eobp)) ;; this is like `next-line'
  5140. (get-char-property (1- (point)) 'invisible))
  5141. (beginning-of-line 2)) (setq eol (point)))
  5142. (outline-end-of-heading) (setq eoh (point))
  5143. (org-end-of-subtree t)
  5144. (unless (eobp)
  5145. (skip-chars-forward " \t\n")
  5146. (beginning-of-line 1) ; in case this is an item
  5147. )
  5148. (setq eos (1- (point))))
  5149. ;; Find out what to do next and set `this-command'
  5150. (cond
  5151. ((= eos eoh)
  5152. ;; Nothing is hidden behind this heading
  5153. (message "EMPTY ENTRY")
  5154. (setq org-cycle-subtree-status nil)
  5155. (save-excursion
  5156. (goto-char eos)
  5157. (outline-next-heading)
  5158. (if (org-invisible-p) (org-flag-heading nil))))
  5159. ((or (>= eol eos)
  5160. (not (string-match "\\S-" (buffer-substring eol eos))))
  5161. ;; Entire subtree is hidden in one line: open it
  5162. (org-show-entry)
  5163. (show-children)
  5164. (message "CHILDREN")
  5165. (save-excursion
  5166. (goto-char eos)
  5167. (outline-next-heading)
  5168. (if (org-invisible-p) (org-flag-heading nil)))
  5169. (setq org-cycle-subtree-status 'children)
  5170. (run-hook-with-args 'org-cycle-hook 'children))
  5171. ((and (eq last-command this-command)
  5172. (eq org-cycle-subtree-status 'children))
  5173. ;; We just showed the children, now show everything.
  5174. (org-show-subtree)
  5175. (message "SUBTREE")
  5176. (setq org-cycle-subtree-status 'subtree)
  5177. (run-hook-with-args 'org-cycle-hook 'subtree))
  5178. (t
  5179. ;; Default action: hide the subtree.
  5180. (hide-subtree)
  5181. (message "FOLDED")
  5182. (setq org-cycle-subtree-status 'folded)
  5183. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5184. ;; TAB emulation
  5185. (buffer-read-only (org-back-to-heading))
  5186. ((org-try-cdlatex-tab))
  5187. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5188. (or (not (bolp))
  5189. (not (looking-at outline-regexp))))
  5190. (call-interactively (global-key-binding "\t")))
  5191. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5192. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5193. (or (and (eq org-cycle-emulate-tab 'white)
  5194. (= (match-end 0) (point-at-eol)))
  5195. (and (eq org-cycle-emulate-tab 'whitestart)
  5196. (>= (match-end 0) pos))))
  5197. t
  5198. (eq org-cycle-emulate-tab t))
  5199. ; (if (and (looking-at "[ \n\r\t]")
  5200. ; (string-match "^[ \t]*$" (buffer-substring
  5201. ; (point-at-bol) (point))))
  5202. ; (progn
  5203. ; (beginning-of-line 1)
  5204. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5205. (call-interactively (global-key-binding "\t")))
  5206. (t (save-excursion
  5207. (org-back-to-heading)
  5208. (org-cycle))))))
  5209. ;;;###autoload
  5210. (defun org-global-cycle (&optional arg)
  5211. "Cycle the global visibility. For details see `org-cycle'."
  5212. (interactive "P")
  5213. (let ((org-cycle-include-plain-lists
  5214. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5215. (if (integerp arg)
  5216. (progn
  5217. (show-all)
  5218. (hide-sublevels arg)
  5219. (setq org-cycle-global-status 'contents))
  5220. (org-cycle '(4)))))
  5221. (defun org-overview ()
  5222. "Switch to overview mode, shoing only top-level headlines.
  5223. Really, this shows all headlines with level equal or greater than the level
  5224. of the first headline in the buffer. This is important, because if the
  5225. first headline is not level one, then (hide-sublevels 1) gives confusing
  5226. results."
  5227. (interactive)
  5228. (let ((level (save-excursion
  5229. (goto-char (point-min))
  5230. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5231. (progn
  5232. (goto-char (match-beginning 0))
  5233. (funcall outline-level))))))
  5234. (and level (hide-sublevels level))))
  5235. (defun org-content (&optional arg)
  5236. "Show all headlines in the buffer, like a table of contents.
  5237. With numerical argument N, show content up to level N."
  5238. (interactive "P")
  5239. (save-excursion
  5240. ;; Visit all headings and show their offspring
  5241. (and (integerp arg) (org-overview))
  5242. (goto-char (point-max))
  5243. (catch 'exit
  5244. (while (and (progn (condition-case nil
  5245. (outline-previous-visible-heading 1)
  5246. (error (goto-char (point-min))))
  5247. t)
  5248. (looking-at outline-regexp))
  5249. (if (integerp arg)
  5250. (show-children (1- arg))
  5251. (show-branches))
  5252. (if (bobp) (throw 'exit nil))))))
  5253. (defun org-optimize-window-after-visibility-change (state)
  5254. "Adjust the window after a change in outline visibility.
  5255. This function is the default value of the hook `org-cycle-hook'."
  5256. (when (get-buffer-window (current-buffer))
  5257. (cond
  5258. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5259. ; ((eq state 'overview) (org-beginning-of-line))
  5260. ((eq state 'content) nil)
  5261. ((eq state 'all) nil)
  5262. ((eq state 'folded) nil)
  5263. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5264. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5265. (defun org-compact-display-after-subtree-move ()
  5266. (let (beg end)
  5267. (save-excursion
  5268. (if (org-up-heading-safe)
  5269. (progn
  5270. (hide-subtree)
  5271. (show-entry)
  5272. (show-children)
  5273. (org-cycle-show-empty-lines 'children)
  5274. (org-cycle-hide-drawers 'children))
  5275. (org-overview)))))
  5276. (defun org-cycle-show-empty-lines (state)
  5277. "Show empty lines above all visible headlines.
  5278. The region to be covered depends on STATE when called through
  5279. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5280. entire buffer covered. Note that an empty line is only shown if there
  5281. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5282. (when (> org-cycle-separator-lines 0)
  5283. (save-excursion
  5284. (let* ((n org-cycle-separator-lines)
  5285. (re (cond
  5286. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5287. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5288. (t (let ((ns (number-to-string (- n 2))))
  5289. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5290. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5291. beg end)
  5292. (cond
  5293. ((memq state '(overview contents t))
  5294. (setq beg (point-min) end (point-max)))
  5295. ((memq state '(children folded))
  5296. (setq beg (point) end (progn (org-end-of-subtree t t)
  5297. (beginning-of-line 2)
  5298. (point)))))
  5299. (when beg
  5300. (goto-char beg)
  5301. (while (re-search-forward re end t)
  5302. (if (not (get-char-property (match-end 1) 'invisible))
  5303. (outline-flag-region
  5304. (match-beginning 1) (match-end 1) nil)))))))
  5305. ;; Never hide empty lines at the end of the file.
  5306. (save-excursion
  5307. (goto-char (point-max))
  5308. (outline-previous-heading)
  5309. (outline-end-of-heading)
  5310. (if (and (looking-at "[ \t\n]+")
  5311. (= (match-end 0) (point-max)))
  5312. (outline-flag-region (point) (match-end 0) nil))))
  5313. (defun org-subtree-end-visible-p ()
  5314. "Is the end of the current subtree visible?"
  5315. (pos-visible-in-window-p
  5316. (save-excursion (org-end-of-subtree t) (point))))
  5317. (defun org-first-headline-recenter (&optional N)
  5318. "Move cursor to the first headline and recenter the headline.
  5319. Optional argument N means, put the headline into the Nth line of the window."
  5320. (goto-char (point-min))
  5321. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5322. (beginning-of-line)
  5323. (recenter (prefix-numeric-value N))))
  5324. ;;; Org-goto
  5325. (defvar org-goto-window-configuration nil)
  5326. (defvar org-goto-marker nil)
  5327. (defvar org-goto-map
  5328. (let ((map (make-sparse-keymap)))
  5329. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5330. (while (setq cmd (pop cmds))
  5331. (substitute-key-definition cmd cmd map global-map)))
  5332. (suppress-keymap map)
  5333. (org-defkey map "\C-m" 'org-goto-ret)
  5334. (org-defkey map [(return)] 'org-goto-ret)
  5335. (org-defkey map [(left)] 'org-goto-left)
  5336. (org-defkey map [(right)] 'org-goto-right)
  5337. (org-defkey map [(control ?g)] 'org-goto-quit)
  5338. (org-defkey map "\C-i" 'org-cycle)
  5339. (org-defkey map [(tab)] 'org-cycle)
  5340. (org-defkey map [(down)] 'outline-next-visible-heading)
  5341. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5342. (if org-goto-auto-isearch
  5343. (if (fboundp 'define-key-after)
  5344. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5345. nil)
  5346. (org-defkey map "q" 'org-goto-quit)
  5347. (org-defkey map "n" 'outline-next-visible-heading)
  5348. (org-defkey map "p" 'outline-previous-visible-heading)
  5349. (org-defkey map "f" 'outline-forward-same-level)
  5350. (org-defkey map "b" 'outline-backward-same-level)
  5351. (org-defkey map "u" 'outline-up-heading))
  5352. (org-defkey map "/" 'org-occur)
  5353. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5354. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5355. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5356. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5357. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5358. map))
  5359. (defconst org-goto-help
  5360. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5361. RET=jump to location [Q]uit and return to previous location
  5362. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5363. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5364. (defun org-goto (&optional alternative-interface)
  5365. "Look up a different location in the current file, keeping current visibility.
  5366. When you want look-up or go to a different location in a document, the
  5367. fastest way is often to fold the entire buffer and then dive into the tree.
  5368. This method has the disadvantage, that the previous location will be folded,
  5369. which may not be what you want.
  5370. This command works around this by showing a copy of the current buffer
  5371. in an indirect buffer, in overview mode. You can dive into the tree in
  5372. that copy, use org-occur and incremental search to find a location.
  5373. When pressing RET or `Q', the command returns to the original buffer in
  5374. which the visibility is still unchanged. After RET is will also jump to
  5375. the location selected in the indirect buffer and expose the
  5376. the headline hierarchy above."
  5377. (interactive "P")
  5378. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5379. (org-refile-use-outline-path t)
  5380. (interface
  5381. (if (not alternative-interface)
  5382. org-goto-interface
  5383. (if (eq org-goto-interface 'outline)
  5384. 'outline-path-completion
  5385. 'outline)))
  5386. (org-goto-start-pos (point))
  5387. (selected-point
  5388. (if (eq interface 'outline)
  5389. (car (org-get-location (current-buffer) org-goto-help))
  5390. (nth 3 (org-refile-get-location "Goto: ")))))
  5391. (if selected-point
  5392. (progn
  5393. (org-mark-ring-push org-goto-start-pos)
  5394. (goto-char selected-point)
  5395. (if (or (org-invisible-p) (org-invisible-p2))
  5396. (org-show-context 'org-goto)))
  5397. (message "Quit"))))
  5398. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5399. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5400. (defvar org-goto-local-auto-isearch-map) ; defined below
  5401. (defun org-get-location (buf help)
  5402. "Let the user select a location in the Org-mode buffer BUF.
  5403. This function uses a recursive edit. It returns the selected position
  5404. or nil."
  5405. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5406. (isearch-hide-immediately nil)
  5407. (isearch-search-fun-function
  5408. (lambda () 'org-goto-local-search-forward-headings))
  5409. (org-goto-selected-point org-goto-exit-command))
  5410. (save-excursion
  5411. (save-window-excursion
  5412. (delete-other-windows)
  5413. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5414. (switch-to-buffer
  5415. (condition-case nil
  5416. (make-indirect-buffer (current-buffer) "*org-goto*")
  5417. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5418. (with-output-to-temp-buffer "*Help*"
  5419. (princ help))
  5420. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5421. (setq buffer-read-only nil)
  5422. (let ((org-startup-truncated t)
  5423. (org-startup-folded nil)
  5424. (org-startup-align-all-tables nil))
  5425. (org-mode)
  5426. (org-overview))
  5427. (setq buffer-read-only t)
  5428. (if (and (boundp 'org-goto-start-pos)
  5429. (integer-or-marker-p org-goto-start-pos))
  5430. (let ((org-show-hierarchy-above t)
  5431. (org-show-siblings t)
  5432. (org-show-following-heading t))
  5433. (goto-char org-goto-start-pos)
  5434. (and (org-invisible-p) (org-show-context)))
  5435. (goto-char (point-min)))
  5436. (org-beginning-of-line)
  5437. (message "Select location and press RET")
  5438. (use-local-map org-goto-map)
  5439. (recursive-edit)
  5440. ))
  5441. (kill-buffer "*org-goto*")
  5442. (cons org-goto-selected-point org-goto-exit-command)))
  5443. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5444. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5445. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5446. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5447. (defun org-goto-local-search-forward-headings (string bound noerror)
  5448. "Search and make sure that anu matches are in headlines."
  5449. (catch 'return
  5450. (while (search-forward string bound noerror)
  5451. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5452. (and (member :headline context)
  5453. (not (member :tags context))))
  5454. (throw 'return (point))))))
  5455. (defun org-goto-local-auto-isearch ()
  5456. "Start isearch."
  5457. (interactive)
  5458. (goto-char (point-min))
  5459. (let ((keys (this-command-keys)))
  5460. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5461. (isearch-mode t)
  5462. (isearch-process-search-char (string-to-char keys)))))
  5463. (defun org-goto-ret (&optional arg)
  5464. "Finish `org-goto' by going to the new location."
  5465. (interactive "P")
  5466. (setq org-goto-selected-point (point)
  5467. org-goto-exit-command 'return)
  5468. (throw 'exit nil))
  5469. (defun org-goto-left ()
  5470. "Finish `org-goto' by going to the new location."
  5471. (interactive)
  5472. (if (org-on-heading-p)
  5473. (progn
  5474. (beginning-of-line 1)
  5475. (setq org-goto-selected-point (point)
  5476. org-goto-exit-command 'left)
  5477. (throw 'exit nil))
  5478. (error "Not on a heading")))
  5479. (defun org-goto-right ()
  5480. "Finish `org-goto' by going to the new location."
  5481. (interactive)
  5482. (if (org-on-heading-p)
  5483. (progn
  5484. (setq org-goto-selected-point (point)
  5485. org-goto-exit-command 'right)
  5486. (throw 'exit nil))
  5487. (error "Not on a heading")))
  5488. (defun org-goto-quit ()
  5489. "Finish `org-goto' without cursor motion."
  5490. (interactive)
  5491. (setq org-goto-selected-point nil)
  5492. (setq org-goto-exit-command 'quit)
  5493. (throw 'exit nil))
  5494. ;;; Indirect buffer display of subtrees
  5495. (defvar org-indirect-dedicated-frame nil
  5496. "This is the frame being used for indirect tree display.")
  5497. (defvar org-last-indirect-buffer nil)
  5498. (defun org-tree-to-indirect-buffer (&optional arg)
  5499. "Create indirect buffer and narrow it to current subtree.
  5500. With numerical prefix ARG, go up to this level and then take that tree.
  5501. If ARG is negative, go up that many levels.
  5502. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5503. indirect buffer previously made with this command, to avoid proliferation of
  5504. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5505. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5506. is kept so that you can work with several indirect buffers at the same time.
  5507. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5508. requests that a new frame be made for the new buffer, so that the dedicated
  5509. frame is not changed."
  5510. (interactive "P")
  5511. (let ((cbuf (current-buffer))
  5512. (cwin (selected-window))
  5513. (pos (point))
  5514. beg end level heading ibuf)
  5515. (save-excursion
  5516. (org-back-to-heading t)
  5517. (when (numberp arg)
  5518. (setq level (org-outline-level))
  5519. (if (< arg 0) (setq arg (+ level arg)))
  5520. (while (> (setq level (org-outline-level)) arg)
  5521. (outline-up-heading 1 t)))
  5522. (setq beg (point)
  5523. heading (org-get-heading))
  5524. (org-end-of-subtree t) (setq end (point)))
  5525. (if (and (buffer-live-p org-last-indirect-buffer)
  5526. (not (eq org-indirect-buffer-display 'new-frame))
  5527. (not arg))
  5528. (kill-buffer org-last-indirect-buffer))
  5529. (setq ibuf (org-get-indirect-buffer cbuf)
  5530. org-last-indirect-buffer ibuf)
  5531. (cond
  5532. ((or (eq org-indirect-buffer-display 'new-frame)
  5533. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5534. (select-frame (make-frame))
  5535. (delete-other-windows)
  5536. (switch-to-buffer ibuf)
  5537. (org-set-frame-title heading))
  5538. ((eq org-indirect-buffer-display 'dedicated-frame)
  5539. (raise-frame
  5540. (select-frame (or (and org-indirect-dedicated-frame
  5541. (frame-live-p org-indirect-dedicated-frame)
  5542. org-indirect-dedicated-frame)
  5543. (setq org-indirect-dedicated-frame (make-frame)))))
  5544. (delete-other-windows)
  5545. (switch-to-buffer ibuf)
  5546. (org-set-frame-title (concat "Indirect: " heading)))
  5547. ((eq org-indirect-buffer-display 'current-window)
  5548. (switch-to-buffer ibuf))
  5549. ((eq org-indirect-buffer-display 'other-window)
  5550. (pop-to-buffer ibuf))
  5551. (t (error "Invalid value.")))
  5552. (if (featurep 'xemacs)
  5553. (save-excursion (org-mode) (turn-on-font-lock)))
  5554. (narrow-to-region beg end)
  5555. (show-all)
  5556. (goto-char pos)
  5557. (and (window-live-p cwin) (select-window cwin))))
  5558. (defun org-get-indirect-buffer (&optional buffer)
  5559. (setq buffer (or buffer (current-buffer)))
  5560. (let ((n 1) (base (buffer-name buffer)) bname)
  5561. (while (buffer-live-p
  5562. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5563. (setq n (1+ n)))
  5564. (condition-case nil
  5565. (make-indirect-buffer buffer bname 'clone)
  5566. (error (make-indirect-buffer buffer bname)))))
  5567. (defun org-set-frame-title (title)
  5568. "Set the title of the current frame to the string TITLE."
  5569. ;; FIXME: how to name a single frame in XEmacs???
  5570. (unless (featurep 'xemacs)
  5571. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5572. ;;;; Structure editing
  5573. ;;; Inserting headlines
  5574. (defun org-insert-heading (&optional force-heading)
  5575. "Insert a new heading or item with same depth at point.
  5576. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5577. If point is at the beginning of a headline, insert a sibling before the
  5578. current headline. If point is not at the beginning, do not split the line,
  5579. but create the new hedline after the current line."
  5580. (interactive "P")
  5581. (if (= (buffer-size) 0)
  5582. (insert "\n* ")
  5583. (when (or force-heading (not (org-insert-item)))
  5584. (let* ((head (save-excursion
  5585. (condition-case nil
  5586. (progn
  5587. (org-back-to-heading)
  5588. (match-string 0))
  5589. (error "*"))))
  5590. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5591. pos)
  5592. (cond
  5593. ((and (org-on-heading-p) (bolp)
  5594. (or (bobp)
  5595. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5596. ;; insert before the current line
  5597. (open-line (if blank 2 1)))
  5598. ((and (bolp)
  5599. (or (bobp)
  5600. (save-excursion
  5601. (backward-char 1) (not (org-invisible-p)))))
  5602. ;; insert right here
  5603. nil)
  5604. (t
  5605. ; ;; in the middle of the line
  5606. ; (org-show-entry)
  5607. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5608. ; (if (and
  5609. ; (org-on-heading-p)
  5610. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5611. ; ;; protect the tags
  5612. ;; (let ((tags (match-string 2)) pos)
  5613. ; (delete-region (match-beginning 1) (match-end 1))
  5614. ; (setq pos (point-at-bol))
  5615. ; (newline (if blank 2 1))
  5616. ; (save-excursion
  5617. ; (goto-char pos)
  5618. ; (end-of-line 1)
  5619. ; (insert " " tags)
  5620. ; (org-set-tags nil 'align)))
  5621. ; (newline (if blank 2 1)))
  5622. ; (newline (if blank 2 1))))
  5623. ;; in the middle of the line
  5624. (org-show-entry)
  5625. (let ((split
  5626. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5627. tags pos)
  5628. (if (org-on-heading-p)
  5629. (progn
  5630. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5631. (setq tags (and (match-end 2) (match-string 2)))
  5632. (and (match-end 1)
  5633. (delete-region (match-beginning 1) (match-end 1)))
  5634. (setq pos (point-at-bol))
  5635. (or split (end-of-line 1))
  5636. (delete-horizontal-space)
  5637. (newline (if blank 2 1))
  5638. (when tags
  5639. (save-excursion
  5640. (goto-char pos)
  5641. (end-of-line 1)
  5642. (insert " " tags)
  5643. (org-set-tags nil 'align))))
  5644. (or split (end-of-line 1))
  5645. (newline (if blank 2 1))))))
  5646. (insert head) (just-one-space)
  5647. (setq pos (point))
  5648. (end-of-line 1)
  5649. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5650. (run-hooks 'org-insert-heading-hook)))))
  5651. (defun org-insert-heading-after-current ()
  5652. "Insert a new heading with same level as current, after current subtree."
  5653. (interactive)
  5654. (org-back-to-heading)
  5655. (org-insert-heading)
  5656. (org-move-subtree-down)
  5657. (end-of-line 1))
  5658. (defun org-insert-todo-heading (arg)
  5659. "Insert a new heading with the same level and TODO state as current heading.
  5660. If the heading has no TODO state, or if the state is DONE, use the first
  5661. state (TODO by default). Also with prefix arg, force first state."
  5662. (interactive "P")
  5663. (when (not (org-insert-item 'checkbox))
  5664. (org-insert-heading)
  5665. (save-excursion
  5666. (org-back-to-heading)
  5667. (outline-previous-heading)
  5668. (looking-at org-todo-line-regexp))
  5669. (if (or arg
  5670. (not (match-beginning 2))
  5671. (member (match-string 2) org-done-keywords))
  5672. (insert (car org-todo-keywords-1) " ")
  5673. (insert (match-string 2) " "))))
  5674. (defun org-insert-subheading (arg)
  5675. "Insert a new subheading and demote it.
  5676. Works for outline headings and for plain lists alike."
  5677. (interactive "P")
  5678. (org-insert-heading arg)
  5679. (cond
  5680. ((org-on-heading-p) (org-do-demote))
  5681. ((org-at-item-p) (org-indent-item 1))))
  5682. (defun org-insert-todo-subheading (arg)
  5683. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5684. Works for outline headings and for plain lists alike."
  5685. (interactive "P")
  5686. (org-insert-todo-heading arg)
  5687. (cond
  5688. ((org-on-heading-p) (org-do-demote))
  5689. ((org-at-item-p) (org-indent-item 1))))
  5690. ;;; Promotion and Demotion
  5691. (defun org-promote-subtree ()
  5692. "Promote the entire subtree.
  5693. See also `org-promote'."
  5694. (interactive)
  5695. (save-excursion
  5696. (org-map-tree 'org-promote))
  5697. (org-fix-position-after-promote))
  5698. (defun org-demote-subtree ()
  5699. "Demote the entire subtree. See `org-demote'.
  5700. See also `org-promote'."
  5701. (interactive)
  5702. (save-excursion
  5703. (org-map-tree 'org-demote))
  5704. (org-fix-position-after-promote))
  5705. (defun org-do-promote ()
  5706. "Promote the current heading higher up the tree.
  5707. If the region is active in `transient-mark-mode', promote all headings
  5708. in the region."
  5709. (interactive)
  5710. (save-excursion
  5711. (if (org-region-active-p)
  5712. (org-map-region 'org-promote (region-beginning) (region-end))
  5713. (org-promote)))
  5714. (org-fix-position-after-promote))
  5715. (defun org-do-demote ()
  5716. "Demote the current heading lower down the tree.
  5717. If the region is active in `transient-mark-mode', demote all headings
  5718. in the region."
  5719. (interactive)
  5720. (save-excursion
  5721. (if (org-region-active-p)
  5722. (org-map-region 'org-demote (region-beginning) (region-end))
  5723. (org-demote)))
  5724. (org-fix-position-after-promote))
  5725. (defun org-fix-position-after-promote ()
  5726. "Make sure that after pro/demotion cursor position is right."
  5727. (let ((pos (point)))
  5728. (when (save-excursion
  5729. (beginning-of-line 1)
  5730. (looking-at org-todo-line-regexp)
  5731. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5732. (cond ((eobp) (insert " "))
  5733. ((eolp) (insert " "))
  5734. ((equal (char-after) ?\ ) (forward-char 1))))))
  5735. (defun org-reduced-level (l)
  5736. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5737. (defun org-get-legal-level (level &optional change)
  5738. "Rectify a level change under the influence of `org-odd-levels-only'
  5739. LEVEL is a current level, CHANGE is by how much the level should be
  5740. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5741. even level numbers will become the next higher odd number."
  5742. (if org-odd-levels-only
  5743. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5744. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5745. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5746. (max 1 (+ level change))))
  5747. (defun org-promote ()
  5748. "Promote the current heading higher up the tree.
  5749. If the region is active in `transient-mark-mode', promote all headings
  5750. in the region."
  5751. (org-back-to-heading t)
  5752. (let* ((level (save-match-data (funcall outline-level)))
  5753. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5754. (diff (abs (- level (length up-head) -1))))
  5755. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5756. (replace-match up-head nil t)
  5757. ;; Fixup tag positioning
  5758. (and org-auto-align-tags (org-set-tags nil t))
  5759. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5760. (defun org-demote ()
  5761. "Demote the current heading lower down the tree.
  5762. If the region is active in `transient-mark-mode', demote all headings
  5763. in the region."
  5764. (org-back-to-heading t)
  5765. (let* ((level (save-match-data (funcall outline-level)))
  5766. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5767. (diff (abs (- level (length down-head) -1))))
  5768. (replace-match down-head nil t)
  5769. ;; Fixup tag positioning
  5770. (and org-auto-align-tags (org-set-tags nil t))
  5771. (if org-adapt-indentation (org-fixup-indentation diff))))
  5772. (defun org-map-tree (fun)
  5773. "Call FUN for every heading underneath the current one."
  5774. (org-back-to-heading)
  5775. (let ((level (funcall outline-level)))
  5776. (save-excursion
  5777. (funcall fun)
  5778. (while (and (progn
  5779. (outline-next-heading)
  5780. (> (funcall outline-level) level))
  5781. (not (eobp)))
  5782. (funcall fun)))))
  5783. (defun org-map-region (fun beg end)
  5784. "Call FUN for every heading between BEG and END."
  5785. (let ((org-ignore-region t))
  5786. (save-excursion
  5787. (setq end (copy-marker end))
  5788. (goto-char beg)
  5789. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5790. (< (point) end))
  5791. (funcall fun))
  5792. (while (and (progn
  5793. (outline-next-heading)
  5794. (< (point) end))
  5795. (not (eobp)))
  5796. (funcall fun)))))
  5797. (defun org-fixup-indentation (diff)
  5798. "Change the indentation in the current entry by DIFF
  5799. However, if any line in the current entry has no indentation, or if it
  5800. would end up with no indentation after the change, nothing at all is done."
  5801. (save-excursion
  5802. (let ((end (save-excursion (outline-next-heading)
  5803. (point-marker)))
  5804. (prohibit (if (> diff 0)
  5805. "^\\S-"
  5806. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5807. col)
  5808. (unless (save-excursion (end-of-line 1)
  5809. (re-search-forward prohibit end t))
  5810. (while (and (< (point) end)
  5811. (re-search-forward "^[ \t]+" end t))
  5812. (goto-char (match-end 0))
  5813. (setq col (current-column))
  5814. (if (< diff 0) (replace-match ""))
  5815. (indent-to (+ diff col))))
  5816. (move-marker end nil))))
  5817. (defun org-convert-to-odd-levels ()
  5818. "Convert an org-mode file with all levels allowed to one with odd levels.
  5819. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5820. level 5 etc."
  5821. (interactive)
  5822. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5823. (let ((org-odd-levels-only nil) n)
  5824. (save-excursion
  5825. (goto-char (point-min))
  5826. (while (re-search-forward "^\\*\\*+ " nil t)
  5827. (setq n (- (length (match-string 0)) 2))
  5828. (while (>= (setq n (1- n)) 0)
  5829. (org-demote))
  5830. (end-of-line 1))))))
  5831. (defun org-convert-to-oddeven-levels ()
  5832. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5833. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5834. section with an even level, conversion would destroy the structure of the file. An error
  5835. is signaled in this case."
  5836. (interactive)
  5837. (goto-char (point-min))
  5838. ;; First check if there are no even levels
  5839. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5840. (org-show-context t)
  5841. (error "Not all levels are odd in this file. Conversion not possible."))
  5842. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5843. (let ((org-odd-levels-only nil) n)
  5844. (save-excursion
  5845. (goto-char (point-min))
  5846. (while (re-search-forward "^\\*\\*+ " nil t)
  5847. (setq n (/ (1- (length (match-string 0))) 2))
  5848. (while (>= (setq n (1- n)) 0)
  5849. (org-promote))
  5850. (end-of-line 1))))))
  5851. (defun org-tr-level (n)
  5852. "Make N odd if required."
  5853. (if org-odd-levels-only (1+ (/ n 2)) n))
  5854. ;;; Vertical tree motion, cutting and pasting of subtrees
  5855. (defun org-move-subtree-up (&optional arg)
  5856. "Move the current subtree up past ARG headlines of the same level."
  5857. (interactive "p")
  5858. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5859. (defun org-move-subtree-down (&optional arg)
  5860. "Move the current subtree down past ARG headlines of the same level."
  5861. (interactive "p")
  5862. (setq arg (prefix-numeric-value arg))
  5863. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5864. 'outline-get-last-sibling))
  5865. (ins-point (make-marker))
  5866. (cnt (abs arg))
  5867. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5868. ;; Select the tree
  5869. (org-back-to-heading)
  5870. (setq beg0 (point))
  5871. (save-excursion
  5872. (setq ne-beg (org-back-over-empty-lines))
  5873. (setq beg (point)))
  5874. (save-match-data
  5875. (save-excursion (outline-end-of-heading)
  5876. (setq folded (org-invisible-p)))
  5877. (outline-end-of-subtree))
  5878. (outline-next-heading)
  5879. (setq ne-end (org-back-over-empty-lines))
  5880. (setq end (point))
  5881. (goto-char beg0)
  5882. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5883. ;; include less whitespace
  5884. (save-excursion
  5885. (goto-char beg)
  5886. (forward-line (- ne-beg ne-end))
  5887. (setq beg (point))))
  5888. ;; Find insertion point, with error handling
  5889. (while (> cnt 0)
  5890. (or (and (funcall movfunc) (looking-at outline-regexp))
  5891. (progn (goto-char beg0)
  5892. (error "Cannot move past superior level or buffer limit")))
  5893. (setq cnt (1- cnt)))
  5894. (if (> arg 0)
  5895. ;; Moving forward - still need to move over subtree
  5896. (progn (org-end-of-subtree t t)
  5897. (save-excursion
  5898. (org-back-over-empty-lines)
  5899. (or (bolp) (newline)))))
  5900. (setq ne-ins (org-back-over-empty-lines))
  5901. (move-marker ins-point (point))
  5902. (setq txt (buffer-substring beg end))
  5903. (delete-region beg end)
  5904. (outline-flag-region (1- beg) beg nil)
  5905. (outline-flag-region (1- (point)) (point) nil)
  5906. (insert txt)
  5907. (or (bolp) (insert "\n"))
  5908. (setq ins-end (point))
  5909. (goto-char ins-point)
  5910. (org-skip-whitespace)
  5911. (when (and (< arg 0)
  5912. (org-first-sibling-p)
  5913. (> ne-ins ne-beg))
  5914. ;; Move whitespace back to beginning
  5915. (save-excursion
  5916. (goto-char ins-end)
  5917. (let ((kill-whole-line t))
  5918. (kill-line (- ne-ins ne-beg)) (point)))
  5919. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5920. (move-marker ins-point nil)
  5921. (org-compact-display-after-subtree-move)
  5922. (unless folded
  5923. (org-show-entry)
  5924. (show-children)
  5925. (org-cycle-hide-drawers 'children))))
  5926. (defvar org-subtree-clip ""
  5927. "Clipboard for cut and paste of subtrees.
  5928. This is actually only a copy of the kill, because we use the normal kill
  5929. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5930. (defvar org-subtree-clip-folded nil
  5931. "Was the last copied subtree folded?
  5932. This is used to fold the tree back after pasting.")
  5933. (defun org-cut-subtree (&optional n)
  5934. "Cut the current subtree into the clipboard.
  5935. With prefix arg N, cut this many sequential subtrees.
  5936. This is a short-hand for marking the subtree and then cutting it."
  5937. (interactive "p")
  5938. (org-copy-subtree n 'cut))
  5939. (defun org-copy-subtree (&optional n cut)
  5940. "Cut the current subtree into the clipboard.
  5941. With prefix arg N, cut this many sequential subtrees.
  5942. This is a short-hand for marking the subtree and then copying it.
  5943. If CUT is non-nil, actually cut the subtree."
  5944. (interactive "p")
  5945. (let (beg end folded (beg0 (point)))
  5946. (if (interactive-p)
  5947. (org-back-to-heading nil) ; take what looks like a subtree
  5948. (org-back-to-heading t)) ; take what is really there
  5949. (org-back-over-empty-lines)
  5950. (setq beg (point))
  5951. (skip-chars-forward " \t\r\n")
  5952. (save-match-data
  5953. (save-excursion (outline-end-of-heading)
  5954. (setq folded (org-invisible-p)))
  5955. (condition-case nil
  5956. (outline-forward-same-level (1- n))
  5957. (error nil))
  5958. (org-end-of-subtree t t))
  5959. (org-back-over-empty-lines)
  5960. (setq end (point))
  5961. (goto-char beg0)
  5962. (when (> end beg)
  5963. (setq org-subtree-clip-folded folded)
  5964. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5965. (setq org-subtree-clip (current-kill 0))
  5966. (message "%s: Subtree(s) with %d characters"
  5967. (if cut "Cut" "Copied")
  5968. (length org-subtree-clip)))))
  5969. (defun org-paste-subtree (&optional level tree)
  5970. "Paste the clipboard as a subtree, with modification of headline level.
  5971. The entire subtree is promoted or demoted in order to match a new headline
  5972. level. By default, the new level is derived from the visible headings
  5973. before and after the insertion point, and taken to be the inferior headline
  5974. level of the two. So if the previous visible heading is level 3 and the
  5975. next is level 4 (or vice versa), level 4 will be used for insertion.
  5976. This makes sure that the subtree remains an independent subtree and does
  5977. not swallow low level entries.
  5978. You can also force a different level, either by using a numeric prefix
  5979. argument, or by inserting the heading marker by hand. For example, if the
  5980. cursor is after \"*****\", then the tree will be shifted to level 5.
  5981. If you want to insert the tree as is, just use \\[yank].
  5982. If optional TREE is given, use this text instead of the kill ring."
  5983. (interactive "P")
  5984. (unless (org-kill-is-subtree-p tree)
  5985. (error "%s"
  5986. (substitute-command-keys
  5987. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5988. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5989. (^re (concat "^\\(" outline-regexp "\\)"))
  5990. (re (concat "\\(" outline-regexp "\\)"))
  5991. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5992. (old-level (if (string-match ^re txt)
  5993. (- (match-end 0) (match-beginning 0) 1)
  5994. -1))
  5995. (force-level (cond (level (prefix-numeric-value level))
  5996. ((string-match
  5997. ^re_ (buffer-substring (point-at-bol) (point)))
  5998. (- (match-end 1) (match-beginning 1)))
  5999. (t nil)))
  6000. (previous-level (save-excursion
  6001. (condition-case nil
  6002. (progn
  6003. (outline-previous-visible-heading 1)
  6004. (if (looking-at re)
  6005. (- (match-end 0) (match-beginning 0) 1)
  6006. 1))
  6007. (error 1))))
  6008. (next-level (save-excursion
  6009. (condition-case nil
  6010. (progn
  6011. (or (looking-at outline-regexp)
  6012. (outline-next-visible-heading 1))
  6013. (if (looking-at re)
  6014. (- (match-end 0) (match-beginning 0) 1)
  6015. 1))
  6016. (error 1))))
  6017. (new-level (or force-level (max previous-level next-level)))
  6018. (shift (if (or (= old-level -1)
  6019. (= new-level -1)
  6020. (= old-level new-level))
  6021. 0
  6022. (- new-level old-level)))
  6023. (delta (if (> shift 0) -1 1))
  6024. (func (if (> shift 0) 'org-demote 'org-promote))
  6025. (org-odd-levels-only nil)
  6026. beg end)
  6027. ;; Remove the forced level indicator
  6028. (if force-level
  6029. (delete-region (point-at-bol) (point)))
  6030. ;; Paste
  6031. (beginning-of-line 1)
  6032. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6033. (setq beg (point))
  6034. (insert-before-markers txt) ;; FIXME: correct fix????
  6035. (unless (string-match "\n\\'" txt) (insert "\n"))
  6036. (setq end (point))
  6037. (goto-char beg)
  6038. (skip-chars-forward " \t\n\r")
  6039. (setq beg (point))
  6040. ;; Shift if necessary
  6041. (unless (= shift 0)
  6042. (save-restriction
  6043. (narrow-to-region beg end)
  6044. (while (not (= shift 0))
  6045. (org-map-region func (point-min) (point-max))
  6046. (setq shift (+ delta shift)))
  6047. (goto-char (point-min))))
  6048. (when (interactive-p)
  6049. (message "Clipboard pasted as level %d subtree" new-level))
  6050. (if (and kill-ring
  6051. (eq org-subtree-clip (current-kill 0))
  6052. org-subtree-clip-folded)
  6053. ;; The tree was folded before it was killed/copied
  6054. (hide-subtree))))
  6055. (defun org-kill-is-subtree-p (&optional txt)
  6056. "Check if the current kill is an outline subtree, or a set of trees.
  6057. Returns nil if kill does not start with a headline, or if the first
  6058. headline level is not the largest headline level in the tree.
  6059. So this will actually accept several entries of equal levels as well,
  6060. which is OK for `org-paste-subtree'.
  6061. If optional TXT is given, check this string instead of the current kill."
  6062. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6063. (start-level (and kill
  6064. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6065. org-outline-regexp "\\)")
  6066. kill)
  6067. (- (match-end 2) (match-beginning 2) 1)))
  6068. (re (concat "^" org-outline-regexp))
  6069. (start (1+ (match-beginning 2))))
  6070. (if (not start-level)
  6071. (progn
  6072. nil) ;; does not even start with a heading
  6073. (catch 'exit
  6074. (while (setq start (string-match re kill (1+ start)))
  6075. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6076. (throw 'exit nil)))
  6077. t))))
  6078. (defun org-narrow-to-subtree ()
  6079. "Narrow buffer to the current subtree."
  6080. (interactive)
  6081. (save-excursion
  6082. (save-match-data
  6083. (narrow-to-region
  6084. (progn (org-back-to-heading) (point))
  6085. (progn (org-end-of-subtree t t) (point))))))
  6086. ;;; Outline Sorting
  6087. (defun org-sort (with-case)
  6088. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6089. Optional argument WITH-CASE means sort case-sensitively."
  6090. (interactive "P")
  6091. (if (org-at-table-p)
  6092. (org-call-with-arg 'org-table-sort-lines with-case)
  6093. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6094. (defvar org-priority-regexp) ; defined later in the file
  6095. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6096. "Sort entries on a certain level of an outline tree.
  6097. If there is an active region, the entries in the region are sorted.
  6098. Else, if the cursor is before the first entry, sort the top-level items.
  6099. Else, the children of the entry at point are sorted.
  6100. Sorting can be alphabetically, numerically, and by date/time as given by
  6101. the first time stamp in the entry. The command prompts for the sorting
  6102. type unless it has been given to the function through the SORTING-TYPE
  6103. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6104. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6105. called with point at the beginning of the record. It must return either
  6106. a string or a number that should serve as the sorting key for that record.
  6107. Comparing entries ignores case by default. However, with an optional argument
  6108. WITH-CASE, the sorting considers case as well."
  6109. (interactive "P")
  6110. (let ((case-func (if with-case 'identity 'downcase))
  6111. start beg end stars re re2
  6112. txt what tmp plain-list-p)
  6113. ;; Find beginning and end of region to sort
  6114. (cond
  6115. ((org-region-active-p)
  6116. ;; we will sort the region
  6117. (setq end (region-end)
  6118. what "region")
  6119. (goto-char (region-beginning))
  6120. (if (not (org-on-heading-p)) (outline-next-heading))
  6121. (setq start (point)))
  6122. ((org-at-item-p)
  6123. ;; we will sort this plain list
  6124. (org-beginning-of-item-list) (setq start (point))
  6125. (org-end-of-item-list) (setq end (point))
  6126. (goto-char start)
  6127. (setq plain-list-p t
  6128. what "plain list"))
  6129. ((or (org-on-heading-p)
  6130. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6131. ;; we will sort the children of the current headline
  6132. (org-back-to-heading)
  6133. (setq start (point)
  6134. end (progn (org-end-of-subtree t t)
  6135. (org-back-over-empty-lines)
  6136. (point))
  6137. what "children")
  6138. (goto-char start)
  6139. (show-subtree)
  6140. (outline-next-heading))
  6141. (t
  6142. ;; we will sort the top-level entries in this file
  6143. (goto-char (point-min))
  6144. (or (org-on-heading-p) (outline-next-heading))
  6145. (setq start (point) end (point-max) what "top-level")
  6146. (goto-char start)
  6147. (show-all)))
  6148. (setq beg (point))
  6149. (if (>= beg end) (error "Nothing to sort"))
  6150. (unless plain-list-p
  6151. (looking-at "\\(\\*+\\)")
  6152. (setq stars (match-string 1)
  6153. re (concat "^" (regexp-quote stars) " +")
  6154. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6155. txt (buffer-substring beg end))
  6156. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6157. (if (and (not (equal stars "*")) (string-match re2 txt))
  6158. (error "Region to sort contains a level above the first entry")))
  6159. (unless sorting-type
  6160. (message
  6161. (if plain-list-p
  6162. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6163. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6164. what)
  6165. (setq sorting-type (read-char-exclusive))
  6166. (and (= (downcase sorting-type) ?f)
  6167. (setq getkey-func
  6168. (completing-read "Sort using function: "
  6169. obarray 'fboundp t nil nil))
  6170. (setq getkey-func (intern getkey-func)))
  6171. (and (= (downcase sorting-type) ?r)
  6172. (setq property
  6173. (completing-read "Property: "
  6174. (mapcar 'list (org-buffer-property-keys t))
  6175. nil t))))
  6176. (message "Sorting entries...")
  6177. (save-restriction
  6178. (narrow-to-region start end)
  6179. (let ((dcst (downcase sorting-type))
  6180. (now (current-time)))
  6181. (sort-subr
  6182. (/= dcst sorting-type)
  6183. ;; This function moves to the beginning character of the "record" to
  6184. ;; be sorted.
  6185. (if plain-list-p
  6186. (lambda nil
  6187. (if (org-at-item-p) t (goto-char (point-max))))
  6188. (lambda nil
  6189. (if (re-search-forward re nil t)
  6190. (goto-char (match-beginning 0))
  6191. (goto-char (point-max)))))
  6192. ;; This function moves to the last character of the "record" being
  6193. ;; sorted.
  6194. (if plain-list-p
  6195. 'org-end-of-item
  6196. (lambda nil
  6197. (save-match-data
  6198. (condition-case nil
  6199. (outline-forward-same-level 1)
  6200. (error
  6201. (goto-char (point-max)))))))
  6202. ;; This function returns the value that gets sorted against.
  6203. (if plain-list-p
  6204. (lambda nil
  6205. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6206. (cond
  6207. ((= dcst ?n)
  6208. (string-to-number (buffer-substring (match-end 0)
  6209. (point-at-eol))))
  6210. ((= dcst ?a)
  6211. (buffer-substring (match-end 0) (point-at-eol)))
  6212. ((= dcst ?t)
  6213. (if (re-search-forward org-ts-regexp
  6214. (point-at-eol) t)
  6215. (org-time-string-to-time (match-string 0))
  6216. now))
  6217. ((= dcst ?f)
  6218. (if getkey-func
  6219. (progn
  6220. (setq tmp (funcall getkey-func))
  6221. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6222. tmp)
  6223. (error "Invalid key function `%s'" getkey-func)))
  6224. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6225. (lambda nil
  6226. (cond
  6227. ((= dcst ?n)
  6228. (if (looking-at outline-regexp)
  6229. (string-to-number (buffer-substring (match-end 0)
  6230. (point-at-eol)))
  6231. nil))
  6232. ((= dcst ?a)
  6233. (funcall case-func (buffer-substring (point-at-bol)
  6234. (point-at-eol))))
  6235. ((= dcst ?t)
  6236. (if (re-search-forward org-ts-regexp
  6237. (save-excursion
  6238. (forward-line 2)
  6239. (point)) t)
  6240. (org-time-string-to-time (match-string 0))
  6241. now))
  6242. ((= dcst ?p)
  6243. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6244. (string-to-char (match-string 2))
  6245. org-default-priority))
  6246. ((= dcst ?r)
  6247. (or (org-entry-get nil property) ""))
  6248. ((= dcst ?f)
  6249. (if getkey-func
  6250. (progn
  6251. (setq tmp (funcall getkey-func))
  6252. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6253. tmp)
  6254. (error "Invalid key function `%s'" getkey-func)))
  6255. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6256. nil
  6257. (cond
  6258. ((= dcst ?a) 'string<)
  6259. ((= dcst ?t) 'time-less-p)
  6260. (t nil)))))
  6261. (message "Sorting entries...done")))
  6262. (defun org-do-sort (table what &optional with-case sorting-type)
  6263. "Sort TABLE of WHAT according to SORTING-TYPE.
  6264. The user will be prompted for the SORTING-TYPE if the call to this
  6265. function does not specify it. WHAT is only for the prompt, to indicate
  6266. what is being sorted. The sorting key will be extracted from
  6267. the car of the elements of the table.
  6268. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6269. (unless sorting-type
  6270. (message
  6271. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6272. what)
  6273. (setq sorting-type (read-char-exclusive)))
  6274. (let ((dcst (downcase sorting-type))
  6275. extractfun comparefun)
  6276. ;; Define the appropriate functions
  6277. (cond
  6278. ((= dcst ?n)
  6279. (setq extractfun 'string-to-number
  6280. comparefun (if (= dcst sorting-type) '< '>)))
  6281. ((= dcst ?a)
  6282. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6283. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6284. comparefun (if (= dcst sorting-type)
  6285. 'string<
  6286. (lambda (a b) (and (not (string< a b))
  6287. (not (string= a b)))))))
  6288. ((= dcst ?t)
  6289. (setq extractfun
  6290. (lambda (x)
  6291. (if (string-match org-ts-regexp x)
  6292. (time-to-seconds
  6293. (org-time-string-to-time (match-string 0 x)))
  6294. 0))
  6295. comparefun (if (= dcst sorting-type) '< '>)))
  6296. (t (error "Invalid sorting type `%c'" sorting-type)))
  6297. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6298. table)
  6299. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6300. ;;;; Plain list items, including checkboxes
  6301. ;;; Plain list items
  6302. (defun org-at-item-p ()
  6303. "Is point in a line starting a hand-formatted item?"
  6304. (let ((llt org-plain-list-ordered-item-terminator))
  6305. (save-excursion
  6306. (goto-char (point-at-bol))
  6307. (looking-at
  6308. (cond
  6309. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6310. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6311. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6312. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6313. (defun org-in-item-p ()
  6314. "It the cursor inside a plain list item.
  6315. Does not have to be the first line."
  6316. (save-excursion
  6317. (condition-case nil
  6318. (progn
  6319. (org-beginning-of-item)
  6320. (org-at-item-p)
  6321. t)
  6322. (error nil))))
  6323. (defun org-insert-item (&optional checkbox)
  6324. "Insert a new item at the current level.
  6325. Return t when things worked, nil when we are not in an item."
  6326. (when (save-excursion
  6327. (condition-case nil
  6328. (progn
  6329. (org-beginning-of-item)
  6330. (org-at-item-p)
  6331. (if (org-invisible-p) (error "Invisible item"))
  6332. t)
  6333. (error nil)))
  6334. (let* ((bul (match-string 0))
  6335. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6336. (match-end 0)))
  6337. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6338. pos)
  6339. (cond
  6340. ((and (org-at-item-p) (<= (point) eow))
  6341. ;; before the bullet
  6342. (beginning-of-line 1)
  6343. (open-line (if blank 2 1)))
  6344. ((<= (point) eow)
  6345. (beginning-of-line 1))
  6346. (t
  6347. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6348. (end-of-line 1)
  6349. (delete-horizontal-space))
  6350. (newline (if blank 2 1))))
  6351. (insert bul (if checkbox "[ ]" ""))
  6352. (just-one-space)
  6353. (setq pos (point))
  6354. (end-of-line 1)
  6355. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6356. (org-maybe-renumber-ordered-list)
  6357. (and checkbox (org-update-checkbox-count-maybe))
  6358. t))
  6359. ;;; Checkboxes
  6360. (defun org-at-item-checkbox-p ()
  6361. "Is point at a line starting a plain-list item with a checklet?"
  6362. (and (org-at-item-p)
  6363. (save-excursion
  6364. (goto-char (match-end 0))
  6365. (skip-chars-forward " \t")
  6366. (looking-at "\\[[- X]\\]"))))
  6367. (defun org-toggle-checkbox (&optional arg)
  6368. "Toggle the checkbox in the current line."
  6369. (interactive "P")
  6370. (catch 'exit
  6371. (let (beg end status (firstnew 'unknown))
  6372. (cond
  6373. ((org-region-active-p)
  6374. (setq beg (region-beginning) end (region-end)))
  6375. ((org-on-heading-p)
  6376. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6377. ((org-at-item-checkbox-p)
  6378. (let ((pos (point)))
  6379. (replace-match
  6380. (cond (arg "[-]")
  6381. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6382. (t "[ ]"))
  6383. t t)
  6384. (goto-char pos))
  6385. (throw 'exit t))
  6386. (t (error "Not at a checkbox or heading, and no active region")))
  6387. (save-excursion
  6388. (goto-char beg)
  6389. (while (< (point) end)
  6390. (when (org-at-item-checkbox-p)
  6391. (setq status (equal (match-string 0) "[X]"))
  6392. (when (eq firstnew 'unknown)
  6393. (setq firstnew (not status)))
  6394. (replace-match
  6395. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6396. (beginning-of-line 2)))))
  6397. (org-update-checkbox-count-maybe))
  6398. (defun org-update-checkbox-count-maybe ()
  6399. "Update checkbox statistics unless turned off by user."
  6400. (when org-provide-checkbox-statistics
  6401. (org-update-checkbox-count)))
  6402. (defun org-update-checkbox-count (&optional all)
  6403. "Update the checkbox statistics in the current section.
  6404. This will find all statistic cookies like [57%] and [6/12] and update them
  6405. with the current numbers. With optional prefix argument ALL, do this for
  6406. the whole buffer."
  6407. (interactive "P")
  6408. (save-excursion
  6409. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6410. (beg (condition-case nil
  6411. (progn (outline-back-to-heading) (point))
  6412. (error (point-min))))
  6413. (end (move-marker (make-marker)
  6414. (progn (outline-next-heading) (point))))
  6415. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6416. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6417. (re-find (concat re "\\|" re-box))
  6418. beg-cookie end-cookie is-percent c-on c-off lim
  6419. eline curr-ind next-ind continue-from startsearch
  6420. (cstat 0)
  6421. )
  6422. (when all
  6423. (goto-char (point-min))
  6424. (outline-next-heading)
  6425. (setq beg (point) end (point-max)))
  6426. (goto-char end)
  6427. ;; find each statistic cookie
  6428. (while (re-search-backward re-find beg t)
  6429. (setq beg-cookie (match-beginning 1)
  6430. end-cookie (match-end 1)
  6431. cstat (+ cstat (if end-cookie 1 0))
  6432. startsearch (point-at-eol)
  6433. continue-from (point-at-bol)
  6434. is-percent (match-beginning 2)
  6435. lim (cond
  6436. ((org-on-heading-p) (outline-next-heading) (point))
  6437. ((org-at-item-p) (org-end-of-item) (point))
  6438. (t nil))
  6439. c-on 0
  6440. c-off 0)
  6441. (when lim
  6442. ;; find first checkbox for this cookie and gather
  6443. ;; statistics from all that are at this indentation level
  6444. (goto-char startsearch)
  6445. (if (re-search-forward re-box lim t)
  6446. (progn
  6447. (org-beginning-of-item)
  6448. (setq curr-ind (org-get-indentation))
  6449. (setq next-ind curr-ind)
  6450. (while (= curr-ind next-ind)
  6451. (save-excursion (end-of-line) (setq eline (point)))
  6452. (if (re-search-forward re-box eline t)
  6453. (if (member (match-string 2) '("[ ]" "[-]"))
  6454. (setq c-off (1+ c-off))
  6455. (setq c-on (1+ c-on))
  6456. )
  6457. )
  6458. (org-end-of-item)
  6459. (setq next-ind (org-get-indentation))
  6460. )))
  6461. (goto-char continue-from)
  6462. ;; update cookie
  6463. (when end-cookie
  6464. (delete-region beg-cookie end-cookie)
  6465. (goto-char beg-cookie)
  6466. (insert
  6467. (if is-percent
  6468. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6469. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6470. ;; update items checkbox if it has one
  6471. (when (org-at-item-p)
  6472. (org-beginning-of-item)
  6473. (when (and (> (+ c-on c-off) 0)
  6474. (re-search-forward re-box (point-at-eol) t))
  6475. (setq beg-cookie (match-beginning 2)
  6476. end-cookie (match-end 2))
  6477. (delete-region beg-cookie end-cookie)
  6478. (goto-char beg-cookie)
  6479. (cond ((= c-off 0) (insert "[X]"))
  6480. ((= c-on 0) (insert "[ ]"))
  6481. (t (insert "[-]")))
  6482. )))
  6483. (goto-char continue-from))
  6484. (when (interactive-p)
  6485. (message "Checkbox satistics updated %s (%d places)"
  6486. (if all "in entire file" "in current outline entry") cstat)))))
  6487. (defun org-get-checkbox-statistics-face ()
  6488. "Select the face for checkbox statistics.
  6489. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6490. it will be `org-todo'."
  6491. (if (match-end 1)
  6492. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6493. (if (and (> (match-end 2) (match-beginning 2))
  6494. (equal (match-string 2) (match-string 3)))
  6495. 'org-done
  6496. 'org-todo)))
  6497. (defun org-get-indentation (&optional line)
  6498. "Get the indentation of the current line, interpreting tabs.
  6499. When LINE is given, assume it represents a line and compute its indentation."
  6500. (if line
  6501. (if (string-match "^ *" (org-remove-tabs line))
  6502. (match-end 0))
  6503. (save-excursion
  6504. (beginning-of-line 1)
  6505. (skip-chars-forward " \t")
  6506. (current-column))))
  6507. (defun org-remove-tabs (s &optional width)
  6508. "Replace tabulators in S with spaces.
  6509. Assumes that s is a single line, starting in column 0."
  6510. (setq width (or width tab-width))
  6511. (while (string-match "\t" s)
  6512. (setq s (replace-match
  6513. (make-string
  6514. (- (* width (/ (+ (match-beginning 0) width) width))
  6515. (match-beginning 0)) ?\ )
  6516. t t s)))
  6517. s)
  6518. (defun org-fix-indentation (line ind)
  6519. "Fix indentation in LINE.
  6520. IND is a cons cell with target and minimum indentation.
  6521. If the current indenation in LINE is smaller than the minimum,
  6522. leave it alone. If it is larger than ind, set it to the target."
  6523. (let* ((l (org-remove-tabs line))
  6524. (i (org-get-indentation l))
  6525. (i1 (car ind)) (i2 (cdr ind)))
  6526. (if (>= i i2) (setq l (substring line i2)))
  6527. (if (> i1 0)
  6528. (concat (make-string i1 ?\ ) l)
  6529. l)))
  6530. (defcustom org-empty-line-terminates-plain-lists nil
  6531. "Non-nil means, an empty line ends all plain list levels.
  6532. When nil, empty lines are part of the preceeding item."
  6533. :group 'org-plain-lists
  6534. :type 'boolean)
  6535. (defun org-beginning-of-item ()
  6536. "Go to the beginning of the current hand-formatted item.
  6537. If the cursor is not in an item, throw an error."
  6538. (interactive)
  6539. (let ((pos (point))
  6540. (limit (save-excursion
  6541. (condition-case nil
  6542. (progn
  6543. (org-back-to-heading)
  6544. (beginning-of-line 2) (point))
  6545. (error (point-min)))))
  6546. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6547. ind ind1)
  6548. (if (org-at-item-p)
  6549. (beginning-of-line 1)
  6550. (beginning-of-line 1)
  6551. (skip-chars-forward " \t")
  6552. (setq ind (current-column))
  6553. (if (catch 'exit
  6554. (while t
  6555. (beginning-of-line 0)
  6556. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6557. (if (looking-at "[ \t]*$")
  6558. (setq ind1 ind-empty)
  6559. (skip-chars-forward " \t")
  6560. (setq ind1 (current-column)))
  6561. (if (< ind1 ind)
  6562. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6563. nil
  6564. (goto-char pos)
  6565. (error "Not in an item")))))
  6566. (defun org-end-of-item ()
  6567. "Go to the end of the current hand-formatted item.
  6568. If the cursor is not in an item, throw an error."
  6569. (interactive)
  6570. (let* ((pos (point))
  6571. ind1
  6572. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6573. (limit (save-excursion (outline-next-heading) (point)))
  6574. (ind (save-excursion
  6575. (org-beginning-of-item)
  6576. (skip-chars-forward " \t")
  6577. (current-column)))
  6578. (end (catch 'exit
  6579. (while t
  6580. (beginning-of-line 2)
  6581. (if (eobp) (throw 'exit (point)))
  6582. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6583. (if (looking-at "[ \t]*$")
  6584. (setq ind1 ind-empty)
  6585. (skip-chars-forward " \t")
  6586. (setq ind1 (current-column)))
  6587. (if (<= ind1 ind)
  6588. (throw 'exit (point-at-bol)))))))
  6589. (if end
  6590. (goto-char end)
  6591. (goto-char pos)
  6592. (error "Not in an item"))))
  6593. (defun org-next-item ()
  6594. "Move to the beginning of the next item in the current plain list.
  6595. Error if not at a plain list, or if this is the last item in the list."
  6596. (interactive)
  6597. (let (ind ind1 (pos (point)))
  6598. (org-beginning-of-item)
  6599. (setq ind (org-get-indentation))
  6600. (org-end-of-item)
  6601. (setq ind1 (org-get-indentation))
  6602. (unless (and (org-at-item-p) (= ind ind1))
  6603. (goto-char pos)
  6604. (error "On last item"))))
  6605. (defun org-previous-item ()
  6606. "Move to the beginning of the previous item in the current plain list.
  6607. Error if not at a plain list, or if this is the first item in the list."
  6608. (interactive)
  6609. (let (beg ind ind1 (pos (point)))
  6610. (org-beginning-of-item)
  6611. (setq beg (point))
  6612. (setq ind (org-get-indentation))
  6613. (goto-char beg)
  6614. (catch 'exit
  6615. (while t
  6616. (beginning-of-line 0)
  6617. (if (looking-at "[ \t]*$")
  6618. nil
  6619. (if (<= (setq ind1 (org-get-indentation)) ind)
  6620. (throw 'exit t)))))
  6621. (condition-case nil
  6622. (if (or (not (org-at-item-p))
  6623. (< ind1 (1- ind)))
  6624. (error "")
  6625. (org-beginning-of-item))
  6626. (error (goto-char pos)
  6627. (error "On first item")))))
  6628. (defun org-first-list-item-p ()
  6629. "Is this heading the item in a plain list?"
  6630. (unless (org-at-item-p)
  6631. (error "Not at a plain list item"))
  6632. (org-beginning-of-item)
  6633. (= (point) (save-excursion (org-beginning-of-item-list))))
  6634. (defun org-move-item-down ()
  6635. "Move the plain list item at point down, i.e. swap with following item.
  6636. Subitems (items with larger indentation) are considered part of the item,
  6637. so this really moves item trees."
  6638. (interactive)
  6639. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6640. (org-beginning-of-item)
  6641. (setq beg0 (point))
  6642. (save-excursion
  6643. (setq ne-beg (org-back-over-empty-lines))
  6644. (setq beg (point)))
  6645. (goto-char beg0)
  6646. (setq ind (org-get-indentation))
  6647. (org-end-of-item)
  6648. (setq end0 (point))
  6649. (setq ind1 (org-get-indentation))
  6650. (setq ne-end (org-back-over-empty-lines))
  6651. (setq end (point))
  6652. (goto-char beg0)
  6653. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6654. ;; include less whitespace
  6655. (save-excursion
  6656. (goto-char beg)
  6657. (forward-line (- ne-beg ne-end))
  6658. (setq beg (point))))
  6659. (goto-char end0)
  6660. (if (and (org-at-item-p) (= ind ind1))
  6661. (progn
  6662. (org-end-of-item)
  6663. (org-back-over-empty-lines)
  6664. (setq txt (buffer-substring beg end))
  6665. (save-excursion
  6666. (delete-region beg end))
  6667. (setq pos (point))
  6668. (insert txt)
  6669. (goto-char pos) (org-skip-whitespace)
  6670. (org-maybe-renumber-ordered-list))
  6671. (goto-char pos)
  6672. (error "Cannot move this item further down"))))
  6673. (defun org-move-item-up (arg)
  6674. "Move the plain list item at point up, i.e. swap with previous item.
  6675. Subitems (items with larger indentation) are considered part of the item,
  6676. so this really moves item trees."
  6677. (interactive "p")
  6678. (let (beg beg0 end ind ind1 (pos (point)) txt
  6679. ne-beg ne-ins ins-end)
  6680. (org-beginning-of-item)
  6681. (setq beg0 (point))
  6682. (setq ind (org-get-indentation))
  6683. (save-excursion
  6684. (setq ne-beg (org-back-over-empty-lines))
  6685. (setq beg (point)))
  6686. (goto-char beg0)
  6687. (org-end-of-item)
  6688. (setq end (point))
  6689. (goto-char beg0)
  6690. (catch 'exit
  6691. (while t
  6692. (beginning-of-line 0)
  6693. (if (looking-at "[ \t]*$")
  6694. (if org-empty-line-terminates-plain-lists
  6695. (progn
  6696. (goto-char pos)
  6697. (error "Cannot move this item further up"))
  6698. nil)
  6699. (if (<= (setq ind1 (org-get-indentation)) ind)
  6700. (throw 'exit t)))))
  6701. (condition-case nil
  6702. (org-beginning-of-item)
  6703. (error (goto-char beg)
  6704. (error "Cannot move this item further up")))
  6705. (setq ind1 (org-get-indentation))
  6706. (if (and (org-at-item-p) (= ind ind1))
  6707. (progn
  6708. (setq ne-ins (org-back-over-empty-lines))
  6709. (setq txt (buffer-substring beg end))
  6710. (save-excursion
  6711. (delete-region beg end))
  6712. (setq pos (point))
  6713. (insert txt)
  6714. (setq ins-end (point))
  6715. (goto-char pos) (org-skip-whitespace)
  6716. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6717. ;; Move whitespace back to beginning
  6718. (save-excursion
  6719. (goto-char ins-end)
  6720. (let ((kill-whole-line t))
  6721. (kill-line (- ne-ins ne-beg)) (point)))
  6722. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6723. (org-maybe-renumber-ordered-list))
  6724. (goto-char pos)
  6725. (error "Cannot move this item further up"))))
  6726. (defun org-maybe-renumber-ordered-list ()
  6727. "Renumber the ordered list at point if setup allows it.
  6728. This tests the user option `org-auto-renumber-ordered-lists' before
  6729. doing the renumbering."
  6730. (interactive)
  6731. (when (and org-auto-renumber-ordered-lists
  6732. (org-at-item-p))
  6733. (if (match-beginning 3)
  6734. (org-renumber-ordered-list 1)
  6735. (org-fix-bullet-type))))
  6736. (defun org-maybe-renumber-ordered-list-safe ()
  6737. (condition-case nil
  6738. (save-excursion
  6739. (org-maybe-renumber-ordered-list))
  6740. (error nil)))
  6741. (defun org-cycle-list-bullet (&optional which)
  6742. "Cycle through the different itemize/enumerate bullets.
  6743. This cycle the entire list level through the sequence:
  6744. `-' -> `+' -> `*' -> `1.' -> `1)'
  6745. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6746. 0 meand `-', 1 means `+' etc."
  6747. (interactive "P")
  6748. (org-preserve-lc
  6749. (org-beginning-of-item-list)
  6750. (org-at-item-p)
  6751. (beginning-of-line 1)
  6752. (let ((current (match-string 0))
  6753. (prevp (eq which 'previous))
  6754. new)
  6755. (setq new (cond
  6756. ((and (numberp which)
  6757. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6758. ((string-match "-" current) (if prevp "1)" "+"))
  6759. ((string-match "\\+" current)
  6760. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6761. ((string-match "\\*" current) (if prevp "+" "1."))
  6762. ((string-match "\\." current) (if prevp "*" "1)"))
  6763. ((string-match ")" current) (if prevp "1." "-"))
  6764. (t (error "This should not happen"))))
  6765. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6766. (org-fix-bullet-type)
  6767. (org-maybe-renumber-ordered-list))))
  6768. (defun org-get-string-indentation (s)
  6769. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6770. (let ((n -1) (i 0) (w tab-width) c)
  6771. (catch 'exit
  6772. (while (< (setq n (1+ n)) (length s))
  6773. (setq c (aref s n))
  6774. (cond ((= c ?\ ) (setq i (1+ i)))
  6775. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6776. (t (throw 'exit t)))))
  6777. i))
  6778. (defun org-renumber-ordered-list (arg)
  6779. "Renumber an ordered plain list.
  6780. Cursor needs to be in the first line of an item, the line that starts
  6781. with something like \"1.\" or \"2)\"."
  6782. (interactive "p")
  6783. (unless (and (org-at-item-p)
  6784. (match-beginning 3))
  6785. (error "This is not an ordered list"))
  6786. (let ((line (org-current-line))
  6787. (col (current-column))
  6788. (ind (org-get-string-indentation
  6789. (buffer-substring (point-at-bol) (match-beginning 3))))
  6790. ;; (term (substring (match-string 3) -1))
  6791. ind1 (n (1- arg))
  6792. fmt)
  6793. ;; find where this list begins
  6794. (org-beginning-of-item-list)
  6795. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6796. (setq fmt (concat "%d" (match-string 1)))
  6797. (beginning-of-line 0)
  6798. ;; walk forward and replace these numbers
  6799. (catch 'exit
  6800. (while t
  6801. (catch 'next
  6802. (beginning-of-line 2)
  6803. (if (eobp) (throw 'exit nil))
  6804. (if (looking-at "[ \t]*$") (throw 'next nil))
  6805. (skip-chars-forward " \t") (setq ind1 (current-column))
  6806. (if (> ind1 ind) (throw 'next t))
  6807. (if (< ind1 ind) (throw 'exit t))
  6808. (if (not (org-at-item-p)) (throw 'exit nil))
  6809. (delete-region (match-beginning 2) (match-end 2))
  6810. (goto-char (match-beginning 2))
  6811. (insert (format fmt (setq n (1+ n)))))))
  6812. (goto-line line)
  6813. (move-to-column col)))
  6814. (defun org-fix-bullet-type ()
  6815. "Make sure all items in this list have the same bullet as the firsst item."
  6816. (interactive)
  6817. (unless (org-at-item-p) (error "This is not a list"))
  6818. (let ((line (org-current-line))
  6819. (col (current-column))
  6820. (ind (current-indentation))
  6821. ind1 bullet)
  6822. ;; find where this list begins
  6823. (org-beginning-of-item-list)
  6824. (beginning-of-line 1)
  6825. ;; find out what the bullet type is
  6826. (looking-at "[ \t]*\\(\\S-+\\)")
  6827. (setq bullet (match-string 1))
  6828. ;; walk forward and replace these numbers
  6829. (beginning-of-line 0)
  6830. (catch 'exit
  6831. (while t
  6832. (catch 'next
  6833. (beginning-of-line 2)
  6834. (if (eobp) (throw 'exit nil))
  6835. (if (looking-at "[ \t]*$") (throw 'next nil))
  6836. (skip-chars-forward " \t") (setq ind1 (current-column))
  6837. (if (> ind1 ind) (throw 'next t))
  6838. (if (< ind1 ind) (throw 'exit t))
  6839. (if (not (org-at-item-p)) (throw 'exit nil))
  6840. (skip-chars-forward " \t")
  6841. (looking-at "\\S-+")
  6842. (replace-match bullet))))
  6843. (goto-line line)
  6844. (move-to-column col)
  6845. (if (string-match "[0-9]" bullet)
  6846. (org-renumber-ordered-list 1))))
  6847. (defun org-beginning-of-item-list ()
  6848. "Go to the beginning of the current item list.
  6849. I.e. to the first item in this list."
  6850. (interactive)
  6851. (org-beginning-of-item)
  6852. (let ((pos (point-at-bol))
  6853. (ind (org-get-indentation))
  6854. ind1)
  6855. ;; find where this list begins
  6856. (catch 'exit
  6857. (while t
  6858. (catch 'next
  6859. (beginning-of-line 0)
  6860. (if (looking-at "[ \t]*$")
  6861. (throw (if (bobp) 'exit 'next) t))
  6862. (skip-chars-forward " \t") (setq ind1 (current-column))
  6863. (if (or (< ind1 ind)
  6864. (and (= ind1 ind)
  6865. (not (org-at-item-p)))
  6866. (bobp))
  6867. (throw 'exit t)
  6868. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6869. (goto-char pos)))
  6870. (defun org-end-of-item-list ()
  6871. "Go to the end of the current item list.
  6872. I.e. to the text after the last item."
  6873. (interactive)
  6874. (org-beginning-of-item)
  6875. (let ((pos (point-at-bol))
  6876. (ind (org-get-indentation))
  6877. ind1)
  6878. ;; find where this list begins
  6879. (catch 'exit
  6880. (while t
  6881. (catch 'next
  6882. (beginning-of-line 2)
  6883. (if (looking-at "[ \t]*$")
  6884. (throw (if (eobp) 'exit 'next) t))
  6885. (skip-chars-forward " \t") (setq ind1 (current-column))
  6886. (if (or (< ind1 ind)
  6887. (and (= ind1 ind)
  6888. (not (org-at-item-p)))
  6889. (eobp))
  6890. (progn
  6891. (setq pos (point-at-bol))
  6892. (throw 'exit t))))))
  6893. (goto-char pos)))
  6894. (defvar org-last-indent-begin-marker (make-marker))
  6895. (defvar org-last-indent-end-marker (make-marker))
  6896. (defun org-outdent-item (arg)
  6897. "Outdent a local list item."
  6898. (interactive "p")
  6899. (org-indent-item (- arg)))
  6900. (defun org-indent-item (arg)
  6901. "Indent a local list item."
  6902. (interactive "p")
  6903. (unless (org-at-item-p)
  6904. (error "Not on an item"))
  6905. (save-excursion
  6906. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6907. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6908. (setq beg org-last-indent-begin-marker
  6909. end org-last-indent-end-marker)
  6910. (org-beginning-of-item)
  6911. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6912. (org-end-of-item)
  6913. (setq end (move-marker org-last-indent-end-marker (point))))
  6914. (goto-char beg)
  6915. (setq tmp (org-item-indent-positions)
  6916. ind (car tmp)
  6917. ind-down (nth 2 tmp)
  6918. ind-up (nth 1 tmp)
  6919. delta (if (> arg 0)
  6920. (if ind-down (- ind-down ind) 2)
  6921. (if ind-up (- ind-up ind) -2)))
  6922. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6923. (while (< (point) end)
  6924. (beginning-of-line 1)
  6925. (skip-chars-forward " \t") (setq ind1 (current-column))
  6926. (delete-region (point-at-bol) (point))
  6927. (or (eolp) (indent-to-column (+ ind1 delta)))
  6928. (beginning-of-line 2))))
  6929. (org-fix-bullet-type)
  6930. (org-maybe-renumber-ordered-list-safe)
  6931. (save-excursion
  6932. (beginning-of-line 0)
  6933. (condition-case nil (org-beginning-of-item) (error nil))
  6934. (org-maybe-renumber-ordered-list-safe)))
  6935. (defun org-item-indent-positions ()
  6936. "Return indentation for plain list items.
  6937. This returns a list with three values: The current indentation, the
  6938. parent indentation and the indentation a child should habe.
  6939. Assumes cursor in item line."
  6940. (let* ((bolpos (point-at-bol))
  6941. (ind (org-get-indentation))
  6942. ind-down ind-up pos)
  6943. (save-excursion
  6944. (org-beginning-of-item-list)
  6945. (skip-chars-backward "\n\r \t")
  6946. (when (org-in-item-p)
  6947. (org-beginning-of-item)
  6948. (setq ind-up (org-get-indentation))))
  6949. (setq pos (point))
  6950. (save-excursion
  6951. (cond
  6952. ((and (condition-case nil (progn (org-previous-item) t)
  6953. (error nil))
  6954. (or (forward-char 1) t)
  6955. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6956. (setq ind-down (org-get-indentation)))
  6957. ((and (goto-char pos)
  6958. (org-at-item-p))
  6959. (goto-char (match-end 0))
  6960. (skip-chars-forward " \t")
  6961. (setq ind-down (current-column)))))
  6962. (list ind ind-up ind-down)))
  6963. ;;; The orgstruct minor mode
  6964. ;; Define a minor mode which can be used in other modes in order to
  6965. ;; integrate the org-mode structure editing commands.
  6966. ;; This is really a hack, because the org-mode structure commands use
  6967. ;; keys which normally belong to the major mode. Here is how it
  6968. ;; works: The minor mode defines all the keys necessary to operate the
  6969. ;; structure commands, but wraps the commands into a function which
  6970. ;; tests if the cursor is currently at a headline or a plain list
  6971. ;; item. If that is the case, the structure command is used,
  6972. ;; temporarily setting many Org-mode variables like regular
  6973. ;; expressions for filling etc. However, when any of those keys is
  6974. ;; used at a different location, function uses `key-binding' to look
  6975. ;; up if the key has an associated command in another currently active
  6976. ;; keymap (minor modes, major mode, global), and executes that
  6977. ;; command. There might be problems if any of the keys is otherwise
  6978. ;; used as a prefix key.
  6979. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6980. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6981. ;; addresses this by checking explicitly for both bindings.
  6982. (defvar orgstruct-mode-map (make-sparse-keymap)
  6983. "Keymap for the minor `orgstruct-mode'.")
  6984. (defvar org-local-vars nil
  6985. "List of local variables, for use by `orgstruct-mode'")
  6986. ;;;###autoload
  6987. (define-minor-mode orgstruct-mode
  6988. "Toggle the minor more `orgstruct-mode'.
  6989. This mode is for using Org-mode structure commands in other modes.
  6990. The following key behave as if Org-mode was active, if the cursor
  6991. is on a headline, or on a plain list item (both in the definition
  6992. of Org-mode).
  6993. M-up Move entry/item up
  6994. M-down Move entry/item down
  6995. M-left Promote
  6996. M-right Demote
  6997. M-S-up Move entry/item up
  6998. M-S-down Move entry/item down
  6999. M-S-left Promote subtree
  7000. M-S-right Demote subtree
  7001. M-q Fill paragraph and items like in Org-mode
  7002. C-c ^ Sort entries
  7003. C-c - Cycle list bullet
  7004. TAB Cycle item visibility
  7005. M-RET Insert new heading/item
  7006. S-M-RET Insert new TODO heading / Chekbox item
  7007. C-c C-c Set tags / toggle checkbox"
  7008. nil " OrgStruct" nil
  7009. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7010. ;;;###autoload
  7011. (defun turn-on-orgstruct ()
  7012. "Unconditionally turn on `orgstruct-mode'."
  7013. (orgstruct-mode 1))
  7014. ;;;###autoload
  7015. (defun turn-on-orgstruct++ ()
  7016. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7017. In addition to setting orgstruct-mode, this also exports all indentation and
  7018. autofilling variables from org-mode into the buffer. Note that turning
  7019. off orgstruct-mode will *not* remove these additional settings."
  7020. (orgstruct-mode 1)
  7021. (let (var val)
  7022. (mapc
  7023. (lambda (x)
  7024. (when (string-match
  7025. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7026. (symbol-name (car x)))
  7027. (setq var (car x) val (nth 1 x))
  7028. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7029. org-local-vars)))
  7030. (defun orgstruct-error ()
  7031. "Error when there is no default binding for a structure key."
  7032. (interactive)
  7033. (error "This key has no function outside structure elements"))
  7034. (defun orgstruct-setup ()
  7035. "Setup orgstruct keymaps."
  7036. (let ((nfunc 0)
  7037. (bindings
  7038. (list
  7039. '([(meta up)] org-metaup)
  7040. '([(meta down)] org-metadown)
  7041. '([(meta left)] org-metaleft)
  7042. '([(meta right)] org-metaright)
  7043. '([(meta shift up)] org-shiftmetaup)
  7044. '([(meta shift down)] org-shiftmetadown)
  7045. '([(meta shift left)] org-shiftmetaleft)
  7046. '([(meta shift right)] org-shiftmetaright)
  7047. '([(shift up)] org-shiftup)
  7048. '([(shift down)] org-shiftdown)
  7049. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7050. '("\M-q" fill-paragraph)
  7051. '("\C-c^" org-sort)
  7052. '("\C-c-" org-cycle-list-bullet)))
  7053. elt key fun cmd)
  7054. (while (setq elt (pop bindings))
  7055. (setq nfunc (1+ nfunc))
  7056. (setq key (org-key (car elt))
  7057. fun (nth 1 elt)
  7058. cmd (orgstruct-make-binding fun nfunc key))
  7059. (org-defkey orgstruct-mode-map key cmd))
  7060. ;; Special treatment needed for TAB and RET
  7061. (org-defkey orgstruct-mode-map [(tab)]
  7062. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7063. (org-defkey orgstruct-mode-map "\C-i"
  7064. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7065. (org-defkey orgstruct-mode-map "\M-\C-m"
  7066. (orgstruct-make-binding 'org-insert-heading 105
  7067. "\M-\C-m" [(meta return)]))
  7068. (org-defkey orgstruct-mode-map [(meta return)]
  7069. (orgstruct-make-binding 'org-insert-heading 106
  7070. [(meta return)] "\M-\C-m"))
  7071. (org-defkey orgstruct-mode-map [(shift meta return)]
  7072. (orgstruct-make-binding 'org-insert-todo-heading 107
  7073. [(meta return)] "\M-\C-m"))
  7074. (unless org-local-vars
  7075. (setq org-local-vars (org-get-local-variables)))
  7076. t))
  7077. (defun orgstruct-make-binding (fun n &rest keys)
  7078. "Create a function for binding in the structure minor mode.
  7079. FUN is the command to call inside a table. N is used to create a unique
  7080. command name. KEYS are keys that should be checked in for a command
  7081. to execute outside of tables."
  7082. (eval
  7083. (list 'defun
  7084. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7085. '(arg)
  7086. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7087. "Outside of structure, run the binding of `"
  7088. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7089. "'.")
  7090. '(interactive "p")
  7091. (list 'if
  7092. '(org-context-p 'headline 'item)
  7093. (list 'org-run-like-in-org-mode (list 'quote fun))
  7094. (list 'let '(orgstruct-mode)
  7095. (list 'call-interactively
  7096. (append '(or)
  7097. (mapcar (lambda (k)
  7098. (list 'key-binding k))
  7099. keys)
  7100. '('orgstruct-error))))))))
  7101. (defun org-context-p (&rest contexts)
  7102. "Check if local context is and of CONTEXTS.
  7103. Possible values in the list of contexts are `table', `headline', and `item'."
  7104. (let ((pos (point)))
  7105. (goto-char (point-at-bol))
  7106. (prog1 (or (and (memq 'table contexts)
  7107. (looking-at "[ \t]*|"))
  7108. (and (memq 'headline contexts)
  7109. (looking-at "\\*+"))
  7110. (and (memq 'item contexts)
  7111. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7112. (goto-char pos))))
  7113. (defun org-get-local-variables ()
  7114. "Return a list of all local variables in an org-mode buffer."
  7115. (let (varlist)
  7116. (with-current-buffer (get-buffer-create "*Org tmp*")
  7117. (erase-buffer)
  7118. (org-mode)
  7119. (setq varlist (buffer-local-variables)))
  7120. (kill-buffer "*Org tmp*")
  7121. (delq nil
  7122. (mapcar
  7123. (lambda (x)
  7124. (setq x
  7125. (if (symbolp x)
  7126. (list x)
  7127. (list (car x) (list 'quote (cdr x)))))
  7128. (if (string-match
  7129. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7130. (symbol-name (car x)))
  7131. x nil))
  7132. varlist))))
  7133. ;;;###autoload
  7134. (defun org-run-like-in-org-mode (cmd)
  7135. (unless org-local-vars
  7136. (setq org-local-vars (org-get-local-variables)))
  7137. (eval (list 'let org-local-vars
  7138. (list 'call-interactively (list 'quote cmd)))))
  7139. ;;;; Archiving
  7140. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7141. (defun org-archive-subtree (&optional find-done)
  7142. "Move the current subtree to the archive.
  7143. The archive can be a certain top-level heading in the current file, or in
  7144. a different file. The tree will be moved to that location, the subtree
  7145. heading be marked DONE, and the current time will be added.
  7146. When called with prefix argument FIND-DONE, find whole trees without any
  7147. open TODO items and archive them (after getting confirmation from the user).
  7148. If the cursor is not at a headline when this comand is called, try all level
  7149. 1 trees. If the cursor is on a headline, only try the direct children of
  7150. this heading."
  7151. (interactive "P")
  7152. (if find-done
  7153. (org-archive-all-done)
  7154. ;; Save all relevant TODO keyword-relatex variables
  7155. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7156. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7157. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7158. (tr-org-done-keywords org-done-keywords)
  7159. (tr-org-todo-regexp org-todo-regexp)
  7160. (tr-org-todo-line-regexp org-todo-line-regexp)
  7161. (tr-org-odd-levels-only org-odd-levels-only)
  7162. (this-buffer (current-buffer))
  7163. (org-archive-location org-archive-location)
  7164. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7165. ;; start of variables that will be used for saving context
  7166. ;; The compiler complains about them - keep them anyway!
  7167. (file (abbreviate-file-name (buffer-file-name)))
  7168. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7169. (time (format-time-string
  7170. (substring (cdr org-time-stamp-formats) 1 -1)
  7171. (current-time)))
  7172. afile heading buffer level newfile-p
  7173. category todo priority
  7174. ;; start of variables that will be used for savind context
  7175. ltags itags prop)
  7176. ;; Try to find a local archive location
  7177. (save-excursion
  7178. (save-restriction
  7179. (widen)
  7180. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7181. (if (and prop (string-match "\\S-" prop))
  7182. (setq org-archive-location prop)
  7183. (if (or (re-search-backward re nil t)
  7184. (re-search-forward re nil t))
  7185. (setq org-archive-location (match-string 1))))))
  7186. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7187. (progn
  7188. (setq afile (format (match-string 1 org-archive-location)
  7189. (file-name-nondirectory buffer-file-name))
  7190. heading (match-string 2 org-archive-location)))
  7191. (error "Invalid `org-archive-location'"))
  7192. (if (> (length afile) 0)
  7193. (setq newfile-p (not (file-exists-p afile))
  7194. buffer (find-file-noselect afile))
  7195. (setq buffer (current-buffer)))
  7196. (unless buffer
  7197. (error "Cannot access file \"%s\"" afile))
  7198. (if (and (> (length heading) 0)
  7199. (string-match "^\\*+" heading))
  7200. (setq level (match-end 0))
  7201. (setq heading nil level 0))
  7202. (save-excursion
  7203. (org-back-to-heading t)
  7204. ;; Get context information that will be lost by moving the tree
  7205. (org-refresh-category-properties)
  7206. (setq category (org-get-category)
  7207. todo (and (looking-at org-todo-line-regexp)
  7208. (match-string 2))
  7209. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7210. ltags (org-get-tags)
  7211. itags (org-delete-all ltags (org-get-tags-at)))
  7212. (setq ltags (mapconcat 'identity ltags " ")
  7213. itags (mapconcat 'identity itags " "))
  7214. ;; We first only copy, in case something goes wrong
  7215. ;; we need to protect this-command, to avoid kill-region sets it,
  7216. ;; which would lead to duplication of subtrees
  7217. (let (this-command) (org-copy-subtree))
  7218. (set-buffer buffer)
  7219. ;; Enforce org-mode for the archive buffer
  7220. (if (not (org-mode-p))
  7221. ;; Force the mode for future visits.
  7222. (let ((org-insert-mode-line-in-empty-file t)
  7223. (org-inhibit-startup t))
  7224. (call-interactively 'org-mode)))
  7225. (when newfile-p
  7226. (goto-char (point-max))
  7227. (insert (format "\nArchived entries from file %s\n\n"
  7228. (buffer-file-name this-buffer))))
  7229. ;; Force the TODO keywords of the original buffer
  7230. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7231. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7232. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7233. (org-done-keywords tr-org-done-keywords)
  7234. (org-todo-regexp tr-org-todo-regexp)
  7235. (org-todo-line-regexp tr-org-todo-line-regexp)
  7236. (org-odd-levels-only
  7237. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7238. org-odd-levels-only
  7239. tr-org-odd-levels-only)))
  7240. (goto-char (point-min))
  7241. (show-all)
  7242. (if heading
  7243. (progn
  7244. (if (re-search-forward
  7245. (concat "^" (regexp-quote heading)
  7246. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7247. nil t)
  7248. (goto-char (match-end 0))
  7249. ;; Heading not found, just insert it at the end
  7250. (goto-char (point-max))
  7251. (or (bolp) (insert "\n"))
  7252. (insert "\n" heading "\n")
  7253. (end-of-line 0))
  7254. ;; Make the subtree visible
  7255. (show-subtree)
  7256. (org-end-of-subtree t)
  7257. (skip-chars-backward " \t\r\n")
  7258. (and (looking-at "[ \t\r\n]*")
  7259. (replace-match "\n\n")))
  7260. ;; No specific heading, just go to end of file.
  7261. (goto-char (point-max)) (insert "\n"))
  7262. ;; Paste
  7263. (org-paste-subtree (org-get-legal-level level 1))
  7264. ;; Mark the entry as done
  7265. (when (and org-archive-mark-done
  7266. (looking-at org-todo-line-regexp)
  7267. (or (not (match-end 2))
  7268. (not (member (match-string 2) org-done-keywords))))
  7269. (let (org-log-done org-todo-log-states)
  7270. (org-todo
  7271. (car (or (member org-archive-mark-done org-done-keywords)
  7272. org-done-keywords)))))
  7273. ;; Add the context info
  7274. (when org-archive-save-context-info
  7275. (let ((l org-archive-save-context-info) e n v)
  7276. (while (setq e (pop l))
  7277. (when (and (setq v (symbol-value e))
  7278. (stringp v) (string-match "\\S-" v))
  7279. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7280. (org-entry-put (point) n v)))))
  7281. ;; Save and kill the buffer, if it is not the same buffer.
  7282. (if (not (eq this-buffer buffer))
  7283. (progn (save-buffer) (kill-buffer buffer)))))
  7284. ;; Here we are back in the original buffer. Everything seems to have
  7285. ;; worked. So now cut the tree and finish up.
  7286. (let (this-command) (org-cut-subtree))
  7287. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7288. (message "Subtree archived %s"
  7289. (if (eq this-buffer buffer)
  7290. (concat "under heading: " heading)
  7291. (concat "in file: " (abbreviate-file-name afile)))))))
  7292. (defun org-refresh-category-properties ()
  7293. "Refresh category text properties in teh buffer."
  7294. (let ((def-cat (cond
  7295. ((null org-category)
  7296. (if buffer-file-name
  7297. (file-name-sans-extension
  7298. (file-name-nondirectory buffer-file-name))
  7299. "???"))
  7300. ((symbolp org-category) (symbol-name org-category))
  7301. (t org-category)))
  7302. beg end cat pos optionp)
  7303. (org-unmodified
  7304. (save-excursion
  7305. (save-restriction
  7306. (widen)
  7307. (goto-char (point-min))
  7308. (put-text-property (point) (point-max) 'org-category def-cat)
  7309. (while (re-search-forward
  7310. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7311. (setq pos (match-end 0)
  7312. optionp (equal (char-after (match-beginning 0)) ?#)
  7313. cat (org-trim (match-string 2)))
  7314. (if optionp
  7315. (setq beg (point-at-bol) end (point-max))
  7316. (org-back-to-heading t)
  7317. (setq beg (point) end (org-end-of-subtree t t)))
  7318. (put-text-property beg end 'org-category cat)
  7319. (goto-char pos)))))))
  7320. (defun org-archive-all-done (&optional tag)
  7321. "Archive sublevels of the current tree without open TODO items.
  7322. If the cursor is not on a headline, try all level 1 trees. If
  7323. it is on a headline, try all direct children.
  7324. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7325. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7326. (rea (concat ".*:" org-archive-tag ":"))
  7327. (begm (make-marker))
  7328. (endm (make-marker))
  7329. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7330. "Move subtree to archive (no open TODO items)? "))
  7331. beg end (cntarch 0))
  7332. (if (org-on-heading-p)
  7333. (progn
  7334. (setq re1 (concat "^" (regexp-quote
  7335. (make-string
  7336. (1+ (- (match-end 0) (match-beginning 0) 1))
  7337. ?*))
  7338. " "))
  7339. (move-marker begm (point))
  7340. (move-marker endm (org-end-of-subtree t)))
  7341. (setq re1 "^* ")
  7342. (move-marker begm (point-min))
  7343. (move-marker endm (point-max)))
  7344. (save-excursion
  7345. (goto-char begm)
  7346. (while (re-search-forward re1 endm t)
  7347. (setq beg (match-beginning 0)
  7348. end (save-excursion (org-end-of-subtree t) (point)))
  7349. (goto-char beg)
  7350. (if (re-search-forward re end t)
  7351. (goto-char end)
  7352. (goto-char beg)
  7353. (if (and (or (not tag) (not (looking-at rea)))
  7354. (y-or-n-p question))
  7355. (progn
  7356. (if tag
  7357. (org-toggle-tag org-archive-tag 'on)
  7358. (org-archive-subtree))
  7359. (setq cntarch (1+ cntarch)))
  7360. (goto-char end)))))
  7361. (message "%d trees archived" cntarch)))
  7362. (defun org-cycle-hide-drawers (state)
  7363. "Re-hide all drawers after a visibility state change."
  7364. (when (and (org-mode-p)
  7365. (not (memq state '(overview folded))))
  7366. (save-excursion
  7367. (let* ((globalp (memq state '(contents all)))
  7368. (beg (if globalp (point-min) (point)))
  7369. (end (if globalp (point-max) (org-end-of-subtree t))))
  7370. (goto-char beg)
  7371. (while (re-search-forward org-drawer-regexp end t)
  7372. (org-flag-drawer t))))))
  7373. (defun org-flag-drawer (flag)
  7374. (save-excursion
  7375. (beginning-of-line 1)
  7376. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7377. (let ((b (match-end 0))
  7378. (outline-regexp org-outline-regexp))
  7379. (if (re-search-forward
  7380. "^[ \t]*:END:"
  7381. (save-excursion (outline-next-heading) (point)) t)
  7382. (outline-flag-region b (point-at-eol) flag)
  7383. (error ":END: line missing"))))))
  7384. (defun org-cycle-hide-archived-subtrees (state)
  7385. "Re-hide all archived subtrees after a visibility state change."
  7386. (when (and (not org-cycle-open-archived-trees)
  7387. (not (memq state '(overview folded))))
  7388. (save-excursion
  7389. (let* ((globalp (memq state '(contents all)))
  7390. (beg (if globalp (point-min) (point)))
  7391. (end (if globalp (point-max) (org-end-of-subtree t))))
  7392. (org-hide-archived-subtrees beg end)
  7393. (goto-char beg)
  7394. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7395. (message "%s" (substitute-command-keys
  7396. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7397. (defun org-force-cycle-archived ()
  7398. "Cycle subtree even if it is archived."
  7399. (interactive)
  7400. (setq this-command 'org-cycle)
  7401. (let ((org-cycle-open-archived-trees t))
  7402. (call-interactively 'org-cycle)))
  7403. (defun org-hide-archived-subtrees (beg end)
  7404. "Re-hide all archived subtrees after a visibility state change."
  7405. (save-excursion
  7406. (let* ((re (concat ":" org-archive-tag ":")))
  7407. (goto-char beg)
  7408. (while (re-search-forward re end t)
  7409. (and (org-on-heading-p) (hide-subtree))
  7410. (org-end-of-subtree t)))))
  7411. (defun org-toggle-tag (tag &optional onoff)
  7412. "Toggle the tag TAG for the current line.
  7413. If ONOFF is `on' or `off', don't toggle but set to this state."
  7414. (unless (org-on-heading-p t) (error "Not on headling"))
  7415. (let (res current)
  7416. (save-excursion
  7417. (beginning-of-line)
  7418. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7419. (point-at-eol) t)
  7420. (progn
  7421. (setq current (match-string 1))
  7422. (replace-match ""))
  7423. (setq current ""))
  7424. (setq current (nreverse (org-split-string current ":")))
  7425. (cond
  7426. ((eq onoff 'on)
  7427. (setq res t)
  7428. (or (member tag current) (push tag current)))
  7429. ((eq onoff 'off)
  7430. (or (not (member tag current)) (setq current (delete tag current))))
  7431. (t (if (member tag current)
  7432. (setq current (delete tag current))
  7433. (setq res t)
  7434. (push tag current))))
  7435. (end-of-line 1)
  7436. (if current
  7437. (progn
  7438. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7439. (org-set-tags nil t))
  7440. (delete-horizontal-space))
  7441. (run-hooks 'org-after-tags-change-hook))
  7442. res))
  7443. (defun org-toggle-archive-tag (&optional arg)
  7444. "Toggle the archive tag for the current headline.
  7445. With prefix ARG, check all children of current headline and offer tagging
  7446. the children that do not contain any open TODO items."
  7447. (interactive "P")
  7448. (if arg
  7449. (org-archive-all-done 'tag)
  7450. (let (set)
  7451. (save-excursion
  7452. (org-back-to-heading t)
  7453. (setq set (org-toggle-tag org-archive-tag))
  7454. (when set (hide-subtree)))
  7455. (and set (beginning-of-line 1))
  7456. (message "Subtree %s" (if set "archived" "unarchived")))))
  7457. ;;;; Tables
  7458. ;;; The table editor
  7459. ;; Watch out: Here we are talking about two different kind of tables.
  7460. ;; Most of the code is for the tables created with the Org-mode table editor.
  7461. ;; Sometimes, we talk about tables created and edited with the table.el
  7462. ;; Emacs package. We call the former org-type tables, and the latter
  7463. ;; table.el-type tables.
  7464. (defun org-before-change-function (beg end)
  7465. "Every change indicates that a table might need an update."
  7466. (setq org-table-may-need-update t))
  7467. (defconst org-table-line-regexp "^[ \t]*|"
  7468. "Detects an org-type table line.")
  7469. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7470. "Detects an org-type table line.")
  7471. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7472. "Detects a table line marked for automatic recalculation.")
  7473. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7474. "Detects a table line marked for automatic recalculation.")
  7475. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7476. "Detects a table line marked for automatic recalculation.")
  7477. (defconst org-table-hline-regexp "^[ \t]*|-"
  7478. "Detects an org-type table hline.")
  7479. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7480. "Detects a table-type table hline.")
  7481. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7482. "Detects an org-type or table-type table.")
  7483. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7484. "Searching from within a table (any type) this finds the first line
  7485. outside the table.")
  7486. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7487. "Searching from within a table (any type) this finds the first line
  7488. outside the table.")
  7489. (defvar org-table-last-highlighted-reference nil)
  7490. (defvar org-table-formula-history nil)
  7491. (defvar org-table-column-names nil
  7492. "Alist with column names, derived from the `!' line.")
  7493. (defvar org-table-column-name-regexp nil
  7494. "Regular expression matching the current column names.")
  7495. (defvar org-table-local-parameters nil
  7496. "Alist with parameter names, derived from the `$' line.")
  7497. (defvar org-table-named-field-locations nil
  7498. "Alist with locations of named fields.")
  7499. (defvar org-table-current-line-types nil
  7500. "Table row types, non-nil only for the duration of a comand.")
  7501. (defvar org-table-current-begin-line nil
  7502. "Table begin line, non-nil only for the duration of a comand.")
  7503. (defvar org-table-current-begin-pos nil
  7504. "Table begin position, non-nil only for the duration of a comand.")
  7505. (defvar org-table-dlines nil
  7506. "Vector of data line line numbers in the current table.")
  7507. (defvar org-table-hlines nil
  7508. "Vector of hline line numbers in the current table.")
  7509. (defconst org-table-range-regexp
  7510. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7511. ;; 1 2 3 4 5
  7512. "Regular expression for matching ranges in formulas.")
  7513. (defconst org-table-range-regexp2
  7514. (concat
  7515. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7516. "\\.\\."
  7517. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7518. "Match a range for reference display.")
  7519. (defconst org-table-translate-regexp
  7520. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7521. "Match a reference that needs translation, for reference display.")
  7522. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7523. (defun org-table-create-with-table.el ()
  7524. "Use the table.el package to insert a new table.
  7525. If there is already a table at point, convert between Org-mode tables
  7526. and table.el tables."
  7527. (interactive)
  7528. (require 'table)
  7529. (cond
  7530. ((org-at-table.el-p)
  7531. (if (y-or-n-p "Convert table to Org-mode table? ")
  7532. (org-table-convert)))
  7533. ((org-at-table-p)
  7534. (if (y-or-n-p "Convert table to table.el table? ")
  7535. (org-table-convert)))
  7536. (t (call-interactively 'table-insert))))
  7537. (defun org-table-create-or-convert-from-region (arg)
  7538. "Convert region to table, or create an empty table.
  7539. If there is an active region, convert it to a table, using the function
  7540. `org-table-convert-region'. See the documentation of that function
  7541. to learn how the prefix argument is interpreted to determine the field
  7542. separator.
  7543. If there is no such region, create an empty table with `org-table-create'."
  7544. (interactive "P")
  7545. (if (org-region-active-p)
  7546. (org-table-convert-region (region-beginning) (region-end) arg)
  7547. (org-table-create arg)))
  7548. (defun org-table-create (&optional size)
  7549. "Query for a size and insert a table skeleton.
  7550. SIZE is a string Columns x Rows like for example \"3x2\"."
  7551. (interactive "P")
  7552. (unless size
  7553. (setq size (read-string
  7554. (concat "Table size Columns x Rows [e.g. "
  7555. org-table-default-size "]: ")
  7556. "" nil org-table-default-size)))
  7557. (let* ((pos (point))
  7558. (indent (make-string (current-column) ?\ ))
  7559. (split (org-split-string size " *x *"))
  7560. (rows (string-to-number (nth 1 split)))
  7561. (columns (string-to-number (car split)))
  7562. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7563. "\n")))
  7564. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7565. (point-at-bol) (point)))
  7566. (beginning-of-line 1)
  7567. (newline))
  7568. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7569. (dotimes (i rows) (insert line))
  7570. (goto-char pos)
  7571. (if (> rows 1)
  7572. ;; Insert a hline after the first row.
  7573. (progn
  7574. (end-of-line 1)
  7575. (insert "\n|-")
  7576. (goto-char pos)))
  7577. (org-table-align)))
  7578. (defun org-table-convert-region (beg0 end0 &optional separator)
  7579. "Convert region to a table.
  7580. The region goes from BEG0 to END0, but these borders will be moved
  7581. slightly, to make sure a beginning of line in the first line is included.
  7582. SEPARATOR specifies the field separator in the lines. It can have the
  7583. following values:
  7584. '(4) Use the comma as a field separator
  7585. '(16) Use a TAB as field separator
  7586. integer When a number, use that many spaces as field separator
  7587. nil When nil, the command tries to be smart and figure out the
  7588. separator in the following way:
  7589. - when each line contains a TAB, assume TAB-separated material
  7590. - when each line contains a comme, assume CSV material
  7591. - else, assume one or more SPACE charcters as separator."
  7592. (interactive "rP")
  7593. (let* ((beg (min beg0 end0))
  7594. (end (max beg0 end0))
  7595. re)
  7596. (goto-char beg)
  7597. (beginning-of-line 1)
  7598. (setq beg (move-marker (make-marker) (point)))
  7599. (goto-char end)
  7600. (if (bolp) (backward-char 1) (end-of-line 1))
  7601. (setq end (move-marker (make-marker) (point)))
  7602. ;; Get the right field separator
  7603. (unless separator
  7604. (goto-char beg)
  7605. (setq separator
  7606. (cond
  7607. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7608. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7609. (t 1))))
  7610. (setq re (cond
  7611. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7612. ((equal separator '(16)) "^\\|\t")
  7613. ((integerp separator)
  7614. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7615. (t (error "This should not happen"))))
  7616. (goto-char beg)
  7617. (while (re-search-forward re end t)
  7618. (replace-match "| " t t))
  7619. (goto-char beg)
  7620. (insert " ")
  7621. (org-table-align)))
  7622. (defun org-table-import (file arg)
  7623. "Import FILE as a table.
  7624. The file is assumed to be tab-separated. Such files can be produced by most
  7625. spreadsheet and database applications. If no tabs (at least one per line)
  7626. are found, lines will be split on whitespace into fields."
  7627. (interactive "f\nP")
  7628. (or (bolp) (newline))
  7629. (let ((beg (point))
  7630. (pm (point-max)))
  7631. (insert-file-contents file)
  7632. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7633. (defun org-table-export ()
  7634. "Export table as a tab-separated file.
  7635. Such a file can be imported into a spreadsheet program like Excel."
  7636. (interactive)
  7637. (let* ((beg (org-table-begin))
  7638. (end (org-table-end))
  7639. (table (buffer-substring beg end))
  7640. (file (read-file-name "Export table to: "))
  7641. buf)
  7642. (unless (or (not (file-exists-p file))
  7643. (y-or-n-p (format "Overwrite file %s? " file)))
  7644. (error "Abort"))
  7645. (with-current-buffer (find-file-noselect file)
  7646. (setq buf (current-buffer))
  7647. (erase-buffer)
  7648. (fundamental-mode)
  7649. (insert table)
  7650. (goto-char (point-min))
  7651. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7652. (replace-match "" t t)
  7653. (end-of-line 1))
  7654. (goto-char (point-min))
  7655. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7656. (replace-match "" t t)
  7657. (goto-char (min (1+ (point)) (point-max))))
  7658. (goto-char (point-min))
  7659. (while (re-search-forward "^-[-+]*$" nil t)
  7660. (replace-match "")
  7661. (if (looking-at "\n")
  7662. (delete-char 1)))
  7663. (goto-char (point-min))
  7664. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7665. (replace-match "\t" t t))
  7666. (save-buffer))
  7667. (kill-buffer buf)))
  7668. (defvar org-table-aligned-begin-marker (make-marker)
  7669. "Marker at the beginning of the table last aligned.
  7670. Used to check if cursor still is in that table, to minimize realignment.")
  7671. (defvar org-table-aligned-end-marker (make-marker)
  7672. "Marker at the end of the table last aligned.
  7673. Used to check if cursor still is in that table, to minimize realignment.")
  7674. (defvar org-table-last-alignment nil
  7675. "List of flags for flushright alignment, from the last re-alignment.
  7676. This is being used to correctly align a single field after TAB or RET.")
  7677. (defvar org-table-last-column-widths nil
  7678. "List of max width of fields in each column.
  7679. This is being used to correctly align a single field after TAB or RET.")
  7680. (defvar org-table-overlay-coordinates nil
  7681. "Overlay coordinates after each align of a table.")
  7682. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7683. (defvar org-last-recalc-line nil)
  7684. (defconst org-narrow-column-arrow "=>"
  7685. "Used as display property in narrowed table columns.")
  7686. (defun org-table-align ()
  7687. "Align the table at point by aligning all vertical bars."
  7688. (interactive)
  7689. (let* (
  7690. ;; Limits of table
  7691. (beg (org-table-begin))
  7692. (end (org-table-end))
  7693. ;; Current cursor position
  7694. (linepos (org-current-line))
  7695. (colpos (org-table-current-column))
  7696. (winstart (window-start))
  7697. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7698. lines (new "") lengths l typenums ty fields maxfields i
  7699. column
  7700. (indent "") cnt frac
  7701. rfmt hfmt
  7702. (spaces '(1 . 1))
  7703. (sp1 (car spaces))
  7704. (sp2 (cdr spaces))
  7705. (rfmt1 (concat
  7706. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7707. (hfmt1 (concat
  7708. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7709. emptystrings links dates emph narrow fmax f1 len c e)
  7710. (untabify beg end)
  7711. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7712. ;; Check if we have links or dates
  7713. (goto-char beg)
  7714. (setq links (re-search-forward org-bracket-link-regexp end t))
  7715. (goto-char beg)
  7716. (setq emph (and org-hide-emphasis-markers
  7717. (re-search-forward org-emph-re end t)))
  7718. (goto-char beg)
  7719. (setq dates (and org-display-custom-times
  7720. (re-search-forward org-ts-regexp-both end t)))
  7721. ;; Make sure the link properties are right
  7722. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7723. ;; Make sure the date properties are right
  7724. (when dates (goto-char beg) (while (org-activate-dates end)))
  7725. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7726. ;; Check if we are narrowing any columns
  7727. (goto-char beg)
  7728. (setq narrow (and org-format-transports-properties-p
  7729. (re-search-forward "<[0-9]+>" end t)))
  7730. ;; Get the rows
  7731. (setq lines (org-split-string
  7732. (buffer-substring beg end) "\n"))
  7733. ;; Store the indentation of the first line
  7734. (if (string-match "^ *" (car lines))
  7735. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7736. ;; Mark the hlines by setting the corresponding element to nil
  7737. ;; At the same time, we remove trailing space.
  7738. (setq lines (mapcar (lambda (l)
  7739. (if (string-match "^ *|-" l)
  7740. nil
  7741. (if (string-match "[ \t]+$" l)
  7742. (substring l 0 (match-beginning 0))
  7743. l)))
  7744. lines))
  7745. ;; Get the data fields by splitting the lines.
  7746. (setq fields (mapcar
  7747. (lambda (l)
  7748. (org-split-string l " *| *"))
  7749. (delq nil (copy-sequence lines))))
  7750. ;; How many fields in the longest line?
  7751. (condition-case nil
  7752. (setq maxfields (apply 'max (mapcar 'length fields)))
  7753. (error
  7754. (kill-region beg end)
  7755. (org-table-create org-table-default-size)
  7756. (error "Empty table - created default table")))
  7757. ;; A list of empty strings to fill any short rows on output
  7758. (setq emptystrings (make-list maxfields ""))
  7759. ;; Check for special formatting.
  7760. (setq i -1)
  7761. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7762. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7763. ;; Check if there is an explicit width specified
  7764. (when narrow
  7765. (setq c column fmax nil)
  7766. (while c
  7767. (setq e (pop c))
  7768. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7769. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7770. ;; Find fields that are wider than fmax, and shorten them
  7771. (when fmax
  7772. (loop for xx in column do
  7773. (when (and (stringp xx)
  7774. (> (org-string-width xx) fmax))
  7775. (org-add-props xx nil
  7776. 'help-echo
  7777. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7778. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7779. (unless (> f1 1)
  7780. (error "Cannot narrow field starting with wide link \"%s\""
  7781. (match-string 0 xx)))
  7782. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7783. (add-text-properties (- f1 2) f1
  7784. (list 'display org-narrow-column-arrow)
  7785. xx)))))
  7786. ;; Get the maximum width for each column
  7787. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7788. ;; Get the fraction of numbers, to decide about alignment of the column
  7789. (setq cnt 0 frac 0.0)
  7790. (loop for x in column do
  7791. (if (equal x "")
  7792. nil
  7793. (setq frac ( / (+ (* frac cnt)
  7794. (if (string-match org-table-number-regexp x) 1 0))
  7795. (setq cnt (1+ cnt))))))
  7796. (push (>= frac org-table-number-fraction) typenums))
  7797. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7798. ;; Store the alignment of this table, for later editing of single fields
  7799. (setq org-table-last-alignment typenums
  7800. org-table-last-column-widths lengths)
  7801. ;; With invisible characters, `format' does not get the field width right
  7802. ;; So we need to make these fields wide by hand.
  7803. (when (or links emph)
  7804. (loop for i from 0 upto (1- maxfields) do
  7805. (setq len (nth i lengths))
  7806. (loop for j from 0 upto (1- (length fields)) do
  7807. (setq c (nthcdr i (car (nthcdr j fields))))
  7808. (if (and (stringp (car c))
  7809. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7810. ; (string-match org-bracket-link-regexp (car c))
  7811. (< (org-string-width (car c)) len))
  7812. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7813. ;; Compute the formats needed for output of the table
  7814. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7815. (while (setq l (pop lengths))
  7816. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7817. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7818. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7819. (setq rfmt (concat rfmt "\n")
  7820. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7821. (setq new (mapconcat
  7822. (lambda (l)
  7823. (if l (apply 'format rfmt
  7824. (append (pop fields) emptystrings))
  7825. hfmt))
  7826. lines ""))
  7827. ;; Replace the old one
  7828. (delete-region beg end)
  7829. (move-marker end nil)
  7830. (move-marker org-table-aligned-begin-marker (point))
  7831. (insert new)
  7832. (move-marker org-table-aligned-end-marker (point))
  7833. (when (and orgtbl-mode (not (org-mode-p)))
  7834. (goto-char org-table-aligned-begin-marker)
  7835. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7836. ;; Try to move to the old location
  7837. (goto-line winstartline)
  7838. (setq winstart (point-at-bol))
  7839. (goto-line linepos)
  7840. (set-window-start (selected-window) winstart 'noforce)
  7841. (org-table-goto-column colpos)
  7842. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7843. (setq org-table-may-need-update nil)
  7844. ))
  7845. (defun org-string-width (s)
  7846. "Compute width of string, ignoring invisible characters.
  7847. This ignores character with invisibility property `org-link', and also
  7848. characters with property `org-cwidth', because these will become invisible
  7849. upon the next fontification round."
  7850. (let (b l)
  7851. (when (or (eq t buffer-invisibility-spec)
  7852. (assq 'org-link buffer-invisibility-spec))
  7853. (while (setq b (text-property-any 0 (length s)
  7854. 'invisible 'org-link s))
  7855. (setq s (concat (substring s 0 b)
  7856. (substring s (or (next-single-property-change
  7857. b 'invisible s) (length s)))))))
  7858. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7859. (setq s (concat (substring s 0 b)
  7860. (substring s (or (next-single-property-change
  7861. b 'org-cwidth s) (length s))))))
  7862. (setq l (string-width s) b -1)
  7863. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7864. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7865. l))
  7866. (defun org-table-begin (&optional table-type)
  7867. "Find the beginning of the table and return its position.
  7868. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7869. (save-excursion
  7870. (if (not (re-search-backward
  7871. (if table-type org-table-any-border-regexp
  7872. org-table-border-regexp)
  7873. nil t))
  7874. (progn (goto-char (point-min)) (point))
  7875. (goto-char (match-beginning 0))
  7876. (beginning-of-line 2)
  7877. (point))))
  7878. (defun org-table-end (&optional table-type)
  7879. "Find the end of the table and return its position.
  7880. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7881. (save-excursion
  7882. (if (not (re-search-forward
  7883. (if table-type org-table-any-border-regexp
  7884. org-table-border-regexp)
  7885. nil t))
  7886. (goto-char (point-max))
  7887. (goto-char (match-beginning 0)))
  7888. (point-marker)))
  7889. (defun org-table-justify-field-maybe (&optional new)
  7890. "Justify the current field, text to left, number to right.
  7891. Optional argument NEW may specify text to replace the current field content."
  7892. (cond
  7893. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7894. ((org-at-table-hline-p))
  7895. ((and (not new)
  7896. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7897. (current-buffer)))
  7898. (< (point) org-table-aligned-begin-marker)
  7899. (>= (point) org-table-aligned-end-marker)))
  7900. ;; This is not the same table, force a full re-align
  7901. (setq org-table-may-need-update t))
  7902. (t ;; realign the current field, based on previous full realign
  7903. (let* ((pos (point)) s
  7904. (col (org-table-current-column))
  7905. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7906. l f n o e)
  7907. (when (> col 0)
  7908. (skip-chars-backward "^|\n")
  7909. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7910. (progn
  7911. (setq s (match-string 1)
  7912. o (match-string 0)
  7913. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7914. e (not (= (match-beginning 2) (match-end 2))))
  7915. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7916. l (if e "|" (setq org-table-may-need-update t) ""))
  7917. n (format f s))
  7918. (if new
  7919. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7920. (setq n (format f new))
  7921. (setq n (concat new "|") org-table-may-need-update t)))
  7922. (or (equal n o)
  7923. (let (org-table-may-need-update)
  7924. (replace-match n t t))))
  7925. (setq org-table-may-need-update t))
  7926. (goto-char pos))))))
  7927. (defun org-table-next-field ()
  7928. "Go to the next field in the current table, creating new lines as needed.
  7929. Before doing so, re-align the table if necessary."
  7930. (interactive)
  7931. (org-table-maybe-eval-formula)
  7932. (org-table-maybe-recalculate-line)
  7933. (if (and org-table-automatic-realign
  7934. org-table-may-need-update)
  7935. (org-table-align))
  7936. (let ((end (org-table-end)))
  7937. (if (org-at-table-hline-p)
  7938. (end-of-line 1))
  7939. (condition-case nil
  7940. (progn
  7941. (re-search-forward "|" end)
  7942. (if (looking-at "[ \t]*$")
  7943. (re-search-forward "|" end))
  7944. (if (and (looking-at "-")
  7945. org-table-tab-jumps-over-hlines
  7946. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7947. (goto-char (match-beginning 1)))
  7948. (if (looking-at "-")
  7949. (progn
  7950. (beginning-of-line 0)
  7951. (org-table-insert-row 'below))
  7952. (if (looking-at " ") (forward-char 1))))
  7953. (error
  7954. (org-table-insert-row 'below)))))
  7955. (defun org-table-previous-field ()
  7956. "Go to the previous field in the table.
  7957. Before doing so, re-align the table if necessary."
  7958. (interactive)
  7959. (org-table-justify-field-maybe)
  7960. (org-table-maybe-recalculate-line)
  7961. (if (and org-table-automatic-realign
  7962. org-table-may-need-update)
  7963. (org-table-align))
  7964. (if (org-at-table-hline-p)
  7965. (end-of-line 1))
  7966. (re-search-backward "|" (org-table-begin))
  7967. (re-search-backward "|" (org-table-begin))
  7968. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7969. (re-search-backward "|" (org-table-begin)))
  7970. (if (looking-at "| ?")
  7971. (goto-char (match-end 0))))
  7972. (defun org-table-next-row ()
  7973. "Go to the next row (same column) in the current table.
  7974. Before doing so, re-align the table if necessary."
  7975. (interactive)
  7976. (org-table-maybe-eval-formula)
  7977. (org-table-maybe-recalculate-line)
  7978. (if (or (looking-at "[ \t]*$")
  7979. (save-excursion (skip-chars-backward " \t") (bolp)))
  7980. (newline)
  7981. (if (and org-table-automatic-realign
  7982. org-table-may-need-update)
  7983. (org-table-align))
  7984. (let ((col (org-table-current-column)))
  7985. (beginning-of-line 2)
  7986. (if (or (not (org-at-table-p))
  7987. (org-at-table-hline-p))
  7988. (progn
  7989. (beginning-of-line 0)
  7990. (org-table-insert-row 'below)))
  7991. (org-table-goto-column col)
  7992. (skip-chars-backward "^|\n\r")
  7993. (if (looking-at " ") (forward-char 1)))))
  7994. (defun org-table-copy-down (n)
  7995. "Copy a field down in the current column.
  7996. If the field at the cursor is empty, copy into it the content of the nearest
  7997. non-empty field above. With argument N, use the Nth non-empty field.
  7998. If the current field is not empty, it is copied down to the next row, and
  7999. the cursor is moved with it. Therefore, repeating this command causes the
  8000. column to be filled row-by-row.
  8001. If the variable `org-table-copy-increment' is non-nil and the field is an
  8002. integer or a timestamp, it will be incremented while copying. In the case of
  8003. a timestamp, if the cursor is on the year, change the year. If it is on the
  8004. month or the day, change that. Point will stay on the current date field
  8005. in order to easily repeat the interval."
  8006. (interactive "p")
  8007. (let* ((colpos (org-table-current-column))
  8008. (col (current-column))
  8009. (field (org-table-get-field))
  8010. (non-empty (string-match "[^ \t]" field))
  8011. (beg (org-table-begin))
  8012. txt)
  8013. (org-table-check-inside-data-field)
  8014. (if non-empty
  8015. (progn
  8016. (setq txt (org-trim field))
  8017. (org-table-next-row)
  8018. (org-table-blank-field))
  8019. (save-excursion
  8020. (setq txt
  8021. (catch 'exit
  8022. (while (progn (beginning-of-line 1)
  8023. (re-search-backward org-table-dataline-regexp
  8024. beg t))
  8025. (org-table-goto-column colpos t)
  8026. (if (and (looking-at
  8027. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8028. (= (setq n (1- n)) 0))
  8029. (throw 'exit (match-string 1))))))))
  8030. (if txt
  8031. (progn
  8032. (if (and org-table-copy-increment
  8033. (string-match "^[0-9]+$" txt))
  8034. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8035. (insert txt)
  8036. (move-to-column col)
  8037. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8038. (org-timestamp-up 1)
  8039. (org-table-maybe-recalculate-line))
  8040. (org-table-align)
  8041. (move-to-column col))
  8042. (error "No non-empty field found"))))
  8043. (defun org-table-check-inside-data-field ()
  8044. "Is point inside a table data field?
  8045. I.e. not on a hline or before the first or after the last column?
  8046. This actually throws an error, so it aborts the current command."
  8047. (if (or (not (org-at-table-p))
  8048. (= (org-table-current-column) 0)
  8049. (org-at-table-hline-p)
  8050. (looking-at "[ \t]*$"))
  8051. (error "Not in table data field")))
  8052. (defvar org-table-clip nil
  8053. "Clipboard for table regions.")
  8054. (defun org-table-blank-field ()
  8055. "Blank the current table field or active region."
  8056. (interactive)
  8057. (org-table-check-inside-data-field)
  8058. (if (and (interactive-p) (org-region-active-p))
  8059. (let (org-table-clip)
  8060. (org-table-cut-region (region-beginning) (region-end)))
  8061. (skip-chars-backward "^|")
  8062. (backward-char 1)
  8063. (if (looking-at "|[^|\n]+")
  8064. (let* ((pos (match-beginning 0))
  8065. (match (match-string 0))
  8066. (len (org-string-width match)))
  8067. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8068. (goto-char (+ 2 pos))
  8069. (substring match 1)))))
  8070. (defun org-table-get-field (&optional n replace)
  8071. "Return the value of the field in column N of current row.
  8072. N defaults to current field.
  8073. If REPLACE is a string, replace field with this value. The return value
  8074. is always the old value."
  8075. (and n (org-table-goto-column n))
  8076. (skip-chars-backward "^|\n")
  8077. (backward-char 1)
  8078. (if (looking-at "|[^|\r\n]*")
  8079. (let* ((pos (match-beginning 0))
  8080. (val (buffer-substring (1+ pos) (match-end 0))))
  8081. (if replace
  8082. (replace-match (concat "|" replace) t t))
  8083. (goto-char (min (point-at-eol) (+ 2 pos)))
  8084. val)
  8085. (forward-char 1) ""))
  8086. (defun org-table-field-info (arg)
  8087. "Show info about the current field, and highlight any reference at point."
  8088. (interactive "P")
  8089. (org-table-get-specials)
  8090. (save-excursion
  8091. (let* ((pos (point))
  8092. (col (org-table-current-column))
  8093. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8094. (name (car (rassoc (list (org-current-line) col)
  8095. org-table-named-field-locations)))
  8096. (eql (org-table-get-stored-formulas))
  8097. (dline (org-table-current-dline))
  8098. (ref (format "@%d$%d" dline col))
  8099. (ref1 (org-table-convert-refs-to-an ref))
  8100. (fequation (or (assoc name eql) (assoc ref eql)))
  8101. (cequation (assoc (int-to-string col) eql))
  8102. (eqn (or fequation cequation)))
  8103. (goto-char pos)
  8104. (condition-case nil
  8105. (org-table-show-reference 'local)
  8106. (error nil))
  8107. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8108. dline col
  8109. (if cname (concat " or $" cname) "")
  8110. dline col ref1
  8111. (if name (concat " or $" name) "")
  8112. ;; FIXME: formula info not correct if special table line
  8113. (if eqn
  8114. (concat ", formula: "
  8115. (org-table-formula-to-user
  8116. (concat
  8117. (if (string-match "^[$@]"(car eqn)) "" "$")
  8118. (car eqn) "=" (cdr eqn))))
  8119. "")))))
  8120. (defun org-table-current-column ()
  8121. "Find out which column we are in.
  8122. When called interactively, column is also displayed in echo area."
  8123. (interactive)
  8124. (if (interactive-p) (org-table-check-inside-data-field))
  8125. (save-excursion
  8126. (let ((cnt 0) (pos (point)))
  8127. (beginning-of-line 1)
  8128. (while (search-forward "|" pos t)
  8129. (setq cnt (1+ cnt)))
  8130. (if (interactive-p) (message "This is table column %d" cnt))
  8131. cnt)))
  8132. (defun org-table-current-dline ()
  8133. "Find out what table data line we are in.
  8134. Only datalins count for this."
  8135. (interactive)
  8136. (if (interactive-p) (org-table-check-inside-data-field))
  8137. (save-excursion
  8138. (let ((cnt 0) (pos (point)))
  8139. (goto-char (org-table-begin))
  8140. (while (<= (point) pos)
  8141. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8142. (beginning-of-line 2))
  8143. (if (interactive-p) (message "This is table line %d" cnt))
  8144. cnt)))
  8145. (defun org-table-goto-column (n &optional on-delim force)
  8146. "Move the cursor to the Nth column in the current table line.
  8147. With optional argument ON-DELIM, stop with point before the left delimiter
  8148. of the field.
  8149. If there are less than N fields, just go to after the last delimiter.
  8150. However, when FORCE is non-nil, create new columns if necessary."
  8151. (interactive "p")
  8152. (let ((pos (point-at-eol)))
  8153. (beginning-of-line 1)
  8154. (when (> n 0)
  8155. (while (and (> (setq n (1- n)) -1)
  8156. (or (search-forward "|" pos t)
  8157. (and force
  8158. (progn (end-of-line 1)
  8159. (skip-chars-backward "^|")
  8160. (insert " | "))))))
  8161. ; (backward-char 2) t)))))
  8162. (when (and force (not (looking-at ".*|")))
  8163. (save-excursion (end-of-line 1) (insert " | ")))
  8164. (if on-delim
  8165. (backward-char 1)
  8166. (if (looking-at " ") (forward-char 1))))))
  8167. (defun org-at-table-p (&optional table-type)
  8168. "Return t if the cursor is inside an org-type table.
  8169. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8170. (if org-enable-table-editor
  8171. (save-excursion
  8172. (beginning-of-line 1)
  8173. (looking-at (if table-type org-table-any-line-regexp
  8174. org-table-line-regexp)))
  8175. nil))
  8176. (defun org-at-table.el-p ()
  8177. "Return t if and only if we are at a table.el table."
  8178. (and (org-at-table-p 'any)
  8179. (save-excursion
  8180. (goto-char (org-table-begin 'any))
  8181. (looking-at org-table1-hline-regexp))))
  8182. (defun org-table-recognize-table.el ()
  8183. "If there is a table.el table nearby, recognize it and move into it."
  8184. (if org-table-tab-recognizes-table.el
  8185. (if (org-at-table.el-p)
  8186. (progn
  8187. (beginning-of-line 1)
  8188. (if (looking-at org-table-dataline-regexp)
  8189. nil
  8190. (if (looking-at org-table1-hline-regexp)
  8191. (progn
  8192. (beginning-of-line 2)
  8193. (if (looking-at org-table-any-border-regexp)
  8194. (beginning-of-line -1)))))
  8195. (if (re-search-forward "|" (org-table-end t) t)
  8196. (progn
  8197. (require 'table)
  8198. (if (table--at-cell-p (point))
  8199. t
  8200. (message "recognizing table.el table...")
  8201. (table-recognize-table)
  8202. (message "recognizing table.el table...done")))
  8203. (error "This should not happen..."))
  8204. t)
  8205. nil)
  8206. nil))
  8207. (defun org-at-table-hline-p ()
  8208. "Return t if the cursor is inside a hline in a table."
  8209. (if org-enable-table-editor
  8210. (save-excursion
  8211. (beginning-of-line 1)
  8212. (looking-at org-table-hline-regexp))
  8213. nil))
  8214. (defun org-table-insert-column ()
  8215. "Insert a new column into the table."
  8216. (interactive)
  8217. (if (not (org-at-table-p))
  8218. (error "Not at a table"))
  8219. (org-table-find-dataline)
  8220. (let* ((col (max 1 (org-table-current-column)))
  8221. (beg (org-table-begin))
  8222. (end (org-table-end))
  8223. ;; Current cursor position
  8224. (linepos (org-current-line))
  8225. (colpos col))
  8226. (goto-char beg)
  8227. (while (< (point) end)
  8228. (if (org-at-table-hline-p)
  8229. nil
  8230. (org-table-goto-column col t)
  8231. (insert "| "))
  8232. (beginning-of-line 2))
  8233. (move-marker end nil)
  8234. (goto-line linepos)
  8235. (org-table-goto-column colpos)
  8236. (org-table-align)
  8237. (org-table-fix-formulas "$" nil (1- col) 1)))
  8238. (defun org-table-find-dataline ()
  8239. "Find a dataline in the current table, which is needed for column commands."
  8240. (if (and (org-at-table-p)
  8241. (not (org-at-table-hline-p)))
  8242. t
  8243. (let ((col (current-column))
  8244. (end (org-table-end)))
  8245. (move-to-column col)
  8246. (while (and (< (point) end)
  8247. (or (not (= (current-column) col))
  8248. (org-at-table-hline-p)))
  8249. (beginning-of-line 2)
  8250. (move-to-column col))
  8251. (if (and (org-at-table-p)
  8252. (not (org-at-table-hline-p)))
  8253. t
  8254. (error
  8255. "Please position cursor in a data line for column operations")))))
  8256. (defun org-table-delete-column ()
  8257. "Delete a column from the table."
  8258. (interactive)
  8259. (if (not (org-at-table-p))
  8260. (error "Not at a table"))
  8261. (org-table-find-dataline)
  8262. (org-table-check-inside-data-field)
  8263. (let* ((col (org-table-current-column))
  8264. (beg (org-table-begin))
  8265. (end (org-table-end))
  8266. ;; Current cursor position
  8267. (linepos (org-current-line))
  8268. (colpos col))
  8269. (goto-char beg)
  8270. (while (< (point) end)
  8271. (if (org-at-table-hline-p)
  8272. nil
  8273. (org-table-goto-column col t)
  8274. (and (looking-at "|[^|\n]+|")
  8275. (replace-match "|")))
  8276. (beginning-of-line 2))
  8277. (move-marker end nil)
  8278. (goto-line linepos)
  8279. (org-table-goto-column colpos)
  8280. (org-table-align)
  8281. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8282. col -1 col)))
  8283. (defun org-table-move-column-right ()
  8284. "Move column to the right."
  8285. (interactive)
  8286. (org-table-move-column nil))
  8287. (defun org-table-move-column-left ()
  8288. "Move column to the left."
  8289. (interactive)
  8290. (org-table-move-column 'left))
  8291. (defun org-table-move-column (&optional left)
  8292. "Move the current column to the right. With arg LEFT, move to the left."
  8293. (interactive "P")
  8294. (if (not (org-at-table-p))
  8295. (error "Not at a table"))
  8296. (org-table-find-dataline)
  8297. (org-table-check-inside-data-field)
  8298. (let* ((col (org-table-current-column))
  8299. (col1 (if left (1- col) col))
  8300. (beg (org-table-begin))
  8301. (end (org-table-end))
  8302. ;; Current cursor position
  8303. (linepos (org-current-line))
  8304. (colpos (if left (1- col) (1+ col))))
  8305. (if (and left (= col 1))
  8306. (error "Cannot move column further left"))
  8307. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8308. (error "Cannot move column further right"))
  8309. (goto-char beg)
  8310. (while (< (point) end)
  8311. (if (org-at-table-hline-p)
  8312. nil
  8313. (org-table-goto-column col1 t)
  8314. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8315. (replace-match "|\\2|\\1|")))
  8316. (beginning-of-line 2))
  8317. (move-marker end nil)
  8318. (goto-line linepos)
  8319. (org-table-goto-column colpos)
  8320. (org-table-align)
  8321. (org-table-fix-formulas
  8322. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8323. (cons (number-to-string colpos) (number-to-string col))))))
  8324. (defun org-table-move-row-down ()
  8325. "Move table row down."
  8326. (interactive)
  8327. (org-table-move-row nil))
  8328. (defun org-table-move-row-up ()
  8329. "Move table row up."
  8330. (interactive)
  8331. (org-table-move-row 'up))
  8332. (defun org-table-move-row (&optional up)
  8333. "Move the current table line down. With arg UP, move it up."
  8334. (interactive "P")
  8335. (let* ((col (current-column))
  8336. (pos (point))
  8337. (hline1p (save-excursion (beginning-of-line 1)
  8338. (looking-at org-table-hline-regexp)))
  8339. (dline1 (org-table-current-dline))
  8340. (dline2 (+ dline1 (if up -1 1)))
  8341. (tonew (if up 0 2))
  8342. txt hline2p)
  8343. (beginning-of-line tonew)
  8344. (unless (org-at-table-p)
  8345. (goto-char pos)
  8346. (error "Cannot move row further"))
  8347. (setq hline2p (looking-at org-table-hline-regexp))
  8348. (goto-char pos)
  8349. (beginning-of-line 1)
  8350. (setq pos (point))
  8351. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8352. (delete-region (point) (1+ (point-at-eol)))
  8353. (beginning-of-line tonew)
  8354. (insert txt)
  8355. (beginning-of-line 0)
  8356. (move-to-column col)
  8357. (unless (or hline1p hline2p)
  8358. (org-table-fix-formulas
  8359. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8360. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8361. (defun org-table-insert-row (&optional arg)
  8362. "Insert a new row above the current line into the table.
  8363. With prefix ARG, insert below the current line."
  8364. (interactive "P")
  8365. (if (not (org-at-table-p))
  8366. (error "Not at a table"))
  8367. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8368. (new (org-table-clean-line line)))
  8369. ;; Fix the first field if necessary
  8370. (if (string-match "^[ \t]*| *[#$] *|" line)
  8371. (setq new (replace-match (match-string 0 line) t t new)))
  8372. (beginning-of-line (if arg 2 1))
  8373. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8374. (beginning-of-line 0)
  8375. (re-search-forward "| ?" (point-at-eol) t)
  8376. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8377. (org-table-align))
  8378. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8379. (defun org-table-insert-hline (&optional above)
  8380. "Insert a horizontal-line below the current line into the table.
  8381. With prefix ABOVE, insert above the current line."
  8382. (interactive "P")
  8383. (if (not (org-at-table-p))
  8384. (error "Not at a table"))
  8385. (let ((line (org-table-clean-line
  8386. (buffer-substring (point-at-bol) (point-at-eol))))
  8387. (col (current-column)))
  8388. (while (string-match "|\\( +\\)|" line)
  8389. (setq line (replace-match
  8390. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8391. ?-) "|") t t line)))
  8392. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8393. (beginning-of-line (if above 1 2))
  8394. (insert line "\n")
  8395. (beginning-of-line (if above 1 -1))
  8396. (move-to-column col)
  8397. (and org-table-overlay-coordinates (org-table-align))))
  8398. (defun org-table-hline-and-move (&optional same-column)
  8399. "Insert a hline and move to the row below that line."
  8400. (interactive "P")
  8401. (let ((col (org-table-current-column)))
  8402. (org-table-maybe-eval-formula)
  8403. (org-table-maybe-recalculate-line)
  8404. (org-table-insert-hline)
  8405. (end-of-line 2)
  8406. (if (looking-at "\n[ \t]*|-")
  8407. (progn (insert "\n|") (org-table-align))
  8408. (org-table-next-field))
  8409. (if same-column (org-table-goto-column col))))
  8410. (defun org-table-clean-line (s)
  8411. "Convert a table line S into a string with only \"|\" and space.
  8412. In particular, this does handle wide and invisible characters."
  8413. (if (string-match "^[ \t]*|-" s)
  8414. ;; It's a hline, just map the characters
  8415. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8416. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8417. (setq s (replace-match
  8418. (concat "|" (make-string (org-string-width (match-string 1 s))
  8419. ?\ ) "|")
  8420. t t s)))
  8421. s))
  8422. (defun org-table-kill-row ()
  8423. "Delete the current row or horizontal line from the table."
  8424. (interactive)
  8425. (if (not (org-at-table-p))
  8426. (error "Not at a table"))
  8427. (let ((col (current-column))
  8428. (dline (org-table-current-dline)))
  8429. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8430. (if (not (org-at-table-p)) (beginning-of-line 0))
  8431. (move-to-column col)
  8432. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8433. dline -1 dline)))
  8434. (defun org-table-sort-lines (with-case &optional sorting-type)
  8435. "Sort table lines according to the column at point.
  8436. The position of point indicates the column to be used for
  8437. sorting, and the range of lines is the range between the nearest
  8438. horizontal separator lines, or the entire table of no such lines
  8439. exist. If point is before the first column, you will be prompted
  8440. for the sorting column. If there is an active region, the mark
  8441. specifies the first line and the sorting column, while point
  8442. should be in the last line to be included into the sorting.
  8443. The command then prompts for the sorting type which can be
  8444. alphabetically, numerically, or by time (as given in a time stamp
  8445. in the field). Sorting in reverse order is also possible.
  8446. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8447. If SORTING-TYPE is specified when this function is called from a Lisp
  8448. program, no prompting will take place. SORTING-TYPE must be a character,
  8449. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8450. should be done in reverse order."
  8451. (interactive "P")
  8452. (let* ((thisline (org-current-line))
  8453. (thiscol (org-table-current-column))
  8454. beg end bcol ecol tend tbeg column lns pos)
  8455. (when (equal thiscol 0)
  8456. (if (interactive-p)
  8457. (setq thiscol
  8458. (string-to-number
  8459. (read-string "Use column N for sorting: ")))
  8460. (setq thiscol 1))
  8461. (org-table-goto-column thiscol))
  8462. (org-table-check-inside-data-field)
  8463. (if (org-region-active-p)
  8464. (progn
  8465. (setq beg (region-beginning) end (region-end))
  8466. (goto-char beg)
  8467. (setq column (org-table-current-column)
  8468. beg (point-at-bol))
  8469. (goto-char end)
  8470. (setq end (point-at-bol 2)))
  8471. (setq column (org-table-current-column)
  8472. pos (point)
  8473. tbeg (org-table-begin)
  8474. tend (org-table-end))
  8475. (if (re-search-backward org-table-hline-regexp tbeg t)
  8476. (setq beg (point-at-bol 2))
  8477. (goto-char tbeg)
  8478. (setq beg (point-at-bol 1)))
  8479. (goto-char pos)
  8480. (if (re-search-forward org-table-hline-regexp tend t)
  8481. (setq end (point-at-bol 1))
  8482. (goto-char tend)
  8483. (setq end (point-at-bol))))
  8484. (setq beg (move-marker (make-marker) beg)
  8485. end (move-marker (make-marker) end))
  8486. (untabify beg end)
  8487. (goto-char beg)
  8488. (org-table-goto-column column)
  8489. (skip-chars-backward "^|")
  8490. (setq bcol (current-column))
  8491. (org-table-goto-column (1+ column))
  8492. (skip-chars-backward "^|")
  8493. (setq ecol (1- (current-column)))
  8494. (org-table-goto-column column)
  8495. (setq lns (mapcar (lambda(x) (cons
  8496. (org-sort-remove-invisible
  8497. (nth (1- column)
  8498. (org-split-string x "[ \t]*|[ \t]*")))
  8499. x))
  8500. (org-split-string (buffer-substring beg end) "\n")))
  8501. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8502. (delete-region beg end)
  8503. (move-marker beg nil)
  8504. (move-marker end nil)
  8505. (insert (mapconcat 'cdr lns "\n") "\n")
  8506. (goto-line thisline)
  8507. (org-table-goto-column thiscol)
  8508. (message "%d lines sorted, based on column %d" (length lns) column)))
  8509. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8510. (defun org-sort-remove-invisible (s)
  8511. (remove-text-properties 0 (length s) org-rm-props s)
  8512. (while (string-match org-bracket-link-regexp s)
  8513. (setq s (replace-match (if (match-end 2)
  8514. (match-string 3 s)
  8515. (match-string 1 s)) t t s)))
  8516. s)
  8517. (defun org-table-cut-region (beg end)
  8518. "Copy region in table to the clipboard and blank all relevant fields."
  8519. (interactive "r")
  8520. (org-table-copy-region beg end 'cut))
  8521. (defun org-table-copy-region (beg end &optional cut)
  8522. "Copy rectangular region in table to clipboard.
  8523. A special clipboard is used which can only be accessed
  8524. with `org-table-paste-rectangle'."
  8525. (interactive "rP")
  8526. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8527. region cols
  8528. (rpl (if cut " " nil)))
  8529. (goto-char beg)
  8530. (org-table-check-inside-data-field)
  8531. (setq l01 (org-current-line)
  8532. c01 (org-table-current-column))
  8533. (goto-char end)
  8534. (org-table-check-inside-data-field)
  8535. (setq l02 (org-current-line)
  8536. c02 (org-table-current-column))
  8537. (setq l1 (min l01 l02) l2 (max l01 l02)
  8538. c1 (min c01 c02) c2 (max c01 c02))
  8539. (catch 'exit
  8540. (while t
  8541. (catch 'nextline
  8542. (if (> l1 l2) (throw 'exit t))
  8543. (goto-line l1)
  8544. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8545. (setq cols nil ic1 c1 ic2 c2)
  8546. (while (< ic1 (1+ ic2))
  8547. (push (org-table-get-field ic1 rpl) cols)
  8548. (setq ic1 (1+ ic1)))
  8549. (push (nreverse cols) region)
  8550. (setq l1 (1+ l1)))))
  8551. (setq org-table-clip (nreverse region))
  8552. (if cut (org-table-align))
  8553. org-table-clip))
  8554. (defun org-table-paste-rectangle ()
  8555. "Paste a rectangular region into a table.
  8556. The upper right corner ends up in the current field. All involved fields
  8557. will be overwritten. If the rectangle does not fit into the present table,
  8558. the table is enlarged as needed. The process ignores horizontal separator
  8559. lines."
  8560. (interactive)
  8561. (unless (and org-table-clip (listp org-table-clip))
  8562. (error "First cut/copy a region to paste!"))
  8563. (org-table-check-inside-data-field)
  8564. (let* ((clip org-table-clip)
  8565. (line (org-current-line))
  8566. (col (org-table-current-column))
  8567. (org-enable-table-editor t)
  8568. (org-table-automatic-realign nil)
  8569. c cols field)
  8570. (while (setq cols (pop clip))
  8571. (while (org-at-table-hline-p) (beginning-of-line 2))
  8572. (if (not (org-at-table-p))
  8573. (progn (end-of-line 0) (org-table-next-field)))
  8574. (setq c col)
  8575. (while (setq field (pop cols))
  8576. (org-table-goto-column c nil 'force)
  8577. (org-table-get-field nil field)
  8578. (setq c (1+ c)))
  8579. (beginning-of-line 2))
  8580. (goto-line line)
  8581. (org-table-goto-column col)
  8582. (org-table-align)))
  8583. (defun org-table-convert ()
  8584. "Convert from `org-mode' table to table.el and back.
  8585. Obviously, this only works within limits. When an Org-mode table is
  8586. converted to table.el, all horizontal separator lines get lost, because
  8587. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8588. A table.el table can be converted to an Org-mode table only if it does not
  8589. do row or column spanning. Multiline cells will become multiple cells.
  8590. Beware, Org-mode does not test if the table can be successfully converted - it
  8591. blindly applies a recipe that works for simple tables."
  8592. (interactive)
  8593. (require 'table)
  8594. (if (org-at-table.el-p)
  8595. ;; convert to Org-mode table
  8596. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8597. (end (move-marker (make-marker) (org-table-end t))))
  8598. (table-unrecognize-region beg end)
  8599. (goto-char beg)
  8600. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8601. (replace-match ""))
  8602. (goto-char beg))
  8603. (if (org-at-table-p)
  8604. ;; convert to table.el table
  8605. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8606. (end (move-marker (make-marker) (org-table-end))))
  8607. ;; first, get rid of all horizontal lines
  8608. (goto-char beg)
  8609. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8610. (replace-match ""))
  8611. ;; insert a hline before first
  8612. (goto-char beg)
  8613. (org-table-insert-hline 'above)
  8614. (beginning-of-line -1)
  8615. ;; insert a hline after each line
  8616. (while (progn (beginning-of-line 3) (< (point) end))
  8617. (org-table-insert-hline))
  8618. (goto-char beg)
  8619. (setq end (move-marker end (org-table-end)))
  8620. ;; replace "+" at beginning and ending of hlines
  8621. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8622. (replace-match "\\1+-"))
  8623. (goto-char beg)
  8624. (while (re-search-forward "-|[ \t]*$" end t)
  8625. (replace-match "-+"))
  8626. (goto-char beg)))))
  8627. (defun org-table-wrap-region (arg)
  8628. "Wrap several fields in a column like a paragraph.
  8629. This is useful if you'd like to spread the contents of a field over several
  8630. lines, in order to keep the table compact.
  8631. If there is an active region, and both point and mark are in the same column,
  8632. the text in the column is wrapped to minimum width for the given number of
  8633. lines. Generally, this makes the table more compact. A prefix ARG may be
  8634. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8635. formats the selected text to two lines. If the region was longer than two
  8636. lines, the remaining lines remain empty. A negative prefix argument reduces
  8637. the current number of lines by that amount. The wrapped text is pasted back
  8638. into the table. If you formatted it to more lines than it was before, fields
  8639. further down in the table get overwritten - so you might need to make space in
  8640. the table first.
  8641. If there is no region, the current field is split at the cursor position and
  8642. the text fragment to the right of the cursor is prepended to the field one
  8643. line down.
  8644. If there is no region, but you specify a prefix ARG, the current field gets
  8645. blank, and the content is appended to the field above."
  8646. (interactive "P")
  8647. (org-table-check-inside-data-field)
  8648. (if (org-region-active-p)
  8649. ;; There is a region: fill as a paragraph
  8650. (let* ((beg (region-beginning))
  8651. (cline (save-excursion (goto-char beg) (org-current-line)))
  8652. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8653. nlines)
  8654. (org-table-cut-region (region-beginning) (region-end))
  8655. (if (> (length (car org-table-clip)) 1)
  8656. (error "Region must be limited to single column"))
  8657. (setq nlines (if arg
  8658. (if (< arg 1)
  8659. (+ (length org-table-clip) arg)
  8660. arg)
  8661. (length org-table-clip)))
  8662. (setq org-table-clip
  8663. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8664. nil nlines)))
  8665. (goto-line cline)
  8666. (org-table-goto-column ccol)
  8667. (org-table-paste-rectangle))
  8668. ;; No region, split the current field at point
  8669. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8670. (skip-chars-forward "^\r\n|"))
  8671. (if arg
  8672. ;; combine with field above
  8673. (let ((s (org-table-blank-field))
  8674. (col (org-table-current-column)))
  8675. (beginning-of-line 0)
  8676. (while (org-at-table-hline-p) (beginning-of-line 0))
  8677. (org-table-goto-column col)
  8678. (skip-chars-forward "^|")
  8679. (skip-chars-backward " ")
  8680. (insert " " (org-trim s))
  8681. (org-table-align))
  8682. ;; split field
  8683. (if (looking-at "\\([^|]+\\)+|")
  8684. (let ((s (match-string 1)))
  8685. (replace-match " |")
  8686. (goto-char (match-beginning 0))
  8687. (org-table-next-row)
  8688. (insert (org-trim s) " ")
  8689. (org-table-align))
  8690. (org-table-next-row)))))
  8691. (defvar org-field-marker nil)
  8692. (defun org-table-edit-field (arg)
  8693. "Edit table field in a different window.
  8694. This is mainly useful for fields that contain hidden parts.
  8695. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8696. it can be edited in place."
  8697. (interactive "P")
  8698. (if arg
  8699. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8700. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8701. (remove-text-properties b e '(org-cwidth t invisible t
  8702. display t intangible t))
  8703. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8704. (font-lock-fontify-block)))
  8705. (let ((pos (move-marker (make-marker) (point)))
  8706. (field (org-table-get-field))
  8707. (cw (current-window-configuration))
  8708. p)
  8709. (org-switch-to-buffer-other-window "*Org tmp*")
  8710. (erase-buffer)
  8711. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8712. (let ((org-inhibit-startup t)) (org-mode))
  8713. (goto-char (setq p (point-max)))
  8714. (insert (org-trim field))
  8715. (remove-text-properties p (point-max)
  8716. '(invisible t org-cwidth t display t
  8717. intangible t))
  8718. (goto-char p)
  8719. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8720. (org-set-local 'org-window-configuration cw)
  8721. (org-set-local 'org-field-marker pos)
  8722. (message "Edit and finish with C-c C-c"))))
  8723. (defun org-table-finish-edit-field ()
  8724. "Finish editing a table data field.
  8725. Remove all newline characters, insert the result into the table, realign
  8726. the table and kill the editing buffer."
  8727. (let ((pos org-field-marker)
  8728. (cw org-window-configuration)
  8729. (cb (current-buffer))
  8730. text)
  8731. (goto-char (point-min))
  8732. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8733. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8734. (replace-match " "))
  8735. (setq text (org-trim (buffer-string)))
  8736. (set-window-configuration cw)
  8737. (kill-buffer cb)
  8738. (select-window (get-buffer-window (marker-buffer pos)))
  8739. (goto-char pos)
  8740. (move-marker pos nil)
  8741. (org-table-check-inside-data-field)
  8742. (org-table-get-field nil text)
  8743. (org-table-align)
  8744. (message "New field value inserted")))
  8745. (defun org-trim (s)
  8746. "Remove whitespace at beginning and end of string."
  8747. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8748. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8749. s)
  8750. (defun org-wrap (string &optional width lines)
  8751. "Wrap string to either a number of lines, or a width in characters.
  8752. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8753. that costs. If there is a word longer than WIDTH, the text is actually
  8754. wrapped to the length of that word.
  8755. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8756. many lines, whatever width that takes.
  8757. The return value is a list of lines, without newlines at the end."
  8758. (let* ((words (org-split-string string "[ \t\n]+"))
  8759. (maxword (apply 'max (mapcar 'org-string-width words)))
  8760. w ll)
  8761. (cond (width
  8762. (org-do-wrap words (max maxword width)))
  8763. (lines
  8764. (setq w maxword)
  8765. (setq ll (org-do-wrap words maxword))
  8766. (if (<= (length ll) lines)
  8767. ll
  8768. (setq ll words)
  8769. (while (> (length ll) lines)
  8770. (setq w (1+ w))
  8771. (setq ll (org-do-wrap words w)))
  8772. ll))
  8773. (t (error "Cannot wrap this")))))
  8774. (defun org-do-wrap (words width)
  8775. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8776. (let (lines line)
  8777. (while words
  8778. (setq line (pop words))
  8779. (while (and words (< (+ (length line) (length (car words))) width))
  8780. (setq line (concat line " " (pop words))))
  8781. (setq lines (push line lines)))
  8782. (nreverse lines)))
  8783. (defun org-split-string (string &optional separators)
  8784. "Splits STRING into substrings at SEPARATORS.
  8785. No empty strings are returned if there are matches at the beginning
  8786. and end of string."
  8787. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8788. (start 0)
  8789. notfirst
  8790. (list nil))
  8791. (while (and (string-match rexp string
  8792. (if (and notfirst
  8793. (= start (match-beginning 0))
  8794. (< start (length string)))
  8795. (1+ start) start))
  8796. (< (match-beginning 0) (length string)))
  8797. (setq notfirst t)
  8798. (or (eq (match-beginning 0) 0)
  8799. (and (eq (match-beginning 0) (match-end 0))
  8800. (eq (match-beginning 0) start))
  8801. (setq list
  8802. (cons (substring string start (match-beginning 0))
  8803. list)))
  8804. (setq start (match-end 0)))
  8805. (or (eq start (length string))
  8806. (setq list
  8807. (cons (substring string start)
  8808. list)))
  8809. (nreverse list)))
  8810. (defun org-table-map-tables (function)
  8811. "Apply FUNCTION to the start of all tables in the buffer."
  8812. (save-excursion
  8813. (save-restriction
  8814. (widen)
  8815. (goto-char (point-min))
  8816. (while (re-search-forward org-table-any-line-regexp nil t)
  8817. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8818. (beginning-of-line 1)
  8819. (if (looking-at org-table-line-regexp)
  8820. (save-excursion (funcall function)))
  8821. (re-search-forward org-table-any-border-regexp nil 1))))
  8822. (message "Mapping tables: done"))
  8823. (defvar org-timecnt) ; dynamically scoped parameter
  8824. (defun org-table-sum (&optional beg end nlast)
  8825. "Sum numbers in region of current table column.
  8826. The result will be displayed in the echo area, and will be available
  8827. as kill to be inserted with \\[yank].
  8828. If there is an active region, it is interpreted as a rectangle and all
  8829. numbers in that rectangle will be summed. If there is no active
  8830. region and point is located in a table column, sum all numbers in that
  8831. column.
  8832. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8833. numbers are assumed to be times as well (in decimal hours) and the
  8834. numbers are added as such.
  8835. If NLAST is a number, only the NLAST fields will actually be summed."
  8836. (interactive)
  8837. (save-excursion
  8838. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8839. (cond
  8840. ((and beg end)) ; beg and end given explicitly
  8841. ((org-region-active-p)
  8842. (setq beg (region-beginning) end (region-end)))
  8843. (t
  8844. (setq col (org-table-current-column))
  8845. (goto-char (org-table-begin))
  8846. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8847. (error "No table data"))
  8848. (org-table-goto-column col)
  8849. (setq beg (point))
  8850. (goto-char (org-table-end))
  8851. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8852. (error "No table data"))
  8853. (org-table-goto-column col)
  8854. (setq end (point))))
  8855. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8856. (items1 (cond ((not nlast) items)
  8857. ((>= nlast (length items)) items)
  8858. (t (setq items (reverse items))
  8859. (setcdr (nthcdr (1- nlast) items) nil)
  8860. (nreverse items))))
  8861. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8862. items1)))
  8863. (res (apply '+ numbers))
  8864. (sres (if (= org-timecnt 0)
  8865. (format "%g" res)
  8866. (setq diff (* 3600 res)
  8867. h (floor (/ diff 3600)) diff (mod diff 3600)
  8868. m (floor (/ diff 60)) diff (mod diff 60)
  8869. s diff)
  8870. (format "%d:%02d:%02d" h m s))))
  8871. (kill-new sres)
  8872. (if (interactive-p)
  8873. (message "%s"
  8874. (substitute-command-keys
  8875. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8876. (length numbers) sres))))
  8877. sres))))
  8878. (defun org-table-get-number-for-summing (s)
  8879. (let (n)
  8880. (if (string-match "^ *|? *" s)
  8881. (setq s (replace-match "" nil nil s)))
  8882. (if (string-match " *|? *$" s)
  8883. (setq s (replace-match "" nil nil s)))
  8884. (setq n (string-to-number s))
  8885. (cond
  8886. ((and (string-match "0" s)
  8887. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8888. ((string-match "\\`[ \t]+\\'" s) nil)
  8889. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8890. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8891. (m (string-to-number (or (match-string 2 s) "0")))
  8892. (s (string-to-number (or (match-string 4 s) "0"))))
  8893. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8894. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8895. ((equal n 0) nil)
  8896. (t n))))
  8897. (defun org-table-current-field-formula (&optional key noerror)
  8898. "Return the formula active for the current field.
  8899. Assumes that specials are in place.
  8900. If KEY is given, return the key to this formula.
  8901. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8902. (let* ((name (car (rassoc (list (org-current-line)
  8903. (org-table-current-column))
  8904. org-table-named-field-locations)))
  8905. (col (org-table-current-column))
  8906. (scol (int-to-string col))
  8907. (ref (format "@%d$%d" (org-table-current-dline) col))
  8908. (stored-list (org-table-get-stored-formulas noerror))
  8909. (ass (or (assoc name stored-list)
  8910. (assoc ref stored-list)
  8911. (assoc scol stored-list))))
  8912. (if key
  8913. (car ass)
  8914. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8915. (cdr ass))))))
  8916. (defun org-table-get-formula (&optional equation named)
  8917. "Read a formula from the minibuffer, offer stored formula as default.
  8918. When NAMED is non-nil, look for a named equation."
  8919. (let* ((stored-list (org-table-get-stored-formulas))
  8920. (name (car (rassoc (list (org-current-line)
  8921. (org-table-current-column))
  8922. org-table-named-field-locations)))
  8923. (ref (format "@%d$%d" (org-table-current-dline)
  8924. (org-table-current-column)))
  8925. (refass (assoc ref stored-list))
  8926. (scol (if named
  8927. (if name name ref)
  8928. (int-to-string (org-table-current-column))))
  8929. (dummy (and (or name refass) (not named)
  8930. (not (y-or-n-p "Replace field formula with column formula? " ))
  8931. (error "Abort")))
  8932. (name (or name ref))
  8933. (org-table-may-need-update nil)
  8934. (stored (cdr (assoc scol stored-list)))
  8935. (eq (cond
  8936. ((and stored equation (string-match "^ *=? *$" equation))
  8937. stored)
  8938. ((stringp equation)
  8939. equation)
  8940. (t (org-table-formula-from-user
  8941. (read-string
  8942. (org-table-formula-to-user
  8943. (format "%s formula %s%s="
  8944. (if named "Field" "Column")
  8945. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8946. scol))
  8947. (if stored (org-table-formula-to-user stored) "")
  8948. 'org-table-formula-history
  8949. )))))
  8950. mustsave)
  8951. (when (not (string-match "\\S-" eq))
  8952. ;; remove formula
  8953. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8954. (org-table-store-formulas stored-list)
  8955. (error "Formula removed"))
  8956. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8957. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8958. (if (and name (not named))
  8959. ;; We set the column equation, delete the named one.
  8960. (setq stored-list (delq (assoc name stored-list) stored-list)
  8961. mustsave t))
  8962. (if stored
  8963. (setcdr (assoc scol stored-list) eq)
  8964. (setq stored-list (cons (cons scol eq) stored-list)))
  8965. (if (or mustsave (not (equal stored eq)))
  8966. (org-table-store-formulas stored-list))
  8967. eq))
  8968. (defun org-table-store-formulas (alist)
  8969. "Store the list of formulas below the current table."
  8970. (setq alist (sort alist 'org-table-formula-less-p))
  8971. (save-excursion
  8972. (goto-char (org-table-end))
  8973. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8974. (progn
  8975. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8976. (goto-char (match-beginning 2))
  8977. (delete-region (match-beginning 2) (match-end 0)))
  8978. (insert "#+TBLFM:"))
  8979. (insert " "
  8980. (mapconcat (lambda (x)
  8981. (concat
  8982. (if (equal (string-to-char (car x)) ?@) "" "$")
  8983. (car x) "=" (cdr x)))
  8984. alist "::")
  8985. "\n")))
  8986. (defsubst org-table-formula-make-cmp-string (a)
  8987. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8988. (concat
  8989. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8990. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8991. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8992. (defun org-table-formula-less-p (a b)
  8993. "Compare two formulas for sorting."
  8994. (let ((as (org-table-formula-make-cmp-string (car a)))
  8995. (bs (org-table-formula-make-cmp-string (car b))))
  8996. (and as bs (string< as bs))))
  8997. (defun org-table-get-stored-formulas (&optional noerror)
  8998. "Return an alist with the stored formulas directly after current table."
  8999. (interactive)
  9000. (let (scol eq eq-alist strings string seen)
  9001. (save-excursion
  9002. (goto-char (org-table-end))
  9003. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9004. (setq strings (org-split-string (match-string 2) " *:: *"))
  9005. (while (setq string (pop strings))
  9006. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9007. (setq scol (if (match-end 2)
  9008. (match-string 2 string)
  9009. (match-string 1 string))
  9010. eq (match-string 3 string)
  9011. eq-alist (cons (cons scol eq) eq-alist))
  9012. (if (member scol seen)
  9013. (if noerror
  9014. (progn
  9015. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9016. (ding)
  9017. (sit-for 2))
  9018. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9019. (push scol seen))))))
  9020. (nreverse eq-alist)))
  9021. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9022. "Modify the equations after the table structure has been edited.
  9023. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9024. For all numbers larger than LIMIT, shift them by DELTA."
  9025. (save-excursion
  9026. (goto-char (org-table-end))
  9027. (when (looking-at "#\\+TBLFM:")
  9028. (let ((re (concat key "\\([0-9]+\\)"))
  9029. (re2
  9030. (when remove
  9031. (if (equal key "$")
  9032. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9033. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9034. s n a)
  9035. (when remove
  9036. (while (re-search-forward re2 (point-at-eol) t)
  9037. (replace-match "")))
  9038. (while (re-search-forward re (point-at-eol) t)
  9039. (setq s (match-string 1) n (string-to-number s))
  9040. (cond
  9041. ((setq a (assoc s replace))
  9042. (replace-match (concat key (cdr a)) t t))
  9043. ((and limit (> n limit))
  9044. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9045. (defun org-table-get-specials ()
  9046. "Get the column names and local parameters for this table."
  9047. (save-excursion
  9048. (let ((beg (org-table-begin)) (end (org-table-end))
  9049. names name fields fields1 field cnt
  9050. c v l line col types dlines hlines)
  9051. (setq org-table-column-names nil
  9052. org-table-local-parameters nil
  9053. org-table-named-field-locations nil
  9054. org-table-current-begin-line nil
  9055. org-table-current-begin-pos nil
  9056. org-table-current-line-types nil)
  9057. (goto-char beg)
  9058. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9059. (setq names (org-split-string (match-string 1) " *| *")
  9060. cnt 1)
  9061. (while (setq name (pop names))
  9062. (setq cnt (1+ cnt))
  9063. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9064. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9065. (setq org-table-column-names (nreverse org-table-column-names))
  9066. (setq org-table-column-name-regexp
  9067. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9068. (goto-char beg)
  9069. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9070. (setq fields (org-split-string (match-string 1) " *| *"))
  9071. (while (setq field (pop fields))
  9072. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9073. (push (cons (match-string 1 field) (match-string 2 field))
  9074. org-table-local-parameters))))
  9075. (goto-char beg)
  9076. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9077. (setq c (match-string 1)
  9078. fields (org-split-string (match-string 2) " *| *"))
  9079. (save-excursion
  9080. (beginning-of-line (if (equal c "_") 2 0))
  9081. (setq line (org-current-line) col 1)
  9082. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9083. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9084. (while (and fields1 (setq field (pop fields)))
  9085. (setq v (pop fields1) col (1+ col))
  9086. (when (and (stringp field) (stringp v)
  9087. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9088. (push (cons field v) org-table-local-parameters)
  9089. (push (list field line col) org-table-named-field-locations))))
  9090. ;; Analyse the line types
  9091. (goto-char beg)
  9092. (setq org-table-current-begin-line (org-current-line)
  9093. org-table-current-begin-pos (point)
  9094. l org-table-current-begin-line)
  9095. (while (looking-at "[ \t]*|\\(-\\)?")
  9096. (push (if (match-end 1) 'hline 'dline) types)
  9097. (if (match-end 1) (push l hlines) (push l dlines))
  9098. (beginning-of-line 2)
  9099. (setq l (1+ l)))
  9100. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9101. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9102. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9103. (defun org-table-maybe-eval-formula ()
  9104. "Check if the current field starts with \"=\" or \":=\".
  9105. If yes, store the formula and apply it."
  9106. ;; We already know we are in a table. Get field will only return a formula
  9107. ;; when appropriate. It might return a separator line, but no problem.
  9108. (when org-table-formula-evaluate-inline
  9109. (let* ((field (org-trim (or (org-table-get-field) "")))
  9110. named eq)
  9111. (when (string-match "^:?=\\(.*\\)" field)
  9112. (setq named (equal (string-to-char field) ?:)
  9113. eq (match-string 1 field))
  9114. (if (or (fboundp 'calc-eval)
  9115. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9116. (org-table-eval-formula (if named '(4) nil)
  9117. (org-table-formula-from-user eq))
  9118. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9119. (defvar org-recalc-commands nil
  9120. "List of commands triggering the recalculation of a line.
  9121. Will be filled automatically during use.")
  9122. (defvar org-recalc-marks
  9123. '((" " . "Unmarked: no special line, no automatic recalculation")
  9124. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9125. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9126. ("!" . "Column name definition line. Reference in formula as $name.")
  9127. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9128. ("_" . "Names for values in row below this one.")
  9129. ("^" . "Names for values in row above this one.")))
  9130. (defun org-table-rotate-recalc-marks (&optional newchar)
  9131. "Rotate the recalculation mark in the first column.
  9132. If in any row, the first field is not consistent with a mark,
  9133. insert a new column for the markers.
  9134. When there is an active region, change all the lines in the region,
  9135. after prompting for the marking character.
  9136. After each change, a message will be displayed indicating the meaning
  9137. of the new mark."
  9138. (interactive)
  9139. (unless (org-at-table-p) (error "Not at a table"))
  9140. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9141. (beg (org-table-begin))
  9142. (end (org-table-end))
  9143. (l (org-current-line))
  9144. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9145. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9146. (have-col
  9147. (save-excursion
  9148. (goto-char beg)
  9149. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9150. (col (org-table-current-column))
  9151. (forcenew (car (assoc newchar org-recalc-marks)))
  9152. epos new)
  9153. (when l1
  9154. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9155. (setq newchar (char-to-string (read-char-exclusive))
  9156. forcenew (car (assoc newchar org-recalc-marks))))
  9157. (if (and newchar (not forcenew))
  9158. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9159. newchar))
  9160. (if l1 (goto-line l1))
  9161. (save-excursion
  9162. (beginning-of-line 1)
  9163. (unless (looking-at org-table-dataline-regexp)
  9164. (error "Not at a table data line")))
  9165. (unless have-col
  9166. (org-table-goto-column 1)
  9167. (org-table-insert-column)
  9168. (org-table-goto-column (1+ col)))
  9169. (setq epos (point-at-eol))
  9170. (save-excursion
  9171. (beginning-of-line 1)
  9172. (org-table-get-field
  9173. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9174. (concat " "
  9175. (setq new (or forcenew
  9176. (cadr (member (match-string 1) marks))))
  9177. " ")
  9178. " # ")))
  9179. (if (and l1 l2)
  9180. (progn
  9181. (goto-line l1)
  9182. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9183. (and (looking-at org-table-dataline-regexp)
  9184. (org-table-get-field 1 (concat " " new " "))))
  9185. (goto-line l1)))
  9186. (if (not (= epos (point-at-eol))) (org-table-align))
  9187. (goto-line l)
  9188. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9189. (defun org-table-maybe-recalculate-line ()
  9190. "Recompute the current line if marked for it, and if we haven't just done it."
  9191. (interactive)
  9192. (and org-table-allow-automatic-line-recalculation
  9193. (not (and (memq last-command org-recalc-commands)
  9194. (equal org-last-recalc-line (org-current-line))))
  9195. (save-excursion (beginning-of-line 1)
  9196. (looking-at org-table-auto-recalculate-regexp))
  9197. (org-table-recalculate) t))
  9198. (defvar org-table-formula-debug nil
  9199. "Non-nil means, debug table formulas.
  9200. When nil, simply write \"#ERROR\" in corrupted fields.")
  9201. (make-variable-buffer-local 'org-table-formula-debug)
  9202. (defvar modes)
  9203. (defsubst org-set-calc-mode (var &optional value)
  9204. (if (stringp var)
  9205. (setq var (assoc var '(("D" calc-angle-mode deg)
  9206. ("R" calc-angle-mode rad)
  9207. ("F" calc-prefer-frac t)
  9208. ("S" calc-symbolic-mode t)))
  9209. value (nth 2 var) var (nth 1 var)))
  9210. (if (memq var modes)
  9211. (setcar (cdr (memq var modes)) value)
  9212. (cons var (cons value modes)))
  9213. modes)
  9214. (defun org-table-eval-formula (&optional arg equation
  9215. suppress-align suppress-const
  9216. suppress-store suppress-analysis)
  9217. "Replace the table field value at the cursor by the result of a calculation.
  9218. This function makes use of Dave Gillespie's Calc package, in my view the
  9219. most exciting program ever written for GNU Emacs. So you need to have Calc
  9220. installed in order to use this function.
  9221. In a table, this command replaces the value in the current field with the
  9222. result of a formula. It also installs the formula as the \"current\" column
  9223. formula, by storing it in a special line below the table. When called
  9224. with a `C-u' prefix, the current field must ba a named field, and the
  9225. formula is installed as valid in only this specific field.
  9226. When called with two `C-u' prefixes, insert the active equation
  9227. for the field back into the current field, so that it can be
  9228. edited there. This is useful in order to use \\[org-table-show-reference]
  9229. to check the referenced fields.
  9230. When called, the command first prompts for a formula, which is read in
  9231. the minibuffer. Previously entered formulas are available through the
  9232. history list, and the last used formula is offered as a default.
  9233. These stored formulas are adapted correctly when moving, inserting, or
  9234. deleting columns with the corresponding commands.
  9235. The formula can be any algebraic expression understood by the Calc package.
  9236. For details, see the Org-mode manual.
  9237. This function can also be called from Lisp programs and offers
  9238. additional arguments: EQUATION can be the formula to apply. If this
  9239. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9240. used to speed-up recursive calls by by-passing unnecessary aligns.
  9241. SUPPRESS-CONST suppresses the interpretation of constants in the
  9242. formula, assuming that this has been done already outside the function.
  9243. SUPPRESS-STORE means the formula should not be stored, either because
  9244. it is already stored, or because it is a modified equation that should
  9245. not overwrite the stored one."
  9246. (interactive "P")
  9247. (org-table-check-inside-data-field)
  9248. (or suppress-analysis (org-table-get-specials))
  9249. (if (equal arg '(16))
  9250. (let ((eq (org-table-current-field-formula)))
  9251. (or eq (error "No equation active for current field"))
  9252. (org-table-get-field nil eq)
  9253. (org-table-align)
  9254. (setq org-table-may-need-update t))
  9255. (let* (fields
  9256. (ndown (if (integerp arg) arg 1))
  9257. (org-table-automatic-realign nil)
  9258. (case-fold-search nil)
  9259. (down (> ndown 1))
  9260. (formula (if (and equation suppress-store)
  9261. equation
  9262. (org-table-get-formula equation (equal arg '(4)))))
  9263. (n0 (org-table-current-column))
  9264. (modes (copy-sequence org-calc-default-modes))
  9265. (numbers nil) ; was a variable, now fixed default
  9266. (keep-empty nil)
  9267. n form form0 bw fmt x ev orig c lispp literal)
  9268. ;; Parse the format string. Since we have a lot of modes, this is
  9269. ;; a lot of work. However, I think calc still uses most of the time.
  9270. (if (string-match ";" formula)
  9271. (let ((tmp (org-split-string formula ";")))
  9272. (setq formula (car tmp)
  9273. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9274. (nth 1 tmp)))
  9275. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9276. (setq c (string-to-char (match-string 1 fmt))
  9277. n (string-to-number (match-string 2 fmt)))
  9278. (if (= c ?p)
  9279. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9280. (setq modes (org-set-calc-mode
  9281. 'calc-float-format
  9282. (list (cdr (assoc c '((?n . float) (?f . fix)
  9283. (?s . sci) (?e . eng))))
  9284. n))))
  9285. (setq fmt (replace-match "" t t fmt)))
  9286. (if (string-match "[NT]" fmt)
  9287. (setq numbers (equal (match-string 0 fmt) "N")
  9288. fmt (replace-match "" t t fmt)))
  9289. (if (string-match "L" fmt)
  9290. (setq literal t
  9291. fmt (replace-match "" t t fmt)))
  9292. (if (string-match "E" fmt)
  9293. (setq keep-empty t
  9294. fmt (replace-match "" t t fmt)))
  9295. (while (string-match "[DRFS]" fmt)
  9296. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9297. (setq fmt (replace-match "" t t fmt)))
  9298. (unless (string-match "\\S-" fmt)
  9299. (setq fmt nil))))
  9300. (if (and (not suppress-const) org-table-formula-use-constants)
  9301. (setq formula (org-table-formula-substitute-names formula)))
  9302. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9303. (while (> ndown 0)
  9304. (setq fields (org-split-string
  9305. (org-no-properties
  9306. (buffer-substring (point-at-bol) (point-at-eol)))
  9307. " *| *"))
  9308. (if (eq numbers t)
  9309. (setq fields (mapcar
  9310. (lambda (x) (number-to-string (string-to-number x)))
  9311. fields)))
  9312. (setq ndown (1- ndown))
  9313. (setq form (copy-sequence formula)
  9314. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9315. (if (and lispp literal) (setq lispp 'literal))
  9316. ;; Check for old vertical references
  9317. (setq form (org-rewrite-old-row-references form))
  9318. ;; Insert complex ranges
  9319. (while (string-match org-table-range-regexp form)
  9320. (setq form
  9321. (replace-match
  9322. (save-match-data
  9323. (org-table-make-reference
  9324. (org-table-get-range (match-string 0 form) nil n0)
  9325. keep-empty numbers lispp))
  9326. t t form)))
  9327. ;; Insert simple ranges
  9328. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9329. (setq form
  9330. (replace-match
  9331. (save-match-data
  9332. (org-table-make-reference
  9333. (org-sublist
  9334. fields (string-to-number (match-string 1 form))
  9335. (string-to-number (match-string 2 form)))
  9336. keep-empty numbers lispp))
  9337. t t form)))
  9338. (setq form0 form)
  9339. ;; Insert the references to fields in same row
  9340. (while (string-match "\\$\\([0-9]+\\)" form)
  9341. (setq n (string-to-number (match-string 1 form))
  9342. x (nth (1- (if (= n 0) n0 n)) fields))
  9343. (unless x (error "Invalid field specifier \"%s\""
  9344. (match-string 0 form)))
  9345. (setq form (replace-match
  9346. (save-match-data
  9347. (org-table-make-reference x nil numbers lispp))
  9348. t t form)))
  9349. (if lispp
  9350. (setq ev (condition-case nil
  9351. (eval (eval (read form)))
  9352. (error "#ERROR"))
  9353. ev (if (numberp ev) (number-to-string ev) ev))
  9354. (or (fboundp 'calc-eval)
  9355. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9356. (setq ev (calc-eval (cons form modes)
  9357. (if numbers 'num))))
  9358. (when org-table-formula-debug
  9359. (with-output-to-temp-buffer "*Substitution History*"
  9360. (princ (format "Substitution history of formula
  9361. Orig: %s
  9362. $xyz-> %s
  9363. @r$c-> %s
  9364. $1-> %s\n" orig formula form0 form))
  9365. (if (listp ev)
  9366. (princ (format " %s^\nError: %s"
  9367. (make-string (car ev) ?\-) (nth 1 ev)))
  9368. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9369. ev (or fmt "NONE")
  9370. (if fmt (format fmt (string-to-number ev)) ev)))))
  9371. (setq bw (get-buffer-window "*Substitution History*"))
  9372. (shrink-window-if-larger-than-buffer bw)
  9373. (unless (and (interactive-p) (not ndown))
  9374. (unless (let (inhibit-redisplay)
  9375. (y-or-n-p "Debugging Formula. Continue to next? "))
  9376. (org-table-align)
  9377. (error "Abort"))
  9378. (delete-window bw)
  9379. (message "")))
  9380. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9381. (org-table-justify-field-maybe
  9382. (if fmt (format fmt (string-to-number ev)) ev))
  9383. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9384. (call-interactively 'org-return)
  9385. (setq ndown 0)))
  9386. (and down (org-table-maybe-recalculate-line))
  9387. (or suppress-align (and org-table-may-need-update
  9388. (org-table-align))))))
  9389. (defun org-table-put-field-property (prop value)
  9390. (save-excursion
  9391. (put-text-property (progn (skip-chars-backward "^|") (point))
  9392. (progn (skip-chars-forward "^|") (point))
  9393. prop value)))
  9394. (defun org-table-get-range (desc &optional tbeg col highlight)
  9395. "Get a calc vector from a column, accorting to descriptor DESC.
  9396. Optional arguments TBEG and COL can give the beginning of the table and
  9397. the current column, to avoid unnecessary parsing.
  9398. HIGHLIGHT means, just highlight the range."
  9399. (if (not (equal (string-to-char desc) ?@))
  9400. (setq desc (concat "@" desc)))
  9401. (save-excursion
  9402. (or tbeg (setq tbeg (org-table-begin)))
  9403. (or col (setq col (org-table-current-column)))
  9404. (let ((thisline (org-current-line))
  9405. beg end c1 c2 r1 r2 rangep tmp)
  9406. (unless (string-match org-table-range-regexp desc)
  9407. (error "Invalid table range specifier `%s'" desc))
  9408. (setq rangep (match-end 3)
  9409. r1 (and (match-end 1) (match-string 1 desc))
  9410. r2 (and (match-end 4) (match-string 4 desc))
  9411. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9412. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9413. (and c1 (setq c1 (+ (string-to-number c1)
  9414. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9415. (and c2 (setq c2 (+ (string-to-number c2)
  9416. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9417. (if (equal r1 "") (setq r1 nil))
  9418. (if (equal r2 "") (setq r2 nil))
  9419. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9420. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9421. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9422. (if (not r1) (setq r1 thisline))
  9423. (if (not r2) (setq r2 thisline))
  9424. (if (not c1) (setq c1 col))
  9425. (if (not c2) (setq c2 col))
  9426. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9427. ;; just one field
  9428. (progn
  9429. (goto-line r1)
  9430. (while (not (looking-at org-table-dataline-regexp))
  9431. (beginning-of-line 2))
  9432. (prog1 (org-trim (org-table-get-field c1))
  9433. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9434. ;; A range, return a vector
  9435. ;; First sort the numbers to get a regular ractangle
  9436. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9437. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9438. (goto-line r1)
  9439. (while (not (looking-at org-table-dataline-regexp))
  9440. (beginning-of-line 2))
  9441. (org-table-goto-column c1)
  9442. (setq beg (point))
  9443. (goto-line r2)
  9444. (while (not (looking-at org-table-dataline-regexp))
  9445. (beginning-of-line 0))
  9446. (org-table-goto-column c2)
  9447. (setq end (point))
  9448. (if highlight
  9449. (org-table-highlight-rectangle
  9450. beg (progn (skip-chars-forward "^|\n") (point))))
  9451. ;; return string representation of calc vector
  9452. (mapcar 'org-trim
  9453. (apply 'append (org-table-copy-region beg end)))))))
  9454. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9455. "Analyze descriptor DESC and retrieve the corresponding line number.
  9456. The cursor is currently in line CLINE, the table begins in line BLINE,
  9457. and TABLE is a vector with line types."
  9458. (if (string-match "^[0-9]+$" desc)
  9459. (aref org-table-dlines (string-to-number desc))
  9460. (setq cline (or cline (org-current-line))
  9461. bline (or bline org-table-current-begin-line)
  9462. table (or table org-table-current-line-types))
  9463. (if (or
  9464. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9465. ;; 1 2 3 4 5 6
  9466. (and (not (match-end 3)) (not (match-end 6)))
  9467. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9468. (error "invalid row descriptor `%s'" desc))
  9469. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9470. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9471. (odir (and (match-end 5) (match-string 5 desc)))
  9472. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9473. (i (- cline bline))
  9474. (rel (and (match-end 6)
  9475. (or (and (match-end 1) (not (match-end 3)))
  9476. (match-end 5)))))
  9477. (if (and hn (not hdir))
  9478. (progn
  9479. (setq i 0 hdir "+")
  9480. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9481. (if (and (not hn) on (not odir))
  9482. (error "should never happen");;(aref org-table-dlines on)
  9483. (if (and hn (> hn 0))
  9484. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9485. (if on
  9486. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9487. (+ bline i)))))
  9488. (defun org-find-row-type (table i type backwards relative n)
  9489. (let ((l (length table)))
  9490. (while (> n 0)
  9491. (while (and (setq i (+ i (if backwards -1 1)))
  9492. (>= i 0) (< i l)
  9493. (not (eq (aref table i) type))
  9494. (if (and relative (eq (aref table i) 'hline))
  9495. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9496. t)))
  9497. (setq n (1- n)))
  9498. (if (or (< i 0) (>= i l))
  9499. (error "Row descriptior leads outside table")
  9500. i)))
  9501. (defun org-rewrite-old-row-references (s)
  9502. (if (string-match "&[-+0-9I]" s)
  9503. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9504. s))
  9505. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9506. "Convert list ELEMENTS to something appropriate to insert into formula.
  9507. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9508. NUMBERS indicates that everything should be converted to numbers.
  9509. LISPP means to return something appropriate for a Lisp list."
  9510. (if (stringp elements) ; just a single val
  9511. (if lispp
  9512. (if (eq lispp 'literal)
  9513. elements
  9514. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9515. (if (equal elements "") (setq elements "0"))
  9516. (if numbers (number-to-string (string-to-number elements)) elements))
  9517. (unless keep-empty
  9518. (setq elements
  9519. (delq nil
  9520. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9521. elements))))
  9522. (setq elements (or elements '("0")))
  9523. (if lispp
  9524. (mapconcat
  9525. (lambda (x)
  9526. (if (eq lispp 'literal)
  9527. x
  9528. (prin1-to-string (if numbers (string-to-number x) x))))
  9529. elements " ")
  9530. (concat "[" (mapconcat
  9531. (lambda (x)
  9532. (if numbers (number-to-string (string-to-number x)) x))
  9533. elements
  9534. ",") "]"))))
  9535. (defun org-table-recalculate (&optional all noalign)
  9536. "Recalculate the current table line by applying all stored formulas.
  9537. With prefix arg ALL, do this for all lines in the table."
  9538. (interactive "P")
  9539. (or (memq this-command org-recalc-commands)
  9540. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9541. (unless (org-at-table-p) (error "Not at a table"))
  9542. (if (equal all '(16))
  9543. (org-table-iterate)
  9544. (org-table-get-specials)
  9545. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9546. (lambda (a b) (string< (car a) (car b)))))
  9547. (inhibit-redisplay (not debug-on-error))
  9548. (line-re org-table-dataline-regexp)
  9549. (thisline (org-current-line))
  9550. (thiscol (org-table-current-column))
  9551. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9552. ;; Insert constants in all formulas
  9553. (setq eqlist
  9554. (mapcar (lambda (x)
  9555. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9556. x)
  9557. eqlist))
  9558. ;; Split the equation list
  9559. (while (setq eq (pop eqlist))
  9560. (if (<= (string-to-char (car eq)) ?9)
  9561. (push eq eqlnum)
  9562. (push eq eqlname)))
  9563. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9564. (if all
  9565. (progn
  9566. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9567. (goto-char (setq beg (org-table-begin)))
  9568. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9569. ;; This is a table with marked lines, compute selected lines
  9570. (setq line-re org-table-recalculate-regexp)
  9571. ;; Move forward to the first non-header line
  9572. (if (and (re-search-forward org-table-dataline-regexp end t)
  9573. (re-search-forward org-table-hline-regexp end t)
  9574. (re-search-forward org-table-dataline-regexp end t))
  9575. (setq beg (match-beginning 0))
  9576. nil))) ;; just leave beg where it is
  9577. (setq beg (point-at-bol)
  9578. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9579. (goto-char beg)
  9580. (and all (message "Re-applying formulas to full table..."))
  9581. ;; First find the named fields, and mark them untouchanble
  9582. (remove-text-properties beg end '(org-untouchable t))
  9583. (while (setq eq (pop eqlname))
  9584. (setq name (car eq)
  9585. a (assoc name org-table-named-field-locations))
  9586. (and (not a)
  9587. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9588. (setq a (list name
  9589. (aref org-table-dlines
  9590. (string-to-number (match-string 1 name)))
  9591. (string-to-number (match-string 2 name)))))
  9592. (when (and a (or all (equal (nth 1 a) thisline)))
  9593. (message "Re-applying formula to field: %s" name)
  9594. (goto-line (nth 1 a))
  9595. (org-table-goto-column (nth 2 a))
  9596. (push (append a (list (cdr eq))) eqlname1)
  9597. (org-table-put-field-property :org-untouchable t)))
  9598. ;; Now evauluate the column formulas, but skip fields covered by
  9599. ;; field formulas
  9600. (goto-char beg)
  9601. (while (re-search-forward line-re end t)
  9602. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9603. ;; Unprotected line, recalculate
  9604. (and all (message "Re-applying formulas to full table...(line %d)"
  9605. (setq cnt (1+ cnt))))
  9606. (setq org-last-recalc-line (org-current-line))
  9607. (setq eql eqlnum)
  9608. (while (setq entry (pop eql))
  9609. (goto-line org-last-recalc-line)
  9610. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9611. (unless (get-text-property (point) :org-untouchable)
  9612. (org-table-eval-formula nil (cdr entry)
  9613. 'noalign 'nocst 'nostore 'noanalysis)))))
  9614. ;; Now evaluate the field formulas
  9615. (while (setq eq (pop eqlname1))
  9616. (message "Re-applying formula to field: %s" (car eq))
  9617. (goto-line (nth 1 eq))
  9618. (org-table-goto-column (nth 2 eq))
  9619. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9620. 'nostore 'noanalysis))
  9621. (goto-line thisline)
  9622. (org-table-goto-column thiscol)
  9623. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9624. (or noalign (and org-table-may-need-update (org-table-align))
  9625. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9626. ;; back to initial position
  9627. (message "Re-applying formulas...done")
  9628. (goto-line thisline)
  9629. (org-table-goto-column thiscol)
  9630. (or noalign (and org-table-may-need-update (org-table-align))
  9631. (and all (message "Re-applying formulas...done"))))))
  9632. (defun org-table-iterate (&optional arg)
  9633. "Recalculate the table until it does not change anymore."
  9634. (interactive "P")
  9635. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9636. (i 0)
  9637. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9638. thistbl)
  9639. (catch 'exit
  9640. (while (< i imax)
  9641. (setq i (1+ i))
  9642. (org-table-recalculate 'all)
  9643. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9644. (if (not (string= lasttbl thistbl))
  9645. (setq lasttbl thistbl)
  9646. (if (> i 1)
  9647. (message "Convergence after %d iterations" i)
  9648. (message "Table was already stable"))
  9649. (throw 'exit t)))
  9650. (error "No convergence after %d iterations" i))))
  9651. (defun org-table-formula-substitute-names (f)
  9652. "Replace $const with values in string F."
  9653. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9654. ;; First, check for column names
  9655. (while (setq start (string-match org-table-column-name-regexp f start))
  9656. (setq start (1+ start))
  9657. (setq a (assoc (match-string 1 f) org-table-column-names))
  9658. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9659. ;; Parameters and constants
  9660. (setq start 0)
  9661. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9662. (setq start (1+ start))
  9663. (if (setq a (save-match-data
  9664. (org-table-get-constant (match-string 1 f))))
  9665. (setq f (replace-match
  9666. (concat (if pp "(") a (if pp ")")) t t f))))
  9667. (if org-table-formula-debug
  9668. (put-text-property 0 (length f) :orig-formula f1 f))
  9669. f))
  9670. (defun org-table-get-constant (const)
  9671. "Find the value for a parameter or constant in a formula.
  9672. Parameters get priority."
  9673. (or (cdr (assoc const org-table-local-parameters))
  9674. (cdr (assoc const org-table-formula-constants-local))
  9675. (cdr (assoc const org-table-formula-constants))
  9676. (and (fboundp 'constants-get) (constants-get const))
  9677. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9678. (org-entry-get nil (substring const 5) 'inherit))
  9679. "#UNDEFINED_NAME"))
  9680. (defvar org-table-fedit-map
  9681. (let ((map (make-sparse-keymap)))
  9682. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9683. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9684. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9685. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9686. (org-defkey map "\C-c?" 'org-table-show-reference)
  9687. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9688. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9689. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9690. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9691. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9692. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9693. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9694. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9695. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9696. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9697. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9698. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9699. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9700. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9701. map))
  9702. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9703. '("Edit-Formulas"
  9704. ["Finish and Install" org-table-fedit-finish t]
  9705. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9706. ["Abort" org-table-fedit-abort t]
  9707. "--"
  9708. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9709. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9710. "--"
  9711. "Shift Reference at Point"
  9712. ["Up" org-table-fedit-ref-up t]
  9713. ["Down" org-table-fedit-ref-down t]
  9714. ["Left" org-table-fedit-ref-left t]
  9715. ["Right" org-table-fedit-ref-right t]
  9716. "-"
  9717. "Change Test Row for Column Formulas"
  9718. ["Up" org-table-fedit-line-up t]
  9719. ["Down" org-table-fedit-line-down t]
  9720. "--"
  9721. ["Scroll Table Window" org-table-fedit-scroll t]
  9722. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9723. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9724. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9725. org-table-overlay-coordinates)]
  9726. "--"
  9727. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9728. :style toggle :selected org-table-buffer-is-an]))
  9729. (defvar org-pos)
  9730. (defun org-table-edit-formulas ()
  9731. "Edit the formulas of the current table in a separate buffer."
  9732. (interactive)
  9733. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9734. (beginning-of-line 0))
  9735. (unless (org-at-table-p) (error "Not at a table"))
  9736. (org-table-get-specials)
  9737. (let ((key (org-table-current-field-formula 'key 'noerror))
  9738. (eql (sort (org-table-get-stored-formulas 'noerror)
  9739. 'org-table-formula-less-p))
  9740. (pos (move-marker (make-marker) (point)))
  9741. (startline 1)
  9742. (wc (current-window-configuration))
  9743. (titles '((column . "# Column Formulas\n")
  9744. (field . "# Field Formulas\n")
  9745. (named . "# Named Field Formulas\n")))
  9746. entry s type title)
  9747. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9748. (erase-buffer)
  9749. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9750. (let ((font-lock-global-modes '(not fundamental-mode)))
  9751. (fundamental-mode))
  9752. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9753. (org-set-local 'org-pos pos)
  9754. (org-set-local 'org-window-configuration wc)
  9755. (use-local-map org-table-fedit-map)
  9756. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9757. (easy-menu-add org-table-fedit-menu)
  9758. (setq startline (org-current-line))
  9759. (while (setq entry (pop eql))
  9760. (setq type (cond
  9761. ((equal (string-to-char (car entry)) ?@) 'field)
  9762. ((string-match "^[0-9]" (car entry)) 'column)
  9763. (t 'named)))
  9764. (when (setq title (assq type titles))
  9765. (or (bobp) (insert "\n"))
  9766. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9767. (setq titles (delq title titles)))
  9768. (if (equal key (car entry)) (setq startline (org-current-line)))
  9769. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9770. (car entry) " = " (cdr entry) "\n"))
  9771. (remove-text-properties 0 (length s) '(face nil) s)
  9772. (insert s))
  9773. (if (eq org-table-use-standard-references t)
  9774. (org-table-fedit-toggle-ref-type))
  9775. (goto-line startline)
  9776. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9777. (defun org-table-fedit-post-command ()
  9778. (when (not (memq this-command '(lisp-complete-symbol)))
  9779. (let ((win (selected-window)))
  9780. (save-excursion
  9781. (condition-case nil
  9782. (org-table-show-reference)
  9783. (error nil))
  9784. (select-window win)))))
  9785. (defun org-table-formula-to-user (s)
  9786. "Convert a formula from internal to user representation."
  9787. (if (eq org-table-use-standard-references t)
  9788. (org-table-convert-refs-to-an s)
  9789. s))
  9790. (defun org-table-formula-from-user (s)
  9791. "Convert a formula from user to internal representation."
  9792. (if org-table-use-standard-references
  9793. (org-table-convert-refs-to-rc s)
  9794. s))
  9795. (defun org-table-convert-refs-to-rc (s)
  9796. "Convert spreadsheet references from AB7 to @7$28.
  9797. Works for single references, but also for entire formulas and even the
  9798. full TBLFM line."
  9799. (let ((start 0))
  9800. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9801. (cond
  9802. ((match-end 3)
  9803. ;; format match, just advance
  9804. (setq start (match-end 0)))
  9805. ((and (> (match-beginning 0) 0)
  9806. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9807. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9808. ;; 3.e5 or something like this.
  9809. (setq start (match-end 0)))
  9810. (t
  9811. (setq start (match-beginning 0)
  9812. s (replace-match
  9813. (if (equal (match-string 2 s) "&")
  9814. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9815. (format "@%d$%d"
  9816. (string-to-number (match-string 2 s))
  9817. (org-letters-to-number (match-string 1 s))))
  9818. t t s)))))
  9819. s))
  9820. (defun org-table-convert-refs-to-an (s)
  9821. "Convert spreadsheet references from to @7$28 to AB7.
  9822. Works for single references, but also for entire formulas and even the
  9823. full TBLFM line."
  9824. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9825. (setq s (replace-match
  9826. (format "%s%d"
  9827. (org-number-to-letters
  9828. (string-to-number (match-string 2 s)))
  9829. (string-to-number (match-string 1 s)))
  9830. t t s)))
  9831. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9832. (setq s (replace-match (concat "\\1"
  9833. (org-number-to-letters
  9834. (string-to-number (match-string 2 s))) "&")
  9835. t nil s)))
  9836. s)
  9837. (defun org-letters-to-number (s)
  9838. "Convert a base 26 number represented by letters into an integer.
  9839. For example: AB -> 28."
  9840. (let ((n 0))
  9841. (setq s (upcase s))
  9842. (while (> (length s) 0)
  9843. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9844. s (substring s 1)))
  9845. n))
  9846. (defun org-number-to-letters (n)
  9847. "Convert an integer into a base 26 number represented by letters.
  9848. For example: 28 -> AB."
  9849. (let ((s ""))
  9850. (while (> n 0)
  9851. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9852. n (/ (1- n) 26)))
  9853. s))
  9854. (defun org-table-fedit-convert-buffer (function)
  9855. "Convert all references in this buffer, using FUNTION."
  9856. (let ((line (org-current-line)))
  9857. (goto-char (point-min))
  9858. (while (not (eobp))
  9859. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9860. (delete-region (point) (point-at-eol))
  9861. (or (eobp) (forward-char 1)))
  9862. (goto-line line)))
  9863. (defun org-table-fedit-toggle-ref-type ()
  9864. "Convert all references in the buffer from B3 to @3$2 and back."
  9865. (interactive)
  9866. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9867. (org-table-fedit-convert-buffer
  9868. (if org-table-buffer-is-an
  9869. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9870. (message "Reference type switched to %s"
  9871. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9872. (defun org-table-fedit-ref-up ()
  9873. "Shift the reference at point one row/hline up."
  9874. (interactive)
  9875. (org-table-fedit-shift-reference 'up))
  9876. (defun org-table-fedit-ref-down ()
  9877. "Shift the reference at point one row/hline down."
  9878. (interactive)
  9879. (org-table-fedit-shift-reference 'down))
  9880. (defun org-table-fedit-ref-left ()
  9881. "Shift the reference at point one field to the left."
  9882. (interactive)
  9883. (org-table-fedit-shift-reference 'left))
  9884. (defun org-table-fedit-ref-right ()
  9885. "Shift the reference at point one field to the right."
  9886. (interactive)
  9887. (org-table-fedit-shift-reference 'right))
  9888. (defun org-table-fedit-shift-reference (dir)
  9889. (cond
  9890. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9891. (if (memq dir '(left right))
  9892. (org-rematch-and-replace 1 (eq dir 'left))
  9893. (error "Cannot shift reference in this direction")))
  9894. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9895. ;; A B3-like reference
  9896. (if (memq dir '(up down))
  9897. (org-rematch-and-replace 2 (eq dir 'up))
  9898. (org-rematch-and-replace 1 (eq dir 'left))))
  9899. ((org-at-regexp-p
  9900. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9901. ;; An internal reference
  9902. (if (memq dir '(up down))
  9903. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9904. (org-rematch-and-replace 5 (eq dir 'left))))))
  9905. (defun org-rematch-and-replace (n &optional decr hline)
  9906. "Re-match the group N, and replace it with the shifted refrence."
  9907. (or (match-end n) (error "Cannot shift reference in this direction"))
  9908. (goto-char (match-beginning n))
  9909. (and (looking-at (regexp-quote (match-string n)))
  9910. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9911. t t)))
  9912. (defun org-shift-refpart (ref &optional decr hline)
  9913. "Shift a refrence part REF.
  9914. If DECR is set, decrease the references row/column, else increase.
  9915. If HLINE is set, this may be a hline reference, it certainly is not
  9916. a translation reference."
  9917. (save-match-data
  9918. (let* ((sign (string-match "^[-+]" ref)) n)
  9919. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9920. (cond
  9921. ((and hline (string-match "^I+" ref))
  9922. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9923. (setq n (+ n (if decr -1 1)))
  9924. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9925. (if sign
  9926. (setq sign (if (< n 0) "-" "+") n (abs n))
  9927. (setq n (max 1 n)))
  9928. (concat sign (make-string n ?I)))
  9929. ((string-match "^[0-9]+" ref)
  9930. (setq n (string-to-number (concat sign ref)))
  9931. (setq n (+ n (if decr -1 1)))
  9932. (if sign
  9933. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9934. (number-to-string (max 1 n))))
  9935. ((string-match "^[a-zA-Z]+" ref)
  9936. (org-number-to-letters
  9937. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9938. (t (error "Cannot shift reference"))))))
  9939. (defun org-table-fedit-toggle-coordinates ()
  9940. "Toggle the display of coordinates in the refrenced table."
  9941. (interactive)
  9942. (let ((pos (marker-position org-pos)))
  9943. (with-current-buffer (marker-buffer org-pos)
  9944. (save-excursion
  9945. (goto-char pos)
  9946. (org-table-toggle-coordinate-overlays)))))
  9947. (defun org-table-fedit-finish (&optional arg)
  9948. "Parse the buffer for formula definitions and install them.
  9949. With prefix ARG, apply the new formulas to the table."
  9950. (interactive "P")
  9951. (org-table-remove-rectangle-highlight)
  9952. (if org-table-use-standard-references
  9953. (progn
  9954. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9955. (setq org-table-buffer-is-an nil)))
  9956. (let ((pos org-pos) eql var form)
  9957. (goto-char (point-min))
  9958. (while (re-search-forward
  9959. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9960. nil t)
  9961. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9962. form (match-string 3))
  9963. (setq form (org-trim form))
  9964. (when (not (equal form ""))
  9965. (while (string-match "[ \t]*\n[ \t]*" form)
  9966. (setq form (replace-match " " t t form)))
  9967. (when (assoc var eql)
  9968. (error "Double formulas for %s" var))
  9969. (push (cons var form) eql)))
  9970. (setq org-pos nil)
  9971. (set-window-configuration org-window-configuration)
  9972. (select-window (get-buffer-window (marker-buffer pos)))
  9973. (goto-char pos)
  9974. (unless (org-at-table-p)
  9975. (error "Lost table position - cannot install formulae"))
  9976. (org-table-store-formulas eql)
  9977. (move-marker pos nil)
  9978. (kill-buffer "*Edit Formulas*")
  9979. (if arg
  9980. (org-table-recalculate 'all)
  9981. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9982. (defun org-table-fedit-abort ()
  9983. "Abort editing formulas, without installing the changes."
  9984. (interactive)
  9985. (org-table-remove-rectangle-highlight)
  9986. (let ((pos org-pos))
  9987. (set-window-configuration org-window-configuration)
  9988. (select-window (get-buffer-window (marker-buffer pos)))
  9989. (goto-char pos)
  9990. (move-marker pos nil)
  9991. (message "Formula editing aborted without installing changes")))
  9992. (defun org-table-fedit-lisp-indent ()
  9993. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9994. (interactive)
  9995. (let ((pos (point)) beg end ind)
  9996. (beginning-of-line 1)
  9997. (cond
  9998. ((looking-at "[ \t]")
  9999. (goto-char pos)
  10000. (call-interactively 'lisp-indent-line))
  10001. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10002. ((not (fboundp 'pp-buffer))
  10003. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10004. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10005. (goto-char (- (match-end 0) 2))
  10006. (setq beg (point))
  10007. (setq ind (make-string (current-column) ?\ ))
  10008. (condition-case nil (forward-sexp 1)
  10009. (error
  10010. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10011. (setq end (point))
  10012. (save-restriction
  10013. (narrow-to-region beg end)
  10014. (if (eq last-command this-command)
  10015. (progn
  10016. (goto-char (point-min))
  10017. (setq this-command nil)
  10018. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10019. (replace-match " ")))
  10020. (pp-buffer)
  10021. (untabify (point-min) (point-max))
  10022. (goto-char (1+ (point-min)))
  10023. (while (re-search-forward "^." nil t)
  10024. (beginning-of-line 1)
  10025. (insert ind))
  10026. (goto-char (point-max))
  10027. (backward-delete-char 1)))
  10028. (goto-char beg))
  10029. (t nil))))
  10030. (defvar org-show-positions nil)
  10031. (defun org-table-show-reference (&optional local)
  10032. "Show the location/value of the $ expression at point."
  10033. (interactive)
  10034. (org-table-remove-rectangle-highlight)
  10035. (catch 'exit
  10036. (let ((pos (if local (point) org-pos))
  10037. (face2 'highlight)
  10038. (org-inhibit-highlight-removal t)
  10039. (win (selected-window))
  10040. (org-show-positions nil)
  10041. var name e what match dest)
  10042. (if local (org-table-get-specials))
  10043. (setq what (cond
  10044. ((or (org-at-regexp-p org-table-range-regexp2)
  10045. (org-at-regexp-p org-table-translate-regexp)
  10046. (org-at-regexp-p org-table-range-regexp))
  10047. (setq match
  10048. (save-match-data
  10049. (org-table-convert-refs-to-rc (match-string 0))))
  10050. 'range)
  10051. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10052. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10053. ((not local) nil)
  10054. (t (error "No reference at point")))
  10055. match (and what (or match (match-string 0))))
  10056. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10057. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10058. 'secondary-selection))
  10059. (org-add-hook 'before-change-functions
  10060. 'org-table-remove-rectangle-highlight)
  10061. (if (eq what 'name) (setq var (substring match 1)))
  10062. (when (eq what 'range)
  10063. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10064. (setq match (org-table-formula-substitute-names match)))
  10065. (unless local
  10066. (save-excursion
  10067. (end-of-line 1)
  10068. (re-search-backward "^\\S-" nil t)
  10069. (beginning-of-line 1)
  10070. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10071. (setq dest
  10072. (save-match-data
  10073. (org-table-convert-refs-to-rc (match-string 1))))
  10074. (org-table-add-rectangle-overlay
  10075. (match-beginning 1) (match-end 1) face2))))
  10076. (if (and (markerp pos) (marker-buffer pos))
  10077. (if (get-buffer-window (marker-buffer pos))
  10078. (select-window (get-buffer-window (marker-buffer pos)))
  10079. (org-switch-to-buffer-other-window (get-buffer-window
  10080. (marker-buffer pos)))))
  10081. (goto-char pos)
  10082. (org-table-force-dataline)
  10083. (when dest
  10084. (setq name (substring dest 1))
  10085. (cond
  10086. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10087. (setq e (assoc name org-table-named-field-locations))
  10088. (goto-line (nth 1 e))
  10089. (org-table-goto-column (nth 2 e)))
  10090. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10091. (let ((l (string-to-number (match-string 1 dest)))
  10092. (c (string-to-number (match-string 2 dest))))
  10093. (goto-line (aref org-table-dlines l))
  10094. (org-table-goto-column c)))
  10095. (t (org-table-goto-column (string-to-number name))))
  10096. (move-marker pos (point))
  10097. (org-table-highlight-rectangle nil nil face2))
  10098. (cond
  10099. ((equal dest match))
  10100. ((not match))
  10101. ((eq what 'range)
  10102. (condition-case nil
  10103. (save-excursion
  10104. (org-table-get-range match nil nil 'highlight))
  10105. (error nil)))
  10106. ((setq e (assoc var org-table-named-field-locations))
  10107. (goto-line (nth 1 e))
  10108. (org-table-goto-column (nth 2 e))
  10109. (org-table-highlight-rectangle (point) (point))
  10110. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10111. ((setq e (assoc var org-table-column-names))
  10112. (org-table-goto-column (string-to-number (cdr e)))
  10113. (org-table-highlight-rectangle (point) (point))
  10114. (goto-char (org-table-begin))
  10115. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10116. (org-table-end) t)
  10117. (progn
  10118. (goto-char (match-beginning 1))
  10119. (org-table-highlight-rectangle)
  10120. (message "Named column (column %s)" (cdr e)))
  10121. (error "Column name not found")))
  10122. ((eq what 'column)
  10123. ;; column number
  10124. (org-table-goto-column (string-to-number (substring match 1)))
  10125. (org-table-highlight-rectangle (point) (point))
  10126. (message "Column %s" (substring match 1)))
  10127. ((setq e (assoc var org-table-local-parameters))
  10128. (goto-char (org-table-begin))
  10129. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10130. (progn
  10131. (goto-char (match-beginning 1))
  10132. (org-table-highlight-rectangle)
  10133. (message "Local parameter."))
  10134. (error "Parameter not found")))
  10135. (t
  10136. (cond
  10137. ((not var) (error "No reference at point"))
  10138. ((setq e (assoc var org-table-formula-constants-local))
  10139. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10140. var (cdr e)))
  10141. ((setq e (assoc var org-table-formula-constants))
  10142. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10143. var (cdr e)))
  10144. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10145. (message "Constant: $%s=%s, from `constants.el'%s."
  10146. var e (format " (%s units)" constants-unit-system)))
  10147. (t (error "Undefined name $%s" var)))))
  10148. (goto-char pos)
  10149. (when (and org-show-positions
  10150. (not (memq this-command '(org-table-fedit-scroll
  10151. org-table-fedit-scroll-down))))
  10152. (push pos org-show-positions)
  10153. (push org-table-current-begin-pos org-show-positions)
  10154. (let ((min (apply 'min org-show-positions))
  10155. (max (apply 'max org-show-positions)))
  10156. (goto-char min) (recenter 0)
  10157. (goto-char max)
  10158. (or (pos-visible-in-window-p max) (recenter -1))))
  10159. (select-window win))))
  10160. (defun org-table-force-dataline ()
  10161. "Make sure the cursor is in a dataline in a table."
  10162. (unless (save-excursion
  10163. (beginning-of-line 1)
  10164. (looking-at org-table-dataline-regexp))
  10165. (let* ((re org-table-dataline-regexp)
  10166. (p1 (save-excursion (re-search-forward re nil 'move)))
  10167. (p2 (save-excursion (re-search-backward re nil 'move))))
  10168. (cond ((and p1 p2)
  10169. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10170. p1 p2)))
  10171. ((or p1 p2) (goto-char (or p1 p2)))
  10172. (t (error "No table dataline around here"))))))
  10173. (defun org-table-fedit-line-up ()
  10174. "Move cursor one line up in the window showing the table."
  10175. (interactive)
  10176. (org-table-fedit-move 'previous-line))
  10177. (defun org-table-fedit-line-down ()
  10178. "Move cursor one line down in the window showing the table."
  10179. (interactive)
  10180. (org-table-fedit-move 'next-line))
  10181. (defun org-table-fedit-move (command)
  10182. "Move the cursor in the window shoinw the table.
  10183. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10184. (let ((org-table-allow-automatic-line-recalculation nil)
  10185. (pos org-pos) (win (selected-window)) p)
  10186. (select-window (get-buffer-window (marker-buffer org-pos)))
  10187. (setq p (point))
  10188. (call-interactively command)
  10189. (while (and (org-at-table-p)
  10190. (org-at-table-hline-p))
  10191. (call-interactively command))
  10192. (or (org-at-table-p) (goto-char p))
  10193. (move-marker pos (point))
  10194. (select-window win)))
  10195. (defun org-table-fedit-scroll (N)
  10196. (interactive "p")
  10197. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10198. (scroll-other-window N)))
  10199. (defun org-table-fedit-scroll-down (N)
  10200. (interactive "p")
  10201. (org-table-fedit-scroll (- N)))
  10202. (defvar org-table-rectangle-overlays nil)
  10203. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10204. "Add a new overlay."
  10205. (let ((ov (org-make-overlay beg end)))
  10206. (org-overlay-put ov 'face (or face 'secondary-selection))
  10207. (push ov org-table-rectangle-overlays)))
  10208. (defun org-table-highlight-rectangle (&optional beg end face)
  10209. "Highlight rectangular region in a table."
  10210. (setq beg (or beg (point)) end (or end (point)))
  10211. (let ((b (min beg end))
  10212. (e (max beg end))
  10213. l1 c1 l2 c2 tmp)
  10214. (and (boundp 'org-show-positions)
  10215. (setq org-show-positions (cons b (cons e org-show-positions))))
  10216. (goto-char (min beg end))
  10217. (setq l1 (org-current-line)
  10218. c1 (org-table-current-column))
  10219. (goto-char (max beg end))
  10220. (setq l2 (org-current-line)
  10221. c2 (org-table-current-column))
  10222. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10223. (goto-line l1)
  10224. (beginning-of-line 1)
  10225. (loop for line from l1 to l2 do
  10226. (when (looking-at org-table-dataline-regexp)
  10227. (org-table-goto-column c1)
  10228. (skip-chars-backward "^|\n") (setq beg (point))
  10229. (org-table-goto-column c2)
  10230. (skip-chars-forward "^|\n") (setq end (point))
  10231. (org-table-add-rectangle-overlay beg end face))
  10232. (beginning-of-line 2))
  10233. (goto-char b))
  10234. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10235. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10236. "Remove the rectangle overlays."
  10237. (unless org-inhibit-highlight-removal
  10238. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10239. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10240. (setq org-table-rectangle-overlays nil)))
  10241. (defvar org-table-coordinate-overlays nil
  10242. "Collects the cooordinate grid overlays, so that they can be removed.")
  10243. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10244. (defun org-table-overlay-coordinates ()
  10245. "Add overlays to the table at point, to show row/column coordinates."
  10246. (interactive)
  10247. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10248. (setq org-table-coordinate-overlays nil)
  10249. (save-excursion
  10250. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10251. (goto-char (org-table-begin))
  10252. (while (org-at-table-p)
  10253. (setq eol (point-at-eol))
  10254. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10255. (push ov org-table-coordinate-overlays)
  10256. (setq hline (looking-at org-table-hline-regexp))
  10257. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10258. (format "%4d" (setq id (1+ id)))))
  10259. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10260. (when hline
  10261. (setq ic 0)
  10262. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10263. (setq beg (1+ (match-beginning 0))
  10264. ic (1+ ic)
  10265. s1 (concat "$" (int-to-string ic))
  10266. s2 (org-number-to-letters ic)
  10267. str (if (eq org-table-use-standard-references t) s2 s1))
  10268. (setq ov (org-make-overlay beg (+ beg (length str))))
  10269. (push ov org-table-coordinate-overlays)
  10270. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10271. (beginning-of-line 2)))))
  10272. (defun org-table-toggle-coordinate-overlays ()
  10273. "Toggle the display of Row/Column numbers in tables."
  10274. (interactive)
  10275. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10276. (message "Row/Column number display turned %s"
  10277. (if org-table-overlay-coordinates "on" "off"))
  10278. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10279. (org-table-align))
  10280. (unless org-table-overlay-coordinates
  10281. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10282. (setq org-table-coordinate-overlays nil)))
  10283. (defun org-table-toggle-formula-debugger ()
  10284. "Toggle the formula debugger in tables."
  10285. (interactive)
  10286. (setq org-table-formula-debug (not org-table-formula-debug))
  10287. (message "Formula debugging has been turned %s"
  10288. (if org-table-formula-debug "on" "off")))
  10289. ;;; The orgtbl minor mode
  10290. ;; Define a minor mode which can be used in other modes in order to
  10291. ;; integrate the org-mode table editor.
  10292. ;; This is really a hack, because the org-mode table editor uses several
  10293. ;; keys which normally belong to the major mode, for example the TAB and
  10294. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10295. ;; necessary to operate the table editor, but wraps the commands into a
  10296. ;; function which tests if the cursor is currently inside a table. If that
  10297. ;; is the case, the table editor command is executed. However, when any of
  10298. ;; those keys is used outside a table, the function uses `key-binding' to
  10299. ;; look up if the key has an associated command in another currently active
  10300. ;; keymap (minor modes, major mode, global), and executes that command.
  10301. ;; There might be problems if any of the keys used by the table editor is
  10302. ;; otherwise used as a prefix key.
  10303. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10304. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10305. ;; addresses this by checking explicitly for both bindings.
  10306. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10307. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10308. ;; Some modes bind other commands to simple characters, for example
  10309. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10310. ;; active, this binding is ignored inside tables and replaced with a
  10311. ;; modified self-insert.
  10312. (defvar orgtbl-mode nil
  10313. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10314. table editor in arbitrary modes.")
  10315. (make-variable-buffer-local 'orgtbl-mode)
  10316. (defvar orgtbl-mode-map (make-keymap)
  10317. "Keymap for `orgtbl-mode'.")
  10318. ;;;###autoload
  10319. (defun turn-on-orgtbl ()
  10320. "Unconditionally turn on `orgtbl-mode'."
  10321. (orgtbl-mode 1))
  10322. (defvar org-old-auto-fill-inhibit-regexp nil
  10323. "Local variable used by `orgtbl-mode'")
  10324. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10325. "Matches a line belonging to an orgtbl.")
  10326. (defconst orgtbl-extra-font-lock-keywords
  10327. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10328. 0 (quote 'org-table) 'prepend))
  10329. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10330. ;;;###autoload
  10331. (defun orgtbl-mode (&optional arg)
  10332. "The `org-mode' table editor as a minor mode for use in other modes."
  10333. (interactive)
  10334. (if (org-mode-p)
  10335. ;; Exit without error, in case some hook functions calls this
  10336. ;; by accident in org-mode.
  10337. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10338. (setq orgtbl-mode
  10339. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10340. (if orgtbl-mode
  10341. (progn
  10342. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10343. ;; Make sure we are first in minor-mode-map-alist
  10344. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10345. (and c (setq minor-mode-map-alist
  10346. (cons c (delq c minor-mode-map-alist)))))
  10347. (org-set-local (quote org-table-may-need-update) t)
  10348. (org-add-hook 'before-change-functions 'org-before-change-function
  10349. nil 'local)
  10350. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10351. auto-fill-inhibit-regexp)
  10352. (org-set-local 'auto-fill-inhibit-regexp
  10353. (if auto-fill-inhibit-regexp
  10354. (concat orgtbl-line-start-regexp "\\|"
  10355. auto-fill-inhibit-regexp)
  10356. orgtbl-line-start-regexp))
  10357. (org-add-to-invisibility-spec '(org-cwidth))
  10358. (when (fboundp 'font-lock-add-keywords)
  10359. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10360. (org-restart-font-lock))
  10361. (easy-menu-add orgtbl-mode-menu)
  10362. (run-hooks 'orgtbl-mode-hook))
  10363. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10364. (org-cleanup-narrow-column-properties)
  10365. (org-remove-from-invisibility-spec '(org-cwidth))
  10366. (remove-hook 'before-change-functions 'org-before-change-function t)
  10367. (when (fboundp 'font-lock-remove-keywords)
  10368. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10369. (org-restart-font-lock))
  10370. (easy-menu-remove orgtbl-mode-menu)
  10371. (force-mode-line-update 'all))))
  10372. (defun org-cleanup-narrow-column-properties ()
  10373. "Remove all properties related to narrow-column invisibility."
  10374. (let ((s 1))
  10375. (while (setq s (text-property-any s (point-max)
  10376. 'display org-narrow-column-arrow))
  10377. (remove-text-properties s (1+ s) '(display t)))
  10378. (setq s 1)
  10379. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10380. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10381. (setq s 1)
  10382. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10383. (remove-text-properties s (1+ s) '(invisible t)))))
  10384. ;; Install it as a minor mode.
  10385. (put 'orgtbl-mode :included t)
  10386. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10387. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10388. (defun orgtbl-make-binding (fun n &rest keys)
  10389. "Create a function for binding in the table minor mode.
  10390. FUN is the command to call inside a table. N is used to create a unique
  10391. command name. KEYS are keys that should be checked in for a command
  10392. to execute outside of tables."
  10393. (eval
  10394. (list 'defun
  10395. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10396. '(arg)
  10397. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10398. "Outside of tables, run the binding of `"
  10399. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10400. "'.")
  10401. '(interactive "p")
  10402. (list 'if
  10403. '(org-at-table-p)
  10404. (list 'call-interactively (list 'quote fun))
  10405. (list 'let '(orgtbl-mode)
  10406. (list 'call-interactively
  10407. (append '(or)
  10408. (mapcar (lambda (k)
  10409. (list 'key-binding k))
  10410. keys)
  10411. '('orgtbl-error))))))))
  10412. (defun orgtbl-error ()
  10413. "Error when there is no default binding for a table key."
  10414. (interactive)
  10415. (error "This key has no function outside tables"))
  10416. (defun orgtbl-setup ()
  10417. "Setup orgtbl keymaps."
  10418. (let ((nfunc 0)
  10419. (bindings
  10420. (list
  10421. '([(meta shift left)] org-table-delete-column)
  10422. '([(meta left)] org-table-move-column-left)
  10423. '([(meta right)] org-table-move-column-right)
  10424. '([(meta shift right)] org-table-insert-column)
  10425. '([(meta shift up)] org-table-kill-row)
  10426. '([(meta shift down)] org-table-insert-row)
  10427. '([(meta up)] org-table-move-row-up)
  10428. '([(meta down)] org-table-move-row-down)
  10429. '("\C-c\C-w" org-table-cut-region)
  10430. '("\C-c\M-w" org-table-copy-region)
  10431. '("\C-c\C-y" org-table-paste-rectangle)
  10432. '("\C-c-" org-table-insert-hline)
  10433. '("\C-c}" org-table-toggle-coordinate-overlays)
  10434. '("\C-c{" org-table-toggle-formula-debugger)
  10435. '("\C-m" org-table-next-row)
  10436. '([(shift return)] org-table-copy-down)
  10437. '("\C-c\C-q" org-table-wrap-region)
  10438. '("\C-c?" org-table-field-info)
  10439. '("\C-c " org-table-blank-field)
  10440. '("\C-c+" org-table-sum)
  10441. '("\C-c=" org-table-eval-formula)
  10442. '("\C-c'" org-table-edit-formulas)
  10443. '("\C-c`" org-table-edit-field)
  10444. '("\C-c*" org-table-recalculate)
  10445. '("\C-c|" org-table-create-or-convert-from-region)
  10446. '("\C-c^" org-table-sort-lines)
  10447. '([(control ?#)] org-table-rotate-recalc-marks)))
  10448. elt key fun cmd)
  10449. (while (setq elt (pop bindings))
  10450. (setq nfunc (1+ nfunc))
  10451. (setq key (org-key (car elt))
  10452. fun (nth 1 elt)
  10453. cmd (orgtbl-make-binding fun nfunc key))
  10454. (org-defkey orgtbl-mode-map key cmd))
  10455. ;; Special treatment needed for TAB and RET
  10456. (org-defkey orgtbl-mode-map [(return)]
  10457. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10458. (org-defkey orgtbl-mode-map "\C-m"
  10459. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10460. (org-defkey orgtbl-mode-map [(tab)]
  10461. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10462. (org-defkey orgtbl-mode-map "\C-i"
  10463. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10464. (org-defkey orgtbl-mode-map [(shift tab)]
  10465. (orgtbl-make-binding 'org-table-previous-field 104
  10466. [(shift tab)] [(tab)] "\C-i"))
  10467. (org-defkey orgtbl-mode-map "\M-\C-m"
  10468. (orgtbl-make-binding 'org-table-wrap-region 105
  10469. "\M-\C-m" [(meta return)]))
  10470. (org-defkey orgtbl-mode-map [(meta return)]
  10471. (orgtbl-make-binding 'org-table-wrap-region 106
  10472. [(meta return)] "\M-\C-m"))
  10473. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10474. (when orgtbl-optimized
  10475. ;; If the user wants maximum table support, we need to hijack
  10476. ;; some standard editing functions
  10477. (org-remap orgtbl-mode-map
  10478. 'self-insert-command 'orgtbl-self-insert-command
  10479. 'delete-char 'org-delete-char
  10480. 'delete-backward-char 'org-delete-backward-char)
  10481. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10482. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10483. '("OrgTbl"
  10484. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10485. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10486. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10487. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10488. "--"
  10489. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10490. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10491. ["Copy Field from Above"
  10492. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10493. "--"
  10494. ("Column"
  10495. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10496. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10497. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10498. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10499. ("Row"
  10500. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10501. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10502. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10503. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10504. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10505. "--"
  10506. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10507. ("Rectangle"
  10508. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10509. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10510. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10511. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10512. "--"
  10513. ("Radio tables"
  10514. ["Insert table template" orgtbl-insert-radio-table
  10515. (assq major-mode orgtbl-radio-table-templates)]
  10516. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10517. "--"
  10518. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10519. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10520. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10521. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10522. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10523. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10524. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10525. ["Sum Column/Rectangle" org-table-sum
  10526. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10527. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10528. ["Debug Formulas"
  10529. org-table-toggle-formula-debugger :active (org-at-table-p)
  10530. :keys "C-c {"
  10531. :style toggle :selected org-table-formula-debug]
  10532. ["Show Col/Row Numbers"
  10533. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10534. :keys "C-c }"
  10535. :style toggle :selected org-table-overlay-coordinates]
  10536. ))
  10537. t))
  10538. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10539. "If the cursor is inside a table, realign the table.
  10540. It it is a table to be sent away to a receiver, do it.
  10541. With prefix arg, also recompute table."
  10542. (interactive "P")
  10543. (let ((pos (point)) action)
  10544. (save-excursion
  10545. (beginning-of-line 1)
  10546. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10547. ((looking-at "[ \t]*|") pos)
  10548. ((looking-at "#\\+TBLFM:") 'recalc))))
  10549. (cond
  10550. ((integerp action)
  10551. (goto-char action)
  10552. (org-table-maybe-eval-formula)
  10553. (if arg
  10554. (call-interactively 'org-table-recalculate)
  10555. (org-table-maybe-recalculate-line))
  10556. (call-interactively 'org-table-align)
  10557. (orgtbl-send-table 'maybe))
  10558. ((eq action 'recalc)
  10559. (save-excursion
  10560. (beginning-of-line 1)
  10561. (skip-chars-backward " \r\n\t")
  10562. (if (org-at-table-p)
  10563. (org-call-with-arg 'org-table-recalculate t))))
  10564. (t (let (orgtbl-mode)
  10565. (call-interactively (key-binding "\C-c\C-c")))))))
  10566. (defun orgtbl-tab (arg)
  10567. "Justification and field motion for `orgtbl-mode'."
  10568. (interactive "P")
  10569. (if arg (org-table-edit-field t)
  10570. (org-table-justify-field-maybe)
  10571. (org-table-next-field)))
  10572. (defun orgtbl-ret ()
  10573. "Justification and field motion for `orgtbl-mode'."
  10574. (interactive)
  10575. (org-table-justify-field-maybe)
  10576. (org-table-next-row))
  10577. (defun orgtbl-self-insert-command (N)
  10578. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10579. If the cursor is in a table looking at whitespace, the whitespace is
  10580. overwritten, and the table is not marked as requiring realignment."
  10581. (interactive "p")
  10582. (if (and (org-at-table-p)
  10583. (or
  10584. (and org-table-auto-blank-field
  10585. (member last-command
  10586. '(orgtbl-hijacker-command-100
  10587. orgtbl-hijacker-command-101
  10588. orgtbl-hijacker-command-102
  10589. orgtbl-hijacker-command-103
  10590. orgtbl-hijacker-command-104
  10591. orgtbl-hijacker-command-105))
  10592. (org-table-blank-field))
  10593. t)
  10594. (eq N 1)
  10595. (looking-at "[^|\n]* +|"))
  10596. (let (org-table-may-need-update)
  10597. (goto-char (1- (match-end 0)))
  10598. (delete-backward-char 1)
  10599. (goto-char (match-beginning 0))
  10600. (self-insert-command N))
  10601. (setq org-table-may-need-update t)
  10602. (let (orgtbl-mode)
  10603. (call-interactively (key-binding (vector last-input-event))))))
  10604. (defun org-force-self-insert (N)
  10605. "Needed to enforce self-insert under remapping."
  10606. (interactive "p")
  10607. (self-insert-command N))
  10608. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10609. "Regula expression matching exponentials as produced by calc.")
  10610. (defvar org-table-clean-did-remove-column nil)
  10611. (defun orgtbl-export (table target)
  10612. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10613. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10614. org-table-last-alignment org-table-last-column-widths
  10615. maxcol column)
  10616. (if (not (fboundp func))
  10617. (error "Cannot export orgtbl table to %s" target))
  10618. (setq lines (org-table-clean-before-export lines))
  10619. (setq table
  10620. (mapcar
  10621. (lambda (x)
  10622. (if (string-match org-table-hline-regexp x)
  10623. 'hline
  10624. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10625. lines))
  10626. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10627. table)))
  10628. (loop for i from (1- maxcol) downto 0 do
  10629. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10630. (setq column (delq nil column))
  10631. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10632. (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))
  10633. (funcall func table nil)))
  10634. (defun orgtbl-send-table (&optional maybe)
  10635. "Send a tranformed version of this table to the receiver position.
  10636. With argument MAYBE, fail quietly if no transformation is defined for
  10637. this table."
  10638. (interactive)
  10639. (catch 'exit
  10640. (unless (org-at-table-p) (error "Not at a table"))
  10641. ;; when non-interactive, we assume align has just happened.
  10642. (when (interactive-p) (org-table-align))
  10643. (save-excursion
  10644. (goto-char (org-table-begin))
  10645. (beginning-of-line 0)
  10646. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10647. (if maybe
  10648. (throw 'exit nil)
  10649. (error "Don't know how to transform this table."))))
  10650. (let* ((name (match-string 1))
  10651. beg
  10652. (transform (intern (match-string 2)))
  10653. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10654. (skip (plist-get params :skip))
  10655. (skipcols (plist-get params :skipcols))
  10656. (txt (buffer-substring-no-properties
  10657. (org-table-begin) (org-table-end)))
  10658. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10659. (lines (org-table-clean-before-export lines))
  10660. (i0 (if org-table-clean-did-remove-column 2 1))
  10661. (table (mapcar
  10662. (lambda (x)
  10663. (if (string-match org-table-hline-regexp x)
  10664. 'hline
  10665. (org-remove-by-index
  10666. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10667. skipcols i0)))
  10668. lines))
  10669. (fun (if (= i0 2) 'cdr 'identity))
  10670. (org-table-last-alignment
  10671. (org-remove-by-index (funcall fun org-table-last-alignment)
  10672. skipcols i0))
  10673. (org-table-last-column-widths
  10674. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10675. skipcols i0)))
  10676. (unless (fboundp transform)
  10677. (error "No such transformation function %s" transform))
  10678. (setq txt (funcall transform table params))
  10679. ;; Find the insertion place
  10680. (save-excursion
  10681. (goto-char (point-min))
  10682. (unless (re-search-forward
  10683. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10684. (error "Don't know where to insert translated table"))
  10685. (goto-char (match-beginning 0))
  10686. (beginning-of-line 2)
  10687. (setq beg (point))
  10688. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10689. (error "Cannot find end of insertion region"))
  10690. (beginning-of-line 1)
  10691. (delete-region beg (point))
  10692. (goto-char beg)
  10693. (insert txt "\n"))
  10694. (message "Table converted and installed at receiver location"))))
  10695. (defun org-remove-by-index (list indices &optional i0)
  10696. "Remove the elements in LIST with indices in INDICES.
  10697. First element has index 0, or I0 if given."
  10698. (if (not indices)
  10699. list
  10700. (if (integerp indices) (setq indices (list indices)))
  10701. (setq i0 (1- (or i0 0)))
  10702. (delq :rm (mapcar (lambda (x)
  10703. (setq i0 (1+ i0))
  10704. (if (memq i0 indices) :rm x))
  10705. list))))
  10706. (defun orgtbl-toggle-comment ()
  10707. "Comment or uncomment the orgtbl at point."
  10708. (interactive)
  10709. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10710. (re2 (concat "^" orgtbl-line-start-regexp))
  10711. (commented (save-excursion (beginning-of-line 1)
  10712. (cond ((looking-at re1) t)
  10713. ((looking-at re2) nil)
  10714. (t (error "Not at an org table")))))
  10715. (re (if commented re1 re2))
  10716. beg end)
  10717. (save-excursion
  10718. (beginning-of-line 1)
  10719. (while (looking-at re) (beginning-of-line 0))
  10720. (beginning-of-line 2)
  10721. (setq beg (point))
  10722. (while (looking-at re) (beginning-of-line 2))
  10723. (setq end (point)))
  10724. (comment-region beg end (if commented '(4) nil))))
  10725. (defun orgtbl-insert-radio-table ()
  10726. "Insert a radio table template appropriate for this major mode."
  10727. (interactive)
  10728. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10729. (txt (nth 1 e))
  10730. name pos)
  10731. (unless e (error "No radio table setup defined for %s" major-mode))
  10732. (setq name (read-string "Table name: "))
  10733. (while (string-match "%n" txt)
  10734. (setq txt (replace-match name t t txt)))
  10735. (or (bolp) (insert "\n"))
  10736. (setq pos (point))
  10737. (insert txt)
  10738. (goto-char pos)))
  10739. (defun org-get-param (params header i sym &optional hsym)
  10740. "Get parameter value for symbol SYM.
  10741. If this is a header line, actually get the value for the symbol with an
  10742. additional \"h\" inserted after the colon.
  10743. If the value is a protperty list, get the element for the current column.
  10744. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10745. (let ((val (plist-get params sym)))
  10746. (and hsym header (setq val (or (plist-get params hsym) val)))
  10747. (if (consp val) (plist-get val i) val)))
  10748. (defun orgtbl-to-generic (table params)
  10749. "Convert the orgtbl-mode TABLE to some other format.
  10750. This generic routine can be used for many standard cases.
  10751. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10752. separator line, or a list of fields for that line.
  10753. PARAMS is a property list of parameters that can influence the conversion.
  10754. For the generic converter, some parameters are obligatory: You need to
  10755. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10756. :splice, you must have :tstart and :tend.
  10757. Valid parameters are
  10758. :tstart String to start the table. Ignored when :splice is t.
  10759. :tend String to end the table. Ignored when :splice is t.
  10760. :splice When set to t, return only table body lines, don't wrap
  10761. them into :tstart and :tend. Default is nil.
  10762. :hline String to be inserted on horizontal separation lines.
  10763. May be nil to ignore hlines.
  10764. :lstart String to start a new table line.
  10765. :lend String to end a table line
  10766. :sep Separator between two fields
  10767. :lfmt Format for entire line, with enough %s to capture all fields.
  10768. If this is present, :lstart, :lend, and :sep are ignored.
  10769. :fmt A format to be used to wrap the field, should contain
  10770. %s for the original field value. For example, to wrap
  10771. everything in dollars, you could use :fmt \"$%s$\".
  10772. This may also be a property list with column numbers and
  10773. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10774. :hlstart :hlend :hlsep :hlfmt :hfmt
  10775. Same as above, specific for the header lines in the table.
  10776. All lines before the first hline are treated as header.
  10777. If any of these is not present, the data line value is used.
  10778. :efmt Use this format to print numbers with exponentials.
  10779. The format should have %s twice for inserting mantissa
  10780. and exponent, for example \"%s\\\\times10^{%s}\". This
  10781. may also be a property list with column numbers and
  10782. formats. :fmt will still be applied after :efmt.
  10783. In addition to this, the parameters :skip and :skipcols are always handled
  10784. directly by `orgtbl-send-table'. See manual."
  10785. (interactive)
  10786. (let* ((p params)
  10787. (splicep (plist-get p :splice))
  10788. (hline (plist-get p :hline))
  10789. rtn line i fm efm lfmt h)
  10790. ;; Do we have a header?
  10791. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10792. (setq h t))
  10793. ;; Put header
  10794. (unless splicep
  10795. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10796. ;; Now loop over all lines
  10797. (while (setq line (pop table))
  10798. (if (eq line 'hline)
  10799. ;; A horizontal separator line
  10800. (progn (if hline (push hline rtn))
  10801. (setq h nil)) ; no longer in header
  10802. ;; A normal line. Convert the fields, push line onto the result list
  10803. (setq i 0)
  10804. (setq line
  10805. (mapcar
  10806. (lambda (f)
  10807. (setq i (1+ i)
  10808. fm (org-get-param p h i :fmt :hfmt)
  10809. efm (org-get-param p h i :efmt))
  10810. (if (and efm (string-match orgtbl-exp-regexp f))
  10811. (setq f (format
  10812. efm (match-string 1 f) (match-string 2 f))))
  10813. (if fm (setq f (format fm f)))
  10814. f)
  10815. line))
  10816. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10817. (push (apply 'format lfmt line) rtn)
  10818. (push (concat
  10819. (org-get-param p h i :lstart :hlstart)
  10820. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10821. (org-get-param p h i :lend :hlend))
  10822. rtn))))
  10823. (unless splicep
  10824. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10825. (mapconcat 'identity (nreverse rtn) "\n")))
  10826. (defun orgtbl-to-latex (table params)
  10827. "Convert the orgtbl-mode TABLE to LaTeX.
  10828. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10829. separator line, or a list of fields for that line.
  10830. PARAMS is a property list of parameters that can influence the conversion.
  10831. Supports all parameters from `orgtbl-to-generic'. Most important for
  10832. LaTeX are:
  10833. :splice When set to t, return only table body lines, don't wrap
  10834. them into a tabular environment. Default is nil.
  10835. :fmt A format to be used to wrap the field, should contain %s for the
  10836. original field value. For example, to wrap everything in dollars,
  10837. use :fmt \"$%s$\". This may also be a property list with column
  10838. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10839. :efmt Format for transforming numbers with exponentials. The format
  10840. should have %s twice for inserting mantissa and exponent, for
  10841. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10842. This may also be a property list with column numbers and formats.
  10843. The general parameters :skip and :skipcols have already been applied when
  10844. this function is called."
  10845. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10846. org-table-last-alignment ""))
  10847. (params2
  10848. (list
  10849. :tstart (concat "\\begin{tabular}{" alignment "}")
  10850. :tend "\\end{tabular}"
  10851. :lstart "" :lend " \\\\" :sep " & "
  10852. :efmt "%s\\,(%s)" :hline "\\hline")))
  10853. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10854. (defun orgtbl-to-html (table params)
  10855. "Convert the orgtbl-mode TABLE to LaTeX.
  10856. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10857. separator line, or a list of fields for that line.
  10858. PARAMS is a property list of parameters that can influence the conversion.
  10859. Currently this function recognizes the following parameters:
  10860. :splice When set to t, return only table body lines, don't wrap
  10861. them into a <table> environment. Default is nil.
  10862. The general parameters :skip and :skipcols have already been applied when
  10863. this function is called. The function does *not* use `orgtbl-to-generic',
  10864. so you cannot specify parameters for it."
  10865. (let* ((splicep (plist-get params :splice))
  10866. html)
  10867. ;; Just call the formatter we already have
  10868. ;; We need to make text lines for it, so put the fields back together.
  10869. (setq html (org-format-org-table-html
  10870. (mapcar
  10871. (lambda (x)
  10872. (if (eq x 'hline)
  10873. "|----+----|"
  10874. (concat "| " (mapconcat 'identity x " | ") " |")))
  10875. table)
  10876. splicep))
  10877. (if (string-match "\n+\\'" html)
  10878. (setq html (replace-match "" t t html)))
  10879. html))
  10880. (defun orgtbl-to-texinfo (table params)
  10881. "Convert the orgtbl-mode TABLE to TeXInfo.
  10882. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10883. separator line, or a list of fields for that line.
  10884. PARAMS is a property list of parameters that can influence the conversion.
  10885. Supports all parameters from `orgtbl-to-generic'. Most important for
  10886. TeXInfo are:
  10887. :splice nil/t When set to t, return only table body lines, don't wrap
  10888. them into a multitable environment. Default is nil.
  10889. :fmt fmt A format to be used to wrap the field, should contain
  10890. %s for the original field value. For example, to wrap
  10891. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10892. This may also be a property list with column numbers and
  10893. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10894. :cf \"f1 f2..\" The column fractions for the table. By default these
  10895. are computed automatically from the width of the columns
  10896. under org-mode.
  10897. The general parameters :skip and :skipcols have already been applied when
  10898. this function is called."
  10899. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10900. (colfrac (or (plist-get params :cf)
  10901. (mapconcat
  10902. (lambda (x) (format "%.3f" (/ (float x) total)))
  10903. org-table-last-column-widths " ")))
  10904. (params2
  10905. (list
  10906. :tstart (concat "@multitable @columnfractions " colfrac)
  10907. :tend "@end multitable"
  10908. :lstart "@item " :lend "" :sep " @tab "
  10909. :hlstart "@headitem ")))
  10910. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10911. ;;;; Link Stuff
  10912. ;;; Link abbreviations
  10913. (defun org-link-expand-abbrev (link)
  10914. "Apply replacements as defined in `org-link-abbrev-alist."
  10915. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10916. (let* ((key (match-string 1 link))
  10917. (as (or (assoc key org-link-abbrev-alist-local)
  10918. (assoc key org-link-abbrev-alist)))
  10919. (tag (and (match-end 2) (match-string 3 link)))
  10920. rpl)
  10921. (if (not as)
  10922. link
  10923. (setq rpl (cdr as))
  10924. (cond
  10925. ((symbolp rpl) (funcall rpl tag))
  10926. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10927. (t (concat rpl tag)))))
  10928. link))
  10929. ;;; Storing and inserting links
  10930. (defvar org-insert-link-history nil
  10931. "Minibuffer history for links inserted with `org-insert-link'.")
  10932. (defvar org-stored-links nil
  10933. "Contains the links stored with `org-store-link'.")
  10934. (defvar org-store-link-plist nil
  10935. "Plist with info about the most recently link created with `org-store-link'.")
  10936. (defvar org-link-protocols nil
  10937. "Link protocols added to Org-mode using `org-add-link-type'.")
  10938. (defvar org-store-link-functions nil
  10939. "List of functions that are called to create and store a link.
  10940. Each function will be called in turn until one returns a non-nil
  10941. value. Each function should check if it is responsible for creating
  10942. this link (for example by looking at the major mode).
  10943. If not, it must exit and return nil.
  10944. If yes, it should return a non-nil value after a calling
  10945. `org-store-link-props' with a list of properties and values.
  10946. Special properties are:
  10947. :type The link prefix. like \"http\". This must be given.
  10948. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10949. This is obligatory as well.
  10950. :description Optional default description for the second pair
  10951. of brackets in an Org-mode link. The user can still change
  10952. this when inserting this link into an Org-mode buffer.
  10953. In addition to these, any additional properties can be specified
  10954. and then used in remember templates.")
  10955. (defun org-add-link-type (type &optional follow publish)
  10956. "Add TYPE to the list of `org-link-types'.
  10957. Re-compute all regular expressions depending on `org-link-types'
  10958. FOLLOW and PUBLISH are two functions. Both take the link path as
  10959. an argument.
  10960. FOLLOW should do whatever is necessary to follow the link, for example
  10961. to find a file or display a mail message.
  10962. PUBLISH takes the path and retuns the string that should be used when
  10963. this document is published. FIMXE: This is actually not yet implemented."
  10964. (add-to-list 'org-link-types type t)
  10965. (org-make-link-regexps)
  10966. (add-to-list 'org-link-protocols
  10967. (list type follow publish)))
  10968. (defun org-add-agenda-custom-command (entry)
  10969. "Replace or add a command in `org-agenda-custom-commands'.
  10970. This is mostly for hacking and trying a new command - once the command
  10971. works you probably want to add it to `org-agenda-custom-commands' for good."
  10972. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10973. (if ass
  10974. (setcdr ass (cdr entry))
  10975. (push entry org-agenda-custom-commands))))
  10976. ;;;###autoload
  10977. (defun org-store-link (arg)
  10978. "\\<org-mode-map>Store an org-link to the current location.
  10979. This link is added to `org-stored-links' and can later be inserted
  10980. into an org-buffer with \\[org-insert-link].
  10981. For some link types, a prefix arg is interpreted:
  10982. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10983. For file links, arg negates `org-context-in-file-links'."
  10984. (interactive "P")
  10985. (setq org-store-link-plist nil) ; reset
  10986. (let (link cpltxt desc description search txt)
  10987. (cond
  10988. ((run-hook-with-args-until-success 'org-store-link-functions)
  10989. (setq link (plist-get org-store-link-plist :link)
  10990. desc (or (plist-get org-store-link-plist :description) link)))
  10991. ((eq major-mode 'bbdb-mode)
  10992. (let ((name (bbdb-record-name (bbdb-current-record)))
  10993. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10994. (setq cpltxt (concat "bbdb:" (or name company))
  10995. link (org-make-link cpltxt))
  10996. (org-store-link-props :type "bbdb" :name name :company company)))
  10997. ((eq major-mode 'Info-mode)
  10998. (setq link (org-make-link "info:"
  10999. (file-name-nondirectory Info-current-file)
  11000. ":" Info-current-node))
  11001. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  11002. ":" Info-current-node))
  11003. (org-store-link-props :type "info" :file Info-current-file
  11004. :node Info-current-node))
  11005. ((eq major-mode 'calendar-mode)
  11006. (let ((cd (calendar-cursor-to-date)))
  11007. (setq link
  11008. (format-time-string
  11009. (car org-time-stamp-formats)
  11010. (apply 'encode-time
  11011. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11012. nil nil nil))))
  11013. (org-store-link-props :type "calendar" :date cd)))
  11014. ((or (eq major-mode 'vm-summary-mode)
  11015. (eq major-mode 'vm-presentation-mode))
  11016. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11017. (vm-follow-summary-cursor)
  11018. (save-excursion
  11019. (vm-select-folder-buffer)
  11020. (let* ((message (car vm-message-pointer))
  11021. (folder buffer-file-name)
  11022. (subject (vm-su-subject message))
  11023. (to (vm-get-header-contents message "To"))
  11024. (from (vm-get-header-contents message "From"))
  11025. (message-id (vm-su-message-id message)))
  11026. (org-store-link-props :type "vm" :from from :to to :subject subject
  11027. :message-id message-id)
  11028. (setq message-id (org-remove-angle-brackets message-id))
  11029. (setq folder (abbreviate-file-name folder))
  11030. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11031. folder)
  11032. (setq folder (replace-match "" t t folder)))
  11033. (setq cpltxt (org-email-link-description))
  11034. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11035. ((eq major-mode 'wl-summary-mode)
  11036. (let* ((msgnum (wl-summary-message-number))
  11037. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11038. msgnum 'message-id))
  11039. (wl-message-entity
  11040. (if (fboundp 'elmo-message-entity)
  11041. (elmo-message-entity
  11042. wl-summary-buffer-elmo-folder msgnum)
  11043. (elmo-msgdb-overview-get-entity
  11044. msgnum (wl-summary-buffer-msgdb))))
  11045. (from (wl-summary-line-from))
  11046. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11047. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11048. (wl-summary-line-subject))))
  11049. (org-store-link-props :type "wl" :from from :to to
  11050. :subject subject :message-id message-id)
  11051. (setq message-id (org-remove-angle-brackets message-id))
  11052. (setq cpltxt (org-email-link-description))
  11053. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11054. "#" message-id))))
  11055. ((or (equal major-mode 'mh-folder-mode)
  11056. (equal major-mode 'mh-show-mode))
  11057. (let ((from (org-mhe-get-header "From:"))
  11058. (to (org-mhe-get-header "To:"))
  11059. (message-id (org-mhe-get-header "Message-Id:"))
  11060. (subject (org-mhe-get-header "Subject:")))
  11061. (org-store-link-props :type "mh" :from from :to to
  11062. :subject subject :message-id message-id)
  11063. (setq cpltxt (org-email-link-description))
  11064. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11065. (org-remove-angle-brackets message-id)))))
  11066. ((or (eq major-mode 'rmail-mode)
  11067. (eq major-mode 'rmail-summary-mode))
  11068. (save-window-excursion
  11069. (save-restriction
  11070. (when (eq major-mode 'rmail-summary-mode)
  11071. (rmail-show-message rmail-current-message))
  11072. (rmail-narrow-to-non-pruned-header)
  11073. (let ((folder buffer-file-name)
  11074. (message-id (mail-fetch-field "message-id"))
  11075. (from (mail-fetch-field "from"))
  11076. (to (mail-fetch-field "to"))
  11077. (subject (mail-fetch-field "subject")))
  11078. (org-store-link-props
  11079. :type "rmail" :from from :to to
  11080. :subject subject :message-id message-id)
  11081. (setq message-id (org-remove-angle-brackets message-id))
  11082. (setq cpltxt (org-email-link-description))
  11083. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11084. (rmail-show-message rmail-current-message))))
  11085. ((eq major-mode 'gnus-group-mode)
  11086. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11087. (gnus-group-group-name)) ; version
  11088. ((fboundp 'gnus-group-name)
  11089. (gnus-group-name))
  11090. (t "???"))))
  11091. (unless group (error "Not on a group"))
  11092. (org-store-link-props :type "gnus" :group group)
  11093. (setq cpltxt (concat
  11094. (if (org-xor arg org-usenet-links-prefer-google)
  11095. "http://groups.google.com/groups?group="
  11096. "gnus:")
  11097. group)
  11098. link (org-make-link cpltxt))))
  11099. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11100. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11101. (let* ((group gnus-newsgroup-name)
  11102. (article (gnus-summary-article-number))
  11103. (header (gnus-summary-article-header article))
  11104. (from (mail-header-from header))
  11105. (message-id (mail-header-id header))
  11106. (date (mail-header-date header))
  11107. (subject (gnus-summary-subject-string)))
  11108. (org-store-link-props :type "gnus" :from from :subject subject
  11109. :message-id message-id :group group)
  11110. (setq cpltxt (org-email-link-description))
  11111. (if (org-xor arg org-usenet-links-prefer-google)
  11112. (setq link
  11113. (concat
  11114. cpltxt "\n "
  11115. (format "http://groups.google.com/groups?as_umsgid=%s"
  11116. (org-fixup-message-id-for-http message-id))))
  11117. (setq link (org-make-link "gnus:" group
  11118. "#" (number-to-string article))))))
  11119. ((eq major-mode 'w3-mode)
  11120. (setq cpltxt (url-view-url t)
  11121. link (org-make-link cpltxt))
  11122. (org-store-link-props :type "w3" :url (url-view-url t)))
  11123. ((eq major-mode 'w3m-mode)
  11124. (setq cpltxt (or w3m-current-title w3m-current-url)
  11125. link (org-make-link w3m-current-url))
  11126. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11127. ((setq search (run-hook-with-args-until-success
  11128. 'org-create-file-search-functions))
  11129. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11130. "::" search))
  11131. (setq cpltxt (or description link)))
  11132. ((eq major-mode 'image-mode)
  11133. (setq cpltxt (concat "file:"
  11134. (abbreviate-file-name buffer-file-name))
  11135. link (org-make-link cpltxt))
  11136. (org-store-link-props :type "image" :file buffer-file-name))
  11137. ((eq major-mode 'dired-mode)
  11138. ;; link to the file in the current line
  11139. (setq cpltxt (concat "file:"
  11140. (abbreviate-file-name
  11141. (expand-file-name
  11142. (dired-get-filename nil t))))
  11143. link (org-make-link cpltxt)))
  11144. ((and buffer-file-name (org-mode-p))
  11145. ;; Just link to current headline
  11146. (setq cpltxt (concat "file:"
  11147. (abbreviate-file-name buffer-file-name)))
  11148. ;; Add a context search string
  11149. (when (org-xor org-context-in-file-links arg)
  11150. ;; Check if we are on a target
  11151. (if (org-in-regexp "<<\\(.*?\\)>>")
  11152. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11153. (setq txt (cond
  11154. ((org-on-heading-p) nil)
  11155. ((org-region-active-p)
  11156. (buffer-substring (region-beginning) (region-end)))
  11157. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11158. (when (or (null txt) (string-match "\\S-" txt))
  11159. (setq cpltxt
  11160. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11161. desc "NONE"))))
  11162. (if (string-match "::\\'" cpltxt)
  11163. (setq cpltxt (substring cpltxt 0 -2)))
  11164. (setq link (org-make-link cpltxt)))
  11165. ((buffer-file-name (buffer-base-buffer))
  11166. ;; Just link to this file here.
  11167. (setq cpltxt (concat "file:"
  11168. (abbreviate-file-name
  11169. (buffer-file-name (buffer-base-buffer)))))
  11170. ;; Add a context string
  11171. (when (org-xor org-context-in-file-links arg)
  11172. (setq txt (if (org-region-active-p)
  11173. (buffer-substring (region-beginning) (region-end))
  11174. (buffer-substring (point-at-bol) (point-at-eol))))
  11175. ;; Only use search option if there is some text.
  11176. (when (string-match "\\S-" txt)
  11177. (setq cpltxt
  11178. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11179. desc "NONE")))
  11180. (setq link (org-make-link cpltxt)))
  11181. ((interactive-p)
  11182. (error "Cannot link to a buffer which is not visiting a file"))
  11183. (t (setq link nil)))
  11184. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11185. (setq link (or link cpltxt)
  11186. desc (or desc cpltxt))
  11187. (if (equal desc "NONE") (setq desc nil))
  11188. (if (and (interactive-p) link)
  11189. (progn
  11190. (setq org-stored-links
  11191. (cons (list link desc) org-stored-links))
  11192. (message "Stored: %s" (or desc link)))
  11193. (and link (org-make-link-string link desc)))))
  11194. (defun org-store-link-props (&rest plist)
  11195. "Store link properties, extract names and addresses."
  11196. (let (x adr)
  11197. (when (setq x (plist-get plist :from))
  11198. (setq adr (mail-extract-address-components x))
  11199. (plist-put plist :fromname (car adr))
  11200. (plist-put plist :fromaddress (nth 1 adr)))
  11201. (when (setq x (plist-get plist :to))
  11202. (setq adr (mail-extract-address-components x))
  11203. (plist-put plist :toname (car adr))
  11204. (plist-put plist :toaddress (nth 1 adr))))
  11205. (let ((from (plist-get plist :from))
  11206. (to (plist-get plist :to)))
  11207. (when (and from to org-from-is-user-regexp)
  11208. (plist-put plist :fromto
  11209. (if (string-match org-from-is-user-regexp from)
  11210. (concat "to %t")
  11211. (concat "from %f")))))
  11212. (setq org-store-link-plist plist))
  11213. (defun org-email-link-description (&optional fmt)
  11214. "Return the description part of an email link.
  11215. This takes information from `org-store-link-plist' and formats it
  11216. according to FMT (default from `org-email-link-description-format')."
  11217. (setq fmt (or fmt org-email-link-description-format))
  11218. (let* ((p org-store-link-plist)
  11219. (to (plist-get p :toaddress))
  11220. (from (plist-get p :fromaddress))
  11221. (table
  11222. (list
  11223. (cons "%c" (plist-get p :fromto))
  11224. (cons "%F" (plist-get p :from))
  11225. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11226. (cons "%T" (plist-get p :to))
  11227. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11228. (cons "%s" (plist-get p :subject))
  11229. (cons "%m" (plist-get p :message-id)))))
  11230. (when (string-match "%c" fmt)
  11231. ;; Check if the user wrote this message
  11232. (if (and org-from-is-user-regexp from to
  11233. (save-match-data (string-match org-from-is-user-regexp from)))
  11234. (setq fmt (replace-match "to %t" t t fmt))
  11235. (setq fmt (replace-match "from %f" t t fmt))))
  11236. (org-replace-escapes fmt table)))
  11237. (defun org-make-org-heading-search-string (&optional string heading)
  11238. "Make search string for STRING or current headline."
  11239. (interactive)
  11240. (let ((s (or string (org-get-heading))))
  11241. (unless (and string (not heading))
  11242. ;; We are using a headline, clean up garbage in there.
  11243. (if (string-match org-todo-regexp s)
  11244. (setq s (replace-match "" t t s)))
  11245. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11246. (setq s (replace-match "" t t s)))
  11247. (setq s (org-trim s))
  11248. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11249. org-comment-string "\\)") s)
  11250. (setq s (replace-match "" t t s)))
  11251. (while (string-match org-ts-regexp s)
  11252. (setq s (replace-match "" t t s))))
  11253. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11254. (setq s (replace-match " " t t s)))
  11255. (or string (setq s (concat "*" s))) ; Add * for headlines
  11256. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11257. (defun org-make-link (&rest strings)
  11258. "Concatenate STRINGS."
  11259. (apply 'concat strings))
  11260. (defun org-make-link-string (link &optional description)
  11261. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11262. (unless (string-match "\\S-" link)
  11263. (error "Empty link"))
  11264. (when (stringp description)
  11265. ;; Remove brackets from the description, they are fatal.
  11266. (while (string-match "\\[" description)
  11267. (setq description (replace-match "{" t t description)))
  11268. (while (string-match "\\]" description)
  11269. (setq description (replace-match "}" t t description))))
  11270. (when (equal (org-link-escape link) description)
  11271. ;; No description needed, it is identical
  11272. (setq description nil))
  11273. (when (and (not description)
  11274. (not (equal link (org-link-escape link))))
  11275. (setq description link))
  11276. (concat "[[" (org-link-escape link) "]"
  11277. (if description (concat "[" description "]") "")
  11278. "]"))
  11279. (defconst org-link-escape-chars
  11280. '((?\ . "%20")
  11281. (?\[ . "%5B")
  11282. (?\] . "%5D")
  11283. (?\340 . "%E0") ; `a
  11284. (?\342 . "%E2") ; ^a
  11285. (?\347 . "%E7") ; ,c
  11286. (?\350 . "%E8") ; `e
  11287. (?\351 . "%E9") ; 'e
  11288. (?\352 . "%EA") ; ^e
  11289. (?\356 . "%EE") ; ^i
  11290. (?\364 . "%F4") ; ^o
  11291. (?\371 . "%F9") ; `u
  11292. (?\373 . "%FB") ; ^u
  11293. (?\; . "%3B")
  11294. (?? . "%3F")
  11295. (?= . "%3D")
  11296. (?+ . "%2B")
  11297. )
  11298. "Association list of escapes for some characters problematic in links.
  11299. This is the list that is used for internal purposes.")
  11300. (defconst org-link-escape-chars-browser
  11301. '((?\ . "%20")) ; 32 for the SPC char
  11302. "Association list of escapes for some characters problematic in links.
  11303. This is the list that is used before handing over to the browser.")
  11304. (defun org-link-escape (text &optional table)
  11305. "Escape charaters in TEXT that are problematic for links."
  11306. (setq table (or table org-link-escape-chars))
  11307. (when text
  11308. (let ((re (mapconcat (lambda (x) (regexp-quote
  11309. (char-to-string (car x))))
  11310. table "\\|")))
  11311. (while (string-match re text)
  11312. (setq text
  11313. (replace-match
  11314. (cdr (assoc (string-to-char (match-string 0 text))
  11315. table))
  11316. t t text)))
  11317. text)))
  11318. (defun org-link-unescape (text &optional table)
  11319. "Reverse the action of `org-link-escape'."
  11320. (setq table (or table org-link-escape-chars))
  11321. (when text
  11322. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11323. table "\\|")))
  11324. (while (string-match re text)
  11325. (setq text
  11326. (replace-match
  11327. (char-to-string (car (rassoc (match-string 0 text) table)))
  11328. t t text)))
  11329. text)))
  11330. (defun org-xor (a b)
  11331. "Exclusive or."
  11332. (if a (not b) b))
  11333. (defun org-get-header (header)
  11334. "Find a header field in the current buffer."
  11335. (save-excursion
  11336. (goto-char (point-min))
  11337. (let ((case-fold-search t) s)
  11338. (cond
  11339. ((eq header 'from)
  11340. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11341. (setq s (match-string 1)))
  11342. (while (string-match "\"" s)
  11343. (setq s (replace-match "" t t s)))
  11344. (if (string-match "[<(].*" s)
  11345. (setq s (replace-match "" t t s))))
  11346. ((eq header 'message-id)
  11347. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11348. (setq s (match-string 1))))
  11349. ((eq header 'subject)
  11350. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11351. (setq s (match-string 1)))))
  11352. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11353. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11354. s)))
  11355. (defun org-fixup-message-id-for-http (s)
  11356. "Replace special characters in a message id, so it can be used in an http query."
  11357. (while (string-match "<" s)
  11358. (setq s (replace-match "%3C" t t s)))
  11359. (while (string-match ">" s)
  11360. (setq s (replace-match "%3E" t t s)))
  11361. (while (string-match "@" s)
  11362. (setq s (replace-match "%40" t t s)))
  11363. s)
  11364. ;;;###autoload
  11365. (defun org-insert-link-global ()
  11366. "Insert a link like Org-mode does.
  11367. This command can be called in any mode to insert a link in Org-mode syntax."
  11368. (interactive)
  11369. (org-run-like-in-org-mode 'org-insert-link))
  11370. (defun org-insert-link (&optional complete-file)
  11371. "Insert a link. At the prompt, enter the link.
  11372. Completion can be used to select a link previously stored with
  11373. `org-store-link'. When the empty string is entered (i.e. if you just
  11374. press RET at the prompt), the link defaults to the most recently
  11375. stored link. As SPC triggers completion in the minibuffer, you need to
  11376. use M-SPC or C-q SPC to force the insertion of a space character.
  11377. You will also be prompted for a description, and if one is given, it will
  11378. be displayed in the buffer instead of the link.
  11379. If there is already a link at point, this command will allow you to edit link
  11380. and description parts.
  11381. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11382. selected using completion. The path to the file will be relative to
  11383. the current directory if the file is in the current directory or a
  11384. subdirectory. Otherwise, the link will be the absolute path as
  11385. completed in the minibuffer (i.e. normally ~/path/to/file).
  11386. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11387. is in the current directory or below.
  11388. With three \\[universal-argument] prefixes, negate the meaning of
  11389. `org-keep-stored-link-after-insertion'."
  11390. (interactive "P")
  11391. (let* ((wcf (current-window-configuration))
  11392. (region (if (org-region-active-p)
  11393. (buffer-substring (region-beginning) (region-end))))
  11394. (remove (and region (list (region-beginning) (region-end))))
  11395. (desc region)
  11396. tmphist ; byte-compile incorrectly complains about this
  11397. link entry file)
  11398. (cond
  11399. ((org-in-regexp org-bracket-link-regexp 1)
  11400. ;; We do have a link at point, and we are going to edit it.
  11401. (setq remove (list (match-beginning 0) (match-end 0)))
  11402. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11403. (setq link (read-string "Link: "
  11404. (org-link-unescape
  11405. (org-match-string-no-properties 1)))))
  11406. ((or (org-in-regexp org-angle-link-re)
  11407. (org-in-regexp org-plain-link-re))
  11408. ;; Convert to bracket link
  11409. (setq remove (list (match-beginning 0) (match-end 0))
  11410. link (read-string "Link: "
  11411. (org-remove-angle-brackets (match-string 0)))))
  11412. ((equal complete-file '(4))
  11413. ;; Completing read for file names.
  11414. (setq file (read-file-name "File: "))
  11415. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11416. (pwd1 (file-name-as-directory (abbreviate-file-name
  11417. (expand-file-name ".")))))
  11418. (cond
  11419. ((equal complete-file '(16))
  11420. (setq link (org-make-link
  11421. "file:"
  11422. (abbreviate-file-name (expand-file-name file)))))
  11423. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11424. (setq link (org-make-link "file:" (match-string 1 file))))
  11425. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11426. (expand-file-name file))
  11427. (setq link (org-make-link
  11428. "file:" (match-string 1 (expand-file-name file)))))
  11429. (t (setq link (org-make-link "file:" file))))))
  11430. (t
  11431. ;; Read link, with completion for stored links.
  11432. (with-output-to-temp-buffer "*Org Links*"
  11433. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11434. (when org-stored-links
  11435. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11436. (princ (mapconcat
  11437. (lambda (x)
  11438. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11439. (reverse org-stored-links) "\n"))))
  11440. (let ((cw (selected-window)))
  11441. (select-window (get-buffer-window "*Org Links*"))
  11442. (shrink-window-if-larger-than-buffer)
  11443. (setq truncate-lines t)
  11444. (select-window cw))
  11445. ;; Fake a link history, containing the stored links.
  11446. (setq tmphist (append (mapcar 'car org-stored-links)
  11447. org-insert-link-history))
  11448. (unwind-protect
  11449. (setq link (org-completing-read
  11450. "Link: "
  11451. (append
  11452. (mapcar (lambda (x) (list (concat (car x) ":")))
  11453. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11454. (mapcar (lambda (x) (list (concat x ":")))
  11455. org-link-types))
  11456. nil nil nil
  11457. 'tmphist
  11458. (or (car (car org-stored-links)))))
  11459. (set-window-configuration wcf)
  11460. (kill-buffer "*Org Links*"))
  11461. (setq entry (assoc link org-stored-links))
  11462. (or entry (push link org-insert-link-history))
  11463. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11464. (not org-keep-stored-link-after-insertion))
  11465. (setq org-stored-links (delq (assoc link org-stored-links)
  11466. org-stored-links)))
  11467. (setq desc (or desc (nth 1 entry)))))
  11468. (if (string-match org-plain-link-re link)
  11469. ;; URL-like link, normalize the use of angular brackets.
  11470. (setq link (org-make-link (org-remove-angle-brackets link))))
  11471. ;; Check if we are linking to the current file with a search option
  11472. ;; If yes, simplify the link by using only the search option.
  11473. (when (and buffer-file-name
  11474. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11475. (let* ((path (match-string 1 link))
  11476. (case-fold-search nil)
  11477. (search (match-string 2 link)))
  11478. (save-match-data
  11479. (if (equal (file-truename buffer-file-name) (file-truename path))
  11480. ;; We are linking to this same file, with a search option
  11481. (setq link search)))))
  11482. ;; Check if we can/should use a relative path. If yes, simplify the link
  11483. (when (string-match "\\<file:\\(.*\\)" link)
  11484. (let* ((path (match-string 1 link))
  11485. (origpath path)
  11486. (case-fold-search nil))
  11487. (cond
  11488. ((eq org-link-file-path-type 'absolute)
  11489. (setq path (abbreviate-file-name (expand-file-name path))))
  11490. ((eq org-link-file-path-type 'noabbrev)
  11491. (setq path (expand-file-name path)))
  11492. ((eq org-link-file-path-type 'relative)
  11493. (setq path (file-relative-name path)))
  11494. (t
  11495. (save-match-data
  11496. (if (string-match (concat "^" (regexp-quote
  11497. (file-name-as-directory
  11498. (expand-file-name "."))))
  11499. (expand-file-name path))
  11500. ;; We are linking a file with relative path name.
  11501. (setq path (substring (expand-file-name path)
  11502. (match-end 0)))))))
  11503. (setq link (concat "file:" path))
  11504. (if (equal desc origpath)
  11505. (setq desc path))))
  11506. (setq desc (read-string "Description: " desc))
  11507. (unless (string-match "\\S-" desc) (setq desc nil))
  11508. (if remove (apply 'delete-region remove))
  11509. (insert (org-make-link-string link desc))))
  11510. (defun org-completing-read (&rest args)
  11511. (let ((minibuffer-local-completion-map
  11512. (copy-keymap minibuffer-local-completion-map)))
  11513. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11514. (apply 'completing-read args)))
  11515. ;;; Opening/following a link
  11516. (defvar org-link-search-failed nil)
  11517. (defun org-next-link ()
  11518. "Move forward to the next link.
  11519. If the link is in hidden text, expose it."
  11520. (interactive)
  11521. (when (and org-link-search-failed (eq this-command last-command))
  11522. (goto-char (point-min))
  11523. (message "Link search wrapped back to beginning of buffer"))
  11524. (setq org-link-search-failed nil)
  11525. (let* ((pos (point))
  11526. (ct (org-context))
  11527. (a (assoc :link ct)))
  11528. (if a (goto-char (nth 2 a)))
  11529. (if (re-search-forward org-any-link-re nil t)
  11530. (progn
  11531. (goto-char (match-beginning 0))
  11532. (if (org-invisible-p) (org-show-context)))
  11533. (goto-char pos)
  11534. (setq org-link-search-failed t)
  11535. (error "No further link found"))))
  11536. (defun org-previous-link ()
  11537. "Move backward to the previous link.
  11538. If the link is in hidden text, expose it."
  11539. (interactive)
  11540. (when (and org-link-search-failed (eq this-command last-command))
  11541. (goto-char (point-max))
  11542. (message "Link search wrapped back to end of buffer"))
  11543. (setq org-link-search-failed nil)
  11544. (let* ((pos (point))
  11545. (ct (org-context))
  11546. (a (assoc :link ct)))
  11547. (if a (goto-char (nth 1 a)))
  11548. (if (re-search-backward org-any-link-re nil t)
  11549. (progn
  11550. (goto-char (match-beginning 0))
  11551. (if (org-invisible-p) (org-show-context)))
  11552. (goto-char pos)
  11553. (setq org-link-search-failed t)
  11554. (error "No further link found"))))
  11555. (defun org-find-file-at-mouse (ev)
  11556. "Open file link or URL at mouse."
  11557. (interactive "e")
  11558. (mouse-set-point ev)
  11559. (org-open-at-point 'in-emacs))
  11560. (defun org-open-at-mouse (ev)
  11561. "Open file link or URL at mouse."
  11562. (interactive "e")
  11563. (mouse-set-point ev)
  11564. (org-open-at-point))
  11565. (defvar org-window-config-before-follow-link nil
  11566. "The window configuration before following a link.
  11567. This is saved in case the need arises to restore it.")
  11568. (defvar org-open-link-marker (make-marker)
  11569. "Marker pointing to the location where `org-open-at-point; was called.")
  11570. ;;;###autoload
  11571. (defun org-open-at-point-global ()
  11572. "Follow a link like Org-mode does.
  11573. This command can be called in any mode to follow a link that has
  11574. Org-mode syntax."
  11575. (interactive)
  11576. (org-run-like-in-org-mode 'org-open-at-point))
  11577. (defun org-open-at-point (&optional in-emacs)
  11578. "Open link at or after point.
  11579. If there is no link at point, this function will search forward up to
  11580. the end of the current subtree.
  11581. Normally, files will be opened by an appropriate application. If the
  11582. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11583. (interactive "P")
  11584. (move-marker org-open-link-marker (point))
  11585. (setq org-window-config-before-follow-link (current-window-configuration))
  11586. (org-remove-occur-highlights nil nil t)
  11587. (if (org-at-timestamp-p t)
  11588. (org-follow-timestamp-link)
  11589. (let (type path link line search (pos (point)))
  11590. (catch 'match
  11591. (save-excursion
  11592. (skip-chars-forward "^]\n\r")
  11593. (when (org-in-regexp org-bracket-link-regexp)
  11594. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11595. (while (string-match " *\n *" link)
  11596. (setq link (replace-match " " t t link)))
  11597. (setq link (org-link-expand-abbrev link))
  11598. (if (string-match org-link-re-with-space2 link)
  11599. (setq type (match-string 1 link) path (match-string 2 link))
  11600. (setq type "thisfile" path link))
  11601. (throw 'match t)))
  11602. (when (get-text-property (point) 'org-linked-text)
  11603. (setq type "thisfile"
  11604. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11605. (1+ (point)) (point))
  11606. path (buffer-substring
  11607. (previous-single-property-change pos 'org-linked-text)
  11608. (next-single-property-change pos 'org-linked-text)))
  11609. (throw 'match t))
  11610. (save-excursion
  11611. (when (or (org-in-regexp org-angle-link-re)
  11612. (org-in-regexp org-plain-link-re))
  11613. (setq type (match-string 1) path (match-string 2))
  11614. (throw 'match t)))
  11615. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11616. (setq type "tree-match"
  11617. path (match-string 1))
  11618. (throw 'match t))
  11619. (save-excursion
  11620. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11621. (setq type "tags"
  11622. path (match-string 1))
  11623. (while (string-match ":" path)
  11624. (setq path (replace-match "+" t t path)))
  11625. (throw 'match t))))
  11626. (unless path
  11627. (error "No link found"))
  11628. ;; Remove any trailing spaces in path
  11629. (if (string-match " +\\'" path)
  11630. (setq path (replace-match "" t t path)))
  11631. (cond
  11632. ((assoc type org-link-protocols)
  11633. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11634. ((equal type "mailto")
  11635. (let ((cmd (car org-link-mailto-program))
  11636. (args (cdr org-link-mailto-program)) args1
  11637. (address path) (subject "") a)
  11638. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11639. (setq address (match-string 1 path)
  11640. subject (org-link-escape (match-string 2 path))))
  11641. (while args
  11642. (cond
  11643. ((not (stringp (car args))) (push (pop args) args1))
  11644. (t (setq a (pop args))
  11645. (if (string-match "%a" a)
  11646. (setq a (replace-match address t t a)))
  11647. (if (string-match "%s" a)
  11648. (setq a (replace-match subject t t a)))
  11649. (push a args1))))
  11650. (apply cmd (nreverse args1))))
  11651. ((member type '("http" "https" "ftp" "news"))
  11652. (browse-url (concat type ":" (org-link-escape
  11653. path org-link-escape-chars-browser))))
  11654. ((member type '("message"))
  11655. (browse-url (concat type ":" path)))
  11656. ((string= type "tags")
  11657. (org-tags-view in-emacs path))
  11658. ((string= type "thisfile")
  11659. (if in-emacs
  11660. (switch-to-buffer-other-window
  11661. (org-get-buffer-for-internal-link (current-buffer)))
  11662. (org-mark-ring-push))
  11663. (let ((cmd `(org-link-search
  11664. ,path
  11665. ,(cond ((equal in-emacs '(4)) 'occur)
  11666. ((equal in-emacs '(16)) 'org-occur)
  11667. (t nil))
  11668. ,pos)))
  11669. (condition-case nil (eval cmd)
  11670. (error (progn (widen) (eval cmd))))))
  11671. ((string= type "tree-match")
  11672. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11673. ((string= type "file")
  11674. (if (string-match "::\\([0-9]+\\)\\'" path)
  11675. (setq line (string-to-number (match-string 1 path))
  11676. path (substring path 0 (match-beginning 0)))
  11677. (if (string-match "::\\(.+\\)\\'" path)
  11678. (setq search (match-string 1 path)
  11679. path (substring path 0 (match-beginning 0)))))
  11680. (if (string-match "[*?{]" (file-name-nondirectory path))
  11681. (dired path)
  11682. (org-open-file path in-emacs line search)))
  11683. ((string= type "news")
  11684. (org-follow-gnus-link path))
  11685. ((string= type "bbdb")
  11686. (org-follow-bbdb-link path))
  11687. ((string= type "info")
  11688. (org-follow-info-link path))
  11689. ((string= type "gnus")
  11690. (let (group article)
  11691. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11692. (error "Error in Gnus link"))
  11693. (setq group (match-string 1 path)
  11694. article (match-string 3 path))
  11695. (org-follow-gnus-link group article)))
  11696. ((string= type "vm")
  11697. (let (folder article)
  11698. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11699. (error "Error in VM link"))
  11700. (setq folder (match-string 1 path)
  11701. article (match-string 3 path))
  11702. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11703. (org-follow-vm-link folder article in-emacs)))
  11704. ((string= type "wl")
  11705. (let (folder article)
  11706. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11707. (error "Error in Wanderlust link"))
  11708. (setq folder (match-string 1 path)
  11709. article (match-string 3 path))
  11710. (org-follow-wl-link folder article)))
  11711. ((string= type "mhe")
  11712. (let (folder article)
  11713. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11714. (error "Error in MHE link"))
  11715. (setq folder (match-string 1 path)
  11716. article (match-string 3 path))
  11717. (org-follow-mhe-link folder article)))
  11718. ((string= type "rmail")
  11719. (let (folder article)
  11720. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11721. (error "Error in RMAIL link"))
  11722. (setq folder (match-string 1 path)
  11723. article (match-string 3 path))
  11724. (org-follow-rmail-link folder article)))
  11725. ((string= type "shell")
  11726. (let ((cmd path))
  11727. (if (or (not org-confirm-shell-link-function)
  11728. (funcall org-confirm-shell-link-function
  11729. (format "Execute \"%s\" in shell? "
  11730. (org-add-props cmd nil
  11731. 'face 'org-warning))))
  11732. (progn
  11733. (message "Executing %s" cmd)
  11734. (shell-command cmd))
  11735. (error "Abort"))))
  11736. ((string= type "elisp")
  11737. (let ((cmd path))
  11738. (if (or (not org-confirm-elisp-link-function)
  11739. (funcall org-confirm-elisp-link-function
  11740. (format "Execute \"%s\" as elisp? "
  11741. (org-add-props cmd nil
  11742. 'face 'org-warning))))
  11743. (message "%s => %s" cmd (eval (read cmd)))
  11744. (error "Abort"))))
  11745. (t
  11746. (browse-url-at-point)))))
  11747. (move-marker org-open-link-marker nil))
  11748. ;;; File search
  11749. (defvar org-create-file-search-functions nil
  11750. "List of functions to construct the right search string for a file link.
  11751. These functions are called in turn with point at the location to
  11752. which the link should point.
  11753. A function in the hook should first test if it would like to
  11754. handle this file type, for example by checking the major-mode or
  11755. the file extension. If it decides not to handle this file, it
  11756. should just return nil to give other functions a chance. If it
  11757. does handle the file, it must return the search string to be used
  11758. when following the link. The search string will be part of the
  11759. file link, given after a double colon, and `org-open-at-point'
  11760. will automatically search for it. If special measures must be
  11761. taken to make the search successful, another function should be
  11762. added to the companion hook `org-execute-file-search-functions',
  11763. which see.
  11764. A function in this hook may also use `setq' to set the variable
  11765. `description' to provide a suggestion for the descriptive text to
  11766. be used for this link when it gets inserted into an Org-mode
  11767. buffer with \\[org-insert-link].")
  11768. (defvar org-execute-file-search-functions nil
  11769. "List of functions to execute a file search triggered by a link.
  11770. Functions added to this hook must accept a single argument, the
  11771. search string that was part of the file link, the part after the
  11772. double colon. The function must first check if it would like to
  11773. handle this search, for example by checking the major-mode or the
  11774. file extension. If it decides not to handle this search, it
  11775. should just return nil to give other functions a chance. If it
  11776. does handle the search, it must return a non-nil value to keep
  11777. other functions from trying.
  11778. Each function can access the current prefix argument through the
  11779. variable `current-prefix-argument'. Note that a single prefix is
  11780. used to force opening a link in Emacs, so it may be good to only
  11781. use a numeric or double prefix to guide the search function.
  11782. In case this is needed, a function in this hook can also restore
  11783. the window configuration before `org-open-at-point' was called using:
  11784. (set-window-configuration org-window-config-before-follow-link)")
  11785. (defun org-link-search (s &optional type avoid-pos)
  11786. "Search for a link search option.
  11787. If S is surrounded by forward slashes, it is interpreted as a
  11788. regular expression. In org-mode files, this will create an `org-occur'
  11789. sparse tree. In ordinary files, `occur' will be used to list matches.
  11790. If the current buffer is in `dired-mode', grep will be used to search
  11791. in all files. If AVOID-POS is given, ignore matches near that position."
  11792. (let ((case-fold-search t)
  11793. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11794. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11795. (append '(("") (" ") ("\t") ("\n"))
  11796. org-emphasis-alist)
  11797. "\\|") "\\)"))
  11798. (pos (point))
  11799. (pre "") (post "")
  11800. words re0 re1 re2 re3 re4 re5 re2a reall)
  11801. (cond
  11802. ;; First check if there are any special
  11803. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11804. ;; Now try the builtin stuff
  11805. ((save-excursion
  11806. (goto-char (point-min))
  11807. (and
  11808. (re-search-forward
  11809. (concat "<<" (regexp-quote s0) ">>") nil t)
  11810. (setq pos (match-beginning 0))))
  11811. ;; There is an exact target for this
  11812. (goto-char pos))
  11813. ((string-match "^/\\(.*\\)/$" s)
  11814. ;; A regular expression
  11815. (cond
  11816. ((org-mode-p)
  11817. (org-occur (match-string 1 s)))
  11818. ;;((eq major-mode 'dired-mode)
  11819. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11820. (t (org-do-occur (match-string 1 s)))))
  11821. (t
  11822. ;; A normal search strings
  11823. (when (equal (string-to-char s) ?*)
  11824. ;; Anchor on headlines, post may include tags.
  11825. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11826. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11827. s (substring s 1)))
  11828. (remove-text-properties
  11829. 0 (length s)
  11830. '(face nil mouse-face nil keymap nil fontified nil) s)
  11831. ;; Make a series of regular expressions to find a match
  11832. (setq words (org-split-string s "[ \n\r\t]+")
  11833. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11834. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11835. "\\)" markers)
  11836. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11837. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11838. re1 (concat pre re2 post)
  11839. re3 (concat pre re4 post)
  11840. re5 (concat pre ".*" re4)
  11841. re2 (concat pre re2)
  11842. re2a (concat pre re2a)
  11843. re4 (concat pre re4)
  11844. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11845. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11846. re5 "\\)"
  11847. ))
  11848. (cond
  11849. ((eq type 'org-occur) (org-occur reall))
  11850. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11851. (t (goto-char (point-min))
  11852. (if (or (org-search-not-self 1 re0 nil t)
  11853. (org-search-not-self 1 re1 nil t)
  11854. (org-search-not-self 1 re2 nil t)
  11855. (org-search-not-self 1 re2a nil t)
  11856. (org-search-not-self 1 re3 nil t)
  11857. (org-search-not-self 1 re4 nil t)
  11858. (org-search-not-self 1 re5 nil t)
  11859. )
  11860. (goto-char (match-beginning 1))
  11861. (goto-char pos)
  11862. (error "No match")))))
  11863. (t
  11864. ;; Normal string-search
  11865. (goto-char (point-min))
  11866. (if (search-forward s nil t)
  11867. (goto-char (match-beginning 0))
  11868. (error "No match"))))
  11869. (and (org-mode-p) (org-show-context 'link-search))))
  11870. (defun org-search-not-self (group &rest args)
  11871. "Execute `re-search-forward', but only accept matches that do not
  11872. enclose the position of `org-open-link-marker'."
  11873. (let ((m org-open-link-marker))
  11874. (catch 'exit
  11875. (while (apply 're-search-forward args)
  11876. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11877. (goto-char (match-end group))
  11878. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11879. (> (match-beginning 0) (marker-position m))
  11880. (< (match-end 0) (marker-position m)))
  11881. (save-match-data
  11882. (or (not (org-in-regexp
  11883. org-bracket-link-analytic-regexp 1))
  11884. (not (match-end 4)) ; no description
  11885. (and (<= (match-beginning 4) (point))
  11886. (>= (match-end 4) (point))))))
  11887. (throw 'exit (point))))))))
  11888. (defun org-get-buffer-for-internal-link (buffer)
  11889. "Return a buffer to be used for displaying the link target of internal links."
  11890. (cond
  11891. ((not org-display-internal-link-with-indirect-buffer)
  11892. buffer)
  11893. ((string-match "(Clone)$" (buffer-name buffer))
  11894. (message "Buffer is already a clone, not making another one")
  11895. ;; we also do not modify visibility in this case
  11896. buffer)
  11897. (t ; make a new indirect buffer for displaying the link
  11898. (let* ((bn (buffer-name buffer))
  11899. (ibn (concat bn "(Clone)"))
  11900. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11901. (with-current-buffer ib (org-overview))
  11902. ib))))
  11903. (defun org-do-occur (regexp &optional cleanup)
  11904. "Call the Emacs command `occur'.
  11905. If CLEANUP is non-nil, remove the printout of the regular expression
  11906. in the *Occur* buffer. This is useful if the regex is long and not useful
  11907. to read."
  11908. (occur regexp)
  11909. (when cleanup
  11910. (let ((cwin (selected-window)) win beg end)
  11911. (when (setq win (get-buffer-window "*Occur*"))
  11912. (select-window win))
  11913. (goto-char (point-min))
  11914. (when (re-search-forward "match[a-z]+" nil t)
  11915. (setq beg (match-end 0))
  11916. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11917. (setq end (1- (match-beginning 0)))))
  11918. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11919. (goto-char (point-min))
  11920. (select-window cwin))))
  11921. ;;; The mark ring for links jumps
  11922. (defvar org-mark-ring nil
  11923. "Mark ring for positions before jumps in Org-mode.")
  11924. (defvar org-mark-ring-last-goto nil
  11925. "Last position in the mark ring used to go back.")
  11926. ;; Fill and close the ring
  11927. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11928. (loop for i from 1 to org-mark-ring-length do
  11929. (push (make-marker) org-mark-ring))
  11930. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11931. org-mark-ring)
  11932. (defun org-mark-ring-push (&optional pos buffer)
  11933. "Put the current position or POS into the mark ring and rotate it."
  11934. (interactive)
  11935. (setq pos (or pos (point)))
  11936. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11937. (move-marker (car org-mark-ring)
  11938. (or pos (point))
  11939. (or buffer (current-buffer)))
  11940. (message "%s"
  11941. (substitute-command-keys
  11942. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11943. (defun org-mark-ring-goto (&optional n)
  11944. "Jump to the previous position in the mark ring.
  11945. With prefix arg N, jump back that many stored positions. When
  11946. called several times in succession, walk through the entire ring.
  11947. Org-mode commands jumping to a different position in the current file,
  11948. or to another Org-mode file, automatically push the old position
  11949. onto the ring."
  11950. (interactive "p")
  11951. (let (p m)
  11952. (if (eq last-command this-command)
  11953. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11954. (setq p org-mark-ring))
  11955. (setq org-mark-ring-last-goto p)
  11956. (setq m (car p))
  11957. (switch-to-buffer (marker-buffer m))
  11958. (goto-char m)
  11959. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11960. (defun org-remove-angle-brackets (s)
  11961. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11962. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11963. s)
  11964. (defun org-add-angle-brackets (s)
  11965. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11966. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11967. s)
  11968. ;;; Following specific links
  11969. (defun org-follow-timestamp-link ()
  11970. (cond
  11971. ((org-at-date-range-p t)
  11972. (let ((org-agenda-start-on-weekday)
  11973. (t1 (match-string 1))
  11974. (t2 (match-string 2)))
  11975. (setq t1 (time-to-days (org-time-string-to-time t1))
  11976. t2 (time-to-days (org-time-string-to-time t2)))
  11977. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11978. ((org-at-timestamp-p t)
  11979. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11980. (substring (match-string 1) 0 10)))
  11981. 1))
  11982. (t (error "This should not happen"))))
  11983. (defun org-follow-bbdb-link (name)
  11984. "Follow a BBDB link to NAME."
  11985. (require 'bbdb)
  11986. (let ((inhibit-redisplay (not debug-on-error))
  11987. (bbdb-electric-p nil))
  11988. (catch 'exit
  11989. ;; Exact match on name
  11990. (bbdb-name (concat "\\`" name "\\'") nil)
  11991. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11992. ;; Exact match on name
  11993. (bbdb-company (concat "\\`" name "\\'") nil)
  11994. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11995. ;; Partial match on name
  11996. (bbdb-name name nil)
  11997. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11998. ;; Partial match on company
  11999. (bbdb-company name nil)
  12000. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  12001. ;; General match including network address and notes
  12002. (bbdb name nil)
  12003. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  12004. (delete-window (get-buffer-window "*BBDB*"))
  12005. (error "No matching BBDB record")))))
  12006. (defun org-follow-info-link (name)
  12007. "Follow an info file & node link to NAME."
  12008. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12009. (string-match "\\(.*\\)" name))
  12010. (progn
  12011. (require 'info)
  12012. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12013. (Info-find-node (match-string 1 name) (match-string 2 name))
  12014. (Info-find-node (match-string 1 name) "Top")))
  12015. (message "Could not open: %s" name)))
  12016. (defun org-follow-gnus-link (&optional group article)
  12017. "Follow a Gnus link to GROUP and ARTICLE."
  12018. (require 'gnus)
  12019. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12020. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12021. (cond ((and group article)
  12022. (gnus-group-read-group 1 nil group)
  12023. (gnus-summary-goto-article (string-to-number article) nil t))
  12024. (group (gnus-group-jump-to-group group))))
  12025. (defun org-follow-vm-link (&optional folder article readonly)
  12026. "Follow a VM link to FOLDER and ARTICLE."
  12027. (require 'vm)
  12028. (setq article (org-add-angle-brackets article))
  12029. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12030. ;; ange-ftp or efs or tramp access
  12031. (let ((user (or (match-string 1 folder) (user-login-name)))
  12032. (host (match-string 2 folder))
  12033. (file (match-string 3 folder)))
  12034. (cond
  12035. ((featurep 'tramp)
  12036. ;; use tramp to access the file
  12037. (if (featurep 'xemacs)
  12038. (setq folder (format "[%s@%s]%s" user host file))
  12039. (setq folder (format "/%s@%s:%s" user host file))))
  12040. (t
  12041. ;; use ange-ftp or efs
  12042. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12043. (setq folder (format "/%s@%s:%s" user host file))))))
  12044. (when folder
  12045. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12046. (sit-for 0.1)
  12047. (when article
  12048. (vm-select-folder-buffer)
  12049. (widen)
  12050. (let ((case-fold-search t))
  12051. (goto-char (point-min))
  12052. (if (not (re-search-forward
  12053. (concat "^" "message-id: *" (regexp-quote article))))
  12054. (error "Could not find the specified message in this folder"))
  12055. (vm-isearch-update)
  12056. (vm-isearch-narrow)
  12057. (vm-beginning-of-message)
  12058. (vm-summarize)))))
  12059. (defun org-follow-wl-link (folder article)
  12060. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12061. (if (and (string= folder "%")
  12062. article
  12063. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12064. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12065. ;; Thus, we recompose folder and article ids.
  12066. (setq folder (format "%s#%s" folder (match-string 1 article))
  12067. article (match-string 3 article)))
  12068. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12069. (error "No such folder: %s" folder))
  12070. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12071. (and article
  12072. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12073. (wl-summary-redisplay)))
  12074. (defun org-follow-rmail-link (folder article)
  12075. "Follow an RMAIL link to FOLDER and ARTICLE."
  12076. (setq article (org-add-angle-brackets article))
  12077. (let (message-number)
  12078. (save-excursion
  12079. (save-window-excursion
  12080. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12081. (setq message-number
  12082. (save-restriction
  12083. (widen)
  12084. (goto-char (point-max))
  12085. (if (re-search-backward
  12086. (concat "^Message-ID:\\s-+" (regexp-quote
  12087. (or article "")))
  12088. nil t)
  12089. (rmail-what-message))))))
  12090. (if message-number
  12091. (progn
  12092. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12093. (rmail-show-message message-number)
  12094. message-number)
  12095. (error "Message not found"))))
  12096. ;;; mh-e integration based on planner-mode
  12097. (defun org-mhe-get-message-real-folder ()
  12098. "Return the name of the current message real folder, so if you use
  12099. sequences, it will now work."
  12100. (save-excursion
  12101. (let* ((folder
  12102. (if (equal major-mode 'mh-folder-mode)
  12103. mh-current-folder
  12104. ;; Refer to the show buffer
  12105. mh-show-folder-buffer))
  12106. (end-index
  12107. (if (boundp 'mh-index-folder)
  12108. (min (length mh-index-folder) (length folder))))
  12109. )
  12110. ;; a simple test on mh-index-data does not work, because
  12111. ;; mh-index-data is always nil in a show buffer.
  12112. (if (and (boundp 'mh-index-folder)
  12113. (string= mh-index-folder (substring folder 0 end-index)))
  12114. (if (equal major-mode 'mh-show-mode)
  12115. (save-window-excursion
  12116. (let (pop-up-frames)
  12117. (when (buffer-live-p (get-buffer folder))
  12118. (progn
  12119. (pop-to-buffer folder)
  12120. (org-mhe-get-message-folder-from-index)
  12121. )
  12122. )))
  12123. (org-mhe-get-message-folder-from-index)
  12124. )
  12125. folder
  12126. )
  12127. )))
  12128. (defun org-mhe-get-message-folder-from-index ()
  12129. "Returns the name of the message folder in a index folder buffer."
  12130. (save-excursion
  12131. (mh-index-previous-folder)
  12132. (re-search-forward "^\\(+.*\\)$" nil t)
  12133. (message "%s" (match-string 1))))
  12134. (defun org-mhe-get-message-folder ()
  12135. "Return the name of the current message folder. Be careful if you
  12136. use sequences."
  12137. (save-excursion
  12138. (if (equal major-mode 'mh-folder-mode)
  12139. mh-current-folder
  12140. ;; Refer to the show buffer
  12141. mh-show-folder-buffer)))
  12142. (defun org-mhe-get-message-num ()
  12143. "Return the number of the current message. Be careful if you
  12144. use sequences."
  12145. (save-excursion
  12146. (if (equal major-mode 'mh-folder-mode)
  12147. (mh-get-msg-num nil)
  12148. ;; Refer to the show buffer
  12149. (mh-show-buffer-message-number))))
  12150. (defun org-mhe-get-header (header)
  12151. "Return a header of the message in folder mode. This will create a
  12152. show buffer for the corresponding message. If you have a more clever
  12153. idea..."
  12154. (let* ((folder (org-mhe-get-message-folder))
  12155. (num (org-mhe-get-message-num))
  12156. (buffer (get-buffer-create (concat "show-" folder)))
  12157. (header-field))
  12158. (with-current-buffer buffer
  12159. (mh-display-msg num folder)
  12160. (if (equal major-mode 'mh-folder-mode)
  12161. (mh-header-display)
  12162. (mh-show-header-display))
  12163. (set-buffer buffer)
  12164. (setq header-field (mh-get-header-field header))
  12165. (if (equal major-mode 'mh-folder-mode)
  12166. (mh-show)
  12167. (mh-show-show))
  12168. header-field)))
  12169. (defun org-follow-mhe-link (folder article)
  12170. "Follow an MHE link to FOLDER and ARTICLE.
  12171. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12172. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12173. ARTICLE is searched in all folders. Indexed searches (swish++,
  12174. namazu, and others supported by MH-E) will always search in all
  12175. folders."
  12176. (require 'mh-e)
  12177. (require 'mh-search)
  12178. (require 'mh-utils)
  12179. (mh-find-path)
  12180. (if (not article)
  12181. (mh-visit-folder (mh-normalize-folder-name folder))
  12182. (setq article (org-add-angle-brackets article))
  12183. (mh-search-choose)
  12184. (if (equal mh-searcher 'pick)
  12185. (progn
  12186. (mh-search folder (list "--message-id" article))
  12187. (when (and org-mhe-search-all-folders
  12188. (not (org-mhe-get-message-real-folder)))
  12189. (kill-this-buffer)
  12190. (mh-search "+" (list "--message-id" article))))
  12191. (mh-search "+" article))
  12192. (if (org-mhe-get-message-real-folder)
  12193. (mh-show-msg 1)
  12194. (kill-this-buffer)
  12195. (error "Message not found"))))
  12196. ;;; BibTeX links
  12197. ;; Use the custom search meachnism to construct and use search strings for
  12198. ;; file links to BibTeX database entries.
  12199. (defun org-create-file-search-in-bibtex ()
  12200. "Create the search string and description for a BibTeX database entry."
  12201. (when (eq major-mode 'bibtex-mode)
  12202. ;; yes, we want to construct this search string.
  12203. ;; Make a good description for this entry, using names, year and the title
  12204. ;; Put it into the `description' variable which is dynamically scoped.
  12205. (let ((bibtex-autokey-names 1)
  12206. (bibtex-autokey-names-stretch 1)
  12207. (bibtex-autokey-name-case-convert-function 'identity)
  12208. (bibtex-autokey-name-separator " & ")
  12209. (bibtex-autokey-additional-names " et al.")
  12210. (bibtex-autokey-year-length 4)
  12211. (bibtex-autokey-name-year-separator " ")
  12212. (bibtex-autokey-titlewords 3)
  12213. (bibtex-autokey-titleword-separator " ")
  12214. (bibtex-autokey-titleword-case-convert-function 'identity)
  12215. (bibtex-autokey-titleword-length 'infty)
  12216. (bibtex-autokey-year-title-separator ": "))
  12217. (setq description (bibtex-generate-autokey)))
  12218. ;; Now parse the entry, get the key and return it.
  12219. (save-excursion
  12220. (bibtex-beginning-of-entry)
  12221. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12222. (defun org-execute-file-search-in-bibtex (s)
  12223. "Find the link search string S as a key for a database entry."
  12224. (when (eq major-mode 'bibtex-mode)
  12225. ;; Yes, we want to do the search in this file.
  12226. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12227. (goto-char (point-min))
  12228. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12229. (regexp-quote s) "[ \t\n]*,") nil t)
  12230. (goto-char (match-beginning 0)))
  12231. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12232. ;; Use double prefix to indicate that any web link should be browsed
  12233. (let ((b (current-buffer)) (p (point)))
  12234. ;; Restore the window configuration because we just use the web link
  12235. (set-window-configuration org-window-config-before-follow-link)
  12236. (save-excursion (set-buffer b) (goto-char p)
  12237. (bibtex-url)))
  12238. (recenter 0)) ; Move entry start to beginning of window
  12239. ;; return t to indicate that the search is done.
  12240. t))
  12241. ;; Finally add the functions to the right hooks.
  12242. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12243. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12244. ;; end of Bibtex link setup
  12245. ;;; Following file links
  12246. (defun org-open-file (path &optional in-emacs line search)
  12247. "Open the file at PATH.
  12248. First, this expands any special file name abbreviations. Then the
  12249. configuration variable `org-file-apps' is checked if it contains an
  12250. entry for this file type, and if yes, the corresponding command is launched.
  12251. If no application is found, Emacs simply visits the file.
  12252. With optional argument IN-EMACS, Emacs will visit the file.
  12253. Optional LINE specifies a line to go to, optional SEARCH a string to
  12254. search for. If LINE or SEARCH is given, the file will always be
  12255. opened in Emacs.
  12256. If the file does not exist, an error is thrown."
  12257. (setq in-emacs (or in-emacs line search))
  12258. (let* ((file (if (equal path "")
  12259. buffer-file-name
  12260. (substitute-in-file-name (expand-file-name path))))
  12261. (apps (append org-file-apps (org-default-apps)))
  12262. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12263. (dirp (if remp nil (file-directory-p file)))
  12264. (dfile (downcase file))
  12265. (old-buffer (current-buffer))
  12266. (old-pos (point))
  12267. (old-mode major-mode)
  12268. ext cmd)
  12269. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12270. (setq ext (match-string 1 dfile))
  12271. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12272. (setq ext (match-string 1 dfile))))
  12273. (if in-emacs
  12274. (setq cmd 'emacs)
  12275. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12276. (and dirp (cdr (assoc 'directory apps)))
  12277. (cdr (assoc ext apps))
  12278. (cdr (assoc t apps)))))
  12279. (when (eq cmd 'mailcap)
  12280. (require 'mailcap)
  12281. (mailcap-parse-mailcaps)
  12282. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12283. (command (mailcap-mime-info mime-type)))
  12284. (if (stringp command)
  12285. (setq cmd command)
  12286. (setq cmd 'emacs))))
  12287. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12288. (not (file-exists-p file))
  12289. (not org-open-non-existing-files))
  12290. (error "No such file: %s" file))
  12291. (cond
  12292. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12293. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12294. (while (string-match "['\"]%s['\"]" cmd)
  12295. (setq cmd (replace-match "%s" t t cmd)))
  12296. (while (string-match "%s" cmd)
  12297. (setq cmd (replace-match
  12298. (save-match-data (shell-quote-argument file))
  12299. t t cmd)))
  12300. (save-window-excursion
  12301. (start-process-shell-command cmd nil cmd)))
  12302. ((or (stringp cmd)
  12303. (eq cmd 'emacs))
  12304. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12305. (widen)
  12306. (if line (goto-line line)
  12307. (if search (org-link-search search))))
  12308. ((consp cmd)
  12309. (eval cmd))
  12310. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12311. (and (org-mode-p) (eq old-mode 'org-mode)
  12312. (or (not (equal old-buffer (current-buffer)))
  12313. (not (equal old-pos (point))))
  12314. (org-mark-ring-push old-pos old-buffer))))
  12315. (defun org-default-apps ()
  12316. "Return the default applications for this operating system."
  12317. (cond
  12318. ((eq system-type 'darwin)
  12319. org-file-apps-defaults-macosx)
  12320. ((eq system-type 'windows-nt)
  12321. org-file-apps-defaults-windowsnt)
  12322. (t org-file-apps-defaults-gnu)))
  12323. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12324. (defun org-file-remote-p (file)
  12325. "Test whether FILE specifies a location on a remote system.
  12326. Return non-nil if the location is indeed remote.
  12327. For example, the filename \"/user@host:/foo\" specifies a location
  12328. on the system \"/user@host:\"."
  12329. (cond ((fboundp 'file-remote-p)
  12330. (file-remote-p file))
  12331. ((fboundp 'tramp-handle-file-remote-p)
  12332. (tramp-handle-file-remote-p file))
  12333. ((and (boundp 'ange-ftp-name-format)
  12334. (string-match (car ange-ftp-name-format) file))
  12335. t)
  12336. (t nil)))
  12337. ;;;; Hooks for remember.el, and refiling
  12338. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12339. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12340. ;;;###autoload
  12341. (defun org-remember-insinuate ()
  12342. "Setup remember.el for use wiht Org-mode."
  12343. (require 'remember)
  12344. (setq remember-annotation-functions '(org-remember-annotation))
  12345. (setq remember-handler-functions '(org-remember-handler))
  12346. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12347. ;;;###autoload
  12348. (defun org-remember-annotation ()
  12349. "Return a link to the current location as an annotation for remember.el.
  12350. If you are using Org-mode files as target for data storage with
  12351. remember.el, then the annotations should include a link compatible with the
  12352. conventions in Org-mode. This function returns such a link."
  12353. (org-store-link nil))
  12354. (defconst org-remember-help
  12355. "Select a destination location for the note.
  12356. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12357. RET on headline -> Store as sublevel entry to current headline
  12358. RET at beg-of-buf -> Append to file as level 2 headline
  12359. <left>/<right> -> before/after current headline, same headings level")
  12360. (defvar org-remember-previous-location nil)
  12361. (defvar org-force-remember-template-char) ;; dynamically scoped
  12362. (defun org-select-remember-template (&optional use-char)
  12363. (when org-remember-templates
  12364. (let* ((templates (mapcar (lambda (x)
  12365. (if (stringp (car x))
  12366. (append (list (nth 1 x) (car x)) (cddr x))
  12367. (append (list (car x) "") (cdr x))))
  12368. org-remember-templates))
  12369. (char (or use-char
  12370. (cond
  12371. ((= (length templates) 1)
  12372. (caar templates))
  12373. ((and (boundp 'org-force-remember-template-char)
  12374. org-force-remember-template-char)
  12375. (if (stringp org-force-remember-template-char)
  12376. (string-to-char org-force-remember-template-char)
  12377. org-force-remember-template-char))
  12378. (t
  12379. (message "Select template: %s"
  12380. (mapconcat
  12381. (lambda (x)
  12382. (cond
  12383. ((not (string-match "\\S-" (nth 1 x)))
  12384. (format "[%c]" (car x)))
  12385. ((equal (downcase (car x))
  12386. (downcase (aref (nth 1 x) 0)))
  12387. (format "[%c]%s" (car x)
  12388. (substring (nth 1 x) 1)))
  12389. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12390. templates " "))
  12391. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12392. (when (equal char0 ?\C-g)
  12393. (jump-to-register remember-register)
  12394. (kill-buffer remember-buffer))
  12395. char0))))))
  12396. (cddr (assoc char templates)))))
  12397. (defvar x-last-selected-text)
  12398. (defvar x-last-selected-text-primary)
  12399. ;;;###autoload
  12400. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12401. "Initialize *remember* buffer with template, invoke `org-mode'.
  12402. This function should be placed into `remember-mode-hook' and in fact requires
  12403. to be run from that hook to function properly."
  12404. (if org-remember-templates
  12405. (let* ((entry (org-select-remember-template use-char))
  12406. (tpl (car entry))
  12407. (plist-p (if org-store-link-plist t nil))
  12408. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12409. (string-match "\\S-" (nth 1 entry)))
  12410. (nth 1 entry)
  12411. org-default-notes-file))
  12412. (headline (nth 2 entry))
  12413. (v-c (or (and (eq window-system 'x)
  12414. (fboundp 'x-cut-buffer-or-selection-value)
  12415. (x-cut-buffer-or-selection-value))
  12416. (org-bound-and-true-p x-last-selected-text)
  12417. (org-bound-and-true-p x-last-selected-text-primary)
  12418. (and (> (length kill-ring) 0) (current-kill 0))))
  12419. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12420. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12421. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12422. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12423. ;; `initial' and `annotation' are bound in `remember'
  12424. (v-i (if (boundp 'initial) initial))
  12425. (v-a (if (and (boundp 'annotation) annotation)
  12426. (if (equal annotation "[[]]") "" annotation)
  12427. ""))
  12428. (v-A (if (and v-a
  12429. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12430. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12431. v-a))
  12432. (v-n user-full-name)
  12433. (org-startup-folded nil)
  12434. org-time-was-given org-end-time-was-given x
  12435. prompt completions char time pos default histvar)
  12436. (setq org-store-link-plist
  12437. (append (list :annotation v-a :initial v-i)
  12438. org-store-link-plist))
  12439. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12440. (erase-buffer)
  12441. (insert (substitute-command-keys
  12442. (format
  12443. "## Filing location: Select interactively, default, or last used:
  12444. ## %s to select file and header location interactively.
  12445. ## %s \"%s\" -> \"* %s\"
  12446. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12447. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12448. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12449. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12450. (abbreviate-file-name (or file org-default-notes-file))
  12451. (or headline "")
  12452. (or (car org-remember-previous-location) "???")
  12453. (or (cdr org-remember-previous-location) "???"))))
  12454. (insert tpl) (goto-char (point-min))
  12455. ;; Simple %-escapes
  12456. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12457. (when (and initial (equal (match-string 0) "%i"))
  12458. (save-match-data
  12459. (let* ((lead (buffer-substring
  12460. (point-at-bol) (match-beginning 0))))
  12461. (setq v-i (mapconcat 'identity
  12462. (org-split-string initial "\n")
  12463. (concat "\n" lead))))))
  12464. (replace-match
  12465. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12466. t t))
  12467. ;; %[] Insert contents of a file.
  12468. (goto-char (point-min))
  12469. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12470. (let ((start (match-beginning 0))
  12471. (end (match-end 0))
  12472. (filename (expand-file-name (match-string 1))))
  12473. (goto-char start)
  12474. (delete-region start end)
  12475. (condition-case error
  12476. (insert-file-contents filename)
  12477. (error (insert (format "%%![Couldn't insert %s: %s]"
  12478. filename error))))))
  12479. ;; %() embedded elisp
  12480. (goto-char (point-min))
  12481. (while (re-search-forward "%\\((.+)\\)" nil t)
  12482. (goto-char (match-beginning 0))
  12483. (let ((template-start (point)))
  12484. (forward-char 1)
  12485. (let ((result
  12486. (condition-case error
  12487. (eval (read (current-buffer)))
  12488. (error (format "%%![Error: %s]" error)))))
  12489. (delete-region template-start (point))
  12490. (insert result))))
  12491. ;; From the property list
  12492. (when plist-p
  12493. (goto-char (point-min))
  12494. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12495. (and (setq x (or (plist-get org-store-link-plist
  12496. (intern (match-string 1))) ""))
  12497. (replace-match x t t))))
  12498. ;; Turn on org-mode in the remember buffer, set local variables
  12499. (org-mode)
  12500. (org-set-local 'org-finish-function 'org-remember-finalize)
  12501. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12502. (org-set-local 'org-default-notes-file file))
  12503. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12504. (org-set-local 'org-remember-default-headline headline))
  12505. ;; Interactive template entries
  12506. (goto-char (point-min))
  12507. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12508. (setq char (if (match-end 3) (match-string 3))
  12509. prompt (if (match-end 2) (match-string 2)))
  12510. (goto-char (match-beginning 0))
  12511. (replace-match "")
  12512. (setq completions nil default nil)
  12513. (when prompt
  12514. (setq completions (org-split-string prompt "|")
  12515. prompt (pop completions)
  12516. default (car completions)
  12517. histvar (intern (concat
  12518. "org-remember-template-prompt-history::"
  12519. (or prompt "")))
  12520. completions (mapcar 'list completions)))
  12521. (cond
  12522. ((member char '("G" "g"))
  12523. (let* ((org-last-tags-completion-table
  12524. (org-global-tags-completion-table
  12525. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12526. (org-add-colon-after-tag-completion t)
  12527. (ins (completing-read
  12528. (if prompt (concat prompt ": ") "Tags: ")
  12529. 'org-tags-completion-function nil nil nil
  12530. 'org-tags-history)))
  12531. (setq ins (mapconcat 'identity
  12532. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12533. ":"))
  12534. (when (string-match "\\S-" ins)
  12535. (or (equal (char-before) ?:) (insert ":"))
  12536. (insert ins)
  12537. (or (equal (char-after) ?:) (insert ":")))))
  12538. (char
  12539. (setq org-time-was-given (equal (upcase char) char))
  12540. (setq time (org-read-date (equal (upcase char) "U") t nil
  12541. prompt))
  12542. (org-insert-time-stamp time org-time-was-given
  12543. (member char '("u" "U"))
  12544. nil nil (list org-end-time-was-given)))
  12545. (t
  12546. (insert (org-completing-read
  12547. (concat (if prompt prompt "Enter string")
  12548. (if default (concat " [" default "]"))
  12549. ": ")
  12550. completions nil nil nil histvar default)))))
  12551. (goto-char (point-min))
  12552. (if (re-search-forward "%\\?" nil t)
  12553. (replace-match "")
  12554. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12555. (org-mode)
  12556. (org-set-local 'org-finish-function 'org-remember-finalize))
  12557. (when (save-excursion
  12558. (goto-char (point-min))
  12559. (re-search-forward "%!" nil t))
  12560. (replace-match "")
  12561. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12562. (defun org-remember-finish-immediately ()
  12563. "File remember note immediately.
  12564. This should be run in `post-command-hook' and will remove itself
  12565. from that hook."
  12566. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12567. (when org-finish-function
  12568. (funcall org-finish-function)))
  12569. (defvar org-clock-marker) ; Defined below
  12570. (defun org-remember-finalize ()
  12571. "Finalize the remember process."
  12572. (unless (fboundp 'remember-finalize)
  12573. (defalias 'remember-finalize 'remember-buffer))
  12574. (when (and org-clock-marker
  12575. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12576. ;; FIXME: test this, this is w/o notetaking!
  12577. (let (org-log-note-clock-out) (org-clock-out)))
  12578. (when buffer-file-name
  12579. (save-buffer)
  12580. (setq buffer-file-name nil))
  12581. (remember-finalize))
  12582. ;;;###autoload
  12583. (defun org-remember (&optional goto org-force-remember-template-char)
  12584. "Call `remember'. If this is already a remember buffer, re-apply template.
  12585. If there is an active region, make sure remember uses it as initial content
  12586. of the remember buffer.
  12587. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12588. anything, just go to the file/headline where the selected template usually
  12589. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12590. note stored by remember.
  12591. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12592. associated with a template in `org-remember-templates'."
  12593. (interactive "P")
  12594. (cond
  12595. ((equal goto '(4)) (org-go-to-remember-target))
  12596. ((equal goto '(16)) (org-remember-goto-last-stored))
  12597. (t
  12598. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12599. (progn
  12600. (when (< (length org-remember-templates) 2)
  12601. (error "No other template available"))
  12602. (erase-buffer)
  12603. (let ((annotation (plist-get org-store-link-plist :annotation))
  12604. (initial (plist-get org-store-link-plist :initial)))
  12605. (org-remember-apply-template))
  12606. (message "Press C-c C-c to remember data"))
  12607. (if (org-region-active-p)
  12608. (remember (buffer-substring (point) (mark)))
  12609. (call-interactively 'remember))))))
  12610. (defun org-remember-goto-last-stored ()
  12611. "Go to the location where the last remember note was stored."
  12612. (interactive)
  12613. (bookmark-jump "org-remember-last-stored")
  12614. (message "This is the last note stored by remember"))
  12615. (defun org-go-to-remember-target (&optional template-key)
  12616. "Go to the target location of a remember template.
  12617. The user is queried for the template."
  12618. (interactive)
  12619. (let* ((entry (org-select-remember-template template-key))
  12620. (file (nth 1 entry))
  12621. (heading (nth 2 entry))
  12622. visiting)
  12623. (unless (and file (stringp file) (string-match "\\S-" file))
  12624. (setq file org-default-notes-file))
  12625. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12626. (setq heading org-remember-default-headline))
  12627. (setq visiting (org-find-base-buffer-visiting file))
  12628. (if (not visiting) (find-file-noselect file))
  12629. (switch-to-buffer (or visiting (get-file-buffer file)))
  12630. (widen)
  12631. (goto-char (point-min))
  12632. (if (re-search-forward
  12633. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12634. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12635. nil t)
  12636. (goto-char (match-beginning 0))
  12637. (error "Target headline not found: %s" heading))))
  12638. (defvar org-note-abort nil) ; dynamically scoped
  12639. ;;;###autoload
  12640. (defun org-remember-handler ()
  12641. "Store stuff from remember.el into an org file.
  12642. First prompts for an org file. If the user just presses return, the value
  12643. of `org-default-notes-file' is used.
  12644. Then the command offers the headings tree of the selected file in order to
  12645. file the text at a specific location.
  12646. You can either immediately press RET to get the note appended to the
  12647. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12648. find a better place. Then press RET or <left> or <right> in insert the note.
  12649. Key Cursor position Note gets inserted
  12650. -----------------------------------------------------------------------------
  12651. RET buffer-start as level 1 heading at end of file
  12652. RET on headline as sublevel of the heading at cursor
  12653. RET no heading at cursor position, level taken from context.
  12654. Or use prefix arg to specify level manually.
  12655. <left> on headline as same level, before current heading
  12656. <right> on headline as same level, after current heading
  12657. So the fastest way to store the note is to press RET RET to append it to
  12658. the default file. This way your current train of thought is not
  12659. interrupted, in accordance with the principles of remember.el.
  12660. You can also get the fast execution without prompting by using
  12661. C-u C-c C-c to exit the remember buffer. See also the variable
  12662. `org-remember-store-without-prompt'.
  12663. Before being stored away, the function ensures that the text has a
  12664. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12665. is constructed from the current date and some additional data.
  12666. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12667. also indented so that it starts in the same column as the headline
  12668. \(i.e. after the stars).
  12669. See also the variable `org-reverse-note-order'."
  12670. (goto-char (point-min))
  12671. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12672. (replace-match ""))
  12673. (goto-char (point-max))
  12674. (beginning-of-line 1)
  12675. (while (looking-at "[ \t]*$\\|##.*")
  12676. (delete-region (1- (point)) (point-max))
  12677. (beginning-of-line 1))
  12678. (catch 'quit
  12679. (if org-note-abort (throw 'quit nil))
  12680. (let* ((txt (buffer-substring (point-min) (point-max)))
  12681. (fastp (org-xor (equal current-prefix-arg '(4))
  12682. org-remember-store-without-prompt))
  12683. (file (cond
  12684. (fastp org-default-notes-file)
  12685. ((and (eq org-remember-interactive-interface 'refile)
  12686. org-refile-targets)
  12687. org-default-notes-file)
  12688. ((not (and (equal current-prefix-arg '(16))
  12689. org-remember-previous-location))
  12690. (org-get-org-file))))
  12691. (heading org-remember-default-headline)
  12692. (visiting (and file (org-find-base-buffer-visiting file)))
  12693. (org-startup-folded nil)
  12694. (org-startup-align-all-tables nil)
  12695. (org-goto-start-pos 1)
  12696. spos exitcmd level indent reversed)
  12697. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12698. (setq file (car org-remember-previous-location)
  12699. heading (cdr org-remember-previous-location)
  12700. fastp t))
  12701. (setq current-prefix-arg nil)
  12702. (if (string-match "[ \t\n]+\\'" txt)
  12703. (setq txt (replace-match "" t t txt)))
  12704. ;; Modify text so that it becomes a nice subtree which can be inserted
  12705. ;; into an org tree.
  12706. (let* ((lines (split-string txt "\n"))
  12707. first)
  12708. (setq first (car lines) lines (cdr lines))
  12709. (if (string-match "^\\*+ " first)
  12710. ;; Is already a headline
  12711. (setq indent nil)
  12712. ;; We need to add a headline: Use time and first buffer line
  12713. (setq lines (cons first lines)
  12714. first (concat "* " (current-time-string)
  12715. " (" (remember-buffer-desc) ")")
  12716. indent " "))
  12717. (if (and org-adapt-indentation indent)
  12718. (setq lines (mapcar
  12719. (lambda (x)
  12720. (if (string-match "\\S-" x)
  12721. (concat indent x) x))
  12722. lines)))
  12723. (setq txt (concat first "\n"
  12724. (mapconcat 'identity lines "\n"))))
  12725. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12726. (setq txt (replace-match "\n\n" t t txt))
  12727. (if (string-match "[ \t\n]*\\'" txt)
  12728. (setq txt (replace-match "\n" t t txt))))
  12729. ;; Put the modified text back into the remember buffer, for refile.
  12730. (erase-buffer)
  12731. (insert txt)
  12732. (goto-char (point-min))
  12733. (when (and (eq org-remember-interactive-interface 'refile)
  12734. (not fastp))
  12735. (org-refile nil (or visiting (find-file-noselect file)))
  12736. (throw 'quit t))
  12737. ;; Find the file
  12738. (if (not visiting) (find-file-noselect file))
  12739. (with-current-buffer (or visiting (get-file-buffer file))
  12740. (unless (org-mode-p)
  12741. (error "Target files for remember notes must be in Org-mode"))
  12742. (save-excursion
  12743. (save-restriction
  12744. (widen)
  12745. (and (goto-char (point-min))
  12746. (not (re-search-forward "^\\* " nil t))
  12747. (insert "\n* " (or heading "Notes") "\n"))
  12748. (setq reversed (org-notes-order-reversed-p))
  12749. ;; Find the default location
  12750. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12751. (goto-char (point-min))
  12752. (if (re-search-forward
  12753. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12754. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12755. nil t)
  12756. (setq org-goto-start-pos (match-beginning 0))
  12757. (when fastp
  12758. (goto-char (point-max))
  12759. (unless (bolp) (newline))
  12760. (insert "* " heading "\n")
  12761. (setq org-goto-start-pos (point-at-bol 0)))))
  12762. ;; Ask the User for a location, using the appropriate interface
  12763. (cond
  12764. (fastp (setq spos org-goto-start-pos
  12765. exitcmd 'return))
  12766. ((eq org-remember-interactive-interface 'outline)
  12767. (setq spos (org-get-location (current-buffer)
  12768. org-remember-help)
  12769. exitcmd (cdr spos)
  12770. spos (car spos)))
  12771. ((eq org-remember-interactive-interface 'outline-path-completion)
  12772. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12773. (org-refile-use-outline-path t))
  12774. (setq spos (org-refile-get-location "Heading: ")
  12775. exitcmd 'return
  12776. spos (nth 3 spos))))
  12777. (t (error "this should not hapen")))
  12778. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12779. ; not handle this note
  12780. (goto-char spos)
  12781. (cond ((org-on-heading-p t)
  12782. (org-back-to-heading t)
  12783. (setq level (funcall outline-level))
  12784. (cond
  12785. ((eq exitcmd 'return)
  12786. ;; sublevel of current
  12787. (setq org-remember-previous-location
  12788. (cons (abbreviate-file-name file)
  12789. (org-get-heading 'notags)))
  12790. (if reversed
  12791. (outline-next-heading)
  12792. (org-end-of-subtree t)
  12793. (if (not (bolp))
  12794. (if (looking-at "[ \t]*\n")
  12795. (beginning-of-line 2)
  12796. (end-of-line 1)
  12797. (insert "\n"))))
  12798. (bookmark-set "org-remember-last-stored")
  12799. (org-paste-subtree (org-get-legal-level level 1) txt))
  12800. ((eq exitcmd 'left)
  12801. ;; before current
  12802. (bookmark-set "org-remember-last-stored")
  12803. (org-paste-subtree level txt))
  12804. ((eq exitcmd 'right)
  12805. ;; after current
  12806. (org-end-of-subtree t)
  12807. (bookmark-set "org-remember-last-stored")
  12808. (org-paste-subtree level txt))
  12809. (t (error "This should not happen"))))
  12810. ((and (bobp) (not reversed))
  12811. ;; Put it at the end, one level below level 1
  12812. (save-restriction
  12813. (widen)
  12814. (goto-char (point-max))
  12815. (if (not (bolp)) (newline))
  12816. (bookmark-set "org-remember-last-stored")
  12817. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12818. ((and (bobp) reversed)
  12819. ;; Put it at the start, as level 1
  12820. (save-restriction
  12821. (widen)
  12822. (goto-char (point-min))
  12823. (re-search-forward "^\\*+ " nil t)
  12824. (beginning-of-line 1)
  12825. (bookmark-set "org-remember-last-stored")
  12826. (org-paste-subtree 1 txt)))
  12827. (t
  12828. ;; Put it right there, with automatic level determined by
  12829. ;; org-paste-subtree or from prefix arg
  12830. (bookmark-set "org-remember-last-stored")
  12831. (org-paste-subtree
  12832. (if (numberp current-prefix-arg) current-prefix-arg)
  12833. txt)))
  12834. (when remember-save-after-remembering
  12835. (save-buffer)
  12836. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12837. t) ;; return t to indicate that we took care of this note.
  12838. (defun org-get-org-file ()
  12839. "Read a filename, with default directory `org-directory'."
  12840. (let ((default (or org-default-notes-file remember-data-file)))
  12841. (read-file-name (format "File name [%s]: " default)
  12842. (file-name-as-directory org-directory)
  12843. default)))
  12844. (defun org-notes-order-reversed-p ()
  12845. "Check if the current file should receive notes in reversed order."
  12846. (cond
  12847. ((not org-reverse-note-order) nil)
  12848. ((eq t org-reverse-note-order) t)
  12849. ((not (listp org-reverse-note-order)) nil)
  12850. (t (catch 'exit
  12851. (let ((all org-reverse-note-order)
  12852. entry)
  12853. (while (setq entry (pop all))
  12854. (if (string-match (car entry) buffer-file-name)
  12855. (throw 'exit (cdr entry))))
  12856. nil)))))
  12857. ;;; Refiling
  12858. (defvar org-refile-target-table nil
  12859. "The list of refile targets, created by `org-refile'.")
  12860. (defvar org-agenda-new-buffers nil
  12861. "Buffers created to visit agenda files.")
  12862. (defun org-get-refile-targets (&optional default-buffer)
  12863. "Produce a table with refile targets."
  12864. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12865. targets txt re files f desc descre)
  12866. (with-current-buffer (or default-buffer (current-buffer))
  12867. (while (setq entry (pop entries))
  12868. (setq files (car entry) desc (cdr entry))
  12869. (cond
  12870. ((null files) (setq files (list (current-buffer))))
  12871. ((eq files 'org-agenda-files)
  12872. (setq files (org-agenda-files 'unrestricted)))
  12873. ((and (symbolp files) (fboundp files))
  12874. (setq files (funcall files)))
  12875. ((and (symbolp files) (boundp files))
  12876. (setq files (symbol-value files))))
  12877. (if (stringp files) (setq files (list files)))
  12878. (cond
  12879. ((eq (car desc) :tag)
  12880. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12881. ((eq (car desc) :todo)
  12882. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12883. ((eq (car desc) :regexp)
  12884. (setq descre (cdr desc)))
  12885. ((eq (car desc) :level)
  12886. (setq descre (concat "^\\*\\{" (number-to-string
  12887. (if org-odd-levels-only
  12888. (1- (* 2 (cdr desc)))
  12889. (cdr desc)))
  12890. "\\}[ \t]")))
  12891. ((eq (car desc) :maxlevel)
  12892. (setq descre (concat "^\\*\\{1," (number-to-string
  12893. (if org-odd-levels-only
  12894. (1- (* 2 (cdr desc)))
  12895. (cdr desc)))
  12896. "\\}[ \t]")))
  12897. (t (error "Bad refiling target description %s" desc)))
  12898. (while (setq f (pop files))
  12899. (save-excursion
  12900. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12901. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12902. (save-excursion
  12903. (save-restriction
  12904. (widen)
  12905. (goto-char (point-min))
  12906. (while (re-search-forward descre nil t)
  12907. (goto-char (point-at-bol))
  12908. (when (looking-at org-complex-heading-regexp)
  12909. (setq txt (match-string 4)
  12910. re (concat "^" (regexp-quote
  12911. (buffer-substring (match-beginning 1)
  12912. (match-end 4)))))
  12913. (if (match-end 5) (setq re (concat re "[ \t]+"
  12914. (regexp-quote
  12915. (match-string 5)))))
  12916. (setq re (concat re "[ \t]*$"))
  12917. (when org-refile-use-outline-path
  12918. (setq txt (mapconcat 'identity
  12919. (append
  12920. (if (eq org-refile-use-outline-path 'file)
  12921. (list (file-name-nondirectory
  12922. (buffer-file-name (buffer-base-buffer))))
  12923. (if (eq org-refile-use-outline-path 'full-file-path)
  12924. (list (buffer-file-name (buffer-base-buffer)))))
  12925. (org-get-outline-path)
  12926. (list txt))
  12927. "/")))
  12928. (push (list txt f re (point)) targets))
  12929. (goto-char (point-at-eol))))))))
  12930. (nreverse targets))))
  12931. (defun org-get-outline-path ()
  12932. "Return the outline path to the current entry, as a list."
  12933. (let (rtn)
  12934. (save-excursion
  12935. (while (org-up-heading-safe)
  12936. (when (looking-at org-complex-heading-regexp)
  12937. (push (org-match-string-no-properties 4) rtn)))
  12938. rtn)))
  12939. (defvar org-refile-history nil
  12940. "History for refiling operations.")
  12941. (defun org-refile (&optional goto default-buffer)
  12942. "Move the entry at point to another heading.
  12943. The list of target headings is compiled using the information in
  12944. `org-refile-targets', which see. This list is created upon first use, and
  12945. you can update it by calling this command with a double prefix (`C-u C-u').
  12946. FIXME: Can we find a better way of updating?
  12947. At the target location, the entry is filed as a subitem of the target heading.
  12948. Depending on `org-reverse-note-order', the new subitem will either be the
  12949. first of the last subitem.
  12950. With prefix arg GOTO, the command will only visit the target location,
  12951. not actually move anything.
  12952. With a double prefix `C-c C-c', go to the location where the last refiling
  12953. operation has put the subtree.
  12954. With a double prefix argument, the command can be used to jump to any
  12955. heading in the current buffer."
  12956. (interactive "P")
  12957. (let* ((cbuf (current-buffer))
  12958. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12959. pos it nbuf file re level reversed)
  12960. (if (equal goto '(16))
  12961. (org-refile-goto-last-stored)
  12962. (when (setq it (org-refile-get-location
  12963. (if goto "Goto: " "Refile to: ") default-buffer))
  12964. (setq file (nth 1 it)
  12965. re (nth 2 it)
  12966. pos (nth 3 it))
  12967. (setq nbuf (or (find-buffer-visiting file)
  12968. (find-file-noselect file)))
  12969. (if goto
  12970. (progn
  12971. (switch-to-buffer nbuf)
  12972. (goto-char pos)
  12973. (org-show-context 'org-goto))
  12974. (org-copy-special)
  12975. (save-excursion
  12976. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12977. (find-file-noselect file))))
  12978. (setq reversed (org-notes-order-reversed-p))
  12979. (save-excursion
  12980. (save-restriction
  12981. (widen)
  12982. (goto-char pos)
  12983. (looking-at outline-regexp)
  12984. (setq level (org-get-legal-level (funcall outline-level) 1))
  12985. (goto-char
  12986. (if reversed
  12987. (outline-next-heading)
  12988. (or (save-excursion (outline-get-next-sibling))
  12989. (org-end-of-subtree t t)
  12990. (point-max))))
  12991. (bookmark-set "org-refile-last-stored")
  12992. (org-paste-subtree level))))
  12993. (org-cut-special)
  12994. (message "Entry refiled to \"%s\"" (car it)))))))
  12995. (defun org-refile-goto-last-stored ()
  12996. "Go to the location where the last refile was stored."
  12997. (interactive)
  12998. (bookmark-jump "org-refile-last-stored")
  12999. (message "This is the location of the last refile"))
  13000. (defun org-refile-get-location (&optional prompt default-buffer)
  13001. "Prompt the user for a refile location, using PROMPT."
  13002. (let ((org-refile-targets org-refile-targets)
  13003. (org-refile-use-outline-path org-refile-use-outline-path))
  13004. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  13005. (unless org-refile-target-table
  13006. (error "No refile targets"))
  13007. (let* ((cbuf (current-buffer))
  13008. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13009. (fname (and filename (file-truename filename)))
  13010. (tbl (mapcar
  13011. (lambda (x)
  13012. (if (not (equal fname (file-truename (nth 1 x))))
  13013. (cons (concat (car x) " (" (file-name-nondirectory
  13014. (nth 1 x)) ")")
  13015. (cdr x))
  13016. x))
  13017. org-refile-target-table))
  13018. (completion-ignore-case t))
  13019. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13020. tbl)))
  13021. ;;;; Dynamic blocks
  13022. (defun org-find-dblock (name)
  13023. "Find the first dynamic block with name NAME in the buffer.
  13024. If not found, stay at current position and return nil."
  13025. (let (pos)
  13026. (save-excursion
  13027. (goto-char (point-min))
  13028. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13029. nil t)
  13030. (match-beginning 0))))
  13031. (if pos (goto-char pos))
  13032. pos))
  13033. (defconst org-dblock-start-re
  13034. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13035. "Matches the startline of a dynamic block, with parameters.")
  13036. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13037. "Matches the end of a dyhamic block.")
  13038. (defun org-create-dblock (plist)
  13039. "Create a dynamic block section, with parameters taken from PLIST.
  13040. PLIST must containe a :name entry which is used as name of the block."
  13041. (unless (bolp) (newline))
  13042. (let ((name (plist-get plist :name)))
  13043. (insert "#+BEGIN: " name)
  13044. (while plist
  13045. (if (eq (car plist) :name)
  13046. (setq plist (cddr plist))
  13047. (insert " " (prin1-to-string (pop plist)))))
  13048. (insert "\n\n#+END:\n")
  13049. (beginning-of-line -2)))
  13050. (defun org-prepare-dblock ()
  13051. "Prepare dynamic block for refresh.
  13052. This empties the block, puts the cursor at the insert position and returns
  13053. the property list including an extra property :name with the block name."
  13054. (unless (looking-at org-dblock-start-re)
  13055. (error "Not at a dynamic block"))
  13056. (let* ((begdel (1+ (match-end 0)))
  13057. (name (org-no-properties (match-string 1)))
  13058. (params (append (list :name name)
  13059. (read (concat "(" (match-string 3) ")")))))
  13060. (unless (re-search-forward org-dblock-end-re nil t)
  13061. (error "Dynamic block not terminated"))
  13062. (delete-region begdel (match-beginning 0))
  13063. (goto-char begdel)
  13064. (open-line 1)
  13065. params))
  13066. (defun org-map-dblocks (&optional command)
  13067. "Apply COMMAND to all dynamic blocks in the current buffer.
  13068. If COMMAND is not given, use `org-update-dblock'."
  13069. (let ((cmd (or command 'org-update-dblock))
  13070. pos)
  13071. (save-excursion
  13072. (goto-char (point-min))
  13073. (while (re-search-forward org-dblock-start-re nil t)
  13074. (goto-char (setq pos (match-beginning 0)))
  13075. (condition-case nil
  13076. (funcall cmd)
  13077. (error (message "Error during update of dynamic block")))
  13078. (goto-char pos)
  13079. (unless (re-search-forward org-dblock-end-re nil t)
  13080. (error "Dynamic block not terminated"))))))
  13081. (defun org-dblock-update (&optional arg)
  13082. "User command for updating dynamic blocks.
  13083. Update the dynamic block at point. With prefix ARG, update all dynamic
  13084. blocks in the buffer."
  13085. (interactive "P")
  13086. (if arg
  13087. (org-update-all-dblocks)
  13088. (or (looking-at org-dblock-start-re)
  13089. (org-beginning-of-dblock))
  13090. (org-update-dblock)))
  13091. (defun org-update-dblock ()
  13092. "Update the dynamic block at point
  13093. This means to empty the block, parse for parameters and then call
  13094. the correct writing function."
  13095. (save-window-excursion
  13096. (let* ((pos (point))
  13097. (line (org-current-line))
  13098. (params (org-prepare-dblock))
  13099. (name (plist-get params :name))
  13100. (cmd (intern (concat "org-dblock-write:" name))))
  13101. (message "Updating dynamic block `%s' at line %d..." name line)
  13102. (funcall cmd params)
  13103. (message "Updating dynamic block `%s' at line %d...done" name line)
  13104. (goto-char pos))))
  13105. (defun org-beginning-of-dblock ()
  13106. "Find the beginning of the dynamic block at point.
  13107. Error if there is no scuh block at point."
  13108. (let ((pos (point))
  13109. beg)
  13110. (end-of-line 1)
  13111. (if (and (re-search-backward org-dblock-start-re nil t)
  13112. (setq beg (match-beginning 0))
  13113. (re-search-forward org-dblock-end-re nil t)
  13114. (> (match-end 0) pos))
  13115. (goto-char beg)
  13116. (goto-char pos)
  13117. (error "Not in a dynamic block"))))
  13118. (defun org-update-all-dblocks ()
  13119. "Update all dynamic blocks in the buffer.
  13120. This function can be used in a hook."
  13121. (when (org-mode-p)
  13122. (org-map-dblocks 'org-update-dblock)))
  13123. ;;;; Completion
  13124. (defconst org-additional-option-like-keywords
  13125. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13126. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13127. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13128. (defun org-complete (&optional arg)
  13129. "Perform completion on word at point.
  13130. At the beginning of a headline, this completes TODO keywords as given in
  13131. `org-todo-keywords'.
  13132. If the current word is preceded by a backslash, completes the TeX symbols
  13133. that are supported for HTML support.
  13134. If the current word is preceded by \"#+\", completes special words for
  13135. setting file options.
  13136. In the line after \"#+STARTUP:, complete valid keywords.\"
  13137. At all other locations, this simply calls the value of
  13138. `org-completion-fallback-command'."
  13139. (interactive "P")
  13140. (org-without-partial-completion
  13141. (catch 'exit
  13142. (let* ((end (point))
  13143. (beg1 (save-excursion
  13144. (skip-chars-backward (org-re "[:alnum:]_@"))
  13145. (point)))
  13146. (beg (save-excursion
  13147. (skip-chars-backward "a-zA-Z0-9_:$")
  13148. (point)))
  13149. (confirm (lambda (x) (stringp (car x))))
  13150. (searchhead (equal (char-before beg) ?*))
  13151. (tag (and (equal (char-before beg1) ?:)
  13152. (equal (char-after (point-at-bol)) ?*)))
  13153. (prop (and (equal (char-before beg1) ?:)
  13154. (not (equal (char-after (point-at-bol)) ?*))))
  13155. (texp (equal (char-before beg) ?\\))
  13156. (link (equal (char-before beg) ?\[))
  13157. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13158. beg)
  13159. "#+"))
  13160. (startup (string-match "^#\\+STARTUP:.*"
  13161. (buffer-substring (point-at-bol) (point))))
  13162. (completion-ignore-case opt)
  13163. (type nil)
  13164. (tbl nil)
  13165. (table (cond
  13166. (opt
  13167. (setq type :opt)
  13168. (append
  13169. (mapcar
  13170. (lambda (x)
  13171. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13172. (cons (match-string 2 x) (match-string 1 x)))
  13173. (org-split-string (org-get-current-options) "\n"))
  13174. (mapcar 'list org-additional-option-like-keywords)))
  13175. (startup
  13176. (setq type :startup)
  13177. org-startup-options)
  13178. (link (append org-link-abbrev-alist-local
  13179. org-link-abbrev-alist))
  13180. (texp
  13181. (setq type :tex)
  13182. org-html-entities)
  13183. ((string-match "\\`\\*+[ \t]+\\'"
  13184. (buffer-substring (point-at-bol) beg))
  13185. (setq type :todo)
  13186. (mapcar 'list org-todo-keywords-1))
  13187. (searchhead
  13188. (setq type :searchhead)
  13189. (save-excursion
  13190. (goto-char (point-min))
  13191. (while (re-search-forward org-todo-line-regexp nil t)
  13192. (push (list
  13193. (org-make-org-heading-search-string
  13194. (match-string 3) t))
  13195. tbl)))
  13196. tbl)
  13197. (tag (setq type :tag beg beg1)
  13198. (or org-tag-alist (org-get-buffer-tags)))
  13199. (prop (setq type :prop beg beg1)
  13200. (mapcar 'list (org-buffer-property-keys nil t t)))
  13201. (t (progn
  13202. (call-interactively org-completion-fallback-command)
  13203. (throw 'exit nil)))))
  13204. (pattern (buffer-substring-no-properties beg end))
  13205. (completion (try-completion pattern table confirm)))
  13206. (cond ((eq completion t)
  13207. (if (not (assoc (upcase pattern) table))
  13208. (message "Already complete")
  13209. (if (equal type :opt)
  13210. (insert (substring (cdr (assoc (upcase pattern) table))
  13211. (length pattern)))
  13212. (if (memq type '(:tag :prop)) (insert ":")))))
  13213. ((null completion)
  13214. (message "Can't find completion for \"%s\"" pattern)
  13215. (ding))
  13216. ((not (string= pattern completion))
  13217. (delete-region beg end)
  13218. (if (string-match " +$" completion)
  13219. (setq completion (replace-match "" t t completion)))
  13220. (insert completion)
  13221. (if (get-buffer-window "*Completions*")
  13222. (delete-window (get-buffer-window "*Completions*")))
  13223. (if (assoc completion table)
  13224. (if (eq type :todo) (insert " ")
  13225. (if (memq type '(:tag :prop)) (insert ":"))))
  13226. (if (and (equal type :opt) (assoc completion table))
  13227. (message "%s" (substitute-command-keys
  13228. "Press \\[org-complete] again to insert example settings"))))
  13229. (t
  13230. (message "Making completion list...")
  13231. (let ((list (sort (all-completions pattern table confirm)
  13232. 'string<)))
  13233. (with-output-to-temp-buffer "*Completions*"
  13234. (condition-case nil
  13235. ;; Protection needed for XEmacs and emacs 21
  13236. (display-completion-list list pattern)
  13237. (error (display-completion-list list)))))
  13238. (message "Making completion list...%s" "done")))))))
  13239. ;;;; TODO, DEADLINE, Comments
  13240. (defun org-toggle-comment ()
  13241. "Change the COMMENT state of an entry."
  13242. (interactive)
  13243. (save-excursion
  13244. (org-back-to-heading)
  13245. (let (case-fold-search)
  13246. (if (looking-at (concat outline-regexp
  13247. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13248. (replace-match "" t t nil 1)
  13249. (if (looking-at outline-regexp)
  13250. (progn
  13251. (goto-char (match-end 0))
  13252. (insert org-comment-string " ")))))))
  13253. (defvar org-last-todo-state-is-todo nil
  13254. "This is non-nil when the last TODO state change led to a TODO state.
  13255. If the last change removed the TODO tag or switched to DONE, then
  13256. this is nil.")
  13257. (defvar org-setting-tags nil) ; dynamically skiped
  13258. ;; FIXME: better place
  13259. (defun org-property-or-variable-value (var &optional inherit)
  13260. "Check if there is a property fixing the value of VAR.
  13261. If yes, return this value. If not, return the current value of the variable."
  13262. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13263. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13264. (read prop)
  13265. (symbol-value var))))
  13266. (defun org-parse-local-options (string var)
  13267. "Parse STRING for startup setting relevant for variable VAR."
  13268. (let ((rtn (symbol-value var))
  13269. e opts)
  13270. (save-match-data
  13271. (if (or (not string) (not (string-match "\\S-" string)))
  13272. rtn
  13273. (setq opts (delq nil (mapcar (lambda (x)
  13274. (setq e (assoc x org-startup-options))
  13275. (if (eq (nth 1 e) var) e nil))
  13276. (org-split-string string "[ \t]+"))))
  13277. (if (not opts)
  13278. rtn
  13279. (setq rtn nil)
  13280. (while (setq e (pop opts))
  13281. (if (not (nth 3 e))
  13282. (setq rtn (nth 2 e))
  13283. (if (not (listp rtn)) (setq rtn nil))
  13284. (push (nth 2 e) rtn)))
  13285. rtn)))))
  13286. (defvar org-blocker-hook nil
  13287. "Hook for functions that are allowed to block a state change.
  13288. Each function gets as its single argument a property list, see
  13289. `org-trigger-hook' for more information about this list.
  13290. If any of the functions in this hook returns nil, the state change
  13291. is blocked.")
  13292. (defvar org-trigger-hook nil
  13293. "Hook for functions that are triggered by a state change.
  13294. Each function gets as its single argument a property list with at least
  13295. the following elements:
  13296. (:type type-of-change :position pos-at-entry-start
  13297. :from old-state :to new-state)
  13298. Depending on the type, more properties may be present.
  13299. This mechanism is currently implemented for:
  13300. TODO state changes
  13301. ------------------
  13302. :type todo-state-change
  13303. :from previous state (keyword as a string), or nil
  13304. :to new state (keyword as a string), or nil")
  13305. (defun org-todo (&optional arg)
  13306. "Change the TODO state of an item.
  13307. The state of an item is given by a keyword at the start of the heading,
  13308. like
  13309. *** TODO Write paper
  13310. *** DONE Call mom
  13311. The different keywords are specified in the variable `org-todo-keywords'.
  13312. By default the available states are \"TODO\" and \"DONE\".
  13313. So for this example: when the item starts with TODO, it is changed to DONE.
  13314. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13315. DONE are present, add TODO at the beginning of the heading.
  13316. With C-u prefix arg, use completion to determine the new state.
  13317. With numeric prefix arg, switch to that state.
  13318. For calling through lisp, arg is also interpreted in the following way:
  13319. 'none -> empty state
  13320. \"\"(empty string) -> switch to empty state
  13321. 'done -> switch to DONE
  13322. 'nextset -> switch to the next set of keywords
  13323. 'previousset -> switch to the previous set of keywords
  13324. \"WAITING\" -> switch to the specified keyword, but only if it
  13325. really is a member of `org-todo-keywords'."
  13326. (interactive "P")
  13327. (save-excursion
  13328. (catch 'exit
  13329. (org-back-to-heading)
  13330. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13331. (or (looking-at (concat " +" org-todo-regexp " *"))
  13332. (looking-at " *"))
  13333. (let* ((match-data (match-data))
  13334. (startpos (point-at-bol))
  13335. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13336. (org-log-done org-log-done)
  13337. (org-log-repeat org-log-repeat)
  13338. (org-todo-log-states org-todo-log-states)
  13339. (this (match-string 1))
  13340. (hl-pos (match-beginning 0))
  13341. (head (org-get-todo-sequence-head this))
  13342. (ass (assoc head org-todo-kwd-alist))
  13343. (interpret (nth 1 ass))
  13344. (done-word (nth 3 ass))
  13345. (final-done-word (nth 4 ass))
  13346. (last-state (or this ""))
  13347. (completion-ignore-case t)
  13348. (member (member this org-todo-keywords-1))
  13349. (tail (cdr member))
  13350. (state (cond
  13351. ((and org-todo-key-trigger
  13352. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13353. (and (not arg) org-use-fast-todo-selection
  13354. (not (eq org-use-fast-todo-selection 'prefix)))))
  13355. ;; Use fast selection
  13356. (org-fast-todo-selection))
  13357. ((and (equal arg '(4))
  13358. (or (not org-use-fast-todo-selection)
  13359. (not org-todo-key-trigger)))
  13360. ;; Read a state with completion
  13361. (completing-read "State: " (mapcar (lambda(x) (list x))
  13362. org-todo-keywords-1)
  13363. nil t))
  13364. ((eq arg 'right)
  13365. (if this
  13366. (if tail (car tail) nil)
  13367. (car org-todo-keywords-1)))
  13368. ((eq arg 'left)
  13369. (if (equal member org-todo-keywords-1)
  13370. nil
  13371. (if this
  13372. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13373. org-todo-keywords-1)
  13374. (org-last org-todo-keywords-1))))
  13375. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13376. (setq arg nil))) ; hack to fall back to cycling
  13377. (arg
  13378. ;; user or caller requests a specific state
  13379. (cond
  13380. ((equal arg "") nil)
  13381. ((eq arg 'none) nil)
  13382. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13383. ((eq arg 'nextset)
  13384. (or (car (cdr (member head org-todo-heads)))
  13385. (car org-todo-heads)))
  13386. ((eq arg 'previousset)
  13387. (let ((org-todo-heads (reverse org-todo-heads)))
  13388. (or (car (cdr (member head org-todo-heads)))
  13389. (car org-todo-heads))))
  13390. ((car (member arg org-todo-keywords-1)))
  13391. ((nth (1- (prefix-numeric-value arg))
  13392. org-todo-keywords-1))))
  13393. ((null member) (or head (car org-todo-keywords-1)))
  13394. ((equal this final-done-word) nil) ;; -> make empty
  13395. ((null tail) nil) ;; -> first entry
  13396. ((eq interpret 'sequence)
  13397. (car tail))
  13398. ((memq interpret '(type priority))
  13399. (if (eq this-command last-command)
  13400. (car tail)
  13401. (if (> (length tail) 0)
  13402. (or done-word (car org-done-keywords))
  13403. nil)))
  13404. (t nil)))
  13405. (next (if state (concat " " state " ") " "))
  13406. (change-plist (list :type 'todo-state-change :from this :to state
  13407. :position startpos))
  13408. dolog now-done-p)
  13409. (when org-blocker-hook
  13410. (unless (save-excursion
  13411. (save-match-data
  13412. (run-hook-with-args-until-failure
  13413. 'org-blocker-hook change-plist)))
  13414. (if (interactive-p)
  13415. (error "TODO state change from %s to %s blocked" this state)
  13416. ;; fail silently
  13417. (message "TODO state change from %s to %s blocked" this state)
  13418. (throw 'exit nil))))
  13419. (store-match-data match-data)
  13420. (replace-match next t t)
  13421. (unless (pos-visible-in-window-p hl-pos)
  13422. (message "TODO state changed to %s" (org-trim next)))
  13423. (unless head
  13424. (setq head (org-get-todo-sequence-head state)
  13425. ass (assoc head org-todo-kwd-alist)
  13426. interpret (nth 1 ass)
  13427. done-word (nth 3 ass)
  13428. final-done-word (nth 4 ass)))
  13429. (when (memq arg '(nextset previousset))
  13430. (message "Keyword-Set %d/%d: %s"
  13431. (- (length org-todo-sets) -1
  13432. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13433. (length org-todo-sets)
  13434. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13435. (setq org-last-todo-state-is-todo
  13436. (not (member state org-done-keywords)))
  13437. (setq now-done-p (and (member state org-done-keywords)
  13438. (not (member this org-done-keywords))))
  13439. (and logging (org-local-logging logging))
  13440. (when (and (or org-todo-log-states org-log-done)
  13441. (not (memq arg '(nextset previousset))))
  13442. ;; we need to look at recording a time and note
  13443. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13444. (nth 2 (assoc this org-todo-log-states))))
  13445. (when (and state
  13446. (member state org-not-done-keywords)
  13447. (not (member this org-not-done-keywords)))
  13448. ;; This is now a todo state and was not one before
  13449. ;; If there was a CLOSED time stamp, get rid of it.
  13450. (org-add-planning-info nil nil 'closed))
  13451. (when (and now-done-p org-log-done)
  13452. ;; It is now done, and it was not done before
  13453. (org-add-planning-info 'closed (org-current-time))
  13454. (if (and (not dolog) (eq 'note org-log-done))
  13455. (org-add-log-maybe 'done state 'findpos 'note)))
  13456. (when (and state dolog)
  13457. ;; This is a non-nil state, and we need to log it
  13458. (org-add-log-maybe 'state state 'findpos dolog)))
  13459. ;; Fixup tag positioning
  13460. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13461. (run-hooks 'org-after-todo-state-change-hook)
  13462. (if (and arg (not (member state org-done-keywords)))
  13463. (setq head (org-get-todo-sequence-head state)))
  13464. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13465. ;; Do we need to trigger a repeat?
  13466. (when now-done-p (org-auto-repeat-maybe state))
  13467. ;; Fixup cursor location if close to the keyword
  13468. (if (and (outline-on-heading-p)
  13469. (not (bolp))
  13470. (save-excursion (beginning-of-line 1)
  13471. (looking-at org-todo-line-regexp))
  13472. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13473. (progn
  13474. (goto-char (or (match-end 2) (match-end 1)))
  13475. (just-one-space)))
  13476. (when org-trigger-hook
  13477. (save-excursion
  13478. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13479. (defun org-local-logging (value)
  13480. "Get logging settings from a property VALUE."
  13481. (let* (words w a)
  13482. ;; directly set the variables, they are already local.
  13483. (setq org-log-done nil
  13484. org-log-repeat nil
  13485. org-todo-log-states nil)
  13486. (setq words (org-split-string value))
  13487. (while (setq w (pop words))
  13488. (cond
  13489. ((setq a (assoc w org-startup-options))
  13490. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13491. (set (nth 1 a) (nth 2 a))))
  13492. ((setq a (org-extract-log-state-settings w))
  13493. (and (member (car a) org-todo-keywords-1)
  13494. (push a org-todo-log-states)))))))
  13495. (defun org-get-todo-sequence-head (kwd)
  13496. "Return the head of the TODO sequence to which KWD belongs.
  13497. If KWD is not set, check if there is a text property remembering the
  13498. right sequence."
  13499. (let (p)
  13500. (cond
  13501. ((not kwd)
  13502. (or (get-text-property (point-at-bol) 'org-todo-head)
  13503. (progn
  13504. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13505. nil (point-at-eol)))
  13506. (get-text-property p 'org-todo-head))))
  13507. ((not (member kwd org-todo-keywords-1))
  13508. (car org-todo-keywords-1))
  13509. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13510. (defun org-fast-todo-selection ()
  13511. "Fast TODO keyword selection with single keys.
  13512. Returns the new TODO keyword, or nil if no state change should occur."
  13513. (let* ((fulltable org-todo-key-alist)
  13514. (done-keywords org-done-keywords) ;; needed for the faces.
  13515. (maxlen (apply 'max (mapcar
  13516. (lambda (x)
  13517. (if (stringp (car x)) (string-width (car x)) 0))
  13518. fulltable)))
  13519. (expert nil)
  13520. (fwidth (+ maxlen 3 1 3))
  13521. (ncol (/ (- (window-width) 4) fwidth))
  13522. tg cnt e c tbl
  13523. groups ingroup)
  13524. (save-window-excursion
  13525. (if expert
  13526. (set-buffer (get-buffer-create " *Org todo*"))
  13527. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13528. (erase-buffer)
  13529. (org-set-local 'org-done-keywords done-keywords)
  13530. (setq tbl fulltable cnt 0)
  13531. (while (setq e (pop tbl))
  13532. (cond
  13533. ((equal e '(:startgroup))
  13534. (push '() groups) (setq ingroup t)
  13535. (when (not (= cnt 0))
  13536. (setq cnt 0)
  13537. (insert "\n"))
  13538. (insert "{ "))
  13539. ((equal e '(:endgroup))
  13540. (setq ingroup nil cnt 0)
  13541. (insert "}\n"))
  13542. (t
  13543. (setq tg (car e) c (cdr e))
  13544. (if ingroup (push tg (car groups)))
  13545. (setq tg (org-add-props tg nil 'face
  13546. (org-get-todo-face tg)))
  13547. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13548. (insert "[" c "] " tg (make-string
  13549. (- fwidth 4 (length tg)) ?\ ))
  13550. (when (= (setq cnt (1+ cnt)) ncol)
  13551. (insert "\n")
  13552. (if ingroup (insert " "))
  13553. (setq cnt 0)))))
  13554. (insert "\n")
  13555. (goto-char (point-min))
  13556. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13557. (fit-window-to-buffer))
  13558. (message "[a-z..]:Set [SPC]:clear")
  13559. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13560. (cond
  13561. ((or (= c ?\C-g)
  13562. (and (= c ?q) (not (rassoc c fulltable))))
  13563. (setq quit-flag t))
  13564. ((= c ?\ ) nil)
  13565. ((setq e (rassoc c fulltable) tg (car e))
  13566. tg)
  13567. (t (setq quit-flag t))))))
  13568. (defun org-get-repeat ()
  13569. "Check if tere is a deadline/schedule with repeater in this entry."
  13570. (save-match-data
  13571. (save-excursion
  13572. (org-back-to-heading t)
  13573. (if (re-search-forward
  13574. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13575. (match-string 1)))))
  13576. (defvar org-last-changed-timestamp)
  13577. (defvar org-log-post-message)
  13578. (defvar org-log-note-purpose)
  13579. (defun org-auto-repeat-maybe (done-word)
  13580. "Check if the current headline contains a repeated deadline/schedule.
  13581. If yes, set TODO state back to what it was and change the base date
  13582. of repeating deadline/scheduled time stamps to new date.
  13583. This function is run automatically after each state change to a DONE state."
  13584. ;; last-state is dynamically scoped into this function
  13585. (let* ((repeat (org-get-repeat))
  13586. (aa (assoc last-state org-todo-kwd-alist))
  13587. (interpret (nth 1 aa))
  13588. (head (nth 2 aa))
  13589. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13590. (msg "Entry repeats: ")
  13591. (org-log-done nil)
  13592. (org-todo-log-states nil)
  13593. re type n what ts)
  13594. (when repeat
  13595. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13596. (org-todo (if (eq interpret 'type) last-state head))
  13597. (when (and org-log-repeat
  13598. (or (not (memq 'org-add-log-note
  13599. (default-value 'post-command-hook)))
  13600. (eq org-log-note-purpose 'done)))
  13601. ;; Make sure a note is taken;
  13602. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13603. 'findpos org-log-repeat))
  13604. (org-back-to-heading t)
  13605. (org-add-planning-info nil nil 'closed)
  13606. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13607. org-deadline-time-regexp "\\)\\|\\("
  13608. org-ts-regexp "\\)"))
  13609. (while (re-search-forward
  13610. re (save-excursion (outline-next-heading) (point)) t)
  13611. (setq type (if (match-end 1) org-scheduled-string
  13612. (if (match-end 3) org-deadline-string "Plain:"))
  13613. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13614. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13615. (setq n (string-to-number (match-string 1 ts))
  13616. what (match-string 2 ts))
  13617. (if (equal what "w") (setq n (* n 7) what "d"))
  13618. (org-timestamp-change n (cdr (assoc what whata)))
  13619. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13620. (setq org-log-post-message msg)
  13621. (message "%s" msg))))
  13622. (defun org-show-todo-tree (arg)
  13623. "Make a compact tree which shows all headlines marked with TODO.
  13624. The tree will show the lines where the regexp matches, and all higher
  13625. headlines above the match.
  13626. With a \\[universal-argument] prefix, also show the DONE entries.
  13627. With a numeric prefix N, construct a sparse tree for the Nth element
  13628. of `org-todo-keywords-1'."
  13629. (interactive "P")
  13630. (let ((case-fold-search nil)
  13631. (kwd-re
  13632. (cond ((null arg) org-not-done-regexp)
  13633. ((equal arg '(4))
  13634. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13635. (mapcar 'list org-todo-keywords-1))))
  13636. (concat "\\("
  13637. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13638. "\\)\\>")))
  13639. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13640. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13641. org-todo-keywords-1)))
  13642. (t (error "Invalid prefix argument: %s" arg)))))
  13643. (message "%d TODO entries found"
  13644. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13645. (defun org-deadline (&optional remove)
  13646. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13647. With argument REMOVE, remove any deadline from the item."
  13648. (interactive "P")
  13649. (if remove
  13650. (progn
  13651. (org-remove-timestamp-with-keyword org-deadline-string)
  13652. (message "Item no longer has a deadline."))
  13653. (org-add-planning-info 'deadline nil 'closed)))
  13654. (defun org-schedule (&optional remove)
  13655. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13656. With argument REMOVE, remove any scheduling date from the item."
  13657. (interactive "P")
  13658. (if remove
  13659. (progn
  13660. (org-remove-timestamp-with-keyword org-scheduled-string)
  13661. (message "Item is no longer scheduled."))
  13662. (org-add-planning-info 'scheduled nil 'closed)))
  13663. (defun org-remove-timestamp-with-keyword (keyword)
  13664. "Remove all time stamps with KEYWORD in the current entry."
  13665. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13666. beg)
  13667. (save-excursion
  13668. (org-back-to-heading t)
  13669. (setq beg (point))
  13670. (org-end-of-subtree t t)
  13671. (while (re-search-backward re beg t)
  13672. (replace-match "")
  13673. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13674. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13675. (defun org-add-planning-info (what &optional time &rest remove)
  13676. "Insert new timestamp with keyword in the line directly after the headline.
  13677. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13678. If non is given, the user is prompted for a date.
  13679. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13680. be removed."
  13681. (interactive)
  13682. (let (org-time-was-given org-end-time-was-given)
  13683. (when what (setq time (or time (org-read-date nil 'to-time))))
  13684. (when (and org-insert-labeled-timestamps-at-point
  13685. (member what '(scheduled deadline)))
  13686. (insert
  13687. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13688. (org-insert-time-stamp time org-time-was-given
  13689. nil nil nil (list org-end-time-was-given))
  13690. (setq what nil))
  13691. (save-excursion
  13692. (save-restriction
  13693. (let (col list elt ts buffer-invisibility-spec)
  13694. (org-back-to-heading t)
  13695. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13696. (goto-char (match-end 1))
  13697. (setq col (current-column))
  13698. (goto-char (match-end 0))
  13699. (if (eobp) (insert "\n") (forward-char 1))
  13700. (if (and (not (looking-at outline-regexp))
  13701. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13702. "[^\r\n]*"))
  13703. (not (equal (match-string 1) org-clock-string)))
  13704. (narrow-to-region (match-beginning 0) (match-end 0))
  13705. (insert-before-markers "\n")
  13706. (backward-char 1)
  13707. (narrow-to-region (point) (point))
  13708. (indent-to-column col))
  13709. ;; Check if we have to remove something.
  13710. (setq list (cons what remove))
  13711. (while list
  13712. (setq elt (pop list))
  13713. (goto-char (point-min))
  13714. (when (or (and (eq elt 'scheduled)
  13715. (re-search-forward org-scheduled-time-regexp nil t))
  13716. (and (eq elt 'deadline)
  13717. (re-search-forward org-deadline-time-regexp nil t))
  13718. (and (eq elt 'closed)
  13719. (re-search-forward org-closed-time-regexp nil t)))
  13720. (replace-match "")
  13721. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13722. (if (looking-at " +") (replace-match ""))))
  13723. (goto-char (point-max))
  13724. (when what
  13725. (insert
  13726. (if (not (equal (char-before) ?\ )) " " "")
  13727. (cond ((eq what 'scheduled) org-scheduled-string)
  13728. ((eq what 'deadline) org-deadline-string)
  13729. ((eq what 'closed) org-closed-string))
  13730. " ")
  13731. (setq ts (org-insert-time-stamp
  13732. time
  13733. (or org-time-was-given
  13734. (and (eq what 'closed) org-log-done-with-time))
  13735. (eq what 'closed)
  13736. nil nil (list org-end-time-was-given)))
  13737. (end-of-line 1))
  13738. (goto-char (point-min))
  13739. (widen)
  13740. (if (looking-at "[ \t]+\r?\n")
  13741. (replace-match ""))
  13742. ts)))))
  13743. (defvar org-log-note-marker (make-marker))
  13744. (defvar org-log-note-purpose nil)
  13745. (defvar org-log-note-state nil)
  13746. (defvar org-log-note-how nil)
  13747. (defvar org-log-note-window-configuration nil)
  13748. (defvar org-log-note-return-to (make-marker))
  13749. (defvar org-log-post-message nil
  13750. "Message to be displayed after a log note has been stored.
  13751. The auto-repeater uses this.")
  13752. (defun org-add-log-maybe (&optional purpose state findpos how)
  13753. "Set up the post command hook to take a note.
  13754. If this is about to TODO state change, the new state is expected in STATE.
  13755. When FINDPOS is non-nil, find the correct position for the note in
  13756. the current entry. If not, assume that it can be inserted at point."
  13757. (save-excursion
  13758. (when findpos
  13759. (org-back-to-heading t)
  13760. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13761. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13762. "[^\r\n]*\\)?"))
  13763. (goto-char (match-end 0))
  13764. (unless org-log-states-order-reversed
  13765. (and (= (char-after) ?\n) (forward-char 1))
  13766. (org-skip-over-state-notes)
  13767. (skip-chars-backward " \t\n\r")))
  13768. (move-marker org-log-note-marker (point))
  13769. (setq org-log-note-purpose purpose
  13770. org-log-note-state state
  13771. org-log-note-how how)
  13772. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13773. (defun org-skip-over-state-notes ()
  13774. "Skip past the list of State notes in an entry."
  13775. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13776. (while (looking-at "[ \t]*- State")
  13777. (condition-case nil
  13778. (org-next-item)
  13779. (error (org-end-of-item)))))
  13780. (defun org-add-log-note (&optional purpose)
  13781. "Pop up a window for taking a note, and add this note later at point."
  13782. (remove-hook 'post-command-hook 'org-add-log-note)
  13783. (setq org-log-note-window-configuration (current-window-configuration))
  13784. (delete-other-windows)
  13785. (move-marker org-log-note-return-to (point))
  13786. (switch-to-buffer (marker-buffer org-log-note-marker))
  13787. (goto-char org-log-note-marker)
  13788. (org-switch-to-buffer-other-window "*Org Note*")
  13789. (erase-buffer)
  13790. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13791. (org-store-log-note)
  13792. (let ((org-inhibit-startup t)) (org-mode))
  13793. (insert (format "# Insert note for %s.
  13794. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13795. (cond
  13796. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13797. ((eq org-log-note-purpose 'done) "closed todo item")
  13798. ((eq org-log-note-purpose 'state)
  13799. (format "state change to \"%s\"" org-log-note-state))
  13800. (t (error "This should not happen")))))
  13801. (org-set-local 'org-finish-function 'org-store-log-note)))
  13802. (defun org-store-log-note ()
  13803. "Finish taking a log note, and insert it to where it belongs."
  13804. (let ((txt (buffer-string))
  13805. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13806. lines ind)
  13807. (kill-buffer (current-buffer))
  13808. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13809. (setq txt (replace-match "" t t txt)))
  13810. (if (string-match "\\s-+\\'" txt)
  13811. (setq txt (replace-match "" t t txt)))
  13812. (setq lines (org-split-string txt "\n"))
  13813. (when (and note (string-match "\\S-" note))
  13814. (setq note
  13815. (org-replace-escapes
  13816. note
  13817. (list (cons "%u" (user-login-name))
  13818. (cons "%U" user-full-name)
  13819. (cons "%t" (format-time-string
  13820. (org-time-stamp-format 'long 'inactive)
  13821. (current-time)))
  13822. (cons "%s" (if org-log-note-state
  13823. (concat "\"" org-log-note-state "\"")
  13824. "")))))
  13825. (if lines (setq note (concat note " \\\\")))
  13826. (push note lines))
  13827. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13828. (when lines
  13829. (save-excursion
  13830. (set-buffer (marker-buffer org-log-note-marker))
  13831. (save-excursion
  13832. (goto-char org-log-note-marker)
  13833. (move-marker org-log-note-marker nil)
  13834. (end-of-line 1)
  13835. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13836. (indent-relative nil)
  13837. (insert "- " (pop lines))
  13838. (org-indent-line-function)
  13839. (beginning-of-line 1)
  13840. (looking-at "[ \t]*")
  13841. (setq ind (concat (match-string 0) " "))
  13842. (end-of-line 1)
  13843. (while lines (insert "\n" ind (pop lines)))))))
  13844. (set-window-configuration org-log-note-window-configuration)
  13845. (with-current-buffer (marker-buffer org-log-note-return-to)
  13846. (goto-char org-log-note-return-to))
  13847. (move-marker org-log-note-return-to nil)
  13848. (and org-log-post-message (message "%s" org-log-post-message)))
  13849. ;; FIXME: what else would be useful?
  13850. ;; - priority
  13851. ;; - date
  13852. (defun org-sparse-tree (&optional arg)
  13853. "Create a sparse tree, prompt for the details.
  13854. This command can create sparse trees. You first need to select the type
  13855. of match used to create the tree:
  13856. t Show entries with a specific TODO keyword.
  13857. T Show entries selected by a tags match.
  13858. p Enter a property name and its value (both with completion on existing
  13859. names/values) and show entries with that property.
  13860. r Show entries matching a regular expression
  13861. d Show deadlines due within `org-deadline-warning-days'."
  13862. (interactive "P")
  13863. (let (ans kwd value)
  13864. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13865. (setq ans (read-char-exclusive))
  13866. (cond
  13867. ((equal ans ?d)
  13868. (call-interactively 'org-check-deadlines))
  13869. ((equal ans ?b)
  13870. (call-interactively 'org-check-before-date))
  13871. ((equal ans ?t)
  13872. (org-show-todo-tree '(4)))
  13873. ((equal ans ?T)
  13874. (call-interactively 'org-tags-sparse-tree))
  13875. ((member ans '(?p ?P))
  13876. (setq kwd (completing-read "Property: "
  13877. (mapcar 'list (org-buffer-property-keys))))
  13878. (setq value (completing-read "Value: "
  13879. (mapcar 'list (org-property-values kwd))))
  13880. (unless (string-match "\\`{.*}\\'" value)
  13881. (setq value (concat "\"" value "\"")))
  13882. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13883. ((member ans '(?r ?R ?/))
  13884. (call-interactively 'org-occur))
  13885. (t (error "No such sparse tree command \"%c\"" ans)))))
  13886. (defvar org-occur-highlights nil)
  13887. (make-variable-buffer-local 'org-occur-highlights)
  13888. (defun org-occur (regexp &optional keep-previous callback)
  13889. "Make a compact tree which shows all matches of REGEXP.
  13890. The tree will show the lines where the regexp matches, and all higher
  13891. headlines above the match. It will also show the heading after the match,
  13892. to make sure editing the matching entry is easy.
  13893. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13894. call to `org-occur' will be kept, to allow stacking of calls to this
  13895. command.
  13896. If CALLBACK is non-nil, it is a function which is called to confirm
  13897. that the match should indeed be shown."
  13898. (interactive "sRegexp: \nP")
  13899. (or keep-previous (org-remove-occur-highlights nil nil t))
  13900. (let ((cnt 0))
  13901. (save-excursion
  13902. (goto-char (point-min))
  13903. (if (or (not keep-previous) ; do not want to keep
  13904. (not org-occur-highlights)) ; no previous matches
  13905. ;; hide everything
  13906. (org-overview))
  13907. (while (re-search-forward regexp nil t)
  13908. (when (or (not callback)
  13909. (save-match-data (funcall callback)))
  13910. (setq cnt (1+ cnt))
  13911. (when org-highlight-sparse-tree-matches
  13912. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13913. (org-show-context 'occur-tree))))
  13914. (when org-remove-highlights-with-change
  13915. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13916. nil 'local))
  13917. (unless org-sparse-tree-open-archived-trees
  13918. (org-hide-archived-subtrees (point-min) (point-max)))
  13919. (run-hooks 'org-occur-hook)
  13920. (if (interactive-p)
  13921. (message "%d match(es) for regexp %s" cnt regexp))
  13922. cnt))
  13923. (defun org-show-context (&optional key)
  13924. "Make sure point and context and visible.
  13925. How much context is shown depends upon the variables
  13926. `org-show-hierarchy-above', `org-show-following-heading'. and
  13927. `org-show-siblings'."
  13928. (let ((heading-p (org-on-heading-p t))
  13929. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13930. (following-p (org-get-alist-option org-show-following-heading key))
  13931. (entry-p (org-get-alist-option org-show-entry-below key))
  13932. (siblings-p (org-get-alist-option org-show-siblings key)))
  13933. (catch 'exit
  13934. ;; Show heading or entry text
  13935. (if (and heading-p (not entry-p))
  13936. (org-flag-heading nil) ; only show the heading
  13937. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13938. (org-show-hidden-entry))) ; show entire entry
  13939. (when following-p
  13940. ;; Show next sibling, or heading below text
  13941. (save-excursion
  13942. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13943. (org-flag-heading nil))))
  13944. (when siblings-p (org-show-siblings))
  13945. (when hierarchy-p
  13946. ;; show all higher headings, possibly with siblings
  13947. (save-excursion
  13948. (while (and (condition-case nil
  13949. (progn (org-up-heading-all 1) t)
  13950. (error nil))
  13951. (not (bobp)))
  13952. (org-flag-heading nil)
  13953. (when siblings-p (org-show-siblings))))))))
  13954. (defun org-reveal (&optional siblings)
  13955. "Show current entry, hierarchy above it, and the following headline.
  13956. This can be used to show a consistent set of context around locations
  13957. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13958. not t for the search context.
  13959. With optional argument SIBLINGS, on each level of the hierarchy all
  13960. siblings are shown. This repairs the tree structure to what it would
  13961. look like when opened with hierarchical calls to `org-cycle'."
  13962. (interactive "P")
  13963. (let ((org-show-hierarchy-above t)
  13964. (org-show-following-heading t)
  13965. (org-show-siblings (if siblings t org-show-siblings)))
  13966. (org-show-context nil)))
  13967. (defun org-highlight-new-match (beg end)
  13968. "Highlight from BEG to END and mark the highlight is an occur headline."
  13969. (let ((ov (org-make-overlay beg end)))
  13970. (org-overlay-put ov 'face 'secondary-selection)
  13971. (push ov org-occur-highlights)))
  13972. (defun org-remove-occur-highlights (&optional beg end noremove)
  13973. "Remove the occur highlights from the buffer.
  13974. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13975. from the `before-change-functions' in the current buffer."
  13976. (interactive)
  13977. (unless org-inhibit-highlight-removal
  13978. (mapc 'org-delete-overlay org-occur-highlights)
  13979. (setq org-occur-highlights nil)
  13980. (unless noremove
  13981. (remove-hook 'before-change-functions
  13982. 'org-remove-occur-highlights 'local))))
  13983. ;;;; Priorities
  13984. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13985. "Regular expression matching the priority indicator.")
  13986. (defvar org-remove-priority-next-time nil)
  13987. (defun org-priority-up ()
  13988. "Increase the priority of the current item."
  13989. (interactive)
  13990. (org-priority 'up))
  13991. (defun org-priority-down ()
  13992. "Decrease the priority of the current item."
  13993. (interactive)
  13994. (org-priority 'down))
  13995. (defun org-priority (&optional action)
  13996. "Change the priority of an item by ARG.
  13997. ACTION can be `set', `up', `down', or a character."
  13998. (interactive)
  13999. (setq action (or action 'set))
  14000. (let (current new news have remove)
  14001. (save-excursion
  14002. (org-back-to-heading)
  14003. (if (looking-at org-priority-regexp)
  14004. (setq current (string-to-char (match-string 2))
  14005. have t)
  14006. (setq current org-default-priority))
  14007. (cond
  14008. ((or (eq action 'set) (integerp action))
  14009. (if (integerp action)
  14010. (setq new action)
  14011. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14012. (setq new (read-char-exclusive)))
  14013. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14014. (= (upcase org-lowest-priority) org-lowest-priority))
  14015. (setq new (upcase new)))
  14016. (cond ((equal new ?\ ) (setq remove t))
  14017. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14018. (error "Priority must be between `%c' and `%c'"
  14019. org-highest-priority org-lowest-priority))))
  14020. ((eq action 'up)
  14021. (if (and (not have) (eq last-command this-command))
  14022. (setq new org-lowest-priority)
  14023. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14024. org-default-priority (1- current)))))
  14025. ((eq action 'down)
  14026. (if (and (not have) (eq last-command this-command))
  14027. (setq new org-highest-priority)
  14028. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14029. org-default-priority (1+ current)))))
  14030. (t (error "Invalid action")))
  14031. (if (or (< (upcase new) org-highest-priority)
  14032. (> (upcase new) org-lowest-priority))
  14033. (setq remove t))
  14034. (setq news (format "%c" new))
  14035. (if have
  14036. (if remove
  14037. (replace-match "" t t nil 1)
  14038. (replace-match news t t nil 2))
  14039. (if remove
  14040. (error "No priority cookie found in line")
  14041. (looking-at org-todo-line-regexp)
  14042. (if (match-end 2)
  14043. (progn
  14044. (goto-char (match-end 2))
  14045. (insert " [#" news "]"))
  14046. (goto-char (match-beginning 3))
  14047. (insert "[#" news "] ")))))
  14048. (org-preserve-lc (org-set-tags nil 'align))
  14049. (if remove
  14050. (message "Priority removed")
  14051. (message "Priority of current item set to %s" news))))
  14052. (defun org-get-priority (s)
  14053. "Find priority cookie and return priority."
  14054. (save-match-data
  14055. (if (not (string-match org-priority-regexp s))
  14056. (* 1000 (- org-lowest-priority org-default-priority))
  14057. (* 1000 (- org-lowest-priority
  14058. (string-to-char (match-string 2 s)))))))
  14059. ;;;; Tags
  14060. (defun org-scan-tags (action matcher &optional todo-only)
  14061. "Scan headline tags with inheritance and produce output ACTION.
  14062. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14063. evaluated, testing if a given set of tags qualifies a headline for
  14064. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14065. are included in the output."
  14066. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14067. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14068. (org-re
  14069. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14070. (props (list 'face nil
  14071. 'done-face 'org-done
  14072. 'undone-face nil
  14073. 'mouse-face 'highlight
  14074. 'org-not-done-regexp org-not-done-regexp
  14075. 'org-todo-regexp org-todo-regexp
  14076. 'keymap org-agenda-keymap
  14077. 'help-echo
  14078. (format "mouse-2 or RET jump to org file %s"
  14079. (abbreviate-file-name
  14080. (or (buffer-file-name (buffer-base-buffer))
  14081. (buffer-name (buffer-base-buffer)))))))
  14082. (case-fold-search nil)
  14083. lspos
  14084. tags tags-list tags-alist (llast 0) rtn level category i txt
  14085. todo marker entry priority)
  14086. (save-excursion
  14087. (goto-char (point-min))
  14088. (when (eq action 'sparse-tree)
  14089. (org-overview)
  14090. (org-remove-occur-highlights))
  14091. (while (re-search-forward re nil t)
  14092. (catch :skip
  14093. (setq todo (if (match-end 1) (match-string 2))
  14094. tags (if (match-end 4) (match-string 4)))
  14095. (goto-char (setq lspos (1+ (match-beginning 0))))
  14096. (setq level (org-reduced-level (funcall outline-level))
  14097. category (org-get-category))
  14098. (setq i llast llast level)
  14099. ;; remove tag lists from same and sublevels
  14100. (while (>= i level)
  14101. (when (setq entry (assoc i tags-alist))
  14102. (setq tags-alist (delete entry tags-alist)))
  14103. (setq i (1- i)))
  14104. ;; add the nex tags
  14105. (when tags
  14106. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14107. tags-alist
  14108. (cons (cons level tags) tags-alist)))
  14109. ;; compile tags for current headline
  14110. (setq tags-list
  14111. (if org-use-tag-inheritance
  14112. (apply 'append (mapcar 'cdr tags-alist))
  14113. tags))
  14114. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14115. (eval matcher)
  14116. (or (not org-agenda-skip-archived-trees)
  14117. (not (member org-archive-tag tags-list))))
  14118. (and (eq action 'agenda) (org-agenda-skip))
  14119. ;; list this headline
  14120. (if (eq action 'sparse-tree)
  14121. (progn
  14122. (and org-highlight-sparse-tree-matches
  14123. (org-get-heading) (match-end 0)
  14124. (org-highlight-new-match
  14125. (match-beginning 0) (match-beginning 1)))
  14126. (org-show-context 'tags-tree))
  14127. (setq txt (org-format-agenda-item
  14128. ""
  14129. (concat
  14130. (if org-tags-match-list-sublevels
  14131. (make-string (1- level) ?.) "")
  14132. (org-get-heading))
  14133. category tags-list)
  14134. priority (org-get-priority txt))
  14135. (goto-char lspos)
  14136. (setq marker (org-agenda-new-marker))
  14137. (org-add-props txt props
  14138. 'org-marker marker 'org-hd-marker marker 'org-category category
  14139. 'priority priority 'type "tagsmatch")
  14140. (push txt rtn))
  14141. ;; if we are to skip sublevels, jump to end of subtree
  14142. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14143. (when (and (eq action 'sparse-tree)
  14144. (not org-sparse-tree-open-archived-trees))
  14145. (org-hide-archived-subtrees (point-min) (point-max)))
  14146. (nreverse rtn)))
  14147. (defvar todo-only) ;; dynamically scoped
  14148. (defun org-tags-sparse-tree (&optional todo-only match)
  14149. "Create a sparse tree according to tags string MATCH.
  14150. MATCH can contain positive and negative selection of tags, like
  14151. \"+WORK+URGENT-WITHBOSS\".
  14152. If optional argument TODO_ONLY is non-nil, only select lines that are
  14153. also TODO lines."
  14154. (interactive "P")
  14155. (org-prepare-agenda-buffers (list (current-buffer)))
  14156. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14157. (defvar org-cached-props nil)
  14158. (defun org-cached-entry-get (pom property)
  14159. (if (or (eq t org-use-property-inheritance)
  14160. (member property org-use-property-inheritance))
  14161. ;; Caching is not possible, check it directly
  14162. (org-entry-get pom property 'inherit)
  14163. ;; Get all properties, so that we can do complicated checks easily
  14164. (cdr (assoc property (or org-cached-props
  14165. (setq org-cached-props
  14166. (org-entry-properties pom)))))))
  14167. (defun org-global-tags-completion-table (&optional files)
  14168. "Return the list of all tags in all agenda buffer/files."
  14169. (save-excursion
  14170. (org-uniquify
  14171. (delq nil
  14172. (apply 'append
  14173. (mapcar
  14174. (lambda (file)
  14175. (set-buffer (find-file-noselect file))
  14176. (append (org-get-buffer-tags)
  14177. (mapcar (lambda (x) (if (stringp (car-safe x))
  14178. (list (car-safe x)) nil))
  14179. org-tag-alist)))
  14180. (if (and files (car files))
  14181. files
  14182. (org-agenda-files))))))))
  14183. (defun org-make-tags-matcher (match)
  14184. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14185. ;; todo-only is scoped dynamically into this function, and the function
  14186. ;; may change it it the matcher asksk for it.
  14187. (unless match
  14188. ;; Get a new match request, with completion
  14189. (let ((org-last-tags-completion-table
  14190. (org-global-tags-completion-table)))
  14191. (setq match (completing-read
  14192. "Match: " 'org-tags-completion-function nil nil nil
  14193. 'org-tags-history))))
  14194. ;; Parse the string and create a lisp form
  14195. (let ((match0 match)
  14196. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14197. minus tag mm
  14198. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14199. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14200. (if (string-match "/+" match)
  14201. ;; match contains also a todo-matching request
  14202. (progn
  14203. (setq tagsmatch (substring match 0 (match-beginning 0))
  14204. todomatch (substring match (match-end 0)))
  14205. (if (string-match "^!" todomatch)
  14206. (setq todo-only t todomatch (substring todomatch 1)))
  14207. (if (string-match "^\\s-*$" todomatch)
  14208. (setq todomatch nil)))
  14209. ;; only matching tags
  14210. (setq tagsmatch match todomatch nil))
  14211. ;; Make the tags matcher
  14212. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14213. (setq tagsmatcher t)
  14214. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14215. (while (setq term (pop orterms))
  14216. (while (and (equal (substring term -1) "\\") orterms)
  14217. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14218. (while (string-match re term)
  14219. (setq minus (and (match-end 1)
  14220. (equal (match-string 1 term) "-"))
  14221. tag (match-string 2 term)
  14222. re-p (equal (string-to-char tag) ?{)
  14223. level-p (match-end 3)
  14224. prop-p (match-end 4)
  14225. mm (cond
  14226. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14227. (level-p `(= level ,(string-to-number
  14228. (match-string 3 term))))
  14229. (prop-p
  14230. (setq pn (match-string 4 term)
  14231. pv (match-string 5 term)
  14232. cat-p (equal pn "CATEGORY")
  14233. re-p (equal (string-to-char pv) ?{)
  14234. pv (substring pv 1 -1))
  14235. (if (equal pn "CATEGORY")
  14236. (setq gv '(get-text-property (point) 'org-category))
  14237. (setq gv `(org-cached-entry-get nil ,pn)))
  14238. (if re-p
  14239. `(string-match ,pv (or ,gv ""))
  14240. `(equal ,pv (or ,gv ""))))
  14241. (t `(member ,(downcase tag) tags-list)))
  14242. mm (if minus (list 'not mm) mm)
  14243. term (substring term (match-end 0)))
  14244. (push mm tagsmatcher))
  14245. (push (if (> (length tagsmatcher) 1)
  14246. (cons 'and tagsmatcher)
  14247. (car tagsmatcher))
  14248. orlist)
  14249. (setq tagsmatcher nil))
  14250. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14251. (setq tagsmatcher
  14252. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14253. ;; Make the todo matcher
  14254. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14255. (setq todomatcher t)
  14256. (setq orterms (org-split-string todomatch "|") orlist nil)
  14257. (while (setq term (pop orterms))
  14258. (while (string-match re term)
  14259. (setq minus (and (match-end 1)
  14260. (equal (match-string 1 term) "-"))
  14261. kwd (match-string 2 term)
  14262. re-p (equal (string-to-char kwd) ?{)
  14263. term (substring term (match-end 0))
  14264. mm (if re-p
  14265. `(string-match ,(substring kwd 1 -1) todo)
  14266. (list 'equal 'todo kwd))
  14267. mm (if minus (list 'not mm) mm))
  14268. (push mm todomatcher))
  14269. (push (if (> (length todomatcher) 1)
  14270. (cons 'and todomatcher)
  14271. (car todomatcher))
  14272. orlist)
  14273. (setq todomatcher nil))
  14274. (setq todomatcher (if (> (length orlist) 1)
  14275. (cons 'or orlist) (car orlist))))
  14276. ;; Return the string and lisp forms of the matcher
  14277. (setq matcher (if todomatcher
  14278. (list 'and tagsmatcher todomatcher)
  14279. tagsmatcher))
  14280. (cons match0 matcher)))
  14281. (defun org-match-any-p (re list)
  14282. "Does re match any element of list?"
  14283. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14284. (delq nil list))
  14285. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14286. (defvar org-tags-overlay (org-make-overlay 1 1))
  14287. (org-detach-overlay org-tags-overlay)
  14288. (defun org-align-tags-here (to-col)
  14289. ;; Assumes that this is a headline
  14290. (let ((pos (point)) (col (current-column)) tags)
  14291. (beginning-of-line 1)
  14292. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14293. (< pos (match-beginning 2)))
  14294. (progn
  14295. (setq tags (match-string 2))
  14296. (goto-char (match-beginning 1))
  14297. (insert " ")
  14298. (delete-region (point) (1+ (match-end 0)))
  14299. (backward-char 1)
  14300. (move-to-column
  14301. (max (1+ (current-column))
  14302. (1+ col)
  14303. (if (> to-col 0)
  14304. to-col
  14305. (- (abs to-col) (length tags))))
  14306. t)
  14307. (insert tags)
  14308. (move-to-column (min (current-column) col) t))
  14309. (goto-char pos))))
  14310. (defun org-set-tags (&optional arg just-align)
  14311. "Set the tags for the current headline.
  14312. With prefix ARG, realign all tags in headings in the current buffer."
  14313. (interactive "P")
  14314. (let* ((re (concat "^" outline-regexp))
  14315. (current (org-get-tags-string))
  14316. (col (current-column))
  14317. (org-setting-tags t)
  14318. table current-tags inherited-tags ; computed below when needed
  14319. tags p0 c0 c1 rpl)
  14320. (if arg
  14321. (save-excursion
  14322. (goto-char (point-min))
  14323. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14324. (while (re-search-forward re nil t)
  14325. (org-set-tags nil t)
  14326. (end-of-line 1)))
  14327. (message "All tags realigned to column %d" org-tags-column))
  14328. (if just-align
  14329. (setq tags current)
  14330. ;; Get a new set of tags from the user
  14331. (save-excursion
  14332. (setq table (or org-tag-alist (org-get-buffer-tags))
  14333. org-last-tags-completion-table table
  14334. current-tags (org-split-string current ":")
  14335. inherited-tags (nreverse
  14336. (nthcdr (length current-tags)
  14337. (nreverse (org-get-tags-at))))
  14338. tags
  14339. (if (or (eq t org-use-fast-tag-selection)
  14340. (and org-use-fast-tag-selection
  14341. (delq nil (mapcar 'cdr table))))
  14342. (org-fast-tag-selection
  14343. current-tags inherited-tags table
  14344. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14345. (let ((org-add-colon-after-tag-completion t))
  14346. (org-trim
  14347. (org-without-partial-completion
  14348. (completing-read "Tags: " 'org-tags-completion-function
  14349. nil nil current 'org-tags-history)))))))
  14350. (while (string-match "[-+&]+" tags)
  14351. ;; No boolean logic, just a list
  14352. (setq tags (replace-match ":" t t tags))))
  14353. (if (string-match "\\`[\t ]*\\'" tags)
  14354. (setq tags "")
  14355. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14356. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14357. ;; Insert new tags at the correct column
  14358. (beginning-of-line 1)
  14359. (cond
  14360. ((and (equal current "") (equal tags "")))
  14361. ((re-search-forward
  14362. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14363. (point-at-eol) t)
  14364. (if (equal tags "")
  14365. (setq rpl "")
  14366. (goto-char (match-beginning 0))
  14367. (setq c0 (current-column) p0 (point)
  14368. c1 (max (1+ c0) (if (> org-tags-column 0)
  14369. org-tags-column
  14370. (- (- org-tags-column) (length tags))))
  14371. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14372. (replace-match rpl t t)
  14373. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14374. tags)
  14375. (t (error "Tags alignment failed")))
  14376. (move-to-column col)
  14377. (unless just-align
  14378. (run-hooks 'org-after-tags-change-hook)))))
  14379. (defun org-change-tag-in-region (beg end tag off)
  14380. "Add or remove TAG for each entry in the region.
  14381. This works in the agenda, and also in an org-mode buffer."
  14382. (interactive
  14383. (list (region-beginning) (region-end)
  14384. (let ((org-last-tags-completion-table
  14385. (if (org-mode-p)
  14386. (org-get-buffer-tags)
  14387. (org-global-tags-completion-table))))
  14388. (completing-read
  14389. "Tag: " 'org-tags-completion-function nil nil nil
  14390. 'org-tags-history))
  14391. (progn
  14392. (message "[s]et or [r]emove? ")
  14393. (equal (read-char-exclusive) ?r))))
  14394. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14395. (let ((agendap (equal major-mode 'org-agenda-mode))
  14396. l1 l2 m buf pos newhead (cnt 0))
  14397. (goto-char end)
  14398. (setq l2 (1- (org-current-line)))
  14399. (goto-char beg)
  14400. (setq l1 (org-current-line))
  14401. (loop for l from l1 to l2 do
  14402. (goto-line l)
  14403. (setq m (get-text-property (point) 'org-hd-marker))
  14404. (when (or (and (org-mode-p) (org-on-heading-p))
  14405. (and agendap m))
  14406. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14407. pos (if agendap m (point)))
  14408. (with-current-buffer buf
  14409. (save-excursion
  14410. (save-restriction
  14411. (goto-char pos)
  14412. (setq cnt (1+ cnt))
  14413. (org-toggle-tag tag (if off 'off 'on))
  14414. (setq newhead (org-get-heading)))))
  14415. (and agendap (org-agenda-change-all-lines newhead m))))
  14416. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14417. (defun org-tags-completion-function (string predicate &optional flag)
  14418. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14419. (confirm (lambda (x) (stringp (car x)))))
  14420. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14421. (setq s1 (match-string 1 string)
  14422. s2 (match-string 2 string))
  14423. (setq s1 "" s2 string))
  14424. (cond
  14425. ((eq flag nil)
  14426. ;; try completion
  14427. (setq rtn (try-completion s2 ctable confirm))
  14428. (if (stringp rtn)
  14429. (setq rtn
  14430. (concat s1 s2 (substring rtn (length s2))
  14431. (if (and org-add-colon-after-tag-completion
  14432. (assoc rtn ctable))
  14433. ":" ""))))
  14434. rtn)
  14435. ((eq flag t)
  14436. ;; all-completions
  14437. (all-completions s2 ctable confirm)
  14438. )
  14439. ((eq flag 'lambda)
  14440. ;; exact match?
  14441. (assoc s2 ctable)))
  14442. ))
  14443. (defun org-fast-tag-insert (kwd tags face &optional end)
  14444. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14445. (insert (format "%-12s" (concat kwd ":"))
  14446. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14447. (or end "")))
  14448. (defun org-fast-tag-show-exit (flag)
  14449. (save-excursion
  14450. (goto-line 3)
  14451. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14452. (replace-match ""))
  14453. (when flag
  14454. (end-of-line 1)
  14455. (move-to-column (- (window-width) 19) t)
  14456. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14457. (defun org-set-current-tags-overlay (current prefix)
  14458. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14459. (if (featurep 'xemacs)
  14460. (org-overlay-display org-tags-overlay (concat prefix s)
  14461. 'secondary-selection)
  14462. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14463. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14464. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14465. "Fast tag selection with single keys.
  14466. CURRENT is the current list of tags in the headline, INHERITED is the
  14467. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14468. possibly with grouping information. TODO-TABLE is a similar table with
  14469. TODO keywords, should these have keys assigned to them.
  14470. If the keys are nil, a-z are automatically assigned.
  14471. Returns the new tags string, or nil to not change the current settings."
  14472. (let* ((fulltable (append table todo-table))
  14473. (maxlen (apply 'max (mapcar
  14474. (lambda (x)
  14475. (if (stringp (car x)) (string-width (car x)) 0))
  14476. fulltable)))
  14477. (buf (current-buffer))
  14478. (expert (eq org-fast-tag-selection-single-key 'expert))
  14479. (buffer-tags nil)
  14480. (fwidth (+ maxlen 3 1 3))
  14481. (ncol (/ (- (window-width) 4) fwidth))
  14482. (i-face 'org-done)
  14483. (c-face 'org-todo)
  14484. tg cnt e c char c1 c2 ntable tbl rtn
  14485. ov-start ov-end ov-prefix
  14486. (exit-after-next org-fast-tag-selection-single-key)
  14487. (done-keywords org-done-keywords)
  14488. groups ingroup)
  14489. (save-excursion
  14490. (beginning-of-line 1)
  14491. (if (looking-at
  14492. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14493. (setq ov-start (match-beginning 1)
  14494. ov-end (match-end 1)
  14495. ov-prefix "")
  14496. (setq ov-start (1- (point-at-eol))
  14497. ov-end (1+ ov-start))
  14498. (skip-chars-forward "^\n\r")
  14499. (setq ov-prefix
  14500. (concat
  14501. (buffer-substring (1- (point)) (point))
  14502. (if (> (current-column) org-tags-column)
  14503. " "
  14504. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14505. (org-move-overlay org-tags-overlay ov-start ov-end)
  14506. (save-window-excursion
  14507. (if expert
  14508. (set-buffer (get-buffer-create " *Org tags*"))
  14509. (delete-other-windows)
  14510. (split-window-vertically)
  14511. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14512. (erase-buffer)
  14513. (org-set-local 'org-done-keywords done-keywords)
  14514. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14515. (org-fast-tag-insert "Current" current c-face "\n\n")
  14516. (org-fast-tag-show-exit exit-after-next)
  14517. (org-set-current-tags-overlay current ov-prefix)
  14518. (setq tbl fulltable char ?a cnt 0)
  14519. (while (setq e (pop tbl))
  14520. (cond
  14521. ((equal e '(:startgroup))
  14522. (push '() groups) (setq ingroup t)
  14523. (when (not (= cnt 0))
  14524. (setq cnt 0)
  14525. (insert "\n"))
  14526. (insert "{ "))
  14527. ((equal e '(:endgroup))
  14528. (setq ingroup nil cnt 0)
  14529. (insert "}\n"))
  14530. (t
  14531. (setq tg (car e) c2 nil)
  14532. (if (cdr e)
  14533. (setq c (cdr e))
  14534. ;; automatically assign a character.
  14535. (setq c1 (string-to-char
  14536. (downcase (substring
  14537. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14538. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14539. (while (or (rassoc char ntable) (rassoc char table))
  14540. (setq char (1+ char)))
  14541. (setq c2 c1))
  14542. (setq c (or c2 char)))
  14543. (if ingroup (push tg (car groups)))
  14544. (setq tg (org-add-props tg nil 'face
  14545. (cond
  14546. ((not (assoc tg table))
  14547. (org-get-todo-face tg))
  14548. ((member tg current) c-face)
  14549. ((member tg inherited) i-face)
  14550. (t nil))))
  14551. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14552. (insert "[" c "] " tg (make-string
  14553. (- fwidth 4 (length tg)) ?\ ))
  14554. (push (cons tg c) ntable)
  14555. (when (= (setq cnt (1+ cnt)) ncol)
  14556. (insert "\n")
  14557. (if ingroup (insert " "))
  14558. (setq cnt 0)))))
  14559. (setq ntable (nreverse ntable))
  14560. (insert "\n")
  14561. (goto-char (point-min))
  14562. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14563. (fit-window-to-buffer))
  14564. (setq rtn
  14565. (catch 'exit
  14566. (while t
  14567. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14568. (if groups " [!] no groups" " [!]groups")
  14569. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14570. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14571. (cond
  14572. ((= c ?\r) (throw 'exit t))
  14573. ((= c ?!)
  14574. (setq groups (not groups))
  14575. (goto-char (point-min))
  14576. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14577. ((= c ?\C-c)
  14578. (if (not expert)
  14579. (org-fast-tag-show-exit
  14580. (setq exit-after-next (not exit-after-next)))
  14581. (setq expert nil)
  14582. (delete-other-windows)
  14583. (split-window-vertically)
  14584. (org-switch-to-buffer-other-window " *Org tags*")
  14585. (and (fboundp 'fit-window-to-buffer)
  14586. (fit-window-to-buffer))))
  14587. ((or (= c ?\C-g)
  14588. (and (= c ?q) (not (rassoc c ntable))))
  14589. (org-detach-overlay org-tags-overlay)
  14590. (setq quit-flag t))
  14591. ((= c ?\ )
  14592. (setq current nil)
  14593. (if exit-after-next (setq exit-after-next 'now)))
  14594. ((= c ?\t)
  14595. (condition-case nil
  14596. (setq tg (completing-read
  14597. "Tag: "
  14598. (or buffer-tags
  14599. (with-current-buffer buf
  14600. (org-get-buffer-tags)))))
  14601. (quit (setq tg "")))
  14602. (when (string-match "\\S-" tg)
  14603. (add-to-list 'buffer-tags (list tg))
  14604. (if (member tg current)
  14605. (setq current (delete tg current))
  14606. (push tg current)))
  14607. (if exit-after-next (setq exit-after-next 'now)))
  14608. ((setq e (rassoc c todo-table) tg (car e))
  14609. (with-current-buffer buf
  14610. (save-excursion (org-todo tg)))
  14611. (if exit-after-next (setq exit-after-next 'now)))
  14612. ((setq e (rassoc c ntable) tg (car e))
  14613. (if (member tg current)
  14614. (setq current (delete tg current))
  14615. (loop for g in groups do
  14616. (if (member tg g)
  14617. (mapc (lambda (x)
  14618. (setq current (delete x current)))
  14619. g)))
  14620. (push tg current))
  14621. (if exit-after-next (setq exit-after-next 'now))))
  14622. ;; Create a sorted list
  14623. (setq current
  14624. (sort current
  14625. (lambda (a b)
  14626. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14627. (if (eq exit-after-next 'now) (throw 'exit t))
  14628. (goto-char (point-min))
  14629. (beginning-of-line 2)
  14630. (delete-region (point) (point-at-eol))
  14631. (org-fast-tag-insert "Current" current c-face)
  14632. (org-set-current-tags-overlay current ov-prefix)
  14633. (while (re-search-forward
  14634. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14635. (setq tg (match-string 1))
  14636. (add-text-properties
  14637. (match-beginning 1) (match-end 1)
  14638. (list 'face
  14639. (cond
  14640. ((member tg current) c-face)
  14641. ((member tg inherited) i-face)
  14642. (t (get-text-property (match-beginning 1) 'face))))))
  14643. (goto-char (point-min)))))
  14644. (org-detach-overlay org-tags-overlay)
  14645. (if rtn
  14646. (mapconcat 'identity current ":")
  14647. nil))))
  14648. (defun org-get-tags-string ()
  14649. "Get the TAGS string in the current headline."
  14650. (unless (org-on-heading-p t)
  14651. (error "Not on a heading"))
  14652. (save-excursion
  14653. (beginning-of-line 1)
  14654. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14655. (org-match-string-no-properties 1)
  14656. "")))
  14657. (defun org-get-tags ()
  14658. "Get the list of tags specified in the current headline."
  14659. (org-split-string (org-get-tags-string) ":"))
  14660. (defun org-get-buffer-tags ()
  14661. "Get a table of all tags used in the buffer, for completion."
  14662. (let (tags)
  14663. (save-excursion
  14664. (goto-char (point-min))
  14665. (while (re-search-forward
  14666. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14667. (when (equal (char-after (point-at-bol 0)) ?*)
  14668. (mapc (lambda (x) (add-to-list 'tags x))
  14669. (org-split-string (org-match-string-no-properties 1) ":")))))
  14670. (mapcar 'list tags)))
  14671. ;;;; Properties
  14672. ;;; Setting and retrieving properties
  14673. (defconst org-special-properties
  14674. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14675. "TIMESTAMP" "TIMESTAMP_IA")
  14676. "The special properties valid in Org-mode.
  14677. These are properties that are not defined in the property drawer,
  14678. but in some other way.")
  14679. (defconst org-default-properties
  14680. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14681. "LOCATION" "LOGGING" "COLUMNS")
  14682. "Some properties that are used by Org-mode for various purposes.
  14683. Being in this list makes sure that they are offered for completion.")
  14684. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14685. "Regular expression matching the first line of a property drawer.")
  14686. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14687. "Regular expression matching the first line of a property drawer.")
  14688. (defun org-property-action ()
  14689. "Do an action on properties."
  14690. (interactive)
  14691. (let (c)
  14692. (org-at-property-p)
  14693. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14694. (setq c (read-char-exclusive))
  14695. (cond
  14696. ((equal c ?s)
  14697. (call-interactively 'org-set-property))
  14698. ((equal c ?d)
  14699. (call-interactively 'org-delete-property))
  14700. ((equal c ?D)
  14701. (call-interactively 'org-delete-property-globally))
  14702. ((equal c ?c)
  14703. (call-interactively 'org-compute-property-at-point))
  14704. (t (error "No such property action %c" c)))))
  14705. (defun org-at-property-p ()
  14706. "Is the cursor in a property line?"
  14707. ;; FIXME: Does not check if we are actually in the drawer.
  14708. ;; FIXME: also returns true on any drawers.....
  14709. ;; This is used by C-c C-c for property action.
  14710. (save-excursion
  14711. (beginning-of-line 1)
  14712. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14713. (defmacro org-with-point-at (pom &rest body)
  14714. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14715. (declare (indent 1) (debug t))
  14716. `(save-excursion
  14717. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14718. (save-excursion
  14719. (goto-char (or pom (point)))
  14720. ,@body)))
  14721. (defun org-get-property-block (&optional beg end force)
  14722. "Return the (beg . end) range of the body of the property drawer.
  14723. BEG and END can be beginning and end of subtree, if not given
  14724. they will be found.
  14725. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14726. (catch 'exit
  14727. (save-excursion
  14728. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14729. (end (or end (progn (outline-next-heading) (point)))))
  14730. (goto-char beg)
  14731. (if (re-search-forward org-property-start-re end t)
  14732. (setq beg (1+ (match-end 0)))
  14733. (if force
  14734. (save-excursion
  14735. (org-insert-property-drawer)
  14736. (setq end (progn (outline-next-heading) (point))))
  14737. (throw 'exit nil))
  14738. (goto-char beg)
  14739. (if (re-search-forward org-property-start-re end t)
  14740. (setq beg (1+ (match-end 0)))))
  14741. (if (re-search-forward org-property-end-re end t)
  14742. (setq end (match-beginning 0))
  14743. (or force (throw 'exit nil))
  14744. (goto-char beg)
  14745. (setq end beg)
  14746. (org-indent-line-function)
  14747. (insert ":END:\n"))
  14748. (cons beg end)))))
  14749. (defun org-entry-properties (&optional pom which)
  14750. "Get all properties of the entry at point-or-marker POM.
  14751. This includes the TODO keyword, the tags, time strings for deadline,
  14752. scheduled, and clocking, and any additional properties defined in the
  14753. entry. The return value is an alist, keys may occur multiple times
  14754. if the property key was used several times.
  14755. POM may also be nil, in which case the current entry is used.
  14756. If WHICH is nil or `all', get all properties. If WHICH is
  14757. `special' or `standard', only get that subclass."
  14758. (setq which (or which 'all))
  14759. (org-with-point-at pom
  14760. (let ((clockstr (substring org-clock-string 0 -1))
  14761. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14762. beg end range props sum-props key value string clocksum)
  14763. (save-excursion
  14764. (when (condition-case nil (org-back-to-heading t) (error nil))
  14765. (setq beg (point))
  14766. (setq sum-props (get-text-property (point) 'org-summaries))
  14767. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14768. (outline-next-heading)
  14769. (setq end (point))
  14770. (when (memq which '(all special))
  14771. ;; Get the special properties, like TODO and tags
  14772. (goto-char beg)
  14773. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14774. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14775. (when (looking-at org-priority-regexp)
  14776. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14777. (when (and (setq value (org-get-tags-string))
  14778. (string-match "\\S-" value))
  14779. (push (cons "TAGS" value) props))
  14780. (when (setq value (org-get-tags-at))
  14781. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14782. props))
  14783. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14784. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14785. string (if (equal key clockstr)
  14786. (org-no-properties
  14787. (org-trim
  14788. (buffer-substring
  14789. (match-beginning 3) (goto-char (point-at-eol)))))
  14790. (substring (org-match-string-no-properties 3) 1 -1)))
  14791. (unless key
  14792. (if (= (char-after (match-beginning 3)) ?\[)
  14793. (setq key "TIMESTAMP_IA")
  14794. (setq key "TIMESTAMP")))
  14795. (when (or (equal key clockstr) (not (assoc key props)))
  14796. (push (cons key string) props)))
  14797. )
  14798. (when (memq which '(all standard))
  14799. ;; Get the standard properties, like :PORP: ...
  14800. (setq range (org-get-property-block beg end))
  14801. (when range
  14802. (goto-char (car range))
  14803. (while (re-search-forward
  14804. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14805. (cdr range) t)
  14806. (setq key (org-match-string-no-properties 1)
  14807. value (org-trim (or (org-match-string-no-properties 2) "")))
  14808. (unless (member key excluded)
  14809. (push (cons key (or value "")) props)))))
  14810. (if clocksum
  14811. (push (cons "CLOCKSUM"
  14812. (org-column-number-to-string (/ (float clocksum) 60.)
  14813. 'add_times))
  14814. props))
  14815. (append sum-props (nreverse props)))))))
  14816. (defun org-entry-get (pom property &optional inherit)
  14817. "Get value of PROPERTY for entry at point-or-marker POM.
  14818. If INHERIT is non-nil and the entry does not have the property,
  14819. then also check higher levels of the hierarchy.
  14820. If the property is present but empty, the return value is the empty string.
  14821. If the property is not present at all, nil is returned."
  14822. (org-with-point-at pom
  14823. (if inherit
  14824. (org-entry-get-with-inheritance property)
  14825. (if (member property org-special-properties)
  14826. ;; We need a special property. Use brute force, get all properties.
  14827. (cdr (assoc property (org-entry-properties nil 'special)))
  14828. (let ((range (org-get-property-block)))
  14829. (if (and range
  14830. (goto-char (car range))
  14831. (re-search-forward
  14832. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14833. (cdr range) t))
  14834. ;; Found the property, return it.
  14835. (if (match-end 1)
  14836. (org-match-string-no-properties 1)
  14837. "")))))))
  14838. (defun org-entry-delete (pom property)
  14839. "Delete the property PROPERTY from entry at point-or-marker POM."
  14840. (org-with-point-at pom
  14841. (if (member property org-special-properties)
  14842. nil ; cannot delete these properties.
  14843. (let ((range (org-get-property-block)))
  14844. (if (and range
  14845. (goto-char (car range))
  14846. (re-search-forward
  14847. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14848. (cdr range) t))
  14849. (progn
  14850. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14851. t)
  14852. nil)))))
  14853. ;; Multi-values properties are properties that contain multiple values
  14854. ;; These values are assumed to be single words, separated by whitespace.
  14855. (defun org-entry-add-to-multivalued-property (pom property value)
  14856. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14857. (let* ((old (org-entry-get pom property))
  14858. (values (and old (org-split-string old "[ \t]"))))
  14859. (unless (member value values)
  14860. (setq values (cons value values))
  14861. (org-entry-put pom property
  14862. (mapconcat 'identity values " ")))))
  14863. (defun org-entry-remove-from-multivalued-property (pom property value)
  14864. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14865. (let* ((old (org-entry-get pom property))
  14866. (values (and old (org-split-string old "[ \t]"))))
  14867. (when (member value values)
  14868. (setq values (delete value values))
  14869. (org-entry-put pom property
  14870. (mapconcat 'identity values " ")))))
  14871. (defun org-entry-member-in-multivalued-property (pom property value)
  14872. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14873. (let* ((old (org-entry-get pom property))
  14874. (values (and old (org-split-string old "[ \t]"))))
  14875. (member value values)))
  14876. (defvar org-entry-property-inherited-from (make-marker))
  14877. (defun org-entry-get-with-inheritance (property)
  14878. "Get entry property, and search higher levels if not present."
  14879. (let (tmp)
  14880. (save-excursion
  14881. (save-restriction
  14882. (widen)
  14883. (catch 'ex
  14884. (while t
  14885. (when (setq tmp (org-entry-get nil property))
  14886. (org-back-to-heading t)
  14887. (move-marker org-entry-property-inherited-from (point))
  14888. (throw 'ex tmp))
  14889. (or (org-up-heading-safe) (throw 'ex nil)))))
  14890. (or tmp (cdr (assoc property org-local-properties))
  14891. (cdr (assoc property org-global-properties))))))
  14892. (defun org-entry-put (pom property value)
  14893. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14894. (org-with-point-at pom
  14895. (org-back-to-heading t)
  14896. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14897. range)
  14898. (cond
  14899. ((equal property "TODO")
  14900. (when (and (stringp value) (string-match "\\S-" value)
  14901. (not (member value org-todo-keywords-1)))
  14902. (error "\"%s\" is not a valid TODO state" value))
  14903. (if (or (not value)
  14904. (not (string-match "\\S-" value)))
  14905. (setq value 'none))
  14906. (org-todo value)
  14907. (org-set-tags nil 'align))
  14908. ((equal property "PRIORITY")
  14909. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14910. (string-to-char value) ?\ ))
  14911. (org-set-tags nil 'align))
  14912. ((equal property "SCHEDULED")
  14913. (if (re-search-forward org-scheduled-time-regexp end t)
  14914. (cond
  14915. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14916. ((eq value 'later) (org-timestamp-change 1 'day))
  14917. (t (call-interactively 'org-schedule)))
  14918. (call-interactively 'org-schedule)))
  14919. ((equal property "DEADLINE")
  14920. (if (re-search-forward org-deadline-time-regexp end t)
  14921. (cond
  14922. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14923. ((eq value 'later) (org-timestamp-change 1 'day))
  14924. (t (call-interactively 'org-deadline)))
  14925. (call-interactively 'org-deadline)))
  14926. ((member property org-special-properties)
  14927. (error "The %s property can not yet be set with `org-entry-put'"
  14928. property))
  14929. (t ; a non-special property
  14930. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14931. (setq range (org-get-property-block beg end 'force))
  14932. (goto-char (car range))
  14933. (if (re-search-forward
  14934. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14935. (progn
  14936. (delete-region (match-beginning 1) (match-end 1))
  14937. (goto-char (match-beginning 1)))
  14938. (goto-char (cdr range))
  14939. (insert "\n")
  14940. (backward-char 1)
  14941. (org-indent-line-function)
  14942. (insert ":" property ":"))
  14943. (and value (insert " " value))
  14944. (org-indent-line-function)))))))
  14945. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14946. "Get all property keys in the current buffer.
  14947. With INCLUDE-SPECIALS, also list the special properties that relect things
  14948. like tags and TODO state.
  14949. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14950. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14951. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14952. formats in the current buffer."
  14953. (let (rtn range cfmt cols s p)
  14954. (save-excursion
  14955. (save-restriction
  14956. (widen)
  14957. (goto-char (point-min))
  14958. (while (re-search-forward org-property-start-re nil t)
  14959. (setq range (org-get-property-block))
  14960. (goto-char (car range))
  14961. (while (re-search-forward
  14962. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14963. (cdr range) t)
  14964. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14965. (outline-next-heading))))
  14966. (when include-specials
  14967. (setq rtn (append org-special-properties rtn)))
  14968. (when include-defaults
  14969. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14970. (when include-columns
  14971. (save-excursion
  14972. (save-restriction
  14973. (widen)
  14974. (goto-char (point-min))
  14975. (while (re-search-forward
  14976. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14977. nil t)
  14978. (setq cfmt (match-string 2) s 0)
  14979. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14980. cfmt s)
  14981. (setq s (match-end 0)
  14982. p (match-string 1 cfmt))
  14983. (unless (or (equal p "ITEM")
  14984. (member p org-special-properties))
  14985. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14986. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14987. (defun org-property-values (key)
  14988. "Return a list of all values of property KEY."
  14989. (save-excursion
  14990. (save-restriction
  14991. (widen)
  14992. (goto-char (point-min))
  14993. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14994. values)
  14995. (while (re-search-forward re nil t)
  14996. (add-to-list 'values (org-trim (match-string 1))))
  14997. (delete "" values)))))
  14998. (defun org-insert-property-drawer ()
  14999. "Insert a property drawer into the current entry."
  15000. (interactive)
  15001. (org-back-to-heading t)
  15002. (looking-at outline-regexp)
  15003. (let ((indent (- (match-end 0)(match-beginning 0)))
  15004. (beg (point))
  15005. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15006. end hiddenp)
  15007. (outline-next-heading)
  15008. (setq end (point))
  15009. (goto-char beg)
  15010. (while (re-search-forward re end t))
  15011. (setq hiddenp (org-invisible-p))
  15012. (end-of-line 1)
  15013. (and (equal (char-after) ?\n) (forward-char 1))
  15014. (org-skip-over-state-notes)
  15015. (skip-chars-backward " \t\n\r")
  15016. (if (eq (char-before) ?*) (forward-char 1))
  15017. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15018. (beginning-of-line 0)
  15019. (indent-to-column indent)
  15020. (beginning-of-line 2)
  15021. (indent-to-column indent)
  15022. (beginning-of-line 0)
  15023. (if hiddenp
  15024. (save-excursion
  15025. (org-back-to-heading t)
  15026. (hide-entry))
  15027. (org-flag-drawer t))))
  15028. (defun org-set-property (property value)
  15029. "In the current entry, set PROPERTY to VALUE.
  15030. When called interactively, this will prompt for a property name, offering
  15031. completion on existing and default properties. And then it will prompt
  15032. for a value, offering competion either on allowed values (via an inherited
  15033. xxx_ALL property) or on existing values in other instances of this property
  15034. in the current file."
  15035. (interactive
  15036. (let* ((prop (completing-read
  15037. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15038. (cur (org-entry-get nil prop))
  15039. (allowed (org-property-get-allowed-values nil prop 'table))
  15040. (existing (mapcar 'list (org-property-values prop)))
  15041. (val (if allowed
  15042. (completing-read "Value: " allowed nil 'req-match)
  15043. (completing-read
  15044. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15045. (concat "[" cur "]") "")
  15046. ": ")
  15047. existing nil nil "" nil cur))))
  15048. (list prop (if (equal val "") cur val))))
  15049. (unless (equal (org-entry-get nil property) value)
  15050. (org-entry-put nil property value)))
  15051. (defun org-delete-property (property)
  15052. "In the current entry, delete PROPERTY."
  15053. (interactive
  15054. (let* ((prop (completing-read
  15055. "Property: " (org-entry-properties nil 'standard))))
  15056. (list prop)))
  15057. (message "Property %s %s" property
  15058. (if (org-entry-delete nil property)
  15059. "deleted"
  15060. "was not present in the entry")))
  15061. (defun org-delete-property-globally (property)
  15062. "Remove PROPERTY globally, from all entries."
  15063. (interactive
  15064. (let* ((prop (completing-read
  15065. "Globally remove property: "
  15066. (mapcar 'list (org-buffer-property-keys)))))
  15067. (list prop)))
  15068. (save-excursion
  15069. (save-restriction
  15070. (widen)
  15071. (goto-char (point-min))
  15072. (let ((cnt 0))
  15073. (while (re-search-forward
  15074. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15075. nil t)
  15076. (setq cnt (1+ cnt))
  15077. (replace-match ""))
  15078. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15079. (defvar org-columns-current-fmt-compiled) ; defined below
  15080. (defun org-compute-property-at-point ()
  15081. "Compute the property at point.
  15082. This looks for an enclosing column format, extracts the operator and
  15083. then applies it to the proerty in the column format's scope."
  15084. (interactive)
  15085. (unless (org-at-property-p)
  15086. (error "Not at a property"))
  15087. (let ((prop (org-match-string-no-properties 2)))
  15088. (org-columns-get-format-and-top-level)
  15089. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15090. (error "No operator defined for property %s" prop))
  15091. (org-columns-compute prop)))
  15092. (defun org-property-get-allowed-values (pom property &optional table)
  15093. "Get allowed values for the property PROPERTY.
  15094. When TABLE is non-nil, return an alist that can directly be used for
  15095. completion."
  15096. (let (vals)
  15097. (cond
  15098. ((equal property "TODO")
  15099. (setq vals (org-with-point-at pom
  15100. (append org-todo-keywords-1 '("")))))
  15101. ((equal property "PRIORITY")
  15102. (let ((n org-lowest-priority))
  15103. (while (>= n org-highest-priority)
  15104. (push (char-to-string n) vals)
  15105. (setq n (1- n)))))
  15106. ((member property org-special-properties))
  15107. (t
  15108. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15109. (when (and vals (string-match "\\S-" vals))
  15110. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15111. (setq vals (mapcar (lambda (x)
  15112. (cond ((stringp x) x)
  15113. ((numberp x) (number-to-string x))
  15114. ((symbolp x) (symbol-name x))
  15115. (t "???")))
  15116. vals)))))
  15117. (if table (mapcar 'list vals) vals)))
  15118. (defun org-property-previous-allowed-value (&optional previous)
  15119. "Switch to the next allowed value for this property."
  15120. (interactive)
  15121. (org-property-next-allowed-value t))
  15122. (defun org-property-next-allowed-value (&optional previous)
  15123. "Switch to the next allowed value for this property."
  15124. (interactive)
  15125. (unless (org-at-property-p)
  15126. (error "Not at a property"))
  15127. (let* ((key (match-string 2))
  15128. (value (match-string 3))
  15129. (allowed (or (org-property-get-allowed-values (point) key)
  15130. (and (member value '("[ ]" "[-]" "[X]"))
  15131. '("[ ]" "[X]"))))
  15132. nval)
  15133. (unless allowed
  15134. (error "Allowed values for this property have not been defined"))
  15135. (if previous (setq allowed (reverse allowed)))
  15136. (if (member value allowed)
  15137. (setq nval (car (cdr (member value allowed)))))
  15138. (setq nval (or nval (car allowed)))
  15139. (if (equal nval value)
  15140. (error "Only one allowed value for this property"))
  15141. (org-at-property-p)
  15142. (replace-match (concat " :" key ": " nval) t t)
  15143. (org-indent-line-function)
  15144. (beginning-of-line 1)
  15145. (skip-chars-forward " \t")))
  15146. (defun org-find-entry-with-id (ident)
  15147. "Locate the entry that contains the ID property with exact value IDENT.
  15148. IDENT can be a string, a symbol or a number, this function will search for
  15149. the string representation of it.
  15150. Return the position where this entry starts, or nil if there is no such entry."
  15151. (let ((id (cond
  15152. ((stringp ident) ident)
  15153. ((symbol-name ident) (symbol-name ident))
  15154. ((numberp ident) (number-to-string ident))
  15155. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15156. (case-fold-search nil))
  15157. (save-excursion
  15158. (save-restriction
  15159. (widen)
  15160. (goto-char (point-min))
  15161. (when (re-search-forward
  15162. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15163. nil t)
  15164. (org-back-to-heading)
  15165. (point))))))
  15166. ;;; Column View
  15167. (defvar org-columns-overlays nil
  15168. "Holds the list of current column overlays.")
  15169. (defvar org-columns-current-fmt nil
  15170. "Local variable, holds the currently active column format.")
  15171. (defvar org-columns-current-fmt-compiled nil
  15172. "Local variable, holds the currently active column format.
  15173. This is the compiled version of the format.")
  15174. (defvar org-columns-current-widths nil
  15175. "Loval variable, holds the currently widths of fields.")
  15176. (defvar org-columns-current-maxwidths nil
  15177. "Loval variable, holds the currently active maximum column widths.")
  15178. (defvar org-columns-begin-marker (make-marker)
  15179. "Points to the position where last a column creation command was called.")
  15180. (defvar org-columns-top-level-marker (make-marker)
  15181. "Points to the position where current columns region starts.")
  15182. (defvar org-columns-map (make-sparse-keymap)
  15183. "The keymap valid in column display.")
  15184. (defun org-columns-content ()
  15185. "Switch to contents view while in columns view."
  15186. (interactive)
  15187. (org-overview)
  15188. (org-content))
  15189. (org-defkey org-columns-map "c" 'org-columns-content)
  15190. (org-defkey org-columns-map "o" 'org-overview)
  15191. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15192. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15193. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15194. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15195. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15196. (org-defkey org-columns-map "q" 'org-columns-quit)
  15197. (org-defkey org-columns-map "r" 'org-columns-redo)
  15198. (org-defkey org-columns-map "g" 'org-columns-redo)
  15199. (org-defkey org-columns-map [left] 'backward-char)
  15200. (org-defkey org-columns-map "\M-b" 'backward-char)
  15201. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15202. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15203. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15204. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15205. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15206. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15207. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15208. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15209. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15210. (org-defkey org-columns-map ">" 'org-columns-widen)
  15211. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15212. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15213. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15214. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15215. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15216. '("Column"
  15217. ["Edit property" org-columns-edit-value t]
  15218. ["Next allowed value" org-columns-next-allowed-value t]
  15219. ["Previous allowed value" org-columns-previous-allowed-value t]
  15220. ["Show full value" org-columns-show-value t]
  15221. ["Edit allowed values" org-columns-edit-allowed t]
  15222. "--"
  15223. ["Edit column attributes" org-columns-edit-attributes t]
  15224. ["Increase column width" org-columns-widen t]
  15225. ["Decrease column width" org-columns-narrow t]
  15226. "--"
  15227. ["Move column right" org-columns-move-right t]
  15228. ["Move column left" org-columns-move-left t]
  15229. ["Add column" org-columns-new t]
  15230. ["Delete column" org-columns-delete t]
  15231. "--"
  15232. ["CONTENTS" org-columns-content t]
  15233. ["OVERVIEW" org-overview t]
  15234. ["Refresh columns display" org-columns-redo t]
  15235. "--"
  15236. ["Open link" org-columns-open-link t]
  15237. "--"
  15238. ["Quit" org-columns-quit t]))
  15239. (defun org-columns-new-overlay (beg end &optional string face)
  15240. "Create a new column overlay and add it to the list."
  15241. (let ((ov (org-make-overlay beg end)))
  15242. (org-overlay-put ov 'face (or face 'secondary-selection))
  15243. (org-overlay-display ov string face)
  15244. (push ov org-columns-overlays)
  15245. ov))
  15246. (defun org-columns-display-here (&optional props)
  15247. "Overlay the current line with column display."
  15248. (interactive)
  15249. (let* ((fmt org-columns-current-fmt-compiled)
  15250. (beg (point-at-bol))
  15251. (level-face (save-excursion
  15252. (beginning-of-line 1)
  15253. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15254. (org-get-level-face 2))))
  15255. (color (list :foreground
  15256. (face-attribute (or level-face 'default) :foreground)))
  15257. props pom property ass width f string ov column val modval)
  15258. ;; Check if the entry is in another buffer.
  15259. (unless props
  15260. (if (eq major-mode 'org-agenda-mode)
  15261. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15262. (get-text-property (point) 'org-marker))
  15263. props (if pom (org-entry-properties pom) nil))
  15264. (setq props (org-entry-properties nil))))
  15265. ;; Walk the format
  15266. (while (setq column (pop fmt))
  15267. (setq property (car column)
  15268. ass (if (equal property "ITEM")
  15269. (cons "ITEM"
  15270. (save-match-data
  15271. (org-no-properties
  15272. (org-remove-tabs
  15273. (buffer-substring-no-properties
  15274. (point-at-bol) (point-at-eol))))))
  15275. (assoc property props))
  15276. width (or (cdr (assoc property org-columns-current-maxwidths))
  15277. (nth 2 column)
  15278. (length property))
  15279. f (format "%%-%d.%ds | " width width)
  15280. val (or (cdr ass) "")
  15281. modval (if (equal property "ITEM")
  15282. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15283. string (format f (or modval val)))
  15284. ;; Create the overlay
  15285. (org-unmodified
  15286. (setq ov (org-columns-new-overlay
  15287. beg (setq beg (1+ beg)) string
  15288. (list color 'org-column)))
  15289. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15290. (org-overlay-put ov 'keymap org-columns-map)
  15291. (org-overlay-put ov 'org-columns-key property)
  15292. (org-overlay-put ov 'org-columns-value (cdr ass))
  15293. (org-overlay-put ov 'org-columns-value-modified modval)
  15294. (org-overlay-put ov 'org-columns-pom pom)
  15295. (org-overlay-put ov 'org-columns-format f))
  15296. (if (or (not (char-after beg))
  15297. (equal (char-after beg) ?\n))
  15298. (let ((inhibit-read-only t))
  15299. (save-excursion
  15300. (goto-char beg)
  15301. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15302. ;; Make the rest of the line disappear.
  15303. (org-unmodified
  15304. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15305. (org-overlay-put ov 'invisible t)
  15306. (org-overlay-put ov 'keymap org-columns-map)
  15307. (org-overlay-put ov 'intangible t)
  15308. (push ov org-columns-overlays)
  15309. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15310. (org-overlay-put ov 'keymap org-columns-map)
  15311. (push ov org-columns-overlays)
  15312. (let ((inhibit-read-only t))
  15313. (put-text-property (max (point-min) (1- (point-at-bol)))
  15314. (min (point-max) (1+ (point-at-eol)))
  15315. 'read-only "Type `e' to edit property")))))
  15316. (defvar org-previous-header-line-format nil
  15317. "The header line format before column view was turned on.")
  15318. (defvar org-columns-inhibit-recalculation nil
  15319. "Inhibit recomputing of columns on column view startup.")
  15320. (defvar header-line-format)
  15321. (defun org-columns-display-here-title ()
  15322. "Overlay the newline before the current line with the table title."
  15323. (interactive)
  15324. (let ((fmt org-columns-current-fmt-compiled)
  15325. string (title "")
  15326. property width f column str widths)
  15327. (while (setq column (pop fmt))
  15328. (setq property (car column)
  15329. str (or (nth 1 column) property)
  15330. width (or (cdr (assoc property org-columns-current-maxwidths))
  15331. (nth 2 column)
  15332. (length str))
  15333. widths (push width widths)
  15334. f (format "%%-%d.%ds | " width width)
  15335. string (format f str)
  15336. title (concat title string)))
  15337. (setq title (concat
  15338. (org-add-props " " nil 'display '(space :align-to 0))
  15339. (org-add-props title nil 'face '(:weight bold :underline t))))
  15340. (org-set-local 'org-previous-header-line-format header-line-format)
  15341. (org-set-local 'org-columns-current-widths (nreverse widths))
  15342. (setq header-line-format title)))
  15343. (defun org-columns-remove-overlays ()
  15344. "Remove all currently active column overlays."
  15345. (interactive)
  15346. (when (marker-buffer org-columns-begin-marker)
  15347. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15348. (when (local-variable-p 'org-previous-header-line-format)
  15349. (setq header-line-format org-previous-header-line-format)
  15350. (kill-local-variable 'org-previous-header-line-format))
  15351. (move-marker org-columns-begin-marker nil)
  15352. (move-marker org-columns-top-level-marker nil)
  15353. (org-unmodified
  15354. (mapc 'org-delete-overlay org-columns-overlays)
  15355. (setq org-columns-overlays nil)
  15356. (let ((inhibit-read-only t))
  15357. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15358. (defun org-columns-cleanup-item (item fmt)
  15359. "Remove from ITEM what is a column in the format FMT."
  15360. (if (not org-complex-heading-regexp)
  15361. item
  15362. (when (string-match org-complex-heading-regexp item)
  15363. (concat
  15364. (org-add-props (concat (match-string 1 item) " ") nil
  15365. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15366. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15367. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15368. " " (match-string 4 item)
  15369. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15370. (defun org-columns-show-value ()
  15371. "Show the full value of the property."
  15372. (interactive)
  15373. (let ((value (get-char-property (point) 'org-columns-value)))
  15374. (message "Value is: %s" (or value ""))))
  15375. (defun org-columns-quit ()
  15376. "Remove the column overlays and in this way exit column editing."
  15377. (interactive)
  15378. (org-unmodified
  15379. (org-columns-remove-overlays)
  15380. (let ((inhibit-read-only t))
  15381. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15382. (when (eq major-mode 'org-agenda-mode)
  15383. (message
  15384. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15385. (defun org-columns-check-computed ()
  15386. "Check if this column value is computed.
  15387. If yes, throw an error indicating that changing it does not make sense."
  15388. (let ((val (get-char-property (point) 'org-columns-value)))
  15389. (when (and (stringp val)
  15390. (get-char-property 0 'org-computed val))
  15391. (error "This value is computed from the entry's children"))))
  15392. (defun org-columns-todo (&optional arg)
  15393. "Change the TODO state during column view."
  15394. (interactive "P")
  15395. (org-columns-edit-value "TODO"))
  15396. (defun org-columns-set-tags-or-toggle (&optional arg)
  15397. "Toggle checkbox at point, or set tags for current headline."
  15398. (interactive "P")
  15399. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15400. (get-char-property (point) 'org-columns-value))
  15401. (org-columns-next-allowed-value)
  15402. (org-columns-edit-value "TAGS")))
  15403. (defun org-columns-edit-value (&optional key)
  15404. "Edit the value of the property at point in column view.
  15405. Where possible, use the standard interface for changing this line."
  15406. (interactive)
  15407. (org-columns-check-computed)
  15408. (let* ((external-key key)
  15409. (col (current-column))
  15410. (key (or key (get-char-property (point) 'org-columns-key)))
  15411. (value (get-char-property (point) 'org-columns-value))
  15412. (bol (point-at-bol)) (eol (point-at-eol))
  15413. (pom (or (get-text-property bol 'org-hd-marker)
  15414. (point))) ; keep despite of compiler waring
  15415. (line-overlays
  15416. (delq nil (mapcar (lambda (x)
  15417. (and (eq (overlay-buffer x) (current-buffer))
  15418. (>= (overlay-start x) bol)
  15419. (<= (overlay-start x) eol)
  15420. x))
  15421. org-columns-overlays)))
  15422. nval eval allowed)
  15423. (cond
  15424. ((equal key "CLOCKSUM")
  15425. (error "This special column cannot be edited"))
  15426. ((equal key "ITEM")
  15427. (setq eval '(org-with-point-at pom
  15428. (org-edit-headline))))
  15429. ((equal key "TODO")
  15430. (setq eval '(org-with-point-at pom
  15431. (let ((current-prefix-arg
  15432. (if external-key current-prefix-arg '(4))))
  15433. (call-interactively 'org-todo)))))
  15434. ((equal key "PRIORITY")
  15435. (setq eval '(org-with-point-at pom
  15436. (call-interactively 'org-priority))))
  15437. ((equal key "TAGS")
  15438. (setq eval '(org-with-point-at pom
  15439. (let ((org-fast-tag-selection-single-key
  15440. (if (eq org-fast-tag-selection-single-key 'expert)
  15441. t org-fast-tag-selection-single-key)))
  15442. (call-interactively 'org-set-tags)))))
  15443. ((equal key "DEADLINE")
  15444. (setq eval '(org-with-point-at pom
  15445. (call-interactively 'org-deadline))))
  15446. ((equal key "SCHEDULED")
  15447. (setq eval '(org-with-point-at pom
  15448. (call-interactively 'org-schedule))))
  15449. (t
  15450. (setq allowed (org-property-get-allowed-values pom key 'table))
  15451. (if allowed
  15452. (setq nval (completing-read "Value: " allowed nil t))
  15453. (setq nval (read-string "Edit: " value)))
  15454. (setq nval (org-trim nval))
  15455. (when (not (equal nval value))
  15456. (setq eval '(org-entry-put pom key nval)))))
  15457. (when eval
  15458. (let ((inhibit-read-only t))
  15459. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15460. (unwind-protect
  15461. (progn
  15462. (setq org-columns-overlays
  15463. (org-delete-all line-overlays org-columns-overlays))
  15464. (mapc 'org-delete-overlay line-overlays)
  15465. (org-columns-eval eval))
  15466. (org-columns-display-here))))
  15467. (move-to-column col)
  15468. (if (and (org-mode-p)
  15469. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15470. (org-columns-update key))))
  15471. (defun org-edit-headline () ; FIXME: this is not columns specific
  15472. "Edit the current headline, the part without TODO keyword, TAGS."
  15473. (org-back-to-heading)
  15474. (when (looking-at org-todo-line-regexp)
  15475. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15476. (txt (match-string 3))
  15477. (post "")
  15478. txt2)
  15479. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15480. (setq post (match-string 0 txt)
  15481. txt (substring txt 0 (match-beginning 0))))
  15482. (setq txt2 (read-string "Edit: " txt))
  15483. (when (not (equal txt txt2))
  15484. (beginning-of-line 1)
  15485. (insert pre txt2 post)
  15486. (delete-region (point) (point-at-eol))
  15487. (org-set-tags nil t)))))
  15488. (defun org-columns-edit-allowed ()
  15489. "Edit the list of allowed values for the current property."
  15490. (interactive)
  15491. (let* ((key (get-char-property (point) 'org-columns-key))
  15492. (key1 (concat key "_ALL"))
  15493. (allowed (org-entry-get (point) key1 t))
  15494. nval)
  15495. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15496. (setq nval (read-string "Allowed: " allowed))
  15497. (org-entry-put
  15498. (cond ((marker-position org-entry-property-inherited-from)
  15499. org-entry-property-inherited-from)
  15500. ((marker-position org-columns-top-level-marker)
  15501. org-columns-top-level-marker))
  15502. key1 nval)))
  15503. (defmacro org-no-warnings (&rest body)
  15504. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15505. (defun org-columns-eval (form)
  15506. (let (hidep)
  15507. (save-excursion
  15508. (beginning-of-line 1)
  15509. ;; `next-line' is needed here, because it skips invisible line.
  15510. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15511. (setq hidep (org-on-heading-p 1)))
  15512. (eval form)
  15513. (and hidep (hide-entry))))
  15514. (defun org-columns-previous-allowed-value ()
  15515. "Switch to the previous allowed value for this column."
  15516. (interactive)
  15517. (org-columns-next-allowed-value t))
  15518. (defun org-columns-next-allowed-value (&optional previous)
  15519. "Switch to the next allowed value for this column."
  15520. (interactive)
  15521. (org-columns-check-computed)
  15522. (let* ((col (current-column))
  15523. (key (get-char-property (point) 'org-columns-key))
  15524. (value (get-char-property (point) 'org-columns-value))
  15525. (bol (point-at-bol)) (eol (point-at-eol))
  15526. (pom (or (get-text-property bol 'org-hd-marker)
  15527. (point))) ; keep despite of compiler waring
  15528. (line-overlays
  15529. (delq nil (mapcar (lambda (x)
  15530. (and (eq (overlay-buffer x) (current-buffer))
  15531. (>= (overlay-start x) bol)
  15532. (<= (overlay-start x) eol)
  15533. x))
  15534. org-columns-overlays)))
  15535. (allowed (or (org-property-get-allowed-values pom key)
  15536. (and (memq
  15537. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15538. '(checkbox checkbox-n-of-m checkbox-percent))
  15539. '("[ ]" "[X]"))))
  15540. nval)
  15541. (when (equal key "ITEM")
  15542. (error "Cannot edit item headline from here"))
  15543. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15544. (error "Allowed values for this property have not been defined"))
  15545. (if (member key '("SCHEDULED" "DEADLINE"))
  15546. (setq nval (if previous 'earlier 'later))
  15547. (if previous (setq allowed (reverse allowed)))
  15548. (if (member value allowed)
  15549. (setq nval (car (cdr (member value allowed)))))
  15550. (setq nval (or nval (car allowed)))
  15551. (if (equal nval value)
  15552. (error "Only one allowed value for this property")))
  15553. (let ((inhibit-read-only t))
  15554. (remove-text-properties (1- bol) eol '(read-only t))
  15555. (unwind-protect
  15556. (progn
  15557. (setq org-columns-overlays
  15558. (org-delete-all line-overlays org-columns-overlays))
  15559. (mapc 'org-delete-overlay line-overlays)
  15560. (org-columns-eval '(org-entry-put pom key nval)))
  15561. (org-columns-display-here)))
  15562. (move-to-column col)
  15563. (if (and (org-mode-p)
  15564. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15565. (org-columns-update key))))
  15566. (defun org-verify-version (task)
  15567. (cond
  15568. ((eq task 'columns)
  15569. (if (or (featurep 'xemacs)
  15570. (< emacs-major-version 22))
  15571. (error "Emacs 22 is required for the columns feature")))))
  15572. (defun org-columns-open-link (&optional arg)
  15573. (interactive "P")
  15574. (let ((value (get-char-property (point) 'org-columns-value)))
  15575. (org-open-link-from-string value arg)))
  15576. (defun org-open-link-from-string (s &optional arg)
  15577. "Open a link in the string S, as if it was in Org-mode."
  15578. (interactive)
  15579. (with-temp-buffer
  15580. (let ((org-inhibit-startup t))
  15581. (org-mode)
  15582. (insert s)
  15583. (goto-char (point-min))
  15584. (org-open-at-point arg))))
  15585. (defun org-columns-get-format-and-top-level ()
  15586. (let (fmt)
  15587. (when (condition-case nil (org-back-to-heading) (error nil))
  15588. (move-marker org-entry-property-inherited-from nil)
  15589. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15590. (setq fmt (or fmt org-columns-default-format))
  15591. (org-set-local 'org-columns-current-fmt fmt)
  15592. (org-columns-compile-format fmt)
  15593. (if (marker-position org-entry-property-inherited-from)
  15594. (move-marker org-columns-top-level-marker
  15595. org-entry-property-inherited-from)
  15596. (move-marker org-columns-top-level-marker (point)))
  15597. fmt))
  15598. (defun org-columns ()
  15599. "Turn on column view on an org-mode file."
  15600. (interactive)
  15601. (org-verify-version 'columns)
  15602. (org-columns-remove-overlays)
  15603. (move-marker org-columns-begin-marker (point))
  15604. (let (beg end fmt cache maxwidths)
  15605. (setq fmt (org-columns-get-format-and-top-level))
  15606. (save-excursion
  15607. (goto-char org-columns-top-level-marker)
  15608. (setq beg (point))
  15609. (unless org-columns-inhibit-recalculation
  15610. (org-columns-compute-all))
  15611. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15612. (point-max)))
  15613. ;; Get and cache the properties
  15614. (goto-char beg)
  15615. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15616. (save-excursion
  15617. (save-restriction
  15618. (narrow-to-region beg end)
  15619. (org-clock-sum))))
  15620. (while (re-search-forward (concat "^" outline-regexp) end t)
  15621. (push (cons (org-current-line) (org-entry-properties)) cache))
  15622. (when cache
  15623. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15624. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15625. (org-columns-display-here-title)
  15626. (mapc (lambda (x)
  15627. (goto-line (car x))
  15628. (org-columns-display-here (cdr x)))
  15629. cache)))))
  15630. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15631. "Insert a new column, to the leeft o the current column."
  15632. (interactive)
  15633. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15634. cell)
  15635. (setq prop (completing-read
  15636. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15637. nil nil prop))
  15638. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15639. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15640. (if (string-match "\\S-" width)
  15641. (setq width (string-to-number width))
  15642. (setq width nil))
  15643. (setq fmt (completing-read "Summary [none]: "
  15644. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15645. nil t))
  15646. (if (string-match "\\S-" fmt)
  15647. (setq fmt (intern fmt))
  15648. (setq fmt nil))
  15649. (if (eq fmt 'none) (setq fmt nil))
  15650. (if editp
  15651. (progn
  15652. (setcar editp prop)
  15653. (setcdr editp (list title width nil fmt)))
  15654. (setq cell (nthcdr (1- (current-column))
  15655. org-columns-current-fmt-compiled))
  15656. (setcdr cell (cons (list prop title width nil fmt)
  15657. (cdr cell))))
  15658. (org-columns-store-format)
  15659. (org-columns-redo)))
  15660. (defun org-columns-delete ()
  15661. "Delete the column at point from columns view."
  15662. (interactive)
  15663. (let* ((n (current-column))
  15664. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15665. (when (y-or-n-p
  15666. (format "Are you sure you want to remove column \"%s\"? " title))
  15667. (setq org-columns-current-fmt-compiled
  15668. (delq (nth n org-columns-current-fmt-compiled)
  15669. org-columns-current-fmt-compiled))
  15670. (org-columns-store-format)
  15671. (org-columns-redo)
  15672. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15673. (backward-char 1)))))
  15674. (defun org-columns-edit-attributes ()
  15675. "Edit the attributes of the current column."
  15676. (interactive)
  15677. (let* ((n (current-column))
  15678. (info (nth n org-columns-current-fmt-compiled)))
  15679. (apply 'org-columns-new info)))
  15680. (defun org-columns-widen (arg)
  15681. "Make the column wider by ARG characters."
  15682. (interactive "p")
  15683. (let* ((n (current-column))
  15684. (entry (nth n org-columns-current-fmt-compiled))
  15685. (width (or (nth 2 entry)
  15686. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15687. (setq width (max 1 (+ width arg)))
  15688. (setcar (nthcdr 2 entry) width)
  15689. (org-columns-store-format)
  15690. (org-columns-redo)))
  15691. (defun org-columns-narrow (arg)
  15692. "Make the column nrrower by ARG characters."
  15693. (interactive "p")
  15694. (org-columns-widen (- arg)))
  15695. (defun org-columns-move-right ()
  15696. "Swap this column with the one to the right."
  15697. (interactive)
  15698. (let* ((n (current-column))
  15699. (cell (nthcdr n org-columns-current-fmt-compiled))
  15700. e)
  15701. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15702. (error "Cannot shift this column further to the right"))
  15703. (setq e (car cell))
  15704. (setcar cell (car (cdr cell)))
  15705. (setcdr cell (cons e (cdr (cdr cell))))
  15706. (org-columns-store-format)
  15707. (org-columns-redo)
  15708. (forward-char 1)))
  15709. (defun org-columns-move-left ()
  15710. "Swap this column with the one to the left."
  15711. (interactive)
  15712. (let* ((n (current-column)))
  15713. (when (= n 0)
  15714. (error "Cannot shift this column further to the left"))
  15715. (backward-char 1)
  15716. (org-columns-move-right)
  15717. (backward-char 1)))
  15718. (defun org-columns-store-format ()
  15719. "Store the text version of the current columns format in appropriate place.
  15720. This is either in the COLUMNS property of the node starting the current column
  15721. display, or in the #+COLUMNS line of the current buffer."
  15722. (let (fmt (cnt 0))
  15723. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15724. (org-set-local 'org-columns-current-fmt fmt)
  15725. (if (marker-position org-columns-top-level-marker)
  15726. (save-excursion
  15727. (goto-char org-columns-top-level-marker)
  15728. (if (and (org-at-heading-p)
  15729. (org-entry-get nil "COLUMNS"))
  15730. (org-entry-put nil "COLUMNS" fmt)
  15731. (goto-char (point-min))
  15732. ;; Overwrite all #+COLUMNS lines....
  15733. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15734. (setq cnt (1+ cnt))
  15735. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15736. (unless (> cnt 0)
  15737. (goto-char (point-min))
  15738. (or (org-on-heading-p t) (outline-next-heading))
  15739. (let ((inhibit-read-only t))
  15740. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15741. (org-set-local 'org-columns-default-format fmt))))))
  15742. (defvar org-overriding-columns-format nil
  15743. "When set, overrides any other definition.")
  15744. (defvar org-agenda-view-columns-initially nil
  15745. "When set, switch to columns view immediately after creating the agenda.")
  15746. (defun org-agenda-columns ()
  15747. "Turn on column view in the agenda."
  15748. (interactive)
  15749. (org-verify-version 'columns)
  15750. (org-columns-remove-overlays)
  15751. (move-marker org-columns-begin-marker (point))
  15752. (let (fmt cache maxwidths m)
  15753. (cond
  15754. ((and (local-variable-p 'org-overriding-columns-format)
  15755. org-overriding-columns-format)
  15756. (setq fmt org-overriding-columns-format))
  15757. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15758. (setq fmt (org-entry-get m "COLUMNS" t)))
  15759. ((and (boundp 'org-columns-current-fmt)
  15760. (local-variable-p 'org-columns-current-fmt)
  15761. org-columns-current-fmt)
  15762. (setq fmt org-columns-current-fmt))
  15763. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15764. (setq m (get-text-property m 'org-hd-marker))
  15765. (setq fmt (org-entry-get m "COLUMNS" t))))
  15766. (setq fmt (or fmt org-columns-default-format))
  15767. (org-set-local 'org-columns-current-fmt fmt)
  15768. (org-columns-compile-format fmt)
  15769. (save-excursion
  15770. ;; Get and cache the properties
  15771. (goto-char (point-min))
  15772. (while (not (eobp))
  15773. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15774. (get-text-property (point) 'org-marker)))
  15775. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15776. (beginning-of-line 2))
  15777. (when cache
  15778. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15779. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15780. (org-columns-display-here-title)
  15781. (mapc (lambda (x)
  15782. (goto-line (car x))
  15783. (org-columns-display-here (cdr x)))
  15784. cache)))))
  15785. (defun org-columns-get-autowidth-alist (s cache)
  15786. "Derive the maximum column widths from the format and the cache."
  15787. (let ((start 0) rtn)
  15788. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15789. (push (cons (match-string 1 s) 1) rtn)
  15790. (setq start (match-end 0)))
  15791. (mapc (lambda (x)
  15792. (setcdr x (apply 'max
  15793. (mapcar
  15794. (lambda (y)
  15795. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15796. cache))))
  15797. rtn)
  15798. rtn))
  15799. (defun org-columns-compute-all ()
  15800. "Compute all columns that have operators defined."
  15801. (org-unmodified
  15802. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15803. (let ((columns org-columns-current-fmt-compiled) col)
  15804. (while (setq col (pop columns))
  15805. (when (nth 3 col)
  15806. (save-excursion
  15807. (org-columns-compute (car col)))))))
  15808. (defun org-columns-update (property)
  15809. "Recompute PROPERTY, and update the columns display for it."
  15810. (org-columns-compute property)
  15811. (let (fmt val pos)
  15812. (save-excursion
  15813. (mapc (lambda (ov)
  15814. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15815. (setq pos (org-overlay-start ov))
  15816. (goto-char pos)
  15817. (when (setq val (cdr (assoc property
  15818. (get-text-property
  15819. (point-at-bol) 'org-summaries))))
  15820. (setq fmt (org-overlay-get ov 'org-columns-format))
  15821. (org-overlay-put ov 'org-columns-value val)
  15822. (org-overlay-put ov 'display (format fmt val)))))
  15823. org-columns-overlays))))
  15824. (defun org-columns-compute (property)
  15825. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15826. (interactive)
  15827. (let* ((re (concat "^" outline-regexp))
  15828. (lmax 30) ; Does anyone use deeper levels???
  15829. (lsum (make-vector lmax 0))
  15830. (lflag (make-vector lmax nil))
  15831. (level 0)
  15832. (ass (assoc property org-columns-current-fmt-compiled))
  15833. (format (nth 4 ass))
  15834. (printf (nth 5 ass))
  15835. (beg org-columns-top-level-marker)
  15836. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15837. (save-excursion
  15838. ;; Find the region to compute
  15839. (goto-char beg)
  15840. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15841. (goto-char end)
  15842. ;; Walk the tree from the back and do the computations
  15843. (while (re-search-backward re beg t)
  15844. (setq sumpos (match-beginning 0)
  15845. last-level level
  15846. level (org-outline-level)
  15847. val (org-entry-get nil property)
  15848. valflag (and val (string-match "\\S-" val)))
  15849. (cond
  15850. ((< level last-level)
  15851. ;; put the sum of lower levels here as a property
  15852. (setq sum (aref lsum last-level) ; current sum
  15853. flag (aref lflag last-level) ; any valid entries from children?
  15854. str (org-column-number-to-string sum format printf)
  15855. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15856. useval (if flag str1 (if valflag val ""))
  15857. sum-alist (get-text-property sumpos 'org-summaries))
  15858. (if (assoc property sum-alist)
  15859. (setcdr (assoc property sum-alist) useval)
  15860. (push (cons property useval) sum-alist)
  15861. (org-unmodified
  15862. (add-text-properties sumpos (1+ sumpos)
  15863. (list 'org-summaries sum-alist))))
  15864. (when val
  15865. (org-entry-put nil property (if flag str val)))
  15866. ;; add current to current level accumulator
  15867. (when (or flag valflag)
  15868. (aset lsum level (+ (aref lsum level)
  15869. (if flag sum (org-column-string-to-number
  15870. (if flag str val) format))))
  15871. (aset lflag level t))
  15872. ;; clear accumulators for deeper levels
  15873. (loop for l from (1+ level) to (1- lmax) do
  15874. (aset lsum l 0)
  15875. (aset lflag l nil)))
  15876. ((>= level last-level)
  15877. ;; add what we have here to the accumulator for this level
  15878. (aset lsum level (+ (aref lsum level)
  15879. (org-column-string-to-number (or val "0") format)))
  15880. (and valflag (aset lflag level t)))
  15881. (t (error "This should not happen")))))))
  15882. (defun org-columns-redo ()
  15883. "Construct the column display again."
  15884. (interactive)
  15885. (message "Recomputing columns...")
  15886. (save-excursion
  15887. (if (marker-position org-columns-begin-marker)
  15888. (goto-char org-columns-begin-marker))
  15889. (org-columns-remove-overlays)
  15890. (if (org-mode-p)
  15891. (call-interactively 'org-columns)
  15892. (call-interactively 'org-agenda-columns)))
  15893. (message "Recomputing columns...done"))
  15894. (defun org-columns-not-in-agenda ()
  15895. (if (eq major-mode 'org-agenda-mode)
  15896. (error "This command is only allowed in Org-mode buffers")))
  15897. (defun org-string-to-number (s)
  15898. "Convert string to number, and interpret hh:mm:ss."
  15899. (if (not (string-match ":" s))
  15900. (string-to-number s)
  15901. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15902. (while l
  15903. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15904. sum)))
  15905. (defun org-column-number-to-string (n fmt &optional printf)
  15906. "Convert a computed column number to a string value, according to FMT."
  15907. (cond
  15908. ((eq fmt 'add_times)
  15909. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15910. (format "%d:%02d" h m)))
  15911. ((eq fmt 'checkbox)
  15912. (cond ((= n (floor n)) "[X]")
  15913. ((> n 1.) "[-]")
  15914. (t "[ ]")))
  15915. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15916. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15917. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15918. (printf (format printf n))
  15919. ((eq fmt 'currency)
  15920. (format "%.2f" n))
  15921. (t (number-to-string n))))
  15922. (defun org-nofm-to-completion (n m &optional percent)
  15923. (if (not percent)
  15924. (format "[%d/%d]" n m)
  15925. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15926. (defun org-column-string-to-number (s fmt)
  15927. "Convert a column value to a number that can be used for column computing."
  15928. (cond
  15929. ((string-match ":" s)
  15930. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15931. (while l
  15932. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15933. sum))
  15934. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15935. (if (equal s "[X]") 1. 0.000001))
  15936. (t (string-to-number s))))
  15937. (defun org-columns-uncompile-format (cfmt)
  15938. "Turn the compiled columns format back into a string representation."
  15939. (let ((rtn "") e s prop title op width fmt printf)
  15940. (while (setq e (pop cfmt))
  15941. (setq prop (car e)
  15942. title (nth 1 e)
  15943. width (nth 2 e)
  15944. op (nth 3 e)
  15945. fmt (nth 4 e)
  15946. printf (nth 5 e))
  15947. (cond
  15948. ((eq fmt 'add_times) (setq op ":"))
  15949. ((eq fmt 'checkbox) (setq op "X"))
  15950. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15951. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15952. ((eq fmt 'add_numbers) (setq op "+"))
  15953. ((eq fmt 'currency) (setq op "$")))
  15954. (if (and op printf) (setq op (concat op ";" printf)))
  15955. (if (equal title prop) (setq title nil))
  15956. (setq s (concat "%" (if width (number-to-string width))
  15957. prop
  15958. (if title (concat "(" title ")"))
  15959. (if op (concat "{" op "}"))))
  15960. (setq rtn (concat rtn " " s)))
  15961. (org-trim rtn)))
  15962. (defun org-columns-compile-format (fmt)
  15963. "Turn a column format string into an alist of specifications.
  15964. The alist has one entry for each column in the format. The elements of
  15965. that list are:
  15966. property the property
  15967. title the title field for the columns
  15968. width the column width in characters, can be nil for automatic
  15969. operator the operator if any
  15970. format the output format for computed results, derived from operator
  15971. printf a printf format for computed values"
  15972. (let ((start 0) width prop title op f printf)
  15973. (setq org-columns-current-fmt-compiled nil)
  15974. (while (string-match
  15975. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15976. fmt start)
  15977. (setq start (match-end 0)
  15978. width (match-string 1 fmt)
  15979. prop (match-string 2 fmt)
  15980. title (or (match-string 3 fmt) prop)
  15981. op (match-string 4 fmt)
  15982. f nil
  15983. printf nil)
  15984. (if width (setq width (string-to-number width)))
  15985. (when (and op (string-match ";" op))
  15986. (setq printf (substring op (match-end 0))
  15987. op (substring op 0 (match-beginning 0))))
  15988. (cond
  15989. ((equal op "+") (setq f 'add_numbers))
  15990. ((equal op "$") (setq f 'currency))
  15991. ((equal op ":") (setq f 'add_times))
  15992. ((equal op "X") (setq f 'checkbox))
  15993. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15994. ((equal op "X%") (setq f 'checkbox-percent))
  15995. )
  15996. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15997. (setq org-columns-current-fmt-compiled
  15998. (nreverse org-columns-current-fmt-compiled))))
  15999. ;;; Dynamic block for Column view
  16000. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  16001. "Get the column view of the current buffer or subtree.
  16002. The first optional argument MAXLEVEL sets the level limit. A
  16003. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  16004. empty rows, an empty row being one where all the column view
  16005. specifiers except ITEM are empty. This function returns a list
  16006. containing the title row and all other rows. Each row is a list
  16007. of fields."
  16008. (save-excursion
  16009. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16010. (n (length title)) row tbl)
  16011. (goto-char (point-min))
  16012. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  16013. (or (null maxlevel)
  16014. (>= maxlevel
  16015. (if org-odd-levels-only
  16016. (/ (1+ (length (match-string 1))) 2)
  16017. (length (match-string 1))))))
  16018. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16019. (setq row nil)
  16020. (loop for i from 0 to (1- n) do
  16021. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16022. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16023. "")
  16024. row))
  16025. (setq row (nreverse row))
  16026. (unless (and skip-empty-rows
  16027. (eq 1 (length (delete "" (delete-dups row)))))
  16028. (push row tbl))))
  16029. (append (list title 'hline) (nreverse tbl)))))
  16030. (defun org-dblock-write:columnview (params)
  16031. "Write the column view table.
  16032. PARAMS is a property list of parameters:
  16033. :width enforce same column widths with <N> specifiers.
  16034. :id the :ID: property of the entry where the columns view
  16035. should be built, as a string. When `local', call locally.
  16036. When `global' call column view with the cursor at the beginning
  16037. of the buffer (usually this means that the whole buffer switches
  16038. to column view).
  16039. :hlines When t, insert a hline before each item. When a number, insert
  16040. a hline before each level <= that number.
  16041. :vlines When t, make each column a colgroup to enforce vertical lines.
  16042. :maxlevel When set to a number, don't capture headlines below this level.
  16043. :skip-empty-rows
  16044. When t, skip rows where all specifiers other than ITEM are empty."
  16045. (let ((pos (move-marker (make-marker) (point)))
  16046. (hlines (plist-get params :hlines))
  16047. (vlines (plist-get params :vlines))
  16048. (maxlevel (plist-get params :maxlevel))
  16049. (skip-empty-rows (plist-get params :skip-empty-rows))
  16050. tbl id idpos nfields tmp)
  16051. (save-excursion
  16052. (save-restriction
  16053. (when (setq id (plist-get params :id))
  16054. (cond ((not id) nil)
  16055. ((eq id 'global) (goto-char (point-min)))
  16056. ((eq id 'local) nil)
  16057. ((setq idpos (org-find-entry-with-id id))
  16058. (goto-char idpos))
  16059. (t (error "Cannot find entry with :ID: %s" id))))
  16060. (org-columns)
  16061. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  16062. (setq nfields (length (car tbl)))
  16063. (org-columns-quit)))
  16064. (goto-char pos)
  16065. (move-marker pos nil)
  16066. (when tbl
  16067. (when (plist-get params :hlines)
  16068. (setq tmp nil)
  16069. (while tbl
  16070. (if (eq (car tbl) 'hline)
  16071. (push (pop tbl) tmp)
  16072. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16073. (if (and (not (eq (car tmp) 'hline))
  16074. (or (eq hlines t)
  16075. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16076. (push 'hline tmp)))
  16077. (push (pop tbl) tmp)))
  16078. (setq tbl (nreverse tmp)))
  16079. (when vlines
  16080. (setq tbl (mapcar (lambda (x)
  16081. (if (eq 'hline x) x (cons "" x)))
  16082. tbl))
  16083. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16084. (setq pos (point))
  16085. (insert (org-listtable-to-string tbl))
  16086. (when (plist-get params :width)
  16087. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16088. org-columns-current-widths "|")))
  16089. (goto-char pos)
  16090. (org-table-align))))
  16091. (defun org-listtable-to-string (tbl)
  16092. "Convert a listtable TBL to a string that contains the Org-mode table.
  16093. The table still need to be alligned. The resulting string has no leading
  16094. and tailing newline characters."
  16095. (mapconcat
  16096. (lambda (x)
  16097. (cond
  16098. ((listp x)
  16099. (concat "|" (mapconcat 'identity x "|") "|"))
  16100. ((eq x 'hline) "|-|")
  16101. (t (error "Garbage in listtable: %s" x))))
  16102. tbl "\n"))
  16103. (defun org-insert-columns-dblock ()
  16104. "Create a dynamic block capturing a column view table."
  16105. (interactive)
  16106. (let ((defaults '(:name "columnview" :hlines 1))
  16107. (id (completing-read
  16108. "Capture columns (local, global, entry with :ID: property) [local]: "
  16109. (append '(("global") ("local"))
  16110. (mapcar 'list (org-property-values "ID"))))))
  16111. (if (equal id "") (setq id 'local))
  16112. (if (equal id "global") (setq id 'global))
  16113. (setq defaults (append defaults (list :id id)))
  16114. (org-create-dblock defaults)
  16115. (org-update-dblock)))
  16116. ;;;; Timestamps
  16117. (defvar org-last-changed-timestamp nil)
  16118. (defvar org-time-was-given) ; dynamically scoped parameter
  16119. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16120. (defvar org-ts-what) ; dynamically scoped parameter
  16121. (defun org-time-stamp (arg)
  16122. "Prompt for a date/time and insert a time stamp.
  16123. If the user specifies a time like HH:MM, or if this command is called
  16124. with a prefix argument, the time stamp will contain date and time.
  16125. Otherwise, only the date will be included. All parts of a date not
  16126. specified by the user will be filled in from the current date/time.
  16127. So if you press just return without typing anything, the time stamp
  16128. will represent the current date/time. If there is already a timestamp
  16129. at the cursor, it will be modified."
  16130. (interactive "P")
  16131. (let* ((ts nil)
  16132. (default-time
  16133. ;; Default time is either today, or, when entering a range,
  16134. ;; the range start.
  16135. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16136. (save-excursion
  16137. (re-search-backward
  16138. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16139. (- (point) 20) t)))
  16140. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16141. (current-time)))
  16142. (default-input (and ts (org-get-compact-tod ts)))
  16143. org-time-was-given org-end-time-was-given time)
  16144. (cond
  16145. ((and (org-at-timestamp-p)
  16146. (eq last-command 'org-time-stamp)
  16147. (eq this-command 'org-time-stamp))
  16148. (insert "--")
  16149. (setq time (let ((this-command this-command))
  16150. (org-read-date arg 'totime nil nil default-time default-input)))
  16151. (org-insert-time-stamp time (or org-time-was-given arg)))
  16152. ((org-at-timestamp-p)
  16153. (setq time (let ((this-command this-command))
  16154. (org-read-date arg 'totime nil nil default-time default-input)))
  16155. (when (org-at-timestamp-p) ; just to get the match data
  16156. (replace-match "")
  16157. (setq org-last-changed-timestamp
  16158. (org-insert-time-stamp
  16159. time (or org-time-was-given arg)
  16160. nil nil nil (list org-end-time-was-given))))
  16161. (message "Timestamp updated"))
  16162. (t
  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. nil nil nil (list org-end-time-was-given))))))
  16167. ;; FIXME: can we use this for something else????
  16168. ;; like computing time differences?????
  16169. (defun org-get-compact-tod (s)
  16170. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16171. (let* ((t1 (match-string 1 s))
  16172. (h1 (string-to-number (match-string 2 s)))
  16173. (m1 (string-to-number (match-string 3 s)))
  16174. (t2 (and (match-end 4) (match-string 5 s)))
  16175. (h2 (and t2 (string-to-number (match-string 6 s))))
  16176. (m2 (and t2 (string-to-number (match-string 7 s))))
  16177. dh dm)
  16178. (if (not t2)
  16179. t1
  16180. (setq dh (- h2 h1) dm (- m2 m1))
  16181. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16182. (concat t1 "+" (number-to-string dh)
  16183. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16184. (defun org-time-stamp-inactive (&optional arg)
  16185. "Insert an inactive time stamp.
  16186. An inactive time stamp is enclosed in square brackets instead of angle
  16187. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16188. does not link to the calendar and cannot be changed with the S-cursor keys.
  16189. So these are more for recording a certain time/date."
  16190. (interactive "P")
  16191. (let (org-time-was-given org-end-time-was-given time)
  16192. (setq time (org-read-date arg 'totime))
  16193. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16194. nil nil (list org-end-time-was-given))))
  16195. (defvar org-date-ovl (org-make-overlay 1 1))
  16196. (org-overlay-put org-date-ovl 'face 'org-warning)
  16197. (org-detach-overlay org-date-ovl)
  16198. (defvar org-ans1) ; dynamically scoped parameter
  16199. (defvar org-ans2) ; dynamically scoped parameter
  16200. (defvar org-plain-time-of-day-regexp) ; defined below
  16201. (defvar org-read-date-overlay nil)
  16202. (defvar org-dcst nil) ; dynamically scoped
  16203. (defun org-read-date (&optional with-time to-time from-string prompt
  16204. default-time default-input)
  16205. "Read a date, possibly a time, and make things smooth for the user.
  16206. The prompt will suggest to enter an ISO date, but you can also enter anything
  16207. which will at least partially be understood by `parse-time-string'.
  16208. Unrecognized parts of the date will default to the current day, month, year,
  16209. hour and minute. If this command is called to replace a timestamp at point,
  16210. of to enter the second timestamp of a range, the default time is taken from the
  16211. existing stamp. For example,
  16212. 3-2-5 --> 2003-02-05
  16213. feb 15 --> currentyear-02-15
  16214. sep 12 9 --> 2009-09-12
  16215. 12:45 --> today 12:45
  16216. 22 sept 0:34 --> currentyear-09-22 0:34
  16217. 12 --> currentyear-currentmonth-12
  16218. Fri --> nearest Friday (today or later)
  16219. etc.
  16220. Furthermore you can specify a relative date by giving, as the *first* thing
  16221. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16222. change in days weeks, months, years.
  16223. With a single plus or minus, the date is relative to today. With a double
  16224. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16225. +4d --> four days from today
  16226. +4 --> same as above
  16227. +2w --> two weeks from today
  16228. ++5 --> five days from default date
  16229. The function understands only English month and weekday abbreviations,
  16230. but this can be configured with the variables `parse-time-months' and
  16231. `parse-time-weekdays'.
  16232. While prompting, a calendar is popped up - you can also select the
  16233. date with the mouse (button 1). The calendar shows a period of three
  16234. months. To scroll it to other months, use the keys `>' and `<'.
  16235. If you don't like the calendar, turn it off with
  16236. \(setq org-read-date-popup-calendar nil)
  16237. With optional argument TO-TIME, the date will immediately be converted
  16238. to an internal time.
  16239. With an optional argument WITH-TIME, the prompt will suggest to also
  16240. insert a time. Note that when WITH-TIME is not set, you can still
  16241. enter a time, and this function will inform the calling routine about
  16242. this change. The calling routine may then choose to change the format
  16243. used to insert the time stamp into the buffer to include the time.
  16244. With optional argument FROM-STRING, read from this string instead from
  16245. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16246. the time/date that is used for everything that is not specified by the
  16247. user."
  16248. (require 'parse-time)
  16249. (let* ((org-time-stamp-rounding-minutes
  16250. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16251. (org-dcst org-display-custom-times)
  16252. (ct (org-current-time))
  16253. (def (or default-time ct))
  16254. (defdecode (decode-time def))
  16255. (dummy (progn
  16256. (when (< (nth 2 defdecode) org-extend-today-until)
  16257. (setcar (nthcdr 2 defdecode) -1)
  16258. (setcar (nthcdr 1 defdecode) 59)
  16259. (setq def (apply 'encode-time defdecode)
  16260. defdecode (decode-time def)))))
  16261. (calendar-move-hook nil)
  16262. (view-diary-entries-initially nil)
  16263. (view-calendar-holidays-initially nil)
  16264. (timestr (format-time-string
  16265. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16266. (prompt (concat (if prompt (concat prompt " ") "")
  16267. (format "Date+time [%s]: " timestr)))
  16268. ans (org-ans0 "") org-ans1 org-ans2 final)
  16269. (cond
  16270. (from-string (setq ans from-string))
  16271. (org-read-date-popup-calendar
  16272. (save-excursion
  16273. (save-window-excursion
  16274. (calendar)
  16275. (calendar-forward-day (- (time-to-days def)
  16276. (calendar-absolute-from-gregorian
  16277. (calendar-current-date))))
  16278. (org-eval-in-calendar nil t)
  16279. (let* ((old-map (current-local-map))
  16280. (map (copy-keymap calendar-mode-map))
  16281. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16282. (org-defkey map (kbd "RET") 'org-calendar-select)
  16283. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16284. 'org-calendar-select-mouse)
  16285. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16286. 'org-calendar-select-mouse)
  16287. (org-defkey minibuffer-local-map [(meta shift left)]
  16288. (lambda () (interactive)
  16289. (org-eval-in-calendar '(calendar-backward-month 1))))
  16290. (org-defkey minibuffer-local-map [(meta shift right)]
  16291. (lambda () (interactive)
  16292. (org-eval-in-calendar '(calendar-forward-month 1))))
  16293. (org-defkey minibuffer-local-map [(meta shift up)]
  16294. (lambda () (interactive)
  16295. (org-eval-in-calendar '(calendar-backward-year 1))))
  16296. (org-defkey minibuffer-local-map [(meta shift down)]
  16297. (lambda () (interactive)
  16298. (org-eval-in-calendar '(calendar-forward-year 1))))
  16299. (org-defkey minibuffer-local-map [(shift up)]
  16300. (lambda () (interactive)
  16301. (org-eval-in-calendar '(calendar-backward-week 1))))
  16302. (org-defkey minibuffer-local-map [(shift down)]
  16303. (lambda () (interactive)
  16304. (org-eval-in-calendar '(calendar-forward-week 1))))
  16305. (org-defkey minibuffer-local-map [(shift left)]
  16306. (lambda () (interactive)
  16307. (org-eval-in-calendar '(calendar-backward-day 1))))
  16308. (org-defkey minibuffer-local-map [(shift right)]
  16309. (lambda () (interactive)
  16310. (org-eval-in-calendar '(calendar-forward-day 1))))
  16311. (org-defkey minibuffer-local-map ">"
  16312. (lambda () (interactive)
  16313. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16314. (org-defkey minibuffer-local-map "<"
  16315. (lambda () (interactive)
  16316. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16317. (unwind-protect
  16318. (progn
  16319. (use-local-map map)
  16320. (add-hook 'post-command-hook 'org-read-date-display)
  16321. (setq org-ans0 (read-string prompt default-input nil nil))
  16322. ;; org-ans0: from prompt
  16323. ;; org-ans1: from mouse click
  16324. ;; org-ans2: from calendar motion
  16325. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16326. (remove-hook 'post-command-hook 'org-read-date-display)
  16327. (use-local-map old-map)
  16328. (when org-read-date-overlay
  16329. (org-delete-overlay org-read-date-overlay)
  16330. (setq org-read-date-overlay nil)))))))
  16331. (t ; Naked prompt only
  16332. (unwind-protect
  16333. (setq ans (read-string prompt default-input nil timestr))
  16334. (when org-read-date-overlay
  16335. (org-delete-overlay org-read-date-overlay)
  16336. (setq org-read-date-overlay nil)))))
  16337. (setq final (org-read-date-analyze ans def defdecode))
  16338. (if to-time
  16339. (apply 'encode-time final)
  16340. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16341. (format "%04d-%02d-%02d %02d:%02d"
  16342. (nth 5 final) (nth 4 final) (nth 3 final)
  16343. (nth 2 final) (nth 1 final))
  16344. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16345. (defvar def)
  16346. (defvar defdecode)
  16347. (defvar with-time)
  16348. (defun org-read-date-display ()
  16349. "Display the currrent date prompt interpretation in the minibuffer."
  16350. (when org-read-date-display-live
  16351. (when org-read-date-overlay
  16352. (org-delete-overlay org-read-date-overlay))
  16353. (let ((p (point)))
  16354. (end-of-line 1)
  16355. (while (not (equal (buffer-substring
  16356. (max (point-min) (- (point) 4)) (point))
  16357. " "))
  16358. (insert " "))
  16359. (goto-char p))
  16360. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16361. " " (or org-ans1 org-ans2)))
  16362. (org-end-time-was-given nil)
  16363. (f (org-read-date-analyze ans def defdecode))
  16364. (fmts (if org-dcst
  16365. org-time-stamp-custom-formats
  16366. org-time-stamp-formats))
  16367. (fmt (if (or with-time
  16368. (and (boundp 'org-time-was-given) org-time-was-given))
  16369. (cdr fmts)
  16370. (car fmts)))
  16371. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16372. (when (and org-end-time-was-given
  16373. (string-match org-plain-time-of-day-regexp txt))
  16374. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16375. org-end-time-was-given
  16376. (substring txt (match-end 0)))))
  16377. (setq org-read-date-overlay
  16378. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16379. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16380. (defun org-read-date-analyze (ans def defdecode)
  16381. "Analyze the combined answer of the date prompt."
  16382. ;; FIXME: cleanup and comment
  16383. (let (delta deltan deltaw deltadef year month day
  16384. hour minute second wday pm h2 m2 tl wday1)
  16385. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16386. (setq ans (replace-match "" t t ans)
  16387. deltan (car delta)
  16388. deltaw (nth 1 delta)
  16389. deltadef (nth 2 delta)))
  16390. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16391. (when (string-match
  16392. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16393. (setq year (if (match-end 2)
  16394. (string-to-number (match-string 2 ans))
  16395. (string-to-number (format-time-string "%Y")))
  16396. month (string-to-number (match-string 3 ans))
  16397. day (string-to-number (match-string 4 ans)))
  16398. (if (< year 100) (setq year (+ 2000 year)))
  16399. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16400. t nil ans)))
  16401. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16402. ;; If there is a time with am/pm, and *no* time without it, we convert
  16403. ;; so that matching will be successful.
  16404. (loop for i from 1 to 2 do ; twice, for end time as well
  16405. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16406. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16407. (setq hour (string-to-number (match-string 1 ans))
  16408. minute (if (match-end 3)
  16409. (string-to-number (match-string 3 ans))
  16410. 0)
  16411. pm (equal ?p
  16412. (string-to-char (downcase (match-string 4 ans)))))
  16413. (if (and (= hour 12) (not pm))
  16414. (setq hour 0)
  16415. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16416. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16417. t t ans))))
  16418. ;; Check if a time range is given as a duration
  16419. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16420. (setq hour (string-to-number (match-string 1 ans))
  16421. h2 (+ hour (string-to-number (match-string 3 ans)))
  16422. minute (string-to-number (match-string 2 ans))
  16423. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16424. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16425. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16426. ;; Check if there is a time range
  16427. (when (boundp 'org-end-time-was-given)
  16428. (setq org-time-was-given nil)
  16429. (when (and (string-match org-plain-time-of-day-regexp ans)
  16430. (match-end 8))
  16431. (setq org-end-time-was-given (match-string 8 ans))
  16432. (setq ans (concat (substring ans 0 (match-beginning 7))
  16433. (substring ans (match-end 7))))))
  16434. (setq tl (parse-time-string ans)
  16435. day (or (nth 3 tl) (nth 3 defdecode))
  16436. month (or (nth 4 tl)
  16437. (if (and org-read-date-prefer-future
  16438. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16439. (1+ (nth 4 defdecode))
  16440. (nth 4 defdecode)))
  16441. year (or (nth 5 tl)
  16442. (if (and org-read-date-prefer-future
  16443. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16444. (1+ (nth 5 defdecode))
  16445. (nth 5 defdecode)))
  16446. hour (or (nth 2 tl) (nth 2 defdecode))
  16447. minute (or (nth 1 tl) (nth 1 defdecode))
  16448. second (or (nth 0 tl) 0)
  16449. wday (nth 6 tl))
  16450. (when deltan
  16451. (unless deltadef
  16452. (let ((now (decode-time (current-time))))
  16453. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16454. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16455. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16456. ((equal deltaw "m") (setq month (+ month deltan)))
  16457. ((equal deltaw "y") (setq year (+ year deltan)))))
  16458. (when (and wday (not (nth 3 tl)))
  16459. ;; Weekday was given, but no day, so pick that day in the week
  16460. ;; on or after the derived date.
  16461. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16462. (unless (equal wday wday1)
  16463. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16464. (if (and (boundp 'org-time-was-given)
  16465. (nth 2 tl))
  16466. (setq org-time-was-given t))
  16467. (if (< year 100) (setq year (+ 2000 year)))
  16468. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16469. (list second minute hour day month year)))
  16470. (defvar parse-time-weekdays)
  16471. (defun org-read-date-get-relative (s today default)
  16472. "Check string S for special relative date string.
  16473. TODAY and DEFAULT are internal times, for today and for a default.
  16474. Return shift list (N what def-flag)
  16475. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16476. N is the number of WHATs to shift.
  16477. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16478. the DEFAULT date rather than TODAY."
  16479. (when (string-match
  16480. (concat
  16481. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16482. "\\([0-9]+\\)?"
  16483. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16484. "\\([ \t]\\|$\\)") s)
  16485. (let* ((dir (if (match-end 1)
  16486. (string-to-char (substring (match-string 1 s) -1))
  16487. ?+))
  16488. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16489. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16490. (what (if (match-end 3) (match-string 3 s) "d"))
  16491. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16492. (date (if rel default today))
  16493. (wday (nth 6 (decode-time date)))
  16494. delta)
  16495. (if wday1
  16496. (progn
  16497. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16498. (if (= dir ?-) (setq delta (- delta 7)))
  16499. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16500. (list delta "d" rel))
  16501. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16502. (defun org-eval-in-calendar (form &optional keepdate)
  16503. "Eval FORM in the calendar window and return to current window.
  16504. Also, store the cursor date in variable org-ans2."
  16505. (let ((sw (selected-window)))
  16506. (select-window (get-buffer-window "*Calendar*"))
  16507. (eval form)
  16508. (when (and (not keepdate) (calendar-cursor-to-date))
  16509. (let* ((date (calendar-cursor-to-date))
  16510. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16511. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16512. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16513. (select-window sw)))
  16514. ; ;; Update the prompt to show new default date
  16515. ; (save-excursion
  16516. ; (goto-char (point-min))
  16517. ; (when (and org-ans2
  16518. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16519. ; (get-text-property (match-end 0) 'field))
  16520. ; (let ((inhibit-read-only t))
  16521. ; (replace-match (concat "[" org-ans2 "]") t t)
  16522. ; (add-text-properties (point-min) (1+ (match-end 0))
  16523. ; (text-properties-at (1+ (point-min)))))))))
  16524. (defun org-calendar-select ()
  16525. "Return to `org-read-date' with the date currently selected.
  16526. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16527. (interactive)
  16528. (when (calendar-cursor-to-date)
  16529. (let* ((date (calendar-cursor-to-date))
  16530. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16531. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16532. (if (active-minibuffer-window) (exit-minibuffer))))
  16533. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16534. "Insert a date stamp for the date given by the internal TIME.
  16535. WITH-HM means, use the stamp format that includes the time of the day.
  16536. INACTIVE means use square brackets instead of angular ones, so that the
  16537. stamp will not contribute to the agenda.
  16538. PRE and POST are optional strings to be inserted before and after the
  16539. stamp.
  16540. The command returns the inserted time stamp."
  16541. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16542. stamp)
  16543. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16544. (insert-before-markers (or pre ""))
  16545. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16546. (when (listp extra)
  16547. (setq extra (car extra))
  16548. (if (and (stringp extra)
  16549. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16550. (setq extra (format "-%02d:%02d"
  16551. (string-to-number (match-string 1 extra))
  16552. (string-to-number (match-string 2 extra))))
  16553. (setq extra nil)))
  16554. (when extra
  16555. (backward-char 1)
  16556. (insert-before-markers extra)
  16557. (forward-char 1))
  16558. (insert-before-markers (or post ""))
  16559. stamp))
  16560. (defun org-toggle-time-stamp-overlays ()
  16561. "Toggle the use of custom time stamp formats."
  16562. (interactive)
  16563. (setq org-display-custom-times (not org-display-custom-times))
  16564. (unless org-display-custom-times
  16565. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16566. (while (setq p (next-single-property-change p 'display))
  16567. (if (and (get-text-property p 'display)
  16568. (eq (get-text-property p 'face) 'org-date))
  16569. (remove-text-properties
  16570. p (setq p (next-single-property-change p 'display))
  16571. '(display t))))
  16572. (set-buffer-modified-p bmp)))
  16573. (if (featurep 'xemacs)
  16574. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16575. (org-restart-font-lock)
  16576. (setq org-table-may-need-update t)
  16577. (if org-display-custom-times
  16578. (message "Time stamps are overlayed with custom format")
  16579. (message "Time stamp overlays removed")))
  16580. (defun org-display-custom-time (beg end)
  16581. "Overlay modified time stamp format over timestamp between BED and END."
  16582. (let* ((ts (buffer-substring beg end))
  16583. t1 w1 with-hm tf time str w2 (off 0))
  16584. (save-match-data
  16585. (setq t1 (org-parse-time-string ts t))
  16586. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16587. (setq off (- (match-end 0) (match-beginning 0)))))
  16588. (setq end (- end off))
  16589. (setq w1 (- end beg)
  16590. with-hm (and (nth 1 t1) (nth 2 t1))
  16591. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16592. time (org-fix-decoded-time t1)
  16593. str (org-add-props
  16594. (format-time-string
  16595. (substring tf 1 -1) (apply 'encode-time time))
  16596. nil 'mouse-face 'highlight)
  16597. w2 (length str))
  16598. (if (not (= w2 w1))
  16599. (add-text-properties (1+ beg) (+ 2 beg)
  16600. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16601. (if (featurep 'xemacs)
  16602. (progn
  16603. (put-text-property beg end 'invisible t)
  16604. (put-text-property beg end 'end-glyph (make-glyph str)))
  16605. (put-text-property beg end 'display str))))
  16606. (defun org-translate-time (string)
  16607. "Translate all timestamps in STRING to custom format.
  16608. But do this only if the variable `org-display-custom-times' is set."
  16609. (when org-display-custom-times
  16610. (save-match-data
  16611. (let* ((start 0)
  16612. (re org-ts-regexp-both)
  16613. t1 with-hm inactive tf time str beg end)
  16614. (while (setq start (string-match re string start))
  16615. (setq beg (match-beginning 0)
  16616. end (match-end 0)
  16617. t1 (save-match-data
  16618. (org-parse-time-string (substring string beg end) t))
  16619. with-hm (and (nth 1 t1) (nth 2 t1))
  16620. inactive (equal (substring string beg (1+ beg)) "[")
  16621. tf (funcall (if with-hm 'cdr 'car)
  16622. org-time-stamp-custom-formats)
  16623. time (org-fix-decoded-time t1)
  16624. str (format-time-string
  16625. (concat
  16626. (if inactive "[" "<") (substring tf 1 -1)
  16627. (if inactive "]" ">"))
  16628. (apply 'encode-time time))
  16629. string (replace-match str t t string)
  16630. start (+ start (length str)))))))
  16631. string)
  16632. (defun org-fix-decoded-time (time)
  16633. "Set 0 instead of nil for the first 6 elements of time.
  16634. Don't touch the rest."
  16635. (let ((n 0))
  16636. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16637. (defun org-days-to-time (timestamp-string)
  16638. "Difference between TIMESTAMP-STRING and now in days."
  16639. (- (time-to-days (org-time-string-to-time timestamp-string))
  16640. (time-to-days (current-time))))
  16641. (defun org-deadline-close (timestamp-string &optional ndays)
  16642. "Is the time in TIMESTAMP-STRING close to the current date?"
  16643. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16644. (and (< (org-days-to-time timestamp-string) ndays)
  16645. (not (org-entry-is-done-p))))
  16646. (defun org-get-wdays (ts)
  16647. "Get the deadline lead time appropriate for timestring TS."
  16648. (cond
  16649. ((<= org-deadline-warning-days 0)
  16650. ;; 0 or negative, enforce this value no matter what
  16651. (- org-deadline-warning-days))
  16652. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16653. ;; lead time is specified.
  16654. (floor (* (string-to-number (match-string 1 ts))
  16655. (cdr (assoc (match-string 2 ts)
  16656. '(("d" . 1) ("w" . 7)
  16657. ("m" . 30.4) ("y" . 365.25)))))))
  16658. ;; go for the default.
  16659. (t org-deadline-warning-days)))
  16660. (defun org-calendar-select-mouse (ev)
  16661. "Return to `org-read-date' with the date currently selected.
  16662. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16663. (interactive "e")
  16664. (mouse-set-point ev)
  16665. (when (calendar-cursor-to-date)
  16666. (let* ((date (calendar-cursor-to-date))
  16667. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16668. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16669. (if (active-minibuffer-window) (exit-minibuffer))))
  16670. (defun org-check-deadlines (ndays)
  16671. "Check if there are any deadlines due or past due.
  16672. A deadline is considered due if it happens within `org-deadline-warning-days'
  16673. days from today's date. If the deadline appears in an entry marked DONE,
  16674. it is not shown. The prefix arg NDAYS can be used to test that many
  16675. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16676. (interactive "P")
  16677. (let* ((org-warn-days
  16678. (cond
  16679. ((equal ndays '(4)) 100000)
  16680. (ndays (prefix-numeric-value ndays))
  16681. (t (abs org-deadline-warning-days))))
  16682. (case-fold-search nil)
  16683. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16684. (callback
  16685. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16686. (message "%d deadlines past-due or due within %d days"
  16687. (org-occur regexp nil callback)
  16688. org-warn-days)))
  16689. (defun org-check-before-date (date)
  16690. "Check if there are deadlines or scheduled entries before DATE."
  16691. (interactive (list (org-read-date)))
  16692. (let ((case-fold-search nil)
  16693. (regexp (concat "\\<\\(" org-deadline-string
  16694. "\\|" org-scheduled-string
  16695. "\\) *<\\([^>]+\\)>"))
  16696. (callback
  16697. (lambda () (time-less-p
  16698. (org-time-string-to-time (match-string 2))
  16699. (org-time-string-to-time date)))))
  16700. (message "%d entries before %s"
  16701. (org-occur regexp nil callback) date)))
  16702. (defun org-evaluate-time-range (&optional to-buffer)
  16703. "Evaluate a time range by computing the difference between start and end.
  16704. Normally the result is just printed in the echo area, but with prefix arg
  16705. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16706. If the time range is actually in a table, the result is inserted into the
  16707. next column.
  16708. For time difference computation, a year is assumed to be exactly 365
  16709. days in order to avoid rounding problems."
  16710. (interactive "P")
  16711. (or
  16712. (org-clock-update-time-maybe)
  16713. (save-excursion
  16714. (unless (org-at-date-range-p t)
  16715. (goto-char (point-at-bol))
  16716. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16717. (if (not (org-at-date-range-p t))
  16718. (error "Not at a time-stamp range, and none found in current line")))
  16719. (let* ((ts1 (match-string 1))
  16720. (ts2 (match-string 2))
  16721. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16722. (match-end (match-end 0))
  16723. (time1 (org-time-string-to-time ts1))
  16724. (time2 (org-time-string-to-time ts2))
  16725. (t1 (time-to-seconds time1))
  16726. (t2 (time-to-seconds time2))
  16727. (diff (abs (- t2 t1)))
  16728. (negative (< (- t2 t1) 0))
  16729. ;; (ys (floor (* 365 24 60 60)))
  16730. (ds (* 24 60 60))
  16731. (hs (* 60 60))
  16732. (fy "%dy %dd %02d:%02d")
  16733. (fy1 "%dy %dd")
  16734. (fd "%dd %02d:%02d")
  16735. (fd1 "%dd")
  16736. (fh "%02d:%02d")
  16737. y d h m align)
  16738. (if havetime
  16739. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16740. y 0
  16741. d (floor (/ diff ds)) diff (mod diff ds)
  16742. h (floor (/ diff hs)) diff (mod diff hs)
  16743. m (floor (/ diff 60)))
  16744. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16745. y 0
  16746. d (floor (+ (/ diff ds) 0.5))
  16747. h 0 m 0))
  16748. (if (not to-buffer)
  16749. (message "%s" (org-make-tdiff-string y d h m))
  16750. (if (org-at-table-p)
  16751. (progn
  16752. (goto-char match-end)
  16753. (setq align t)
  16754. (and (looking-at " *|") (goto-char (match-end 0))))
  16755. (goto-char match-end))
  16756. (if (looking-at
  16757. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16758. (replace-match ""))
  16759. (if negative (insert " -"))
  16760. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16761. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16762. (insert " " (format fh h m))))
  16763. (if align (org-table-align))
  16764. (message "Time difference inserted")))))
  16765. (defun org-make-tdiff-string (y d h m)
  16766. (let ((fmt "")
  16767. (l nil))
  16768. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16769. l (push y l)))
  16770. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16771. l (push d l)))
  16772. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16773. l (push h l)))
  16774. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16775. l (push m l)))
  16776. (apply 'format fmt (nreverse l))))
  16777. (defun org-time-string-to-time (s)
  16778. (apply 'encode-time (org-parse-time-string s)))
  16779. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16780. "Convert a time stamp to an absolute day number.
  16781. If there is a specifyer for a cyclic time stamp, get the closest date to
  16782. DAYNR."
  16783. (cond
  16784. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16785. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16786. daynr
  16787. (+ daynr 1000)))
  16788. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16789. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16790. (time-to-days (current-time))) (match-string 0 s)
  16791. prefer))
  16792. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16793. (defun org-time-from-absolute (d)
  16794. "Return the time corresponding to date D.
  16795. D may be an absolute day number, or a calendar-type list (month day year)."
  16796. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16797. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16798. (defun org-calendar-holiday ()
  16799. "List of holidays, for Diary display in Org-mode."
  16800. (require 'holidays)
  16801. (let ((hl (funcall
  16802. (if (fboundp 'calendar-check-holidays)
  16803. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16804. (if hl (mapconcat 'identity hl "; "))))
  16805. (defun org-diary-sexp-entry (sexp entry date)
  16806. "Process a SEXP diary ENTRY for DATE."
  16807. (require 'diary-lib)
  16808. (let ((result (if calendar-debug-sexp
  16809. (let ((stack-trace-on-error t))
  16810. (eval (car (read-from-string sexp))))
  16811. (condition-case nil
  16812. (eval (car (read-from-string sexp)))
  16813. (error
  16814. (beep)
  16815. (message "Bad sexp at line %d in %s: %s"
  16816. (org-current-line)
  16817. (buffer-file-name) sexp)
  16818. (sleep-for 2))))))
  16819. (cond ((stringp result) result)
  16820. ((and (consp result)
  16821. (stringp (cdr result))) (cdr result))
  16822. (result entry)
  16823. (t nil))))
  16824. (defun org-diary-to-ical-string (frombuf)
  16825. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16826. This uses the icalendar.el library."
  16827. (let* ((tmpdir (if (featurep 'xemacs)
  16828. (temp-directory)
  16829. temporary-file-directory))
  16830. (tmpfile (make-temp-name
  16831. (expand-file-name "orgics" tmpdir)))
  16832. buf rtn b e)
  16833. (save-excursion
  16834. (set-buffer frombuf)
  16835. (icalendar-export-region (point-min) (point-max) tmpfile)
  16836. (setq buf (find-buffer-visiting tmpfile))
  16837. (set-buffer buf)
  16838. (goto-char (point-min))
  16839. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16840. (setq b (match-beginning 0)))
  16841. (goto-char (point-max))
  16842. (if (re-search-backward "^END:VEVENT" nil t)
  16843. (setq e (match-end 0)))
  16844. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16845. (kill-buffer buf)
  16846. (kill-buffer frombuf)
  16847. (delete-file tmpfile)
  16848. rtn))
  16849. (defun org-closest-date (start current change prefer)
  16850. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16851. When PREFER is `past' return a date that is either CURRENT or past.
  16852. When PREFER is `future', return a date that is either CURRENT or future."
  16853. ;; Make the proper lists from the dates
  16854. (catch 'exit
  16855. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16856. dn dw sday cday n1 n2
  16857. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16858. (setq start (org-date-to-gregorian start)
  16859. current (org-date-to-gregorian
  16860. (if org-agenda-repeating-timestamp-show-all
  16861. current
  16862. (time-to-days (current-time))))
  16863. sday (calendar-absolute-from-gregorian start)
  16864. cday (calendar-absolute-from-gregorian current))
  16865. (if (<= cday sday) (throw 'exit sday))
  16866. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16867. (setq dn (string-to-number (match-string 1 change))
  16868. dw (cdr (assoc (match-string 2 change) a1)))
  16869. (error "Invalid change specifyer: %s" change))
  16870. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16871. (cond
  16872. ((eq dw 'day)
  16873. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16874. n2 (+ n1 dn)))
  16875. ((eq dw 'year)
  16876. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16877. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16878. (setq date1 (list m d y1)
  16879. n1 (calendar-absolute-from-gregorian date1)
  16880. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16881. n2 (calendar-absolute-from-gregorian date2)))
  16882. ((eq dw 'month)
  16883. ;; approx number of month between the tow dates
  16884. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16885. ;; How often does dn fit in there?
  16886. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16887. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16888. m (+ m nm)
  16889. ny (floor (/ m 12))
  16890. y (+ y ny)
  16891. m (- m (* ny 12)))
  16892. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16893. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16894. (setq m2 (+ m dn) y2 y)
  16895. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16896. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16897. (while (< n2 cday)
  16898. (setq n1 n2 m m2 y y2)
  16899. (setq m2 (+ m dn) y2 y)
  16900. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16901. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16902. (if org-agenda-repeating-timestamp-show-all
  16903. (cond
  16904. ((eq prefer 'past) n1)
  16905. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16906. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16907. (cond
  16908. ((eq prefer 'past) n1)
  16909. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16910. (t (if (= cday n1) n1 n2)))))))
  16911. (defun org-date-to-gregorian (date)
  16912. "Turn any specification of DATE into a gregorian date for the calendar."
  16913. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16914. ((and (listp date) (= (length date) 3)) date)
  16915. ((stringp date)
  16916. (setq date (org-parse-time-string date))
  16917. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16918. ((listp date)
  16919. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16920. (defun org-parse-time-string (s &optional nodefault)
  16921. "Parse the standard Org-mode time string.
  16922. This should be a lot faster than the normal `parse-time-string'.
  16923. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16924. hour and minute fields will be nil if not given."
  16925. (if (string-match org-ts-regexp0 s)
  16926. (list 0
  16927. (if (or (match-beginning 8) (not nodefault))
  16928. (string-to-number (or (match-string 8 s) "0")))
  16929. (if (or (match-beginning 7) (not nodefault))
  16930. (string-to-number (or (match-string 7 s) "0")))
  16931. (string-to-number (match-string 4 s))
  16932. (string-to-number (match-string 3 s))
  16933. (string-to-number (match-string 2 s))
  16934. nil nil nil)
  16935. (make-list 9 0)))
  16936. (defun org-timestamp-up (&optional arg)
  16937. "Increase the date item at the cursor by one.
  16938. If the cursor is on the year, change the year. If it is on the month or
  16939. the day, change that.
  16940. With prefix ARG, change by that many units."
  16941. (interactive "p")
  16942. (org-timestamp-change (prefix-numeric-value arg)))
  16943. (defun org-timestamp-down (&optional arg)
  16944. "Decrease the date item at the cursor by one.
  16945. If the cursor is on the year, change the year. If it is on the month or
  16946. the day, change that.
  16947. With prefix ARG, change by that many units."
  16948. (interactive "p")
  16949. (org-timestamp-change (- (prefix-numeric-value arg))))
  16950. (defun org-timestamp-up-day (&optional arg)
  16951. "Increase the date in the time stamp by one day.
  16952. With prefix ARG, change that many days."
  16953. (interactive "p")
  16954. (if (and (not (org-at-timestamp-p t))
  16955. (org-on-heading-p))
  16956. (org-todo 'up)
  16957. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16958. (defun org-timestamp-down-day (&optional arg)
  16959. "Decrease the date in the time stamp by one day.
  16960. With prefix ARG, change that many days."
  16961. (interactive "p")
  16962. (if (and (not (org-at-timestamp-p t))
  16963. (org-on-heading-p))
  16964. (org-todo 'down)
  16965. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16966. (defsubst org-pos-in-match-range (pos n)
  16967. (and (match-beginning n)
  16968. (<= (match-beginning n) pos)
  16969. (>= (match-end n) pos)))
  16970. (defun org-at-timestamp-p (&optional inactive-ok)
  16971. "Determine if the cursor is in or at a timestamp."
  16972. (interactive)
  16973. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16974. (pos (point))
  16975. (ans (or (looking-at tsr)
  16976. (save-excursion
  16977. (skip-chars-backward "^[<\n\r\t")
  16978. (if (> (point) (point-min)) (backward-char 1))
  16979. (and (looking-at tsr)
  16980. (> (- (match-end 0) pos) -1))))))
  16981. (and ans
  16982. (boundp 'org-ts-what)
  16983. (setq org-ts-what
  16984. (cond
  16985. ((= pos (match-beginning 0)) 'bracket)
  16986. ((= pos (1- (match-end 0))) 'bracket)
  16987. ((org-pos-in-match-range pos 2) 'year)
  16988. ((org-pos-in-match-range pos 3) 'month)
  16989. ((org-pos-in-match-range pos 7) 'hour)
  16990. ((org-pos-in-match-range pos 8) 'minute)
  16991. ((or (org-pos-in-match-range pos 4)
  16992. (org-pos-in-match-range pos 5)) 'day)
  16993. ((and (> pos (or (match-end 8) (match-end 5)))
  16994. (< pos (match-end 0)))
  16995. (- pos (or (match-end 8) (match-end 5))))
  16996. (t 'day))))
  16997. ans))
  16998. (defun org-toggle-timestamp-type ()
  16999. ""
  17000. (interactive)
  17001. (when (org-at-timestamp-p t)
  17002. (save-excursion
  17003. (goto-char (match-beginning 0))
  17004. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  17005. (goto-char (1- (match-end 0)))
  17006. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  17007. (message "Timestamp is now %sactive"
  17008. (if (equal (char-before) ?>) "in" ""))))
  17009. (defun org-timestamp-change (n &optional what)
  17010. "Change the date in the time stamp at point.
  17011. The date will be changed by N times WHAT. WHAT can be `day', `month',
  17012. `year', `minute', `second'. If WHAT is not given, the cursor position
  17013. in the timestamp determines what will be changed."
  17014. (let ((pos (point))
  17015. with-hm inactive
  17016. org-ts-what
  17017. extra
  17018. ts time time0)
  17019. (if (not (org-at-timestamp-p t))
  17020. (error "Not at a timestamp"))
  17021. (if (and (not what) (eq org-ts-what 'bracket))
  17022. (org-toggle-timestamp-type)
  17023. (if (and (not what) (not (eq org-ts-what 'day))
  17024. org-display-custom-times
  17025. (get-text-property (point) 'display)
  17026. (not (get-text-property (1- (point)) 'display)))
  17027. (setq org-ts-what 'day))
  17028. (setq org-ts-what (or what org-ts-what)
  17029. inactive (= (char-after (match-beginning 0)) ?\[)
  17030. ts (match-string 0))
  17031. (replace-match "")
  17032. (if (string-match
  17033. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17034. ts)
  17035. (setq extra (match-string 1 ts)))
  17036. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17037. (setq with-hm t))
  17038. (setq time0 (org-parse-time-string ts))
  17039. (setq time
  17040. (encode-time (or (car time0) 0)
  17041. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17042. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17043. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17044. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17045. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17046. (nthcdr 6 time0)))
  17047. (when (integerp org-ts-what)
  17048. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17049. (if (eq what 'calendar)
  17050. (let ((cal-date (org-get-date-from-calendar)))
  17051. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17052. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17053. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17054. (setcar time0 (or (car time0) 0))
  17055. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17056. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17057. (setq time (apply 'encode-time time0))))
  17058. (setq org-last-changed-timestamp
  17059. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17060. (org-clock-update-time-maybe)
  17061. (goto-char pos)
  17062. ;; Try to recenter the calendar window, if any
  17063. (if (and org-calendar-follow-timestamp-change
  17064. (get-buffer-window "*Calendar*" t)
  17065. (memq org-ts-what '(day month year)))
  17066. (org-recenter-calendar (time-to-days time))))))
  17067. ;; FIXME: does not yet work for lead times
  17068. (defun org-modify-ts-extra (s pos n)
  17069. "Change the different parts of the lead-time and repeat fields in timestamp."
  17070. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17071. ng h m new)
  17072. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17073. (cond
  17074. ((or (org-pos-in-match-range pos 2)
  17075. (org-pos-in-match-range pos 3))
  17076. (setq m (string-to-number (match-string 3 s))
  17077. h (string-to-number (match-string 2 s)))
  17078. (if (org-pos-in-match-range pos 2)
  17079. (setq h (+ h n))
  17080. (setq m (+ m n)))
  17081. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17082. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17083. (setq h (min 24 (max 0 h)))
  17084. (setq ng 1 new (format "-%02d:%02d" h m)))
  17085. ((org-pos-in-match-range pos 6)
  17086. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17087. ((org-pos-in-match-range pos 5)
  17088. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17089. (when ng
  17090. (setq s (concat
  17091. (substring s 0 (match-beginning ng))
  17092. new
  17093. (substring s (match-end ng))))))
  17094. s))
  17095. (defun org-recenter-calendar (date)
  17096. "If the calendar is visible, recenter it to DATE."
  17097. (let* ((win (selected-window))
  17098. (cwin (get-buffer-window "*Calendar*" t))
  17099. (calendar-move-hook nil))
  17100. (when cwin
  17101. (select-window cwin)
  17102. (calendar-goto-date (if (listp date) date
  17103. (calendar-gregorian-from-absolute date)))
  17104. (select-window win))))
  17105. (defun org-goto-calendar (&optional arg)
  17106. "Go to the Emacs calendar at the current date.
  17107. If there is a time stamp in the current line, go to that date.
  17108. A prefix ARG can be used to force the current date."
  17109. (interactive "P")
  17110. (let ((tsr org-ts-regexp) diff
  17111. (calendar-move-hook nil)
  17112. (view-calendar-holidays-initially nil)
  17113. (view-diary-entries-initially nil))
  17114. (if (or (org-at-timestamp-p)
  17115. (save-excursion
  17116. (beginning-of-line 1)
  17117. (looking-at (concat ".*" tsr))))
  17118. (let ((d1 (time-to-days (current-time)))
  17119. (d2 (time-to-days
  17120. (org-time-string-to-time (match-string 1)))))
  17121. (setq diff (- d2 d1))))
  17122. (calendar)
  17123. (calendar-goto-today)
  17124. (if (and diff (not arg)) (calendar-forward-day diff))))
  17125. (defun org-get-date-from-calendar ()
  17126. "Return a list (month day year) of date at point in calendar."
  17127. (with-current-buffer "*Calendar*"
  17128. (save-match-data
  17129. (calendar-cursor-to-date))))
  17130. (defun org-date-from-calendar ()
  17131. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17132. If there is already a time stamp at the cursor position, update it."
  17133. (interactive)
  17134. (if (org-at-timestamp-p t)
  17135. (org-timestamp-change 0 'calendar)
  17136. (let ((cal-date (org-get-date-from-calendar)))
  17137. (org-insert-time-stamp
  17138. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17139. (defvar appt-time-msg-list)
  17140. ;;;###autoload
  17141. (defun org-agenda-to-appt (&optional refresh filter)
  17142. "Activate appointments found in `org-agenda-files'.
  17143. With a \\[universal-argument] prefix, refresh the list of
  17144. appointements.
  17145. If FILTER is t, interactively prompt the user for a regular
  17146. expression, and filter out entries that don't match it.
  17147. If FILTER is a string, use this string as a regular expression
  17148. for filtering entries out.
  17149. FILTER can also be an alist with the car of each cell being
  17150. either 'headline or 'category. For example:
  17151. '((headline \"IMPORTANT\")
  17152. (category \"Work\"))
  17153. will only add headlines containing IMPORTANT or headlines
  17154. belonging to the \"Work\" category."
  17155. (interactive "P")
  17156. (require 'calendar)
  17157. (if refresh (setq appt-time-msg-list nil))
  17158. (if (eq filter t)
  17159. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17160. (let* ((cnt 0) ; count added events
  17161. (org-agenda-new-buffers nil)
  17162. (org-deadline-warning-days 0)
  17163. (today (org-date-to-gregorian
  17164. (time-to-days (current-time))))
  17165. (files (org-agenda-files)) entries file)
  17166. ;; Get all entries which may contain an appt
  17167. (while (setq file (pop files))
  17168. (setq entries
  17169. (append entries
  17170. (org-agenda-get-day-entries
  17171. file today :timestamp :scheduled :deadline))))
  17172. (setq entries (delq nil entries))
  17173. ;; Map thru entries and find if we should filter them out
  17174. (mapc
  17175. (lambda(x)
  17176. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17177. (cat (get-text-property 1 'org-category x))
  17178. (tod (get-text-property 1 'time-of-day x))
  17179. (ok (or (null filter)
  17180. (and (stringp filter) (string-match filter evt))
  17181. (and (listp filter)
  17182. (or (string-match
  17183. (cadr (assoc 'category filter)) cat)
  17184. (string-match
  17185. (cadr (assoc 'headline filter)) evt))))))
  17186. ;; FIXME: Shall we remove text-properties for the appt text?
  17187. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17188. (when (and ok tod)
  17189. (setq tod (number-to-string tod)
  17190. tod (when (string-match
  17191. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17192. (concat (match-string 1 tod) ":"
  17193. (match-string 2 tod))))
  17194. (appt-add tod evt)
  17195. (setq cnt (1+ cnt))))) entries)
  17196. (org-release-buffers org-agenda-new-buffers)
  17197. (if (eq cnt 0)
  17198. (message "No event to add")
  17199. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17200. ;;; The clock for measuring work time.
  17201. (defvar org-mode-line-string "")
  17202. (put 'org-mode-line-string 'risky-local-variable t)
  17203. (defvar org-mode-line-timer nil)
  17204. (defvar org-clock-heading "")
  17205. (defvar org-clock-start-time "")
  17206. (defun org-update-mode-line ()
  17207. (let* ((delta (- (time-to-seconds (current-time))
  17208. (time-to-seconds org-clock-start-time)))
  17209. (h (floor delta 3600))
  17210. (m (floor (- delta (* 3600 h)) 60)))
  17211. (setq org-mode-line-string
  17212. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17213. 'help-echo "Org-mode clock is running"))
  17214. (force-mode-line-update)))
  17215. (defvar org-clock-marker (make-marker)
  17216. "Marker recording the last clock-in.")
  17217. (defvar org-clock-mode-line-entry nil
  17218. "Information for the modeline about the running clock.")
  17219. (defun org-clock-in ()
  17220. "Start the clock on the current item.
  17221. If necessary, clock-out of the currently active clock."
  17222. (interactive)
  17223. (org-clock-out t)
  17224. (let (ts)
  17225. (save-excursion
  17226. (org-back-to-heading t)
  17227. (when (and org-clock-in-switch-to-state
  17228. (not (looking-at (concat outline-regexp "[ \t]*"
  17229. org-clock-in-switch-to-state
  17230. "\\>"))))
  17231. (org-todo org-clock-in-switch-to-state))
  17232. (if (and org-clock-heading-function
  17233. (functionp org-clock-heading-function))
  17234. (setq org-clock-heading (funcall org-clock-heading-function))
  17235. (if (looking-at org-complex-heading-regexp)
  17236. (setq org-clock-heading (match-string 4))
  17237. (setq org-clock-heading "???")))
  17238. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17239. (org-clock-find-position)
  17240. (insert "\n") (backward-char 1)
  17241. (indent-relative)
  17242. (insert org-clock-string " ")
  17243. (setq org-clock-start-time (current-time))
  17244. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17245. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17246. (or global-mode-string (setq global-mode-string '("")))
  17247. (or (memq 'org-mode-line-string global-mode-string)
  17248. (setq global-mode-string
  17249. (append global-mode-string '(org-mode-line-string))))
  17250. (org-update-mode-line)
  17251. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17252. (message "Clock started at %s" ts))))
  17253. (defun org-clock-find-position ()
  17254. "Find the location where the next clock line should be inserted."
  17255. (org-back-to-heading t)
  17256. (catch 'exit
  17257. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17258. (re (concat "^[ \t]*" org-clock-string))
  17259. (cnt 0)
  17260. first last)
  17261. (goto-char beg)
  17262. (when (eobp) (newline) (setq end (max (point) end)))
  17263. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17264. ;; we seem to have a CLOCK drawer, so go there.
  17265. (beginning-of-line 2)
  17266. (throw 'exit t))
  17267. ;; Lets count the CLOCK lines
  17268. (goto-char beg)
  17269. (while (re-search-forward re end t)
  17270. (setq first (or first (match-beginning 0))
  17271. last (match-beginning 0)
  17272. cnt (1+ cnt)))
  17273. (when (and (integerp org-clock-into-drawer)
  17274. (>= (1+ cnt) org-clock-into-drawer))
  17275. ;; Wrap current entries into a new drawer
  17276. (goto-char last)
  17277. (beginning-of-line 2)
  17278. (if (org-at-item-p) (org-end-of-item))
  17279. (insert ":END:\n")
  17280. (beginning-of-line 0)
  17281. (org-indent-line-function)
  17282. (goto-char first)
  17283. (insert ":CLOCK:\n")
  17284. (beginning-of-line 0)
  17285. (org-indent-line-function)
  17286. (org-flag-drawer t)
  17287. (beginning-of-line 2)
  17288. (throw 'exit nil))
  17289. (goto-char beg)
  17290. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17291. (not (equal (match-string 1) org-clock-string)))
  17292. ;; Planning info, skip to after it
  17293. (beginning-of-line 2)
  17294. (or (bolp) (newline)))
  17295. (when (eq t org-clock-into-drawer)
  17296. (insert ":CLOCK:\n:END:\n")
  17297. (beginning-of-line -1)
  17298. (org-indent-line-function)
  17299. (org-flag-drawer t)
  17300. (beginning-of-line 2)
  17301. (org-indent-line-function)))))
  17302. (defun org-clock-out (&optional fail-quietly)
  17303. "Stop the currently running clock.
  17304. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17305. (interactive)
  17306. (catch 'exit
  17307. (if (not (marker-buffer org-clock-marker))
  17308. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17309. (let (ts te s h m)
  17310. (save-excursion
  17311. (set-buffer (marker-buffer org-clock-marker))
  17312. (goto-char org-clock-marker)
  17313. (beginning-of-line 1)
  17314. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17315. (equal (match-string 1) org-clock-string))
  17316. (setq ts (match-string 2))
  17317. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17318. (goto-char (match-end 0))
  17319. (delete-region (point) (point-at-eol))
  17320. (insert "--")
  17321. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17322. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17323. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17324. h (floor (/ s 3600))
  17325. s (- s (* 3600 h))
  17326. m (floor (/ s 60))
  17327. s (- s (* 60 s)))
  17328. (insert " => " (format "%2d:%02d" h m))
  17329. (move-marker org-clock-marker nil)
  17330. (when org-log-note-clock-out
  17331. (org-add-log-maybe 'clock-out))
  17332. (when org-mode-line-timer
  17333. (cancel-timer org-mode-line-timer)
  17334. (setq org-mode-line-timer nil))
  17335. (setq global-mode-string
  17336. (delq 'org-mode-line-string global-mode-string))
  17337. (force-mode-line-update)
  17338. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17339. (defun org-clock-cancel ()
  17340. "Cancel the running clock be removing the start timestamp."
  17341. (interactive)
  17342. (if (not (marker-buffer org-clock-marker))
  17343. (error "No active clock"))
  17344. (save-excursion
  17345. (set-buffer (marker-buffer org-clock-marker))
  17346. (goto-char org-clock-marker)
  17347. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17348. (setq global-mode-string
  17349. (delq 'org-mode-line-string global-mode-string))
  17350. (force-mode-line-update)
  17351. (message "Clock canceled"))
  17352. (defun org-clock-goto (&optional delete-windows)
  17353. "Go to the currently clocked-in entry."
  17354. (interactive "P")
  17355. (if (not (marker-buffer org-clock-marker))
  17356. (error "No active clock"))
  17357. (switch-to-buffer-other-window
  17358. (marker-buffer org-clock-marker))
  17359. (if delete-windows (delete-other-windows))
  17360. (goto-char org-clock-marker)
  17361. (org-show-entry)
  17362. (org-back-to-heading)
  17363. (recenter))
  17364. (defvar org-clock-file-total-minutes nil
  17365. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17366. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17367. (defun org-clock-sum (&optional tstart tend)
  17368. "Sum the times for each subtree.
  17369. Puts the resulting times in minutes as a text property on each headline."
  17370. (interactive)
  17371. (let* ((bmp (buffer-modified-p))
  17372. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17373. org-clock-string
  17374. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17375. (lmax 30)
  17376. (ltimes (make-vector lmax 0))
  17377. (t1 0)
  17378. (level 0)
  17379. ts te dt
  17380. time)
  17381. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17382. (save-excursion
  17383. (goto-char (point-max))
  17384. (while (re-search-backward re nil t)
  17385. (cond
  17386. ((match-end 2)
  17387. ;; Two time stamps
  17388. (setq ts (match-string 2)
  17389. te (match-string 3)
  17390. ts (time-to-seconds
  17391. (apply 'encode-time (org-parse-time-string ts)))
  17392. te (time-to-seconds
  17393. (apply 'encode-time (org-parse-time-string te)))
  17394. ts (if tstart (max ts tstart) ts)
  17395. te (if tend (min te tend) te)
  17396. dt (- te ts)
  17397. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17398. ((match-end 4)
  17399. ;; A naket time
  17400. (setq t1 (+ t1 (string-to-number (match-string 5))
  17401. (* 60 (string-to-number (match-string 4))))))
  17402. (t ;; A headline
  17403. (setq level (- (match-end 1) (match-beginning 1)))
  17404. (when (or (> t1 0) (> (aref ltimes level) 0))
  17405. (loop for l from 0 to level do
  17406. (aset ltimes l (+ (aref ltimes l) t1)))
  17407. (setq t1 0 time (aref ltimes level))
  17408. (loop for l from level to (1- lmax) do
  17409. (aset ltimes l 0))
  17410. (goto-char (match-beginning 0))
  17411. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17412. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17413. (set-buffer-modified-p bmp)))
  17414. (defun org-clock-display (&optional total-only)
  17415. "Show subtree times in the entire buffer.
  17416. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17417. in the echo area."
  17418. (interactive)
  17419. (org-remove-clock-overlays)
  17420. (let (time h m p)
  17421. (org-clock-sum)
  17422. (unless total-only
  17423. (save-excursion
  17424. (goto-char (point-min))
  17425. (while (or (and (equal (setq p (point)) (point-min))
  17426. (get-text-property p :org-clock-minutes))
  17427. (setq p (next-single-property-change
  17428. (point) :org-clock-minutes)))
  17429. (goto-char p)
  17430. (when (setq time (get-text-property p :org-clock-minutes))
  17431. (org-put-clock-overlay time (funcall outline-level))))
  17432. (setq h (/ org-clock-file-total-minutes 60)
  17433. m (- org-clock-file-total-minutes (* 60 h)))
  17434. ;; Arrange to remove the overlays upon next change.
  17435. (when org-remove-highlights-with-change
  17436. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17437. nil 'local))))
  17438. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17439. (defvar org-clock-overlays nil)
  17440. (make-variable-buffer-local 'org-clock-overlays)
  17441. (defun org-put-clock-overlay (time &optional level)
  17442. "Put an overlays on the current line, displaying TIME.
  17443. If LEVEL is given, prefix time with a corresponding number of stars.
  17444. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17445. will be easy to remove."
  17446. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17447. (l (if level (org-get-legal-level level 0) 0))
  17448. (off 0)
  17449. ov tx)
  17450. (move-to-column c)
  17451. (unless (eolp) (skip-chars-backward "^ \t"))
  17452. (skip-chars-backward " \t")
  17453. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17454. tx (concat (buffer-substring (1- (point)) (point))
  17455. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17456. (org-add-props (format "%s %2d:%02d%s"
  17457. (make-string l ?*) h m
  17458. (make-string (- 16 l) ?\ ))
  17459. '(face secondary-selection))
  17460. ""))
  17461. (if (not (featurep 'xemacs))
  17462. (org-overlay-put ov 'display tx)
  17463. (org-overlay-put ov 'invisible t)
  17464. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17465. (push ov org-clock-overlays)))
  17466. (defun org-remove-clock-overlays (&optional beg end noremove)
  17467. "Remove the occur highlights from the buffer.
  17468. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17469. from the `before-change-functions' in the current buffer."
  17470. (interactive)
  17471. (unless org-inhibit-highlight-removal
  17472. (mapc 'org-delete-overlay org-clock-overlays)
  17473. (setq org-clock-overlays nil)
  17474. (unless noremove
  17475. (remove-hook 'before-change-functions
  17476. 'org-remove-clock-overlays 'local))))
  17477. (defun org-clock-out-if-current ()
  17478. "Clock out if the current entry contains the running clock.
  17479. This is used to stop the clock after a TODO entry is marked DONE,
  17480. and is only done if the variable `org-clock-out-when-done' is not nil."
  17481. (when (and org-clock-out-when-done
  17482. (member state org-done-keywords)
  17483. (equal (marker-buffer org-clock-marker) (current-buffer))
  17484. (< (point) org-clock-marker)
  17485. (> (save-excursion (outline-next-heading) (point))
  17486. org-clock-marker))
  17487. ;; Clock out, but don't accept a logging message for this.
  17488. (let ((org-log-note-clock-out nil))
  17489. (org-clock-out))))
  17490. (add-hook 'org-after-todo-state-change-hook
  17491. 'org-clock-out-if-current)
  17492. (defun org-check-running-clock ()
  17493. "Check if the current buffer contains the running clock.
  17494. If yes, offer to stop it and to save the buffer with the changes."
  17495. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17496. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17497. (buffer-name))))
  17498. (org-clock-out)
  17499. (when (y-or-n-p "Save changed buffer?")
  17500. (save-buffer))))
  17501. (defun org-clock-report (&optional arg)
  17502. "Create a table containing a report about clocked time.
  17503. If the cursor is inside an existing clocktable block, then the table
  17504. will be updated. If not, a new clocktable will be inserted.
  17505. When called with a prefix argument, move to the first clock table in the
  17506. buffer and update it."
  17507. (interactive "P")
  17508. (org-remove-clock-overlays)
  17509. (when arg
  17510. (org-find-dblock "clocktable")
  17511. (org-show-entry))
  17512. (if (org-in-clocktable-p)
  17513. (goto-char (org-in-clocktable-p))
  17514. (org-create-dblock (list :name "clocktable"
  17515. :maxlevel 2 :scope 'file)))
  17516. (org-update-dblock))
  17517. (defun org-in-clocktable-p ()
  17518. "Check if the cursor is in a clocktable."
  17519. (let ((pos (point)) start)
  17520. (save-excursion
  17521. (end-of-line 1)
  17522. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17523. (setq start (match-beginning 0))
  17524. (re-search-forward "^#\\+END:.*" nil t)
  17525. (>= (match-end 0) pos)
  17526. start))))
  17527. (defun org-clock-update-time-maybe ()
  17528. "If this is a CLOCK line, update it and return t.
  17529. Otherwise, return nil."
  17530. (interactive)
  17531. (save-excursion
  17532. (beginning-of-line 1)
  17533. (skip-chars-forward " \t")
  17534. (when (looking-at org-clock-string)
  17535. (let ((re (concat "[ \t]*" org-clock-string
  17536. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17537. "\\([ \t]*=>.*\\)?"))
  17538. ts te h m s)
  17539. (if (not (looking-at re))
  17540. nil
  17541. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17542. (end-of-line 1)
  17543. (setq ts (match-string 1)
  17544. te (match-string 2))
  17545. (setq s (- (time-to-seconds
  17546. (apply 'encode-time (org-parse-time-string te)))
  17547. (time-to-seconds
  17548. (apply 'encode-time (org-parse-time-string ts))))
  17549. h (floor (/ s 3600))
  17550. s (- s (* 3600 h))
  17551. m (floor (/ s 60))
  17552. s (- s (* 60 s)))
  17553. (insert " => " (format "%2d:%02d" h m))
  17554. t)))))
  17555. (defun org-clock-special-range (key &optional time as-strings)
  17556. "Return two times bordering a special time range.
  17557. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17558. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17559. A week starts Monday 0:00 and ends Sunday 24:00.
  17560. The range is determined relative to TIME. TIME defaults to the current time.
  17561. The return value is a cons cell with two internal times like the ones
  17562. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17563. the returned times will be formatted strings."
  17564. (let* ((tm (decode-time (or time (current-time))))
  17565. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17566. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17567. (dow (nth 6 tm))
  17568. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17569. (cond
  17570. ((eq key 'today)
  17571. (setq h 0 m 0 h1 24 m1 0))
  17572. ((eq key 'yesterday)
  17573. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17574. ((eq key 'thisweek)
  17575. (setq diff (if (= dow 0) 6 (1- dow))
  17576. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17577. ((eq key 'lastweek)
  17578. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17579. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17580. ((eq key 'thismonth)
  17581. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17582. ((eq key 'lastmonth)
  17583. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17584. ((eq key 'thisyear)
  17585. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17586. ((eq key 'lastyear)
  17587. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17588. (t (error "No such time block %s" key)))
  17589. (setq ts (encode-time s m h d month y)
  17590. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17591. (or d1 d) (or month1 month) (or y1 y)))
  17592. (setq fm (cdr org-time-stamp-formats))
  17593. (if as-strings
  17594. (cons (format-time-string fm ts) (format-time-string fm te))
  17595. (cons ts te))))
  17596. (defun org-dblock-write:clocktable (params)
  17597. "Write the standard clocktable."
  17598. (catch 'exit
  17599. (let* ((hlchars '((1 . "*") (2 . "/")))
  17600. (ins (make-marker))
  17601. (total-time nil)
  17602. (scope (plist-get params :scope))
  17603. (tostring (plist-get params :tostring))
  17604. (multifile (plist-get params :multifile))
  17605. (header (plist-get params :header))
  17606. (maxlevel (or (plist-get params :maxlevel) 3))
  17607. (step (plist-get params :step))
  17608. (emph (plist-get params :emphasize))
  17609. (ts (plist-get params :tstart))
  17610. (te (plist-get params :tend))
  17611. (block (plist-get params :block))
  17612. ipos time h m p level hlc hdl
  17613. cc beg end pos tbl)
  17614. (when step
  17615. (org-clocktable-steps params)
  17616. (throw 'exit nil))
  17617. (when block
  17618. (setq cc (org-clock-special-range block nil t)
  17619. ts (car cc) te (cdr cc)))
  17620. (if ts (setq ts (time-to-seconds
  17621. (apply 'encode-time (org-parse-time-string ts)))))
  17622. (if te (setq te (time-to-seconds
  17623. (apply 'encode-time (org-parse-time-string te)))))
  17624. (move-marker ins (point))
  17625. (setq ipos (point))
  17626. ;; Get the right scope
  17627. (setq pos (point))
  17628. (save-restriction
  17629. (cond
  17630. ((not scope))
  17631. ((eq scope 'file) (widen))
  17632. ((eq scope 'subtree) (org-narrow-to-subtree))
  17633. ((eq scope 'tree)
  17634. (while (org-up-heading-safe))
  17635. (org-narrow-to-subtree))
  17636. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17637. (symbol-name scope)))
  17638. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17639. (catch 'exit
  17640. (while (org-up-heading-safe)
  17641. (looking-at outline-regexp)
  17642. (if (<= (org-reduced-level (funcall outline-level)) level)
  17643. (throw 'exit nil))))
  17644. (org-narrow-to-subtree))
  17645. ((or (listp scope) (eq scope 'agenda))
  17646. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17647. (scope 'agenda)
  17648. (p1 (copy-sequence params))
  17649. file)
  17650. (plist-put p1 :tostring t)
  17651. (plist-put p1 :multifile t)
  17652. (plist-put p1 :scope 'file)
  17653. (org-prepare-agenda-buffers files)
  17654. (while (setq file (pop files))
  17655. (with-current-buffer (find-buffer-visiting file)
  17656. (push (org-clocktable-add-file
  17657. file (org-dblock-write:clocktable p1)) tbl)
  17658. (setq total-time (+ (or total-time 0)
  17659. org-clock-file-total-minutes)))))))
  17660. (goto-char pos)
  17661. (unless (eq scope 'agenda)
  17662. (org-clock-sum ts te)
  17663. (goto-char (point-min))
  17664. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17665. (goto-char p)
  17666. (when (setq time (get-text-property p :org-clock-minutes))
  17667. (save-excursion
  17668. (beginning-of-line 1)
  17669. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17670. (setq level (org-reduced-level
  17671. (- (match-end 1) (match-beginning 1))))
  17672. (<= level maxlevel))
  17673. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17674. hdl (match-string 2)
  17675. h (/ time 60)
  17676. m (- time (* 60 h)))
  17677. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17678. (push (concat
  17679. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17680. (make-string (1- level) ?|)
  17681. hlc (format "%d:%02d" h m) hlc
  17682. " |") tbl))))))
  17683. (setq tbl (nreverse tbl))
  17684. (if tostring
  17685. (if tbl (mapconcat 'identity tbl "\n") nil)
  17686. (goto-char ins)
  17687. (insert-before-markers
  17688. (or header
  17689. (concat
  17690. "Clock summary at ["
  17691. (substring
  17692. (format-time-string (cdr org-time-stamp-formats))
  17693. 1 -1)
  17694. "]."
  17695. (if block
  17696. (format " Considered range is /%s/." block)
  17697. "")
  17698. "\n\n"))
  17699. (if (eq scope 'agenda) "|File" "")
  17700. "|L|Headline|Time|\n")
  17701. (setq total-time (or total-time org-clock-file-total-minutes)
  17702. h (/ total-time 60)
  17703. m (- total-time (* 60 h)))
  17704. (insert-before-markers
  17705. "|-\n|"
  17706. (if (eq scope 'agenda) "|" "")
  17707. "|"
  17708. "*Total time*| "
  17709. (format "*%d:%02d*" h m)
  17710. "|\n|-\n")
  17711. (setq tbl (delq nil tbl))
  17712. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17713. (equal (substring (car tbl) 0 2) "|-"))
  17714. (pop tbl))
  17715. (insert-before-markers (mapconcat
  17716. 'identity (delq nil tbl)
  17717. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17718. (backward-delete-char 1)
  17719. (goto-char ipos)
  17720. (skip-chars-forward "^|")
  17721. (org-table-align))))))
  17722. (defun org-clocktable-steps (params)
  17723. (let* ((p1 (copy-sequence params))
  17724. (ts (plist-get p1 :tstart))
  17725. (te (plist-get p1 :tend))
  17726. (step0 (plist-get p1 :step))
  17727. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17728. (block (plist-get p1 :block))
  17729. cc)
  17730. (when block
  17731. (setq cc (org-clock-special-range block nil t)
  17732. ts (car cc) te (cdr cc)))
  17733. (if ts (setq ts (time-to-seconds
  17734. (apply 'encode-time (org-parse-time-string ts)))))
  17735. (if te (setq te (time-to-seconds
  17736. (apply 'encode-time (org-parse-time-string te)))))
  17737. (plist-put p1 :header "")
  17738. (plist-put p1 :step nil)
  17739. (plist-put p1 :block nil)
  17740. (while (< ts te)
  17741. (or (bolp) (insert "\n"))
  17742. (plist-put p1 :tstart (format-time-string
  17743. (car org-time-stamp-formats)
  17744. (seconds-to-time ts)))
  17745. (plist-put p1 :tend (format-time-string
  17746. (car org-time-stamp-formats)
  17747. (seconds-to-time (setq ts (+ ts step)))))
  17748. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17749. (plist-get p1 :tstart) "\n")
  17750. (org-dblock-write:clocktable p1)
  17751. (re-search-forward "#\\+END:")
  17752. (end-of-line 0))))
  17753. (defun org-clocktable-add-file (file table)
  17754. (if table
  17755. (let ((lines (org-split-string table "\n"))
  17756. (ff (file-name-nondirectory file)))
  17757. (mapconcat 'identity
  17758. (mapcar (lambda (x)
  17759. (if (string-match org-table-dataline-regexp x)
  17760. (concat "|" ff x)
  17761. x))
  17762. lines)
  17763. "\n"))))
  17764. ;; FIXME: I don't think anybody uses this, ask David
  17765. (defun org-collect-clock-time-entries ()
  17766. "Return an internal list with clocking information.
  17767. This list has one entry for each CLOCK interval.
  17768. FIXME: describe the elements."
  17769. (interactive)
  17770. (let ((re (concat "^[ \t]*" org-clock-string
  17771. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17772. rtn beg end next cont level title total closedp leafp
  17773. clockpos titlepos h m donep)
  17774. (save-excursion
  17775. (org-clock-sum)
  17776. (goto-char (point-min))
  17777. (while (re-search-forward re nil t)
  17778. (setq clockpos (match-beginning 0)
  17779. beg (match-string 1) end (match-string 2)
  17780. cont (match-end 0))
  17781. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17782. end (apply 'encode-time (org-parse-time-string end)))
  17783. (org-back-to-heading t)
  17784. (setq donep (org-entry-is-done-p))
  17785. (setq titlepos (point)
  17786. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17787. h (/ total 60) m (- total (* 60 h))
  17788. total (cons h m))
  17789. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17790. (setq level (- (match-end 1) (match-beginning 1))
  17791. title (org-match-string-no-properties 2))
  17792. (save-excursion (outline-next-heading) (setq next (point)))
  17793. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17794. (goto-char next)
  17795. (setq leafp (and (looking-at "^\\*+ ")
  17796. (<= (- (match-end 0) (point)) level)))
  17797. (push (list beg end clockpos closedp donep
  17798. total title titlepos level leafp)
  17799. rtn)
  17800. (goto-char cont)))
  17801. (nreverse rtn)))
  17802. ;;;; Agenda, and Diary Integration
  17803. ;;; Define the Org-agenda-mode
  17804. (defvar org-agenda-mode-map (make-sparse-keymap)
  17805. "Keymap for `org-agenda-mode'.")
  17806. (defvar org-agenda-menu) ; defined later in this file.
  17807. (defvar org-agenda-follow-mode nil)
  17808. (defvar org-agenda-show-log nil)
  17809. (defvar org-agenda-redo-command nil)
  17810. (defvar org-agenda-query-string nil)
  17811. (defvar org-agenda-mode-hook nil)
  17812. (defvar org-agenda-type nil)
  17813. (defvar org-agenda-force-single-file nil)
  17814. (defun org-agenda-mode ()
  17815. "Mode for time-sorted view on action items in Org-mode files.
  17816. The following commands are available:
  17817. \\{org-agenda-mode-map}"
  17818. (interactive)
  17819. (kill-all-local-variables)
  17820. (setq org-agenda-undo-list nil
  17821. org-agenda-pending-undo-list nil)
  17822. (setq major-mode 'org-agenda-mode)
  17823. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17824. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17825. (setq mode-name "Org-Agenda")
  17826. (use-local-map org-agenda-mode-map)
  17827. (easy-menu-add org-agenda-menu)
  17828. (if org-startup-truncated (setq truncate-lines t))
  17829. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17830. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17831. ;; Make sure properties are removed when copying text
  17832. (when (boundp 'buffer-substring-filters)
  17833. (org-set-local 'buffer-substring-filters
  17834. (cons (lambda (x)
  17835. (set-text-properties 0 (length x) nil x) x)
  17836. buffer-substring-filters)))
  17837. (unless org-agenda-keep-modes
  17838. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17839. org-agenda-show-log nil))
  17840. (easy-menu-change
  17841. '("Agenda") "Agenda Files"
  17842. (append
  17843. (list
  17844. (vector
  17845. (if (get 'org-agenda-files 'org-restrict)
  17846. "Restricted to single file"
  17847. "Edit File List")
  17848. '(org-edit-agenda-file-list)
  17849. (not (get 'org-agenda-files 'org-restrict)))
  17850. "--")
  17851. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17852. (org-agenda-set-mode-name)
  17853. (apply
  17854. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17855. (list 'org-agenda-mode-hook)))
  17856. (substitute-key-definition 'undo 'org-agenda-undo
  17857. org-agenda-mode-map global-map)
  17858. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17859. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17860. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17861. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17862. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17863. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17864. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17865. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17866. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17867. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17868. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17869. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17870. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17871. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17872. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17873. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17874. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17875. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17876. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17877. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17878. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17879. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17880. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17881. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17882. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17883. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17884. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17885. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17886. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17887. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17888. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17889. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17890. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17891. (while l (org-defkey org-agenda-mode-map
  17892. (int-to-string (pop l)) 'digit-argument)))
  17893. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17894. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17895. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17896. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17897. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17898. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17899. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17900. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17901. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17902. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17903. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17904. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17905. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17906. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17907. (org-defkey org-agenda-mode-map "n" 'next-line)
  17908. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17909. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17910. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17911. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17912. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17913. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17914. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17915. (eval-after-load "calendar"
  17916. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17917. 'org-calendar-goto-agenda))
  17918. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17919. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17920. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17921. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17922. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17923. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17924. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17925. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17926. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17927. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17928. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17929. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17930. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17931. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17932. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17933. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17934. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17935. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17936. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17937. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17938. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17939. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17940. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17941. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17942. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17943. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17944. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17945. "Local keymap for agenda entries from Org-mode.")
  17946. (org-defkey org-agenda-keymap
  17947. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17948. (org-defkey org-agenda-keymap
  17949. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17950. (when org-agenda-mouse-1-follows-link
  17951. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17952. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17953. '("Agenda"
  17954. ("Agenda Files")
  17955. "--"
  17956. ["Show" org-agenda-show t]
  17957. ["Go To (other window)" org-agenda-goto t]
  17958. ["Go To (this window)" org-agenda-switch-to t]
  17959. ["Follow Mode" org-agenda-follow-mode
  17960. :style toggle :selected org-agenda-follow-mode :active t]
  17961. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17962. "--"
  17963. ["Cycle TODO" org-agenda-todo t]
  17964. ["Archive subtree" org-agenda-archive t]
  17965. ["Delete subtree" org-agenda-kill t]
  17966. "--"
  17967. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17968. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17969. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17970. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17971. "--"
  17972. ("Tags and Properties"
  17973. ["Show all Tags" org-agenda-show-tags t]
  17974. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17975. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17976. "--"
  17977. ["Column View" org-columns t])
  17978. ("Date/Schedule"
  17979. ["Schedule" org-agenda-schedule t]
  17980. ["Set Deadline" org-agenda-deadline t]
  17981. "--"
  17982. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17983. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17984. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17985. ("Clock"
  17986. ["Clock in" org-agenda-clock-in t]
  17987. ["Clock out" org-agenda-clock-out t]
  17988. ["Clock cancel" org-agenda-clock-cancel t]
  17989. ["Goto running clock" org-clock-goto t])
  17990. ("Priority"
  17991. ["Set Priority" org-agenda-priority t]
  17992. ["Increase Priority" org-agenda-priority-up t]
  17993. ["Decrease Priority" org-agenda-priority-down t]
  17994. ["Show Priority" org-agenda-show-priority t])
  17995. ("Calendar/Diary"
  17996. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17997. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17998. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17999. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  18000. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  18001. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  18002. "--"
  18003. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  18004. "--"
  18005. ("View"
  18006. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  18007. :style radio :selected (equal org-agenda-ndays 1)]
  18008. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  18009. :style radio :selected (equal org-agenda-ndays 7)]
  18010. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  18011. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  18012. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  18013. :style radio :selected (member org-agenda-ndays '(365 366))]
  18014. "--"
  18015. ["Show Logbook entries" org-agenda-log-mode
  18016. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  18017. ["Include Diary" org-agenda-toggle-diary
  18018. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  18019. ["Use Time Grid" org-agenda-toggle-time-grid
  18020. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  18021. ["Write view to file" org-write-agenda t]
  18022. ["Rebuild buffer" org-agenda-redo t]
  18023. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18024. "--"
  18025. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18026. "--"
  18027. ["Quit" org-agenda-quit t]
  18028. ["Exit and Release Buffers" org-agenda-exit t]
  18029. ))
  18030. ;;; Agenda undo
  18031. (defvar org-agenda-allow-remote-undo t
  18032. "Non-nil means, allow remote undo from the agenda buffer.")
  18033. (defvar org-agenda-undo-list nil
  18034. "List of undoable operations in the agenda since last refresh.")
  18035. (defvar org-agenda-undo-has-started-in nil
  18036. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18037. (defvar org-agenda-pending-undo-list nil
  18038. "In a series of undo commands, this is the list of remaning undo items.")
  18039. (defmacro org-if-unprotected (&rest body)
  18040. "Execute BODY if there is no `org-protected' text property at point."
  18041. (declare (debug t))
  18042. `(unless (get-text-property (point) 'org-protected)
  18043. ,@body))
  18044. (defmacro org-with-remote-undo (_buffer &rest _body)
  18045. "Execute BODY while recording undo information in two buffers."
  18046. (declare (indent 1) (debug t))
  18047. `(let ((_cline (org-current-line))
  18048. (_cmd this-command)
  18049. (_buf1 (current-buffer))
  18050. (_buf2 ,_buffer)
  18051. (_undo1 buffer-undo-list)
  18052. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18053. _c1 _c2)
  18054. ,@_body
  18055. (when org-agenda-allow-remote-undo
  18056. (setq _c1 (org-verify-change-for-undo
  18057. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18058. _c2 (org-verify-change-for-undo
  18059. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18060. (when (or _c1 _c2)
  18061. ;; make sure there are undo boundaries
  18062. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18063. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18064. ;; remember which buffer to undo
  18065. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18066. org-agenda-undo-list)))))
  18067. (defun org-agenda-undo ()
  18068. "Undo a remote editing step in the agenda.
  18069. This undoes changes both in the agenda buffer and in the remote buffer
  18070. that have been changed along."
  18071. (interactive)
  18072. (or org-agenda-allow-remote-undo
  18073. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18074. (if (not (eq this-command last-command))
  18075. (setq org-agenda-undo-has-started-in nil
  18076. org-agenda-pending-undo-list org-agenda-undo-list))
  18077. (if (not org-agenda-pending-undo-list)
  18078. (error "No further undo information"))
  18079. (let* ((entry (pop org-agenda-pending-undo-list))
  18080. buf line cmd rembuf)
  18081. (setq cmd (pop entry) line (pop entry))
  18082. (setq rembuf (nth 2 entry))
  18083. (org-with-remote-undo rembuf
  18084. (while (bufferp (setq buf (pop entry)))
  18085. (if (pop entry)
  18086. (with-current-buffer buf
  18087. (let ((last-undo-buffer buf)
  18088. (inhibit-read-only t))
  18089. (unless (memq buf org-agenda-undo-has-started-in)
  18090. (push buf org-agenda-undo-has-started-in)
  18091. (make-local-variable 'pending-undo-list)
  18092. (undo-start))
  18093. (while (and pending-undo-list
  18094. (listp pending-undo-list)
  18095. (not (car pending-undo-list)))
  18096. (pop pending-undo-list))
  18097. (undo-more 1))))))
  18098. (goto-line line)
  18099. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18100. (defun org-verify-change-for-undo (l1 l2)
  18101. "Verify that a real change occurred between the undo lists L1 and L2."
  18102. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18103. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18104. (not (eq l1 l2)))
  18105. ;;; Agenda dispatch
  18106. (defvar org-agenda-restrict nil)
  18107. (defvar org-agenda-restrict-begin (make-marker))
  18108. (defvar org-agenda-restrict-end (make-marker))
  18109. (defvar org-agenda-last-dispatch-buffer nil)
  18110. (defvar org-agenda-overriding-restriction nil)
  18111. ;;;###autoload
  18112. (defun org-agenda (arg &optional keys restriction)
  18113. "Dispatch agenda commands to collect entries to the agenda buffer.
  18114. Prompts for a command to execute. Any prefix arg will be passed
  18115. on to the selected command. The default selections are:
  18116. a Call `org-agenda-list' to display the agenda for current day or week.
  18117. t Call `org-todo-list' to display the global todo list.
  18118. T Call `org-todo-list' to display the global todo list, select only
  18119. entries with a specific TODO keyword (the user gets a prompt).
  18120. m Call `org-tags-view' to display headlines with tags matching
  18121. a condition (the user is prompted for the condition).
  18122. M Like `m', but select only TODO entries, no ordinary headlines.
  18123. L Create a timeline for the current buffer.
  18124. e Export views to associated files.
  18125. More commands can be added by configuring the variable
  18126. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18127. searches can be pre-defined in this way.
  18128. If the current buffer is in Org-mode and visiting a file, you can also
  18129. first press `<' once to indicate that the agenda should be temporarily
  18130. \(until the next use of \\[org-agenda]) restricted to the current file.
  18131. Pressing `<' twice means to restrict to the current subtree or region
  18132. \(if active)."
  18133. (interactive "P")
  18134. (catch 'exit
  18135. (let* ((prefix-descriptions nil)
  18136. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18137. (org-agenda-custom-commands
  18138. ;; normalize different versions
  18139. (delq nil
  18140. (mapcar
  18141. (lambda (x)
  18142. (cond ((stringp (cdr x))
  18143. (push x prefix-descriptions)
  18144. nil)
  18145. ((stringp (nth 1 x)) x)
  18146. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18147. (t (cons (car x) (cons "" (cdr x))))))
  18148. org-agenda-custom-commands)))
  18149. (buf (current-buffer))
  18150. (bfn (buffer-file-name (buffer-base-buffer)))
  18151. entry key type match lprops ans)
  18152. ;; Turn off restriction unless there is an overriding one
  18153. (unless org-agenda-overriding-restriction
  18154. (put 'org-agenda-files 'org-restrict nil)
  18155. (setq org-agenda-restrict nil)
  18156. (move-marker org-agenda-restrict-begin nil)
  18157. (move-marker org-agenda-restrict-end nil))
  18158. ;; Delete old local properties
  18159. (put 'org-agenda-redo-command 'org-lprops nil)
  18160. ;; Remember where this call originated
  18161. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18162. (unless keys
  18163. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18164. keys (car ans)
  18165. restriction (cdr ans)))
  18166. ;; Estabish the restriction, if any
  18167. (when (and (not org-agenda-overriding-restriction) restriction)
  18168. (put 'org-agenda-files 'org-restrict (list bfn))
  18169. (cond
  18170. ((eq restriction 'region)
  18171. (setq org-agenda-restrict t)
  18172. (move-marker org-agenda-restrict-begin (region-beginning))
  18173. (move-marker org-agenda-restrict-end (region-end)))
  18174. ((eq restriction 'subtree)
  18175. (save-excursion
  18176. (setq org-agenda-restrict t)
  18177. (org-back-to-heading t)
  18178. (move-marker org-agenda-restrict-begin (point))
  18179. (move-marker org-agenda-restrict-end
  18180. (progn (org-end-of-subtree t)))))))
  18181. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18182. ;; For example the todo list should not need it (but does...)
  18183. (cond
  18184. ((setq entry (assoc keys org-agenda-custom-commands))
  18185. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18186. (progn
  18187. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18188. (put 'org-agenda-redo-command 'org-lprops lprops)
  18189. (cond
  18190. ((eq type 'agenda)
  18191. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18192. ((eq type 'alltodo)
  18193. (org-let lprops '(org-todo-list current-prefix-arg)))
  18194. ((eq type 'search)
  18195. (org-let lprops '(org-search-view current-prefix-arg match)))
  18196. ((eq type 'stuck)
  18197. (org-let lprops '(org-agenda-list-stuck-projects
  18198. current-prefix-arg)))
  18199. ((eq type 'tags)
  18200. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18201. ((eq type 'tags-todo)
  18202. (org-let lprops '(org-tags-view '(4) match)))
  18203. ((eq type 'todo)
  18204. (org-let lprops '(org-todo-list match)))
  18205. ((eq type 'tags-tree)
  18206. (org-check-for-org-mode)
  18207. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18208. ((eq type 'todo-tree)
  18209. (org-check-for-org-mode)
  18210. (org-let lprops
  18211. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18212. (regexp-quote match) "\\>"))))
  18213. ((eq type 'occur-tree)
  18214. (org-check-for-org-mode)
  18215. (org-let lprops '(org-occur match)))
  18216. ((functionp type)
  18217. (org-let lprops '(funcall type match)))
  18218. ((fboundp type)
  18219. (org-let lprops '(funcall type match)))
  18220. (t (error "Invalid custom agenda command type %s" type))))
  18221. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18222. ((equal keys "C")
  18223. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18224. (customize-variable 'org-agenda-custom-commands))
  18225. ((equal keys "a") (call-interactively 'org-agenda-list))
  18226. ((equal keys "s") (call-interactively 'org-search-view))
  18227. ((equal keys "t") (call-interactively 'org-todo-list))
  18228. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18229. ((equal keys "m") (call-interactively 'org-tags-view))
  18230. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18231. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18232. ((equal keys "L")
  18233. (unless (org-mode-p)
  18234. (error "This is not an Org-mode file"))
  18235. (unless restriction
  18236. (put 'org-agenda-files 'org-restrict (list bfn))
  18237. (org-call-with-arg 'org-timeline arg)))
  18238. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18239. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18240. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18241. (t (error "Invalid agenda key"))))))
  18242. (defun org-agenda-normalize-custom-commands (cmds)
  18243. (delq nil
  18244. (mapcar
  18245. (lambda (x)
  18246. (cond ((stringp (cdr x)) nil)
  18247. ((stringp (nth 1 x)) x)
  18248. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18249. (t (cons (car x) (cons "" (cdr x))))))
  18250. cmds)))
  18251. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18252. "The user interface for selecting an agenda command."
  18253. (catch 'exit
  18254. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18255. (restrict-ok (and bfn (org-mode-p)))
  18256. (region-p (org-region-active-p))
  18257. (custom org-agenda-custom-commands)
  18258. (selstring "")
  18259. restriction second-time
  18260. c entry key type match prefixes rmheader header-end custom1 desc)
  18261. (save-window-excursion
  18262. (delete-other-windows)
  18263. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18264. (erase-buffer)
  18265. (insert (eval-when-compile
  18266. (let ((header
  18267. "
  18268. Press key for an agenda command: < Buffer,subtree/region restriction
  18269. -------------------------------- > Remove restriction
  18270. a Agenda for current week or day e Export agenda views
  18271. t List of all TODO entries T Entries with special TODO kwd
  18272. m Match a TAGS query M Like m, but only TODO entries
  18273. L Timeline for current buffer # List stuck projects (!=configure)
  18274. s Search for keywords C Configure custom agenda commands
  18275. / Multi-occur
  18276. ")
  18277. (start 0))
  18278. (while (string-match
  18279. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18280. header start)
  18281. (setq start (match-end 0))
  18282. (add-text-properties (match-beginning 2) (match-end 2)
  18283. '(face bold) header))
  18284. header)))
  18285. (setq header-end (move-marker (make-marker) (point)))
  18286. (while t
  18287. (setq custom1 custom)
  18288. (when (eq rmheader t)
  18289. (goto-line 1)
  18290. (re-search-forward ":" nil t)
  18291. (delete-region (match-end 0) (point-at-eol))
  18292. (forward-char 1)
  18293. (looking-at "-+")
  18294. (delete-region (match-end 0) (point-at-eol))
  18295. (move-marker header-end (match-end 0)))
  18296. (goto-char header-end)
  18297. (delete-region (point) (point-max))
  18298. (while (setq entry (pop custom1))
  18299. (setq key (car entry) desc (nth 1 entry)
  18300. type (nth 2 entry) match (nth 3 entry))
  18301. (if (> (length key) 1)
  18302. (add-to-list 'prefixes (string-to-char key))
  18303. (insert
  18304. (format
  18305. "\n%-4s%-14s: %s"
  18306. (org-add-props (copy-sequence key)
  18307. '(face bold))
  18308. (cond
  18309. ((string-match "\\S-" desc) desc)
  18310. ((eq type 'agenda) "Agenda for current week or day")
  18311. ((eq type 'alltodo) "List of all TODO entries")
  18312. ((eq type 'search) "Word search")
  18313. ((eq type 'stuck) "List of stuck projects")
  18314. ((eq type 'todo) "TODO keyword")
  18315. ((eq type 'tags) "Tags query")
  18316. ((eq type 'tags-todo) "Tags (TODO)")
  18317. ((eq type 'tags-tree) "Tags tree")
  18318. ((eq type 'todo-tree) "TODO kwd tree")
  18319. ((eq type 'occur-tree) "Occur tree")
  18320. ((functionp type) (if (symbolp type)
  18321. (symbol-name type)
  18322. "Lambda expression"))
  18323. (t "???"))
  18324. (cond
  18325. ((stringp match)
  18326. (org-add-props match nil 'face 'org-warning))
  18327. (match
  18328. (format "set of %d commands" (length match)))
  18329. (t ""))))))
  18330. (when prefixes
  18331. (mapc (lambda (x)
  18332. (insert
  18333. (format "\n%s %s"
  18334. (org-add-props (char-to-string x)
  18335. nil 'face 'bold)
  18336. (or (cdr (assoc (concat selstring (char-to-string x))
  18337. prefix-descriptions))
  18338. "Prefix key"))))
  18339. prefixes))
  18340. (goto-char (point-min))
  18341. (when (fboundp 'fit-window-to-buffer)
  18342. (if second-time
  18343. (if (not (pos-visible-in-window-p (point-max)))
  18344. (fit-window-to-buffer))
  18345. (setq second-time t)
  18346. (fit-window-to-buffer)))
  18347. (message "Press key for agenda command%s:"
  18348. (if (or restrict-ok org-agenda-overriding-restriction)
  18349. (if org-agenda-overriding-restriction
  18350. " (restriction lock active)"
  18351. (if restriction
  18352. (format " (restricted to %s)" restriction)
  18353. " (unrestricted)"))
  18354. ""))
  18355. (setq c (read-char-exclusive))
  18356. (message "")
  18357. (cond
  18358. ((assoc (char-to-string c) custom)
  18359. (setq selstring (concat selstring (char-to-string c)))
  18360. (throw 'exit (cons selstring restriction)))
  18361. ((memq c prefixes)
  18362. (setq selstring (concat selstring (char-to-string c))
  18363. prefixes nil
  18364. rmheader (or rmheader t)
  18365. custom (delq nil (mapcar
  18366. (lambda (x)
  18367. (if (or (= (length (car x)) 1)
  18368. (/= (string-to-char (car x)) c))
  18369. nil
  18370. (cons (substring (car x) 1) (cdr x))))
  18371. custom))))
  18372. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18373. (message "Restriction is only possible in Org-mode buffers")
  18374. (ding) (sit-for 1))
  18375. ((eq c ?1)
  18376. (org-agenda-remove-restriction-lock 'noupdate)
  18377. (setq restriction 'buffer))
  18378. ((eq c ?0)
  18379. (org-agenda-remove-restriction-lock 'noupdate)
  18380. (setq restriction (if region-p 'region 'subtree)))
  18381. ((eq c ?<)
  18382. (org-agenda-remove-restriction-lock 'noupdate)
  18383. (setq restriction
  18384. (cond
  18385. ((eq restriction 'buffer)
  18386. (if region-p 'region 'subtree))
  18387. ((memq restriction '(subtree region))
  18388. nil)
  18389. (t 'buffer))))
  18390. ((eq c ?>)
  18391. (org-agenda-remove-restriction-lock 'noupdate)
  18392. (setq restriction nil))
  18393. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18394. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18395. ((and (> (length selstring) 0) (eq c ?\d))
  18396. (delete-window)
  18397. (org-agenda-get-restriction-and-command prefix-descriptions))
  18398. ((equal c ?q) (error "Abort"))
  18399. (t (error "Invalid key %c" c))))))))
  18400. (defun org-run-agenda-series (name series)
  18401. (org-prepare-agenda name)
  18402. (let* ((org-agenda-multi t)
  18403. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18404. (cmds (car series))
  18405. (gprops (nth 1 series))
  18406. match ;; The byte compiler incorrectly complains about this. Keep it!
  18407. cmd type lprops)
  18408. (while (setq cmd (pop cmds))
  18409. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18410. (cond
  18411. ((eq type 'agenda)
  18412. (org-let2 gprops lprops
  18413. '(call-interactively 'org-agenda-list)))
  18414. ((eq type 'alltodo)
  18415. (org-let2 gprops lprops
  18416. '(call-interactively 'org-todo-list)))
  18417. ((eq type 'search)
  18418. (org-let2 gprops lprops
  18419. '(org-search-view current-prefix-arg match)))
  18420. ((eq type 'stuck)
  18421. (org-let2 gprops lprops
  18422. '(call-interactively 'org-agenda-list-stuck-projects)))
  18423. ((eq type 'tags)
  18424. (org-let2 gprops lprops
  18425. '(org-tags-view current-prefix-arg match)))
  18426. ((eq type 'tags-todo)
  18427. (org-let2 gprops lprops
  18428. '(org-tags-view '(4) match)))
  18429. ((eq type 'todo)
  18430. (org-let2 gprops lprops
  18431. '(org-todo-list match)))
  18432. ((fboundp type)
  18433. (org-let2 gprops lprops
  18434. '(funcall type match)))
  18435. (t (error "Invalid type in command series"))))
  18436. (widen)
  18437. (setq org-agenda-redo-command redo)
  18438. (goto-char (point-min)))
  18439. (org-finalize-agenda))
  18440. ;;;###autoload
  18441. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18442. "Run an agenda command in batch mode and send the result to STDOUT.
  18443. If CMD-KEY is a string of length 1, it is used as a key in
  18444. `org-agenda-custom-commands' and triggers this command. If it is a
  18445. longer string it is used as a tags/todo match string.
  18446. Paramters are alternating variable names and values that will be bound
  18447. before running the agenda command."
  18448. (let (pars)
  18449. (while parameters
  18450. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18451. (if (> (length cmd-key) 2)
  18452. (eval (list 'let (nreverse pars)
  18453. (list 'org-tags-view nil cmd-key)))
  18454. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18455. (set-buffer org-agenda-buffer-name)
  18456. (princ (org-encode-for-stdout (buffer-string)))))
  18457. (defun org-encode-for-stdout (string)
  18458. (if (fboundp 'encode-coding-string)
  18459. (encode-coding-string string buffer-file-coding-system)
  18460. string))
  18461. (defvar org-agenda-info nil)
  18462. ;;;###autoload
  18463. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18464. "Run an agenda command in batch mode and send the result to STDOUT.
  18465. If CMD-KEY is a string of length 1, it is used as a key in
  18466. `org-agenda-custom-commands' and triggers this command. If it is a
  18467. longer string it is used as a tags/todo match string.
  18468. Paramters are alternating variable names and values that will be bound
  18469. before running the agenda command.
  18470. The output gives a line for each selected agenda item. Each
  18471. item is a list of comma-separated values, like this:
  18472. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18473. category The category of the item
  18474. head The headline, without TODO kwd, TAGS and PRIORITY
  18475. type The type of the agenda entry, can be
  18476. todo selected in TODO match
  18477. tagsmatch selected in tags match
  18478. diary imported from diary
  18479. deadline a deadline on given date
  18480. scheduled scheduled on given date
  18481. timestamp entry has timestamp on given date
  18482. closed entry was closed on given date
  18483. upcoming-deadline warning about deadline
  18484. past-scheduled forwarded scheduled item
  18485. block entry has date block including g. date
  18486. todo The todo keyword, if any
  18487. tags All tags including inherited ones, separated by colons
  18488. date The relevant date, like 2007-2-14
  18489. time The time, like 15:00-16:50
  18490. extra Sting with extra planning info
  18491. priority-l The priority letter if any was given
  18492. priority-n The computed numerical priority
  18493. agenda-day The day in the agenda where this is listed"
  18494. (let (pars)
  18495. (while parameters
  18496. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18497. (push (list 'org-agenda-remove-tags t) pars)
  18498. (if (> (length cmd-key) 2)
  18499. (eval (list 'let (nreverse pars)
  18500. (list 'org-tags-view nil cmd-key)))
  18501. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18502. (set-buffer org-agenda-buffer-name)
  18503. (let* ((lines (org-split-string (buffer-string) "\n"))
  18504. line)
  18505. (while (setq line (pop lines))
  18506. (catch 'next
  18507. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18508. (setq org-agenda-info
  18509. (org-fix-agenda-info (text-properties-at 0 line)))
  18510. (princ
  18511. (org-encode-for-stdout
  18512. (mapconcat 'org-agenda-export-csv-mapper
  18513. '(org-category txt type todo tags date time-of-day extra
  18514. priority-letter priority agenda-day)
  18515. ",")))
  18516. (princ "\n"))))))
  18517. (defun org-fix-agenda-info (props)
  18518. "Make sure all properties on an agenda item have a canonical form,
  18519. so the export commands can easily use it."
  18520. (let (tmp re)
  18521. (when (setq tmp (plist-get props 'tags))
  18522. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18523. (when (setq tmp (plist-get props 'date))
  18524. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18525. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18526. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18527. (setq tmp (calendar-date-string tmp)))
  18528. (setq props (plist-put props 'date tmp)))
  18529. (when (setq tmp (plist-get props 'day))
  18530. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18531. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18532. (setq tmp (calendar-date-string tmp)))
  18533. (setq props (plist-put props 'day tmp))
  18534. (setq props (plist-put props 'agenda-day tmp)))
  18535. (when (setq tmp (plist-get props 'txt))
  18536. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18537. (plist-put props 'priority-letter (match-string 1 tmp))
  18538. (setq tmp (replace-match "" t t tmp)))
  18539. (when (and (setq re (plist-get props 'org-todo-regexp))
  18540. (setq re (concat "\\`\\.*" re " ?"))
  18541. (string-match re tmp))
  18542. (plist-put props 'todo (match-string 1 tmp))
  18543. (setq tmp (replace-match "" t t tmp)))
  18544. (plist-put props 'txt tmp)))
  18545. props)
  18546. (defun org-agenda-export-csv-mapper (prop)
  18547. (let ((res (plist-get org-agenda-info prop)))
  18548. (setq res
  18549. (cond
  18550. ((not res) "")
  18551. ((stringp res) res)
  18552. (t (prin1-to-string res))))
  18553. (while (string-match "," res)
  18554. (setq res (replace-match ";" t t res)))
  18555. (org-trim res)))
  18556. ;;;###autoload
  18557. (defun org-store-agenda-views (&rest parameters)
  18558. (interactive)
  18559. (eval (list 'org-batch-store-agenda-views)))
  18560. ;; FIXME, why is this a macro?????
  18561. ;;;###autoload
  18562. (defmacro org-batch-store-agenda-views (&rest parameters)
  18563. "Run all custom agenda commands that have a file argument."
  18564. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18565. (pop-up-frames nil)
  18566. (dir default-directory)
  18567. pars cmd thiscmdkey files opts)
  18568. (while parameters
  18569. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18570. (setq pars (reverse pars))
  18571. (save-window-excursion
  18572. (while cmds
  18573. (setq cmd (pop cmds)
  18574. thiscmdkey (car cmd)
  18575. opts (nth 4 cmd)
  18576. files (nth 5 cmd))
  18577. (if (stringp files) (setq files (list files)))
  18578. (when files
  18579. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18580. (list 'org-agenda nil thiscmdkey)))
  18581. (set-buffer org-agenda-buffer-name)
  18582. (while files
  18583. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18584. (list 'org-write-agenda
  18585. (expand-file-name (pop files) dir) t))))
  18586. (and (get-buffer org-agenda-buffer-name)
  18587. (kill-buffer org-agenda-buffer-name)))))))
  18588. (defun org-write-agenda (file &optional nosettings)
  18589. "Write the current buffer (an agenda view) as a file.
  18590. Depending on the extension of the file name, plain text (.txt),
  18591. HTML (.html or .htm) or Postscript (.ps) is produced.
  18592. If NOSETTINGS is given, do not scope the settings of
  18593. `org-agenda-exporter-settings' into the export commands. This is used when
  18594. the settings have already been scoped and we do not wish to overrule other,
  18595. higher priority settings."
  18596. (interactive "FWrite agenda to file: ")
  18597. (if (not (file-writable-p file))
  18598. (error "Cannot write agenda to file %s" file))
  18599. (cond
  18600. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18601. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18602. (org-let (if nosettings nil org-agenda-exporter-settings)
  18603. '(save-excursion
  18604. (save-window-excursion
  18605. (cond
  18606. ((string-match "\\.html?\\'" file)
  18607. (set-buffer (htmlize-buffer (current-buffer)))
  18608. (when (and org-agenda-export-html-style
  18609. (string-match "<style>" org-agenda-export-html-style))
  18610. ;; replace <style> section with org-agenda-export-html-style
  18611. (goto-char (point-min))
  18612. (kill-region (- (search-forward "<style") 6)
  18613. (search-forward "</style>"))
  18614. (insert org-agenda-export-html-style))
  18615. (write-file file)
  18616. (kill-buffer (current-buffer))
  18617. (message "HTML written to %s" file))
  18618. ((string-match "\\.ps\\'" file)
  18619. (ps-print-buffer-with-faces file)
  18620. (message "Postscript written to %s" file))
  18621. (t
  18622. (let ((bs (buffer-string)))
  18623. (find-file file)
  18624. (insert bs)
  18625. (save-buffer 0)
  18626. (kill-buffer (current-buffer))
  18627. (message "Plain text written to %s" file))))))
  18628. (set-buffer org-agenda-buffer-name)))
  18629. (defmacro org-no-read-only (&rest body)
  18630. "Inhibit read-only for BODY."
  18631. `(let ((inhibit-read-only t)) ,@body))
  18632. (defun org-check-for-org-mode ()
  18633. "Make sure current buffer is in org-mode. Error if not."
  18634. (or (org-mode-p)
  18635. (error "Cannot execute org-mode agenda command on buffer in %s."
  18636. major-mode)))
  18637. (defun org-fit-agenda-window ()
  18638. "Fit the window to the buffer size."
  18639. (and (memq org-agenda-window-setup '(reorganize-frame))
  18640. (fboundp 'fit-window-to-buffer)
  18641. (fit-window-to-buffer
  18642. nil
  18643. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18644. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18645. ;;; Agenda file list
  18646. (defun org-agenda-files (&optional unrestricted)
  18647. "Get the list of agenda files.
  18648. Optional UNRESTRICTED means return the full list even if a restriction
  18649. is currently in place."
  18650. (let ((files
  18651. (cond
  18652. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18653. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18654. ((listp org-agenda-files) org-agenda-files)
  18655. (t (error "Invalid value of `org-agenda-files'")))))
  18656. (setq files (apply 'append
  18657. (mapcar (lambda (f)
  18658. (if (file-directory-p f)
  18659. (directory-files f t
  18660. org-agenda-file-regexp)
  18661. (list f)))
  18662. files)))
  18663. (if org-agenda-skip-unavailable-files
  18664. (delq nil
  18665. (mapcar (function
  18666. (lambda (file)
  18667. (and (file-readable-p file) file)))
  18668. files))
  18669. files))) ; `org-check-agenda-file' will remove them from the list
  18670. (defun org-edit-agenda-file-list ()
  18671. "Edit the list of agenda files.
  18672. Depending on setup, this either uses customize to edit the variable
  18673. `org-agenda-files', or it visits the file that is holding the list. In the
  18674. latter case, the buffer is set up in a way that saving it automatically kills
  18675. the buffer and restores the previous window configuration."
  18676. (interactive)
  18677. (if (stringp org-agenda-files)
  18678. (let ((cw (current-window-configuration)))
  18679. (find-file org-agenda-files)
  18680. (org-set-local 'org-window-configuration cw)
  18681. (org-add-hook 'after-save-hook
  18682. (lambda ()
  18683. (set-window-configuration
  18684. (prog1 org-window-configuration
  18685. (kill-buffer (current-buffer))))
  18686. (org-install-agenda-files-menu)
  18687. (message "New agenda file list installed"))
  18688. nil 'local)
  18689. (message "%s" (substitute-command-keys
  18690. "Edit list and finish with \\[save-buffer]")))
  18691. (customize-variable 'org-agenda-files)))
  18692. (defun org-store-new-agenda-file-list (list)
  18693. "Set new value for the agenda file list and save it correcly."
  18694. (if (stringp org-agenda-files)
  18695. (let ((f org-agenda-files) b)
  18696. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18697. (with-temp-file f
  18698. (insert (mapconcat 'identity list "\n") "\n")))
  18699. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18700. (setq org-agenda-files list)
  18701. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18702. (defun org-read-agenda-file-list ()
  18703. "Read the list of agenda files from a file."
  18704. (when (stringp org-agenda-files)
  18705. (with-temp-buffer
  18706. (insert-file-contents org-agenda-files)
  18707. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18708. ;;;###autoload
  18709. (defun org-cycle-agenda-files ()
  18710. "Cycle through the files in `org-agenda-files'.
  18711. If the current buffer visits an agenda file, find the next one in the list.
  18712. If the current buffer does not, find the first agenda file."
  18713. (interactive)
  18714. (let* ((fs (org-agenda-files t))
  18715. (files (append fs (list (car fs))))
  18716. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18717. file)
  18718. (unless files (error "No agenda files"))
  18719. (catch 'exit
  18720. (while (setq file (pop files))
  18721. (if (equal (file-truename file) tcf)
  18722. (when (car files)
  18723. (find-file (car files))
  18724. (throw 'exit t))))
  18725. (find-file (car fs)))
  18726. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18727. (defun org-agenda-file-to-front (&optional to-end)
  18728. "Move/add the current file to the top of the agenda file list.
  18729. If the file is not present in the list, it is added to the front. If it is
  18730. present, it is moved there. With optional argument TO-END, add/move to the
  18731. end of the list."
  18732. (interactive "P")
  18733. (let ((org-agenda-skip-unavailable-files nil)
  18734. (file-alist (mapcar (lambda (x)
  18735. (cons (file-truename x) x))
  18736. (org-agenda-files t)))
  18737. (ctf (file-truename buffer-file-name))
  18738. x had)
  18739. (setq x (assoc ctf file-alist) had x)
  18740. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18741. (if to-end
  18742. (setq file-alist (append (delq x file-alist) (list x)))
  18743. (setq file-alist (cons x (delq x file-alist))))
  18744. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18745. (org-install-agenda-files-menu)
  18746. (message "File %s to %s of agenda file list"
  18747. (if had "moved" "added") (if to-end "end" "front"))))
  18748. (defun org-remove-file (&optional file)
  18749. "Remove current file from the list of files in variable `org-agenda-files'.
  18750. These are the files which are being checked for agenda entries.
  18751. Optional argument FILE means, use this file instead of the current."
  18752. (interactive)
  18753. (let* ((org-agenda-skip-unavailable-files nil)
  18754. (file (or file buffer-file-name))
  18755. (true-file (file-truename file))
  18756. (afile (abbreviate-file-name file))
  18757. (files (delq nil (mapcar
  18758. (lambda (x)
  18759. (if (equal true-file
  18760. (file-truename x))
  18761. nil x))
  18762. (org-agenda-files t)))))
  18763. (if (not (= (length files) (length (org-agenda-files t))))
  18764. (progn
  18765. (org-store-new-agenda-file-list files)
  18766. (org-install-agenda-files-menu)
  18767. (message "Removed file: %s" afile))
  18768. (message "File was not in list: %s (not removed)" afile))))
  18769. (defun org-file-menu-entry (file)
  18770. (vector file (list 'find-file file) t))
  18771. (defun org-check-agenda-file (file)
  18772. "Make sure FILE exists. If not, ask user what to do."
  18773. (when (not (file-exists-p file))
  18774. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18775. (abbreviate-file-name file))
  18776. (let ((r (downcase (read-char-exclusive))))
  18777. (cond
  18778. ((equal r ?r)
  18779. (org-remove-file file)
  18780. (throw 'nextfile t))
  18781. (t (error "Abort"))))))
  18782. ;;; Agenda prepare and finalize
  18783. (defvar org-agenda-multi nil) ; dynammically scoped
  18784. (defvar org-agenda-buffer-name "*Org Agenda*")
  18785. (defvar org-pre-agenda-window-conf nil)
  18786. (defvar org-agenda-name nil)
  18787. (defun org-prepare-agenda (&optional name)
  18788. (setq org-todo-keywords-for-agenda nil)
  18789. (setq org-done-keywords-for-agenda nil)
  18790. (if org-agenda-multi
  18791. (progn
  18792. (setq buffer-read-only nil)
  18793. (goto-char (point-max))
  18794. (unless (or (bobp) org-agenda-compact-blocks)
  18795. (insert "\n" (make-string (window-width) ?=) "\n"))
  18796. (narrow-to-region (point) (point-max)))
  18797. (org-agenda-reset-markers)
  18798. (org-prepare-agenda-buffers (org-agenda-files))
  18799. (setq org-todo-keywords-for-agenda
  18800. (org-uniquify org-todo-keywords-for-agenda))
  18801. (setq org-done-keywords-for-agenda
  18802. (org-uniquify org-done-keywords-for-agenda))
  18803. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18804. (awin (get-buffer-window abuf)))
  18805. (cond
  18806. ((equal (current-buffer) abuf) nil)
  18807. (awin (select-window awin))
  18808. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18809. ((equal org-agenda-window-setup 'current-window)
  18810. (switch-to-buffer abuf))
  18811. ((equal org-agenda-window-setup 'other-window)
  18812. (org-switch-to-buffer-other-window abuf))
  18813. ((equal org-agenda-window-setup 'other-frame)
  18814. (switch-to-buffer-other-frame abuf))
  18815. ((equal org-agenda-window-setup 'reorganize-frame)
  18816. (delete-other-windows)
  18817. (org-switch-to-buffer-other-window abuf))))
  18818. (setq buffer-read-only nil)
  18819. (erase-buffer)
  18820. (org-agenda-mode)
  18821. (and name (not org-agenda-name)
  18822. (org-set-local 'org-agenda-name name)))
  18823. (setq buffer-read-only nil))
  18824. (defun org-finalize-agenda ()
  18825. "Finishing touch for the agenda buffer, called just before displaying it."
  18826. (unless org-agenda-multi
  18827. (save-excursion
  18828. (let ((inhibit-read-only t))
  18829. (goto-char (point-min))
  18830. (while (org-activate-bracket-links (point-max))
  18831. (add-text-properties (match-beginning 0) (match-end 0)
  18832. '(face org-link)))
  18833. (org-agenda-align-tags)
  18834. (unless org-agenda-with-colors
  18835. (remove-text-properties (point-min) (point-max) '(face nil))))
  18836. (if (and (boundp 'org-overriding-columns-format)
  18837. org-overriding-columns-format)
  18838. (org-set-local 'org-overriding-columns-format
  18839. org-overriding-columns-format))
  18840. (if (and (boundp 'org-agenda-view-columns-initially)
  18841. org-agenda-view-columns-initially)
  18842. (org-agenda-columns))
  18843. (when org-agenda-fontify-priorities
  18844. (org-fontify-priorities))
  18845. (run-hooks 'org-finalize-agenda-hook)
  18846. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18847. )))
  18848. (defun org-fontify-priorities ()
  18849. "Make highest priority lines bold, and lowest italic."
  18850. (interactive)
  18851. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18852. (org-delete-overlay o)))
  18853. (org-overlays-in (point-min) (point-max)))
  18854. (save-excursion
  18855. (let ((inhibit-read-only t)
  18856. b e p ov h l)
  18857. (goto-char (point-min))
  18858. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18859. (setq h (or (get-char-property (point) 'org-highest-priority)
  18860. org-highest-priority)
  18861. l (or (get-char-property (point) 'org-lowest-priority)
  18862. org-lowest-priority)
  18863. p (string-to-char (match-string 1))
  18864. b (match-beginning 0) e (point-at-eol)
  18865. ov (org-make-overlay b e))
  18866. (org-overlay-put
  18867. ov 'face
  18868. (cond ((listp org-agenda-fontify-priorities)
  18869. (cdr (assoc p org-agenda-fontify-priorities)))
  18870. ((equal p l) 'italic)
  18871. ((equal p h) 'bold)))
  18872. (org-overlay-put ov 'org-type 'org-priority)))))
  18873. (defun org-prepare-agenda-buffers (files)
  18874. "Create buffers for all agenda files, protect archived trees and comments."
  18875. (interactive)
  18876. (let ((pa '(:org-archived t))
  18877. (pc '(:org-comment t))
  18878. (pall '(:org-archived t :org-comment t))
  18879. (inhibit-read-only t)
  18880. (rea (concat ":" org-archive-tag ":"))
  18881. bmp file re)
  18882. (save-excursion
  18883. (save-restriction
  18884. (while (setq file (pop files))
  18885. (if (bufferp file)
  18886. (set-buffer file)
  18887. (org-check-agenda-file file)
  18888. (set-buffer (org-get-agenda-file-buffer file)))
  18889. (widen)
  18890. (setq bmp (buffer-modified-p))
  18891. (org-refresh-category-properties)
  18892. (setq org-todo-keywords-for-agenda
  18893. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18894. (setq org-done-keywords-for-agenda
  18895. (append org-done-keywords-for-agenda org-done-keywords))
  18896. (save-excursion
  18897. (remove-text-properties (point-min) (point-max) pall)
  18898. (when org-agenda-skip-archived-trees
  18899. (goto-char (point-min))
  18900. (while (re-search-forward rea nil t)
  18901. (if (org-on-heading-p t)
  18902. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18903. (goto-char (point-min))
  18904. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18905. (while (re-search-forward re nil t)
  18906. (add-text-properties
  18907. (match-beginning 0) (org-end-of-subtree t) pc)))
  18908. (set-buffer-modified-p bmp))))))
  18909. (defvar org-agenda-skip-function nil
  18910. "Function to be called at each match during agenda construction.
  18911. If this function returns nil, the current match should not be skipped.
  18912. Otherwise, the function must return a position from where the search
  18913. should be continued.
  18914. This may also be a Lisp form, it will be evaluated.
  18915. Never set this variable using `setq' or so, because then it will apply
  18916. to all future agenda commands. Instead, bind it with `let' to scope
  18917. it dynamically into the agenda-constructing command. A good way to set
  18918. it is through options in org-agenda-custom-commands.")
  18919. (defun org-agenda-skip ()
  18920. "Throw to `:skip' in places that should be skipped.
  18921. Also moves point to the end of the skipped region, so that search can
  18922. continue from there."
  18923. (let ((p (point-at-bol)) to fp)
  18924. (and org-agenda-skip-archived-trees
  18925. (get-text-property p :org-archived)
  18926. (org-end-of-subtree t)
  18927. (throw :skip t))
  18928. (and (get-text-property p :org-comment)
  18929. (org-end-of-subtree t)
  18930. (throw :skip t))
  18931. (if (equal (char-after p) ?#) (throw :skip t))
  18932. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18933. (consp org-agenda-skip-function))
  18934. (setq to (save-excursion
  18935. (save-match-data
  18936. (if fp
  18937. (funcall org-agenda-skip-function)
  18938. (eval org-agenda-skip-function))))))
  18939. (goto-char to)
  18940. (throw :skip t))))
  18941. (defvar org-agenda-markers nil
  18942. "List of all currently active markers created by `org-agenda'.")
  18943. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18944. "Creation time of the last agenda marker.")
  18945. (defun org-agenda-new-marker (&optional pos)
  18946. "Return a new agenda marker.
  18947. Org-mode keeps a list of these markers and resets them when they are
  18948. no longer in use."
  18949. (let ((m (copy-marker (or pos (point)))))
  18950. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18951. (push m org-agenda-markers)
  18952. m))
  18953. (defun org-agenda-reset-markers ()
  18954. "Reset markers created by `org-agenda'."
  18955. (while org-agenda-markers
  18956. (move-marker (pop org-agenda-markers) nil)))
  18957. (defun org-get-agenda-file-buffer (file)
  18958. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18959. it to the list of buffers which might be released later."
  18960. (let ((buf (org-find-base-buffer-visiting file)))
  18961. (if buf
  18962. buf ; just return it
  18963. ;; Make a new buffer and remember it
  18964. (setq buf (find-file-noselect file))
  18965. (if buf (push buf org-agenda-new-buffers))
  18966. buf)))
  18967. (defun org-release-buffers (blist)
  18968. "Release all buffers in list, asking the user for confirmation when needed.
  18969. When a buffer is unmodified, it is just killed. When modified, it is saved
  18970. \(if the user agrees) and then killed."
  18971. (let (buf file)
  18972. (while (setq buf (pop blist))
  18973. (setq file (buffer-file-name buf))
  18974. (when (and (buffer-modified-p buf)
  18975. file
  18976. (y-or-n-p (format "Save file %s? " file)))
  18977. (with-current-buffer buf (save-buffer)))
  18978. (kill-buffer buf))))
  18979. (defun org-get-category (&optional pos)
  18980. "Get the category applying to position POS."
  18981. (get-text-property (or pos (point)) 'org-category))
  18982. ;;; Agenda timeline
  18983. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18984. (defun org-timeline (&optional include-all)
  18985. "Show a time-sorted view of the entries in the current org file.
  18986. Only entries with a time stamp of today or later will be listed. With
  18987. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18988. under the current date.
  18989. If the buffer contains an active region, only check the region for
  18990. dates."
  18991. (interactive "P")
  18992. (require 'calendar)
  18993. (org-compile-prefix-format 'timeline)
  18994. (org-set-sorting-strategy 'timeline)
  18995. (let* ((dopast t)
  18996. (dotodo include-all)
  18997. (doclosed org-agenda-show-log)
  18998. (entry buffer-file-name)
  18999. (date (calendar-current-date))
  19000. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  19001. (end (if (org-region-active-p) (region-end) (point-max)))
  19002. (day-numbers (org-get-all-dates beg end 'no-ranges
  19003. t doclosed ; always include today
  19004. org-timeline-show-empty-dates))
  19005. (org-deadline-warning-days 0)
  19006. (org-agenda-only-exact-dates t)
  19007. (today (time-to-days (current-time)))
  19008. (past t)
  19009. args
  19010. s e rtn d emptyp)
  19011. (setq org-agenda-redo-command
  19012. (list 'progn
  19013. (list 'org-switch-to-buffer-other-window (current-buffer))
  19014. (list 'org-timeline (list 'quote include-all))))
  19015. (if (not dopast)
  19016. ;; Remove past dates from the list of dates.
  19017. (setq day-numbers (delq nil (mapcar (lambda(x)
  19018. (if (>= x today) x nil))
  19019. day-numbers))))
  19020. (org-prepare-agenda (concat "Timeline "
  19021. (file-name-nondirectory buffer-file-name)))
  19022. (if doclosed (push :closed args))
  19023. (push :timestamp args)
  19024. (push :deadline args)
  19025. (push :scheduled args)
  19026. (push :sexp args)
  19027. (if dotodo (push :todo args))
  19028. (while (setq d (pop day-numbers))
  19029. (if (and (listp d) (eq (car d) :omitted))
  19030. (progn
  19031. (setq s (point))
  19032. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19033. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19034. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19035. (if (and (>= d today)
  19036. dopast
  19037. past)
  19038. (progn
  19039. (setq past nil)
  19040. (insert (make-string 79 ?-) "\n")))
  19041. (setq date (calendar-gregorian-from-absolute d))
  19042. (setq s (point))
  19043. (setq rtn (and (not emptyp)
  19044. (apply 'org-agenda-get-day-entries entry
  19045. date args)))
  19046. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19047. (progn
  19048. (insert
  19049. (if (stringp org-agenda-format-date)
  19050. (format-time-string org-agenda-format-date
  19051. (org-time-from-absolute date))
  19052. (funcall org-agenda-format-date date))
  19053. "\n")
  19054. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19055. (put-text-property s (1- (point)) 'org-date-line t)
  19056. (if (equal d today)
  19057. (put-text-property s (1- (point)) 'org-today t))
  19058. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19059. (put-text-property s (1- (point)) 'day d)))))
  19060. (goto-char (point-min))
  19061. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19062. (point-min)))
  19063. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19064. (org-finalize-agenda)
  19065. (setq buffer-read-only t)))
  19066. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19067. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19068. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19069. not every single day in the range. If FORCE-TODAY is non-nil, make
  19070. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19071. inactive time stamps (those in square brackets) are included.
  19072. When EMPTY is non-nil, also include days without any entries."
  19073. (let ((re (concat
  19074. (if pre-re pre-re "")
  19075. (if inactive org-ts-regexp-both org-ts-regexp)))
  19076. dates dates1 date day day1 day2 ts1 ts2)
  19077. (if force-today
  19078. (setq dates (list (time-to-days (current-time)))))
  19079. (save-excursion
  19080. (goto-char beg)
  19081. (while (re-search-forward re end t)
  19082. (setq day (time-to-days (org-time-string-to-time
  19083. (substring (match-string 1) 0 10))))
  19084. (or (memq day dates) (push day dates)))
  19085. (unless no-ranges
  19086. (goto-char beg)
  19087. (while (re-search-forward org-tr-regexp end t)
  19088. (setq ts1 (substring (match-string 1) 0 10)
  19089. ts2 (substring (match-string 2) 0 10)
  19090. day1 (time-to-days (org-time-string-to-time ts1))
  19091. day2 (time-to-days (org-time-string-to-time ts2)))
  19092. (while (< (setq day1 (1+ day1)) day2)
  19093. (or (memq day1 dates) (push day1 dates)))))
  19094. (setq dates (sort dates '<))
  19095. (when empty
  19096. (while (setq day (pop dates))
  19097. (setq day2 (car dates))
  19098. (push day dates1)
  19099. (when (and day2 empty)
  19100. (if (or (eq empty t)
  19101. (and (numberp empty) (<= (- day2 day) empty)))
  19102. (while (< (setq day (1+ day)) day2)
  19103. (push (list day) dates1))
  19104. (push (cons :omitted (- day2 day)) dates1))))
  19105. (setq dates (nreverse dates1)))
  19106. dates)))
  19107. ;;; Agenda Daily/Weekly
  19108. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19109. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19110. (defvar org-agenda-last-arguments nil
  19111. "The arguments of the previous call to org-agenda")
  19112. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19113. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19114. (defvar org-include-all-loc nil) ; local variable
  19115. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19116. ;;;###autoload
  19117. (defun org-agenda-list (&optional include-all start-day ndays)
  19118. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19119. The view will be for the current day or week, but from the overview buffer
  19120. you will be able to go to other days/weeks.
  19121. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19122. all unfinished TODO items will also be shown, before the agenda.
  19123. This feature is considered obsolete, please use the TODO list or a block
  19124. agenda instead.
  19125. With a numeric prefix argument in an interactive call, the agenda will
  19126. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19127. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19128. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19129. given in `org-agenda-start-on-weekday'."
  19130. (interactive "P")
  19131. (if (and (integerp include-all) (> include-all 0))
  19132. (setq ndays include-all include-all nil))
  19133. (setq ndays (or ndays org-agenda-ndays)
  19134. start-day (or start-day org-agenda-start-day))
  19135. (if org-agenda-overriding-arguments
  19136. (setq include-all (car org-agenda-overriding-arguments)
  19137. start-day (nth 1 org-agenda-overriding-arguments)
  19138. ndays (nth 2 org-agenda-overriding-arguments)))
  19139. (if (stringp start-day)
  19140. ;; Convert to an absolute day number
  19141. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19142. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19143. (org-compile-prefix-format 'agenda)
  19144. (org-set-sorting-strategy 'agenda)
  19145. (require 'calendar)
  19146. (let* ((org-agenda-start-on-weekday
  19147. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19148. org-agenda-start-on-weekday nil))
  19149. (thefiles (org-agenda-files))
  19150. (files thefiles)
  19151. (today (time-to-days
  19152. (time-subtract (current-time)
  19153. (list 0 (* 3600 org-extend-today-until) 0))))
  19154. (sd (or start-day today))
  19155. (start (if (or (null org-agenda-start-on-weekday)
  19156. (< org-agenda-ndays 7))
  19157. sd
  19158. (let* ((nt (calendar-day-of-week
  19159. (calendar-gregorian-from-absolute sd)))
  19160. (n1 org-agenda-start-on-weekday)
  19161. (d (- nt n1)))
  19162. (- sd (+ (if (< d 0) 7 0) d)))))
  19163. (day-numbers (list start))
  19164. (day-cnt 0)
  19165. (inhibit-redisplay (not debug-on-error))
  19166. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19167. (setq org-agenda-redo-command
  19168. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19169. ;; Make the list of days
  19170. (setq ndays (or ndays org-agenda-ndays)
  19171. nd ndays)
  19172. (while (> ndays 1)
  19173. (push (1+ (car day-numbers)) day-numbers)
  19174. (setq ndays (1- ndays)))
  19175. (setq day-numbers (nreverse day-numbers))
  19176. (org-prepare-agenda "Day/Week")
  19177. (org-set-local 'org-starting-day (car day-numbers))
  19178. (org-set-local 'org-include-all-loc include-all)
  19179. (org-set-local 'org-agenda-span
  19180. (org-agenda-ndays-to-span nd))
  19181. (when (and (or include-all org-agenda-include-all-todo)
  19182. (member today day-numbers))
  19183. (setq files thefiles
  19184. rtnall nil)
  19185. (while (setq file (pop files))
  19186. (catch 'nextfile
  19187. (org-check-agenda-file file)
  19188. (setq date (calendar-gregorian-from-absolute today)
  19189. rtn (org-agenda-get-day-entries
  19190. file date :todo))
  19191. (setq rtnall (append rtnall rtn))))
  19192. (when rtnall
  19193. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19194. (add-text-properties (point-min) (1- (point))
  19195. (list 'face 'org-agenda-structure))
  19196. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19197. (unless org-agenda-compact-blocks
  19198. (setq s (point))
  19199. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19200. "-agenda:\n")
  19201. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19202. 'org-date-line t)))
  19203. (while (setq d (pop day-numbers))
  19204. (setq date (calendar-gregorian-from-absolute d)
  19205. s (point))
  19206. (if (or (setq todayp (= d today))
  19207. (and (not start-pos) (= d sd)))
  19208. (setq start-pos (point))
  19209. (if (and start-pos (not end-pos))
  19210. (setq end-pos (point))))
  19211. (setq files thefiles
  19212. rtnall nil)
  19213. (while (setq file (pop files))
  19214. (catch 'nextfile
  19215. (org-check-agenda-file file)
  19216. (if org-agenda-show-log
  19217. (setq rtn (org-agenda-get-day-entries
  19218. file date
  19219. :deadline :scheduled :timestamp :sexp :closed))
  19220. (setq rtn (org-agenda-get-day-entries
  19221. file date
  19222. :deadline :scheduled :sexp :timestamp)))
  19223. (setq rtnall (append rtnall rtn))))
  19224. (if org-agenda-include-diary
  19225. (progn
  19226. (require 'diary-lib)
  19227. (setq rtn (org-get-entries-from-diary date))
  19228. (setq rtnall (append rtnall rtn))))
  19229. (if (or rtnall org-agenda-show-all-dates)
  19230. (progn
  19231. (setq day-cnt (1+ day-cnt))
  19232. (insert
  19233. (if (stringp org-agenda-format-date)
  19234. (format-time-string org-agenda-format-date
  19235. (org-time-from-absolute date))
  19236. (funcall org-agenda-format-date date))
  19237. "\n")
  19238. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19239. (put-text-property s (1- (point)) 'org-date-line t)
  19240. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19241. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19242. (if rtnall (insert
  19243. (org-finalize-agenda-entries
  19244. (org-agenda-add-time-grid-maybe
  19245. rtnall nd todayp))
  19246. "\n"))
  19247. (put-text-property s (1- (point)) 'day d)
  19248. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19249. (goto-char (point-min))
  19250. (org-fit-agenda-window)
  19251. (unless (and (pos-visible-in-window-p (point-min))
  19252. (pos-visible-in-window-p (point-max)))
  19253. (goto-char (1- (point-max)))
  19254. (recenter -1)
  19255. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19256. (progn
  19257. (goto-char (or start-pos 1))
  19258. (recenter 1))))
  19259. (goto-char (or start-pos 1))
  19260. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19261. (org-finalize-agenda)
  19262. (setq buffer-read-only t)
  19263. (message "")))
  19264. (defun org-agenda-ndays-to-span (n)
  19265. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19266. ;;; Agenda word search
  19267. (defvar org-agenda-search-history nil)
  19268. ;;;###autoload
  19269. (defun org-search-view (&optional arg string)
  19270. "Show all entries that contain words or regular expressions.
  19271. If the first character of the search string is an asterisks,
  19272. search only the headlines.
  19273. The search string is broken into \"words\" by splitting at whitespace.
  19274. The individual words are then interpreted as a boolean expression with
  19275. logical AND. Words prefixed with a minus must not occur in the entry.
  19276. Words without a prefix or prefixed with a plus must occur in the entry.
  19277. Matching is case-insensitive and the words are enclosed by word delimiters.
  19278. Words enclosed by curly braces are interpreted as regular expressions
  19279. that must or must not match in the entry.
  19280. This command searches the agenda files, and in addition the files listed
  19281. in `org-agenda-text-search-extra-files'."
  19282. (interactive "P")
  19283. (org-compile-prefix-format 'search)
  19284. (org-set-sorting-strategy 'search)
  19285. (org-prepare-agenda "SEARCH")
  19286. (let* ((props (list 'face nil
  19287. 'done-face 'org-done
  19288. 'org-not-done-regexp org-not-done-regexp
  19289. 'org-todo-regexp org-todo-regexp
  19290. 'mouse-face 'highlight
  19291. 'keymap org-agenda-keymap
  19292. 'help-echo (format "mouse-2 or RET jump to location")))
  19293. regexp rtn rtnall files file pos
  19294. marker priority category tags c neg re
  19295. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19296. (unless (and (not arg)
  19297. (stringp string)
  19298. (string-match "\\S-" string))
  19299. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19300. (cond
  19301. ((integerp arg) (cons string arg))
  19302. (arg string))
  19303. 'org-agenda-search-history)))
  19304. (setq org-agenda-redo-command
  19305. (list 'org-search-view 'current-prefix-arg string))
  19306. (setq org-agenda-query-string string)
  19307. (if (equal (string-to-char string) ?*)
  19308. (setq hdl-only t
  19309. words (substring string 1))
  19310. (setq words string))
  19311. (setq words (org-split-string words))
  19312. (mapc (lambda (w)
  19313. (setq c (string-to-char w))
  19314. (if (equal c ?-)
  19315. (setq neg t w (substring w 1))
  19316. (if (equal c ?+)
  19317. (setq neg nil w (substring w 1))
  19318. (setq neg nil)))
  19319. (if (string-match "\\`{.*}\\'" w)
  19320. (setq re (substring w 1 -1))
  19321. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19322. (if neg (push re regexps-) (push re regexps+)))
  19323. words)
  19324. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19325. (if (not regexps+)
  19326. (setq regexp (concat "^" org-outline-regexp))
  19327. (setq regexp (pop regexps+))
  19328. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19329. regexp))))
  19330. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19331. rtnall nil)
  19332. (while (setq file (pop files))
  19333. (setq ee nil)
  19334. (catch 'nextfile
  19335. (org-check-agenda-file file)
  19336. (setq buffer (if (file-exists-p file)
  19337. (org-get-agenda-file-buffer file)
  19338. (error "No such file %s" file)))
  19339. (if (not buffer)
  19340. ;; If file does not exist, make sure an error message is sent
  19341. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19342. file))))
  19343. (with-current-buffer buffer
  19344. (unless (org-mode-p)
  19345. (error "Agenda file %s is not in `org-mode'" file))
  19346. (let ((case-fold-search t))
  19347. (save-excursion
  19348. (save-restriction
  19349. (if org-agenda-restrict
  19350. (narrow-to-region org-agenda-restrict-begin
  19351. org-agenda-restrict-end)
  19352. (widen))
  19353. (goto-char (point-min))
  19354. (unless (or (org-on-heading-p)
  19355. (outline-next-heading))
  19356. (throw 'nextfile t))
  19357. (goto-char (max (point-min) (1- (point))))
  19358. (while (re-search-forward regexp nil t)
  19359. (org-back-to-heading t)
  19360. (skip-chars-forward "* ")
  19361. (setq beg (point-at-bol)
  19362. beg1 (point)
  19363. end (progn (outline-next-heading) (point)))
  19364. (catch :skip
  19365. (goto-char beg)
  19366. (org-agenda-skip)
  19367. (setq str (buffer-substring-no-properties
  19368. (point-at-bol)
  19369. (if hdl-only (point-at-eol) end)))
  19370. (mapc (lambda (wr) (when (string-match wr str)
  19371. (goto-char (1- end))
  19372. (throw :skip t)))
  19373. regexps-)
  19374. (mapc (lambda (wr) (unless (string-match wr str)
  19375. (goto-char (1- end))
  19376. (throw :skip t)))
  19377. regexps+)
  19378. (goto-char beg)
  19379. (setq marker (org-agenda-new-marker (point))
  19380. category (org-get-category)
  19381. tags (org-get-tags-at (point))
  19382. txt (org-format-agenda-item
  19383. ""
  19384. (buffer-substring-no-properties
  19385. beg1 (point-at-eol))
  19386. category tags))
  19387. (org-add-props txt props
  19388. 'org-marker marker 'org-hd-marker marker
  19389. 'priority 1000 'org-category category
  19390. 'type "search")
  19391. (push txt ee)
  19392. (goto-char (1- end)))))))))
  19393. (setq rtn (nreverse ee))
  19394. (setq rtnall (append rtnall rtn)))
  19395. (if org-agenda-overriding-header
  19396. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19397. nil 'face 'org-agenda-structure) "\n")
  19398. (insert "Search words: ")
  19399. (add-text-properties (point-min) (1- (point))
  19400. (list 'face 'org-agenda-structure))
  19401. (setq pos (point))
  19402. (insert string "\n")
  19403. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19404. (setq pos (point))
  19405. (unless org-agenda-multi
  19406. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19407. (add-text-properties pos (1- (point))
  19408. (list 'face 'org-agenda-structure))))
  19409. (when rtnall
  19410. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19411. (goto-char (point-min))
  19412. (org-fit-agenda-window)
  19413. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19414. (org-finalize-agenda)
  19415. (setq buffer-read-only t)))
  19416. ;;; Agenda TODO list
  19417. (defvar org-select-this-todo-keyword nil)
  19418. (defvar org-last-arg nil)
  19419. ;;;###autoload
  19420. (defun org-todo-list (arg)
  19421. "Show all TODO entries from all agenda file in a single list.
  19422. The prefix arg can be used to select a specific TODO keyword and limit
  19423. the list to these. When using \\[universal-argument], you will be prompted
  19424. for a keyword. A numeric prefix directly selects the Nth keyword in
  19425. `org-todo-keywords-1'."
  19426. (interactive "P")
  19427. (require 'calendar)
  19428. (org-compile-prefix-format 'todo)
  19429. (org-set-sorting-strategy 'todo)
  19430. (org-prepare-agenda "TODO")
  19431. (let* ((today (time-to-days (current-time)))
  19432. (date (calendar-gregorian-from-absolute today))
  19433. (kwds org-todo-keywords-for-agenda)
  19434. (completion-ignore-case t)
  19435. (org-select-this-todo-keyword
  19436. (if (stringp arg) arg
  19437. (and arg (integerp arg) (> arg 0)
  19438. (nth (1- arg) kwds))))
  19439. rtn rtnall files file pos)
  19440. (when (equal arg '(4))
  19441. (setq org-select-this-todo-keyword
  19442. (completing-read "Keyword (or KWD1|K2D2|...): "
  19443. (mapcar 'list kwds) nil nil)))
  19444. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19445. (org-set-local 'org-last-arg arg)
  19446. (setq org-agenda-redo-command
  19447. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19448. (setq files (org-agenda-files)
  19449. rtnall nil)
  19450. (while (setq file (pop files))
  19451. (catch 'nextfile
  19452. (org-check-agenda-file file)
  19453. (setq rtn (org-agenda-get-day-entries file date :todo))
  19454. (setq rtnall (append rtnall rtn))))
  19455. (if org-agenda-overriding-header
  19456. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19457. nil 'face 'org-agenda-structure) "\n")
  19458. (insert "Global list of TODO items of type: ")
  19459. (add-text-properties (point-min) (1- (point))
  19460. (list 'face 'org-agenda-structure))
  19461. (setq pos (point))
  19462. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19463. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19464. (setq pos (point))
  19465. (unless org-agenda-multi
  19466. (insert "Available with `N r': (0)ALL")
  19467. (let ((n 0) s)
  19468. (mapc (lambda (x)
  19469. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19470. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19471. (insert "\n "))
  19472. (insert " " s))
  19473. kwds))
  19474. (insert "\n"))
  19475. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19476. (when rtnall
  19477. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19478. (goto-char (point-min))
  19479. (org-fit-agenda-window)
  19480. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19481. (org-finalize-agenda)
  19482. (setq buffer-read-only t)))
  19483. ;;; Agenda tags match
  19484. ;;;###autoload
  19485. (defun org-tags-view (&optional todo-only match)
  19486. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19487. The prefix arg TODO-ONLY limits the search to TODO entries."
  19488. (interactive "P")
  19489. (org-compile-prefix-format 'tags)
  19490. (org-set-sorting-strategy 'tags)
  19491. (let* ((org-tags-match-list-sublevels
  19492. (if todo-only t org-tags-match-list-sublevels))
  19493. (completion-ignore-case t)
  19494. rtn rtnall files file pos matcher
  19495. buffer)
  19496. (setq matcher (org-make-tags-matcher match)
  19497. match (car matcher) matcher (cdr matcher))
  19498. (org-prepare-agenda (concat "TAGS " match))
  19499. (setq org-agenda-redo-command
  19500. (list 'org-tags-view (list 'quote todo-only)
  19501. (list 'if 'current-prefix-arg nil match)))
  19502. (setq files (org-agenda-files)
  19503. rtnall nil)
  19504. (while (setq file (pop files))
  19505. (catch 'nextfile
  19506. (org-check-agenda-file file)
  19507. (setq buffer (if (file-exists-p file)
  19508. (org-get-agenda-file-buffer file)
  19509. (error "No such file %s" file)))
  19510. (if (not buffer)
  19511. ;; If file does not exist, merror message to agenda
  19512. (setq rtn (list
  19513. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19514. rtnall (append rtnall rtn))
  19515. (with-current-buffer buffer
  19516. (unless (org-mode-p)
  19517. (error "Agenda file %s is not in `org-mode'" file))
  19518. (save-excursion
  19519. (save-restriction
  19520. (if org-agenda-restrict
  19521. (narrow-to-region org-agenda-restrict-begin
  19522. org-agenda-restrict-end)
  19523. (widen))
  19524. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19525. (setq rtnall (append rtnall rtn))))))))
  19526. (if org-agenda-overriding-header
  19527. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19528. nil 'face 'org-agenda-structure) "\n")
  19529. (insert "Headlines with TAGS match: ")
  19530. (add-text-properties (point-min) (1- (point))
  19531. (list 'face 'org-agenda-structure))
  19532. (setq pos (point))
  19533. (insert match "\n")
  19534. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19535. (setq pos (point))
  19536. (unless org-agenda-multi
  19537. (insert "Press `C-u r' to search again with new search string\n"))
  19538. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19539. (when rtnall
  19540. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19541. (goto-char (point-min))
  19542. (org-fit-agenda-window)
  19543. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19544. (org-finalize-agenda)
  19545. (setq buffer-read-only t)))
  19546. ;;; Agenda Finding stuck projects
  19547. (defvar org-agenda-skip-regexp nil
  19548. "Regular expression used in skipping subtrees for the agenda.
  19549. This is basically a temporary global variable that can be set and then
  19550. used by user-defined selections using `org-agenda-skip-function'.")
  19551. (defvar org-agenda-overriding-header nil
  19552. "When this is set during todo and tags searches, will replace header.")
  19553. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19554. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19555. If yes, it returns the end position of this tree, causing agenda commands
  19556. to skip this subtree. This is a function that can be put into
  19557. `org-agenda-skip-function' for the duration of a command."
  19558. (let ((end (save-excursion (org-end-of-subtree t)))
  19559. skip)
  19560. (save-excursion
  19561. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19562. (and skip end)))
  19563. (defun org-agenda-skip-entry-if (&rest conditions)
  19564. "Skip entry if any of CONDITIONS is true.
  19565. See `org-agenda-skip-if' for details."
  19566. (org-agenda-skip-if nil conditions))
  19567. (defun org-agenda-skip-subtree-if (&rest conditions)
  19568. "Skip entry if any of CONDITIONS is true.
  19569. See `org-agenda-skip-if' for details."
  19570. (org-agenda-skip-if t conditions))
  19571. (defun org-agenda-skip-if (subtree conditions)
  19572. "Checks current entity for CONDITIONS.
  19573. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19574. the entry, i.e. the text before the next heading is checked.
  19575. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19576. from different tests. Valid conditions are:
  19577. scheduled Check if there is a scheduled cookie
  19578. notscheduled Check if there is no scheduled cookie
  19579. deadline Check if there is a deadline
  19580. notdeadline Check if there is no deadline
  19581. regexp Check if regexp matches
  19582. notregexp Check if regexp does not match.
  19583. The regexp is taken from the conditions list, it must come right after
  19584. the `regexp' or `notregexp' element.
  19585. If any of these conditions is met, this function returns the end point of
  19586. the entity, causing the search to continue from there. This is a function
  19587. that can be put into `org-agenda-skip-function' for the duration of a command."
  19588. (let (beg end m)
  19589. (org-back-to-heading t)
  19590. (setq beg (point)
  19591. end (if subtree
  19592. (progn (org-end-of-subtree t) (point))
  19593. (progn (outline-next-heading) (1- (point)))))
  19594. (goto-char beg)
  19595. (and
  19596. (or
  19597. (and (memq 'scheduled conditions)
  19598. (re-search-forward org-scheduled-time-regexp end t))
  19599. (and (memq 'notscheduled conditions)
  19600. (not (re-search-forward org-scheduled-time-regexp end t)))
  19601. (and (memq 'deadline conditions)
  19602. (re-search-forward org-deadline-time-regexp end t))
  19603. (and (memq 'notdeadline conditions)
  19604. (not (re-search-forward org-deadline-time-regexp end t)))
  19605. (and (setq m (memq 'regexp conditions))
  19606. (stringp (nth 1 m))
  19607. (re-search-forward (nth 1 m) end t))
  19608. (and (setq m (memq 'notregexp conditions))
  19609. (stringp (nth 1 m))
  19610. (not (re-search-forward (nth 1 m) end t))))
  19611. end)))
  19612. ;;;###autoload
  19613. (defun org-agenda-list-stuck-projects (&rest ignore)
  19614. "Create agenda view for projects that are stuck.
  19615. Stuck projects are project that have no next actions. For the definitions
  19616. of what a project is and how to check if it stuck, customize the variable
  19617. `org-stuck-projects'.
  19618. MATCH is being ignored."
  19619. (interactive)
  19620. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19621. ;; FIXME: we could have used org-agenda-skip-if here.
  19622. (org-agenda-overriding-header "List of stuck projects: ")
  19623. (matcher (nth 0 org-stuck-projects))
  19624. (todo (nth 1 org-stuck-projects))
  19625. (todo-wds (if (member "*" todo)
  19626. (progn
  19627. (org-prepare-agenda-buffers (org-agenda-files))
  19628. (org-delete-all
  19629. org-done-keywords-for-agenda
  19630. (copy-sequence org-todo-keywords-for-agenda)))
  19631. todo))
  19632. (todo-re (concat "^\\*+[ \t]+\\("
  19633. (mapconcat 'identity todo-wds "\\|")
  19634. "\\)\\>"))
  19635. (tags (nth 2 org-stuck-projects))
  19636. (tags-re (if (member "*" tags)
  19637. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19638. (concat "^\\*+ .*:\\("
  19639. (mapconcat 'identity tags "\\|")
  19640. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19641. (gen-re (nth 3 org-stuck-projects))
  19642. (re-list
  19643. (delq nil
  19644. (list
  19645. (if todo todo-re)
  19646. (if tags tags-re)
  19647. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19648. gen-re)))))
  19649. (setq org-agenda-skip-regexp
  19650. (if re-list
  19651. (mapconcat 'identity re-list "\\|")
  19652. (error "No information how to identify unstuck projects")))
  19653. (org-tags-view nil matcher)
  19654. (with-current-buffer org-agenda-buffer-name
  19655. (setq org-agenda-redo-command
  19656. '(org-agenda-list-stuck-projects
  19657. (or current-prefix-arg org-last-arg))))))
  19658. ;;; Diary integration
  19659. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19660. (defun org-get-entries-from-diary (date)
  19661. "Get the (Emacs Calendar) diary entries for DATE."
  19662. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19663. (diary-display-hook '(fancy-diary-display))
  19664. (pop-up-frames nil)
  19665. (list-diary-entries-hook
  19666. (cons 'org-diary-default-entry list-diary-entries-hook))
  19667. (diary-file-name-prefix-function nil) ; turn this feature off
  19668. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19669. entries
  19670. (org-disable-agenda-to-diary t))
  19671. (save-excursion
  19672. (save-window-excursion
  19673. (funcall (if (fboundp 'diary-list-entries)
  19674. 'diary-list-entries 'list-diary-entries)
  19675. date 1)))
  19676. (if (not (get-buffer fancy-diary-buffer))
  19677. (setq entries nil)
  19678. (with-current-buffer fancy-diary-buffer
  19679. (setq buffer-read-only nil)
  19680. (if (zerop (buffer-size))
  19681. ;; No entries
  19682. (setq entries nil)
  19683. ;; Omit the date and other unnecessary stuff
  19684. (org-agenda-cleanup-fancy-diary)
  19685. ;; Add prefix to each line and extend the text properties
  19686. (if (zerop (buffer-size))
  19687. (setq entries nil)
  19688. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19689. (set-buffer-modified-p nil)
  19690. (kill-buffer fancy-diary-buffer)))
  19691. (when entries
  19692. (setq entries (org-split-string entries "\n"))
  19693. (setq entries
  19694. (mapcar
  19695. (lambda (x)
  19696. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19697. ;; Extend the text properties to the beginning of the line
  19698. (org-add-props x (text-properties-at (1- (length x)) x)
  19699. 'type "diary" 'date date))
  19700. entries)))))
  19701. (defun org-agenda-cleanup-fancy-diary ()
  19702. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19703. This gets rid of the date, the underline under the date, and
  19704. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19705. date. It also removes lines that contain only whitespace."
  19706. (goto-char (point-min))
  19707. (if (looking-at ".*?:[ \t]*")
  19708. (progn
  19709. (replace-match "")
  19710. (re-search-forward "\n=+$" nil t)
  19711. (replace-match "")
  19712. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19713. (re-search-forward "\n=+$" nil t)
  19714. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19715. (goto-char (point-min))
  19716. (while (re-search-forward "^ +\n" nil t)
  19717. (replace-match ""))
  19718. (goto-char (point-min))
  19719. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19720. (replace-match "")))
  19721. ;; Make sure entries from the diary have the right text properties.
  19722. (eval-after-load "diary-lib"
  19723. '(if (boundp 'diary-modify-entry-list-string-function)
  19724. ;; We can rely on the hook, nothing to do
  19725. nil
  19726. ;; Hook not avaiable, must use advice to make this work
  19727. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19728. "Make the position visible."
  19729. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19730. (stringp string)
  19731. buffer-file-name)
  19732. (setq string (org-modify-diary-entry-string string))))))
  19733. (defun org-modify-diary-entry-string (string)
  19734. "Add text properties to string, allowing org-mode to act on it."
  19735. (org-add-props string nil
  19736. 'mouse-face 'highlight
  19737. 'keymap org-agenda-keymap
  19738. 'help-echo (if buffer-file-name
  19739. (format "mouse-2 or RET jump to diary file %s"
  19740. (abbreviate-file-name buffer-file-name))
  19741. "")
  19742. 'org-agenda-diary-link t
  19743. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19744. (defun org-diary-default-entry ()
  19745. "Add a dummy entry to the diary.
  19746. Needed to avoid empty dates which mess up holiday display."
  19747. ;; Catch the error if dealing with the new add-to-diary-alist
  19748. (when org-disable-agenda-to-diary
  19749. (condition-case nil
  19750. (add-to-diary-list original-date "Org-mode dummy" "")
  19751. (error
  19752. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19753. ;;;###autoload
  19754. (defun org-diary (&rest args)
  19755. "Return diary information from org-files.
  19756. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19757. It accesses org files and extracts information from those files to be
  19758. listed in the diary. The function accepts arguments specifying what
  19759. items should be listed. The following arguments are allowed:
  19760. :timestamp List the headlines of items containing a date stamp or
  19761. date range matching the selected date. Deadlines will
  19762. also be listed, on the expiration day.
  19763. :sexp List entries resulting from diary-like sexps.
  19764. :deadline List any deadlines past due, or due within
  19765. `org-deadline-warning-days'. The listing occurs only
  19766. in the diary for *today*, not at any other date. If
  19767. an entry is marked DONE, it is no longer listed.
  19768. :scheduled List all items which are scheduled for the given date.
  19769. The diary for *today* also contains items which were
  19770. scheduled earlier and are not yet marked DONE.
  19771. :todo List all TODO items from the org-file. This may be a
  19772. long list - so this is not turned on by default.
  19773. Like deadlines, these entries only show up in the
  19774. diary for *today*, not at any other date.
  19775. The call in the diary file should look like this:
  19776. &%%(org-diary) ~/path/to/some/orgfile.org
  19777. Use a separate line for each org file to check. Or, if you omit the file name,
  19778. all files listed in `org-agenda-files' will be checked automatically:
  19779. &%%(org-diary)
  19780. If you don't give any arguments (as in the example above), the default
  19781. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19782. So the example above may also be written as
  19783. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19784. The function expects the lisp variables `entry' and `date' to be provided
  19785. by the caller, because this is how the calendar works. Don't use this
  19786. function from a program - use `org-agenda-get-day-entries' instead."
  19787. (when (> (- (time-to-seconds (current-time))
  19788. org-agenda-last-marker-time)
  19789. 5)
  19790. (org-agenda-reset-markers))
  19791. (org-compile-prefix-format 'agenda)
  19792. (org-set-sorting-strategy 'agenda)
  19793. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19794. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19795. (list entry)
  19796. (org-agenda-files t)))
  19797. file rtn results)
  19798. (org-prepare-agenda-buffers files)
  19799. ;; If this is called during org-agenda, don't return any entries to
  19800. ;; the calendar. Org Agenda will list these entries itself.
  19801. (if org-disable-agenda-to-diary (setq files nil))
  19802. (while (setq file (pop files))
  19803. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19804. (setq results (append results rtn)))
  19805. (if results
  19806. (concat (org-finalize-agenda-entries results) "\n"))))
  19807. ;;; Agenda entry finders
  19808. (defun org-agenda-get-day-entries (file date &rest args)
  19809. "Does the work for `org-diary' and `org-agenda'.
  19810. FILE is the path to a file to be checked for entries. DATE is date like
  19811. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19812. which kind of entries should be extracted. For details about these, see
  19813. the documentation of `org-diary'."
  19814. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19815. (let* ((org-startup-folded nil)
  19816. (org-startup-align-all-tables nil)
  19817. (buffer (if (file-exists-p file)
  19818. (org-get-agenda-file-buffer file)
  19819. (error "No such file %s" file)))
  19820. arg results rtn)
  19821. (if (not buffer)
  19822. ;; If file does not exist, make sure an error message ends up in diary
  19823. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19824. (with-current-buffer buffer
  19825. (unless (org-mode-p)
  19826. (error "Agenda file %s is not in `org-mode'" file))
  19827. (let ((case-fold-search nil))
  19828. (save-excursion
  19829. (save-restriction
  19830. (if org-agenda-restrict
  19831. (narrow-to-region org-agenda-restrict-begin
  19832. org-agenda-restrict-end)
  19833. (widen))
  19834. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19835. (while (setq arg (pop args))
  19836. (cond
  19837. ((and (eq arg :todo)
  19838. (equal date (calendar-current-date)))
  19839. (setq rtn (org-agenda-get-todos))
  19840. (setq results (append results rtn)))
  19841. ((eq arg :timestamp)
  19842. (setq rtn (org-agenda-get-blocks))
  19843. (setq results (append results rtn))
  19844. (setq rtn (org-agenda-get-timestamps))
  19845. (setq results (append results rtn)))
  19846. ((eq arg :sexp)
  19847. (setq rtn (org-agenda-get-sexps))
  19848. (setq results (append results rtn)))
  19849. ((eq arg :scheduled)
  19850. (setq rtn (org-agenda-get-scheduled))
  19851. (setq results (append results rtn)))
  19852. ((eq arg :closed)
  19853. (setq rtn (org-agenda-get-closed))
  19854. (setq results (append results rtn)))
  19855. ((eq arg :deadline)
  19856. (setq rtn (org-agenda-get-deadlines))
  19857. (setq results (append results rtn))))))))
  19858. results))))
  19859. (defun org-entry-is-todo-p ()
  19860. (member (org-get-todo-state) org-not-done-keywords))
  19861. (defun org-entry-is-done-p ()
  19862. (member (org-get-todo-state) org-done-keywords))
  19863. (defun org-get-todo-state ()
  19864. (save-excursion
  19865. (org-back-to-heading t)
  19866. (and (looking-at org-todo-line-regexp)
  19867. (match-end 2)
  19868. (match-string 2))))
  19869. (defun org-at-date-range-p (&optional inactive-ok)
  19870. "Is the cursor inside a date range?"
  19871. (interactive)
  19872. (save-excursion
  19873. (catch 'exit
  19874. (let ((pos (point)))
  19875. (skip-chars-backward "^[<\r\n")
  19876. (skip-chars-backward "<[")
  19877. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19878. (>= (match-end 0) pos)
  19879. (throw 'exit t))
  19880. (skip-chars-backward "^<[\r\n")
  19881. (skip-chars-backward "<[")
  19882. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19883. (>= (match-end 0) pos)
  19884. (throw 'exit t)))
  19885. nil)))
  19886. (defun org-agenda-get-todos ()
  19887. "Return the TODO information for agenda display."
  19888. (let* ((props (list 'face nil
  19889. 'done-face 'org-done
  19890. 'org-not-done-regexp org-not-done-regexp
  19891. 'org-todo-regexp org-todo-regexp
  19892. 'mouse-face 'highlight
  19893. 'keymap org-agenda-keymap
  19894. 'help-echo
  19895. (format "mouse-2 or RET jump to org file %s"
  19896. (abbreviate-file-name buffer-file-name))))
  19897. ;; FIXME: get rid of the \n at some point but watch out
  19898. (regexp (concat "^\\*+[ \t]+\\("
  19899. (if org-select-this-todo-keyword
  19900. (if (equal org-select-this-todo-keyword "*")
  19901. org-todo-regexp
  19902. (concat "\\<\\("
  19903. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19904. "\\)\\>"))
  19905. org-not-done-regexp)
  19906. "[^\n\r]*\\)"))
  19907. marker priority category tags
  19908. ee txt beg end)
  19909. (goto-char (point-min))
  19910. (while (re-search-forward regexp nil t)
  19911. (catch :skip
  19912. (save-match-data
  19913. (beginning-of-line)
  19914. (setq beg (point) end (progn (outline-next-heading) (point)))
  19915. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19916. (re-search-forward org-ts-regexp end t))
  19917. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19918. (re-search-forward org-scheduled-time-regexp end t))
  19919. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19920. (re-search-forward org-deadline-time-regexp end t)
  19921. (org-deadline-close (match-string 1))))
  19922. (goto-char (1+ beg))
  19923. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19924. (throw :skip nil)))
  19925. (goto-char beg)
  19926. (org-agenda-skip)
  19927. (goto-char (match-beginning 1))
  19928. (setq marker (org-agenda-new-marker (match-beginning 0))
  19929. category (org-get-category)
  19930. tags (org-get-tags-at (point))
  19931. txt (org-format-agenda-item "" (match-string 1) category tags)
  19932. priority (1+ (org-get-priority txt)))
  19933. (org-add-props txt props
  19934. 'org-marker marker 'org-hd-marker marker
  19935. 'priority priority 'org-category category
  19936. 'type "todo")
  19937. (push txt ee)
  19938. (if org-agenda-todo-list-sublevels
  19939. (goto-char (match-end 1))
  19940. (org-end-of-subtree 'invisible))))
  19941. (nreverse ee)))
  19942. (defconst org-agenda-no-heading-message
  19943. "No heading for this item in buffer or region.")
  19944. (defun org-agenda-get-timestamps ()
  19945. "Return the date stamp information for agenda display."
  19946. (let* ((props (list 'face nil
  19947. 'org-not-done-regexp org-not-done-regexp
  19948. 'org-todo-regexp org-todo-regexp
  19949. 'mouse-face 'highlight
  19950. 'keymap org-agenda-keymap
  19951. 'help-echo
  19952. (format "mouse-2 or RET jump to org file %s"
  19953. (abbreviate-file-name buffer-file-name))))
  19954. (d1 (calendar-absolute-from-gregorian date))
  19955. (remove-re
  19956. (concat
  19957. (regexp-quote
  19958. (format-time-string
  19959. "<%Y-%m-%d"
  19960. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19961. ".*?>"))
  19962. (regexp
  19963. (concat
  19964. (regexp-quote
  19965. (substring
  19966. (format-time-string
  19967. (car org-time-stamp-formats)
  19968. (apply 'encode-time ; DATE bound by calendar
  19969. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19970. 0 11))
  19971. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19972. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19973. marker hdmarker deadlinep scheduledp donep tmp priority category
  19974. ee txt timestr tags b0 b3 e3 head)
  19975. (goto-char (point-min))
  19976. (while (re-search-forward regexp nil t)
  19977. (setq b0 (match-beginning 0)
  19978. b3 (match-beginning 3) e3 (match-end 3))
  19979. (catch :skip
  19980. (and (org-at-date-range-p) (throw :skip nil))
  19981. (org-agenda-skip)
  19982. (if (and (match-end 1)
  19983. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19984. (throw :skip nil))
  19985. (if (and e3
  19986. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19987. (throw :skip nil))
  19988. (setq marker (org-agenda-new-marker b0)
  19989. category (org-get-category b0)
  19990. tmp (buffer-substring (max (point-min)
  19991. (- b0 org-ds-keyword-length))
  19992. b0)
  19993. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19994. deadlinep (string-match org-deadline-regexp tmp)
  19995. scheduledp (string-match org-scheduled-regexp tmp)
  19996. donep (org-entry-is-done-p))
  19997. (if (or scheduledp deadlinep) (throw :skip t))
  19998. (if (string-match ">" timestr)
  19999. ;; substring should only run to end of time stamp
  20000. (setq timestr (substring timestr 0 (match-end 0))))
  20001. (save-excursion
  20002. (if (re-search-backward "^\\*+ " nil t)
  20003. (progn
  20004. (goto-char (match-beginning 0))
  20005. (setq hdmarker (org-agenda-new-marker)
  20006. tags (org-get-tags-at))
  20007. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20008. (setq head (match-string 1))
  20009. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  20010. (setq txt (org-format-agenda-item
  20011. nil head category tags timestr nil
  20012. remove-re)))
  20013. (setq txt org-agenda-no-heading-message))
  20014. (setq priority (org-get-priority txt))
  20015. (org-add-props txt props
  20016. 'org-marker marker 'org-hd-marker hdmarker)
  20017. (org-add-props txt nil 'priority priority
  20018. 'org-category category 'date date
  20019. 'type "timestamp")
  20020. (push txt ee))
  20021. (outline-next-heading)))
  20022. (nreverse ee)))
  20023. (defun org-agenda-get-sexps ()
  20024. "Return the sexp information for agenda display."
  20025. (require 'diary-lib)
  20026. (let* ((props (list 'face nil
  20027. 'mouse-face 'highlight
  20028. 'keymap org-agenda-keymap
  20029. 'help-echo
  20030. (format "mouse-2 or RET jump to org file %s"
  20031. (abbreviate-file-name buffer-file-name))))
  20032. (regexp "^&?%%(")
  20033. marker category ee txt tags entry result beg b sexp sexp-entry)
  20034. (goto-char (point-min))
  20035. (while (re-search-forward regexp nil t)
  20036. (catch :skip
  20037. (org-agenda-skip)
  20038. (setq beg (match-beginning 0))
  20039. (goto-char (1- (match-end 0)))
  20040. (setq b (point))
  20041. (forward-sexp 1)
  20042. (setq sexp (buffer-substring b (point)))
  20043. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20044. (org-trim (match-string 1))
  20045. ""))
  20046. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20047. (when result
  20048. (setq marker (org-agenda-new-marker beg)
  20049. category (org-get-category beg))
  20050. (if (string-match "\\S-" result)
  20051. (setq txt result)
  20052. (setq txt "SEXP entry returned empty string"))
  20053. (setq txt (org-format-agenda-item
  20054. "" txt category tags 'time))
  20055. (org-add-props txt props 'org-marker marker)
  20056. (org-add-props txt nil
  20057. 'org-category category 'date date
  20058. 'type "sexp")
  20059. (push txt ee))))
  20060. (nreverse ee)))
  20061. (defun org-agenda-get-closed ()
  20062. "Return the logged TODO entries for agenda display."
  20063. (let* ((props (list 'mouse-face 'highlight
  20064. 'org-not-done-regexp org-not-done-regexp
  20065. 'org-todo-regexp org-todo-regexp
  20066. 'keymap org-agenda-keymap
  20067. 'help-echo
  20068. (format "mouse-2 or RET jump to org file %s"
  20069. (abbreviate-file-name buffer-file-name))))
  20070. (regexp (concat
  20071. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20072. (regexp-quote
  20073. (substring
  20074. (format-time-string
  20075. (car org-time-stamp-formats)
  20076. (apply 'encode-time ; DATE bound by calendar
  20077. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20078. 1 11))))
  20079. marker hdmarker priority category tags closedp
  20080. ee txt timestr)
  20081. (goto-char (point-min))
  20082. (while (re-search-forward regexp nil t)
  20083. (catch :skip
  20084. (org-agenda-skip)
  20085. (setq marker (org-agenda-new-marker (match-beginning 0))
  20086. closedp (equal (match-string 1) org-closed-string)
  20087. category (org-get-category (match-beginning 0))
  20088. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20089. ;; donep (org-entry-is-done-p)
  20090. )
  20091. (if (string-match "\\]" timestr)
  20092. ;; substring should only run to end of time stamp
  20093. (setq timestr (substring timestr 0 (match-end 0))))
  20094. (save-excursion
  20095. (if (re-search-backward "^\\*+ " nil t)
  20096. (progn
  20097. (goto-char (match-beginning 0))
  20098. (setq hdmarker (org-agenda-new-marker)
  20099. tags (org-get-tags-at))
  20100. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20101. (setq txt (org-format-agenda-item
  20102. (if closedp "Closed: " "Clocked: ")
  20103. (match-string 1) category tags timestr)))
  20104. (setq txt org-agenda-no-heading-message))
  20105. (setq priority 100000)
  20106. (org-add-props txt props
  20107. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20108. 'priority priority 'org-category category
  20109. 'type "closed" 'date date
  20110. 'undone-face 'org-warning 'done-face 'org-done)
  20111. (push txt ee))
  20112. (goto-char (point-at-eol))))
  20113. (nreverse ee)))
  20114. (defun org-agenda-get-deadlines ()
  20115. "Return the deadline information for agenda display."
  20116. (let* ((props (list 'mouse-face 'highlight
  20117. 'org-not-done-regexp org-not-done-regexp
  20118. 'org-todo-regexp org-todo-regexp
  20119. 'keymap org-agenda-keymap
  20120. 'help-echo
  20121. (format "mouse-2 or RET jump to org file %s"
  20122. (abbreviate-file-name buffer-file-name))))
  20123. (regexp org-deadline-time-regexp)
  20124. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20125. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20126. d2 diff dfrac wdays pos pos1 category tags
  20127. ee txt head face s upcomingp donep timestr)
  20128. (goto-char (point-min))
  20129. (while (re-search-forward regexp nil t)
  20130. (catch :skip
  20131. (org-agenda-skip)
  20132. (setq s (match-string 1)
  20133. pos (1- (match-beginning 1))
  20134. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20135. diff (- d2 d1)
  20136. wdays (org-get-wdays s)
  20137. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20138. upcomingp (and todayp (> diff 0)))
  20139. ;; When to show a deadline in the calendar:
  20140. ;; If the expiration is within wdays warning time.
  20141. ;; Past-due deadlines are only shown on the current date
  20142. (if (or (and (<= diff wdays)
  20143. (and todayp (not org-agenda-only-exact-dates)))
  20144. (= diff 0))
  20145. (save-excursion
  20146. (setq category (org-get-category))
  20147. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20148. (progn
  20149. (goto-char (match-end 0))
  20150. (setq pos1 (match-beginning 0))
  20151. (setq tags (org-get-tags-at pos1))
  20152. (setq head (buffer-substring-no-properties
  20153. (point)
  20154. (progn (skip-chars-forward "^\r\n")
  20155. (point))))
  20156. (setq donep (string-match org-looking-at-done-regexp head))
  20157. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20158. (setq timestr
  20159. (concat (substring s (match-beginning 1)) " "))
  20160. (setq timestr 'time))
  20161. (if (and donep
  20162. (or org-agenda-skip-deadline-if-done
  20163. (not (= diff 0))))
  20164. (setq txt nil)
  20165. (setq txt (org-format-agenda-item
  20166. (if (= diff 0)
  20167. (car org-agenda-deadline-leaders)
  20168. (format (nth 1 org-agenda-deadline-leaders)
  20169. diff))
  20170. head category tags timestr))))
  20171. (setq txt org-agenda-no-heading-message))
  20172. (when txt
  20173. (setq face (org-agenda-deadline-face dfrac wdays))
  20174. (org-add-props txt props
  20175. 'org-marker (org-agenda-new-marker pos)
  20176. 'org-hd-marker (org-agenda-new-marker pos1)
  20177. 'priority (+ (- diff)
  20178. (org-get-priority txt))
  20179. 'org-category category
  20180. 'type (if upcomingp "upcoming-deadline" "deadline")
  20181. 'date (if upcomingp date d2)
  20182. 'face (if donep 'org-done face)
  20183. 'undone-face face 'done-face 'org-done)
  20184. (push txt ee))))))
  20185. (nreverse ee)))
  20186. (defun org-agenda-deadline-face (fraction &optional wdays)
  20187. "Return the face to displaying a deadline item.
  20188. FRACTION is what fraction of the head-warning time has passed."
  20189. (if (equal wdays 0) (setq fraction 1.))
  20190. (let ((faces org-agenda-deadline-faces) f)
  20191. (catch 'exit
  20192. (while (setq f (pop faces))
  20193. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20194. (defun org-agenda-get-scheduled ()
  20195. "Return the scheduled information for agenda display."
  20196. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20197. 'org-todo-regexp org-todo-regexp
  20198. 'done-face 'org-done
  20199. 'mouse-face 'highlight
  20200. 'keymap org-agenda-keymap
  20201. 'help-echo
  20202. (format "mouse-2 or RET jump to org file %s"
  20203. (abbreviate-file-name buffer-file-name))))
  20204. (regexp org-scheduled-time-regexp)
  20205. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20206. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20207. d2 diff pos pos1 category tags
  20208. ee txt head pastschedp donep face timestr s)
  20209. (goto-char (point-min))
  20210. (while (re-search-forward regexp nil t)
  20211. (catch :skip
  20212. (org-agenda-skip)
  20213. (setq s (match-string 1)
  20214. pos (1- (match-beginning 1))
  20215. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20216. ;;; is this right?
  20217. ;;; do we need to do this for deadleine too????
  20218. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20219. diff (- d2 d1))
  20220. (setq pastschedp (and todayp (< diff 0)))
  20221. ;; When to show a scheduled item in the calendar:
  20222. ;; If it is on or past the date.
  20223. (if (or (and (< diff 0)
  20224. (and todayp (not org-agenda-only-exact-dates)))
  20225. (= diff 0))
  20226. (save-excursion
  20227. (setq category (org-get-category))
  20228. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20229. (progn
  20230. (goto-char (match-end 0))
  20231. (setq pos1 (match-beginning 0))
  20232. (setq tags (org-get-tags-at))
  20233. (setq head (buffer-substring-no-properties
  20234. (point)
  20235. (progn (skip-chars-forward "^\r\n") (point))))
  20236. (setq donep (string-match org-looking-at-done-regexp head))
  20237. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20238. (setq timestr
  20239. (concat (substring s (match-beginning 1)) " "))
  20240. (setq timestr 'time))
  20241. (if (and donep
  20242. (or org-agenda-skip-scheduled-if-done
  20243. (not (= diff 0))))
  20244. (setq txt nil)
  20245. (setq txt (org-format-agenda-item
  20246. (if (= diff 0)
  20247. (car org-agenda-scheduled-leaders)
  20248. (format (nth 1 org-agenda-scheduled-leaders)
  20249. (- 1 diff)))
  20250. head category tags timestr))))
  20251. (setq txt org-agenda-no-heading-message))
  20252. (when txt
  20253. (setq face (if pastschedp
  20254. 'org-scheduled-previously
  20255. 'org-scheduled-today))
  20256. (org-add-props txt props
  20257. 'undone-face face
  20258. 'face (if donep 'org-done face)
  20259. 'org-marker (org-agenda-new-marker pos)
  20260. 'org-hd-marker (org-agenda-new-marker pos1)
  20261. 'type (if pastschedp "past-scheduled" "scheduled")
  20262. 'date (if pastschedp d2 date)
  20263. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20264. 'org-category category)
  20265. (push txt ee))))))
  20266. (nreverse ee)))
  20267. (defun org-agenda-get-blocks ()
  20268. "Return the date-range information for agenda display."
  20269. (let* ((props (list 'face nil
  20270. 'org-not-done-regexp org-not-done-regexp
  20271. 'org-todo-regexp org-todo-regexp
  20272. 'mouse-face 'highlight
  20273. 'keymap org-agenda-keymap
  20274. 'help-echo
  20275. (format "mouse-2 or RET jump to org file %s"
  20276. (abbreviate-file-name buffer-file-name))))
  20277. (regexp org-tr-regexp)
  20278. (d0 (calendar-absolute-from-gregorian date))
  20279. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20280. donep head)
  20281. (goto-char (point-min))
  20282. (while (re-search-forward regexp nil t)
  20283. (catch :skip
  20284. (org-agenda-skip)
  20285. (setq pos (point))
  20286. (setq timestr (match-string 0)
  20287. s1 (match-string 1)
  20288. s2 (match-string 2)
  20289. d1 (time-to-days (org-time-string-to-time s1))
  20290. d2 (time-to-days (org-time-string-to-time s2)))
  20291. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20292. ;; Only allow days between the limits, because the normal
  20293. ;; date stamps will catch the limits.
  20294. (save-excursion
  20295. (setq marker (org-agenda-new-marker (point)))
  20296. (setq category (org-get-category))
  20297. (if (re-search-backward "^\\*+ " nil t)
  20298. (progn
  20299. (goto-char (match-beginning 0))
  20300. (setq hdmarker (org-agenda-new-marker (point)))
  20301. (setq tags (org-get-tags-at))
  20302. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20303. (setq head (match-string 1))
  20304. (and org-agenda-skip-timestamp-if-done
  20305. (org-entry-is-done-p)
  20306. (throw :skip t))
  20307. (setq txt (org-format-agenda-item
  20308. (format (if (= d1 d2) "" "(%d/%d): ")
  20309. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20310. head category tags
  20311. (if (= d0 d1) timestr))))
  20312. (setq txt org-agenda-no-heading-message))
  20313. (org-add-props txt props
  20314. 'org-marker marker 'org-hd-marker hdmarker
  20315. 'type "block" 'date date
  20316. 'priority (org-get-priority txt) 'org-category category)
  20317. (push txt ee)))
  20318. (goto-char pos)))
  20319. ;; Sort the entries by expiration date.
  20320. (nreverse ee)))
  20321. ;;; Agenda presentation and sorting
  20322. (defconst org-plain-time-of-day-regexp
  20323. (concat
  20324. "\\(\\<[012]?[0-9]"
  20325. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20326. "\\(--?"
  20327. "\\(\\<[012]?[0-9]"
  20328. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20329. "\\)?")
  20330. "Regular expression to match a plain time or time range.
  20331. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20332. groups carry important information:
  20333. 0 the full match
  20334. 1 the first time, range or not
  20335. 8 the second time, if it is a range.")
  20336. (defconst org-plain-time-extension-regexp
  20337. (concat
  20338. "\\(\\<[012]?[0-9]"
  20339. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20340. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20341. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20342. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20343. groups carry important information:
  20344. 0 the full match
  20345. 7 hours of duration
  20346. 9 minutes of duration")
  20347. (defconst org-stamp-time-of-day-regexp
  20348. (concat
  20349. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20350. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20351. "\\(--?"
  20352. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20353. "Regular expression to match a timestamp time or time range.
  20354. After a match, the following groups carry important information:
  20355. 0 the full match
  20356. 1 date plus weekday, for backreferencing to make sure both times on same day
  20357. 2 the first time, range or not
  20358. 4 the second time, if it is a range.")
  20359. (defvar org-prefix-has-time nil
  20360. "A flag, set by `org-compile-prefix-format'.
  20361. The flag is set if the currently compiled format contains a `%t'.")
  20362. (defvar org-prefix-has-tag nil
  20363. "A flag, set by `org-compile-prefix-format'.
  20364. The flag is set if the currently compiled format contains a `%T'.")
  20365. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20366. noprefix remove-re)
  20367. "Format TXT to be inserted into the agenda buffer.
  20368. In particular, it adds the prefix and corresponding text properties. EXTRA
  20369. must be a string and replaces the `%s' specifier in the prefix format.
  20370. CATEGORY (string, symbol or nil) may be used to overrule the default
  20371. category taken from local variable or file name. It will replace the `%c'
  20372. specifier in the format. DOTIME, when non-nil, indicates that a
  20373. time-of-day should be extracted from TXT for sorting of this entry, and for
  20374. the `%t' specifier in the format. When DOTIME is a string, this string is
  20375. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20376. only the correctly processes TXT should be returned - this is used by
  20377. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20378. Any match of REMOVE-RE will be removed from TXT."
  20379. (save-match-data
  20380. ;; Diary entries sometimes have extra whitespace at the beginning
  20381. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20382. (let* ((category (or category
  20383. org-category
  20384. (if buffer-file-name
  20385. (file-name-sans-extension
  20386. (file-name-nondirectory buffer-file-name))
  20387. "")))
  20388. (tag (if tags (nth (1- (length tags)) tags) ""))
  20389. time ; time and tag are needed for the eval of the prefix format
  20390. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20391. (time-of-day (and dotime (org-get-time-of-day ts)))
  20392. stamp plain s0 s1 s2 rtn srp)
  20393. (when (and dotime time-of-day org-prefix-has-time)
  20394. ;; Extract starting and ending time and move them to prefix
  20395. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20396. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20397. (setq s0 (match-string 0 ts)
  20398. srp (and stamp (match-end 3))
  20399. s1 (match-string (if plain 1 2) ts)
  20400. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20401. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20402. ;; them, we might want to remove them there to avoid duplication.
  20403. ;; The user can turn this off with a variable.
  20404. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20405. (string-match (concat (regexp-quote s0) " *") txt)
  20406. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20407. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20408. (= (match-beginning 0) 0)
  20409. t))
  20410. (setq txt (replace-match "" nil nil txt))))
  20411. ;; Normalize the time(s) to 24 hour
  20412. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20413. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20414. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20415. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20416. (let ((m (+ (string-to-number (match-string 2 s1))
  20417. (* 60 (string-to-number (match-string 1 s1)))
  20418. org-agenda-default-appointment-duration))
  20419. h)
  20420. (setq h (/ m 60) m (- m (* h 60)))
  20421. (setq s2 (format "%02d:%02d" h m))))
  20422. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20423. txt)
  20424. ;; Tags are in the string
  20425. (if (or (eq org-agenda-remove-tags t)
  20426. (and org-agenda-remove-tags
  20427. org-prefix-has-tag))
  20428. (setq txt (replace-match "" t t txt))
  20429. (setq txt (replace-match
  20430. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20431. (match-string 2 txt))
  20432. t t txt))))
  20433. (when remove-re
  20434. (while (string-match remove-re txt)
  20435. (setq txt (replace-match "" t t txt))))
  20436. ;; Create the final string
  20437. (if noprefix
  20438. (setq rtn txt)
  20439. ;; Prepare the variables needed in the eval of the compiled format
  20440. (setq time (cond (s2 (concat s1 "-" s2))
  20441. (s1 (concat s1 "......"))
  20442. (t ""))
  20443. extra (or extra "")
  20444. category (if (symbolp category) (symbol-name category) category))
  20445. ;; Evaluate the compiled format
  20446. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20447. ;; And finally add the text properties
  20448. (org-add-props rtn nil
  20449. 'org-category (downcase category) 'tags tags
  20450. 'org-highest-priority org-highest-priority
  20451. 'org-lowest-priority org-lowest-priority
  20452. 'prefix-length (- (length rtn) (length txt))
  20453. 'time-of-day time-of-day
  20454. 'txt txt
  20455. 'time time
  20456. 'extra extra
  20457. 'dotime dotime))))
  20458. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20459. (defvar org-agenda-sorting-strategy-selected nil)
  20460. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20461. (catch 'exit
  20462. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20463. ((and todayp (member 'today (car org-agenda-time-grid))))
  20464. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20465. ((member 'weekly (car org-agenda-time-grid)))
  20466. (t (throw 'exit list)))
  20467. (let* ((have (delq nil (mapcar
  20468. (lambda (x) (get-text-property 1 'time-of-day x))
  20469. list)))
  20470. (string (nth 1 org-agenda-time-grid))
  20471. (gridtimes (nth 2 org-agenda-time-grid))
  20472. (req (car org-agenda-time-grid))
  20473. (remove (member 'remove-match req))
  20474. new time)
  20475. (if (and (member 'require-timed req) (not have))
  20476. ;; don't show empty grid
  20477. (throw 'exit list))
  20478. (while (setq time (pop gridtimes))
  20479. (unless (and remove (member time have))
  20480. (setq time (int-to-string time))
  20481. (push (org-format-agenda-item
  20482. nil string "" nil
  20483. (concat (substring time 0 -2) ":" (substring time -2)))
  20484. new)
  20485. (put-text-property
  20486. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20487. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20488. (append new list)
  20489. (append list new)))))
  20490. (defun org-compile-prefix-format (key)
  20491. "Compile the prefix format into a Lisp form that can be evaluated.
  20492. The resulting form is returned and stored in the variable
  20493. `org-prefix-format-compiled'."
  20494. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20495. (let ((s (cond
  20496. ((stringp org-agenda-prefix-format)
  20497. org-agenda-prefix-format)
  20498. ((assq key org-agenda-prefix-format)
  20499. (cdr (assq key org-agenda-prefix-format)))
  20500. (t " %-12:c%?-12t% s")))
  20501. (start 0)
  20502. varform vars var e c f opt)
  20503. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20504. s start)
  20505. (setq var (cdr (assoc (match-string 4 s)
  20506. '(("c" . category) ("t" . time) ("s" . extra)
  20507. ("T" . tag))))
  20508. c (or (match-string 3 s) "")
  20509. opt (match-beginning 1)
  20510. start (1+ (match-beginning 0)))
  20511. (if (equal var 'time) (setq org-prefix-has-time t))
  20512. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20513. (setq f (concat "%" (match-string 2 s) "s"))
  20514. (if opt
  20515. (setq varform
  20516. `(if (equal "" ,var)
  20517. ""
  20518. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20519. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20520. (setq s (replace-match "%s" t nil s))
  20521. (push varform vars))
  20522. (setq vars (nreverse vars))
  20523. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20524. (defun org-set-sorting-strategy (key)
  20525. (if (symbolp (car org-agenda-sorting-strategy))
  20526. ;; the old format
  20527. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20528. (setq org-agenda-sorting-strategy-selected
  20529. (or (cdr (assq key org-agenda-sorting-strategy))
  20530. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20531. '(time-up category-keep priority-down)))))
  20532. (defun org-get-time-of-day (s &optional string mod24)
  20533. "Check string S for a time of day.
  20534. If found, return it as a military time number between 0 and 2400.
  20535. If not found, return nil.
  20536. The optional STRING argument forces conversion into a 5 character wide string
  20537. HH:MM."
  20538. (save-match-data
  20539. (when
  20540. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20541. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20542. (let* ((h (string-to-number (match-string 1 s)))
  20543. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20544. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20545. (am-p (equal ampm "am"))
  20546. (h1 (cond ((not ampm) h)
  20547. ((= h 12) (if am-p 0 12))
  20548. (t (+ h (if am-p 0 12)))))
  20549. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20550. (mod h1 24) h1))
  20551. (t0 (+ (* 100 h2) m))
  20552. (t1 (concat (if (>= h1 24) "+" " ")
  20553. (if (< t0 100) "0" "")
  20554. (if (< t0 10) "0" "")
  20555. (int-to-string t0))))
  20556. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20557. (defun org-finalize-agenda-entries (list &optional nosort)
  20558. "Sort and concatenate the agenda items."
  20559. (setq list (mapcar 'org-agenda-highlight-todo list))
  20560. (if nosort
  20561. list
  20562. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20563. (defun org-agenda-highlight-todo (x)
  20564. (let (re pl)
  20565. (if (eq x 'line)
  20566. (save-excursion
  20567. (beginning-of-line 1)
  20568. (setq re (get-text-property (point) 'org-todo-regexp))
  20569. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20570. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20571. (add-text-properties (match-beginning 0) (match-end 0)
  20572. (list 'face (org-get-todo-face 0)))
  20573. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20574. (delete-region (match-beginning 1) (1- (match-end 0)))
  20575. (goto-char (match-beginning 1))
  20576. (insert (format org-agenda-todo-keyword-format s)))))
  20577. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20578. pl (get-text-property 0 'prefix-length x))
  20579. (when (and re
  20580. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20581. x (or pl 0)) pl))
  20582. (add-text-properties
  20583. (or (match-end 1) (match-end 0)) (match-end 0)
  20584. (list 'face (org-get-todo-face (match-string 2 x)))
  20585. x)
  20586. (setq x (concat (substring x 0 (match-end 1))
  20587. (format org-agenda-todo-keyword-format
  20588. (match-string 2 x))
  20589. " "
  20590. (substring x (match-end 3)))))
  20591. x)))
  20592. (defsubst org-cmp-priority (a b)
  20593. "Compare the priorities of string A and B."
  20594. (let ((pa (or (get-text-property 1 'priority a) 0))
  20595. (pb (or (get-text-property 1 'priority b) 0)))
  20596. (cond ((> pa pb) +1)
  20597. ((< pa pb) -1)
  20598. (t nil))))
  20599. (defsubst org-cmp-category (a b)
  20600. "Compare the string values of categories of strings A and B."
  20601. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20602. (cb (or (get-text-property 1 'org-category b) "")))
  20603. (cond ((string-lessp ca cb) -1)
  20604. ((string-lessp cb ca) +1)
  20605. (t nil))))
  20606. (defsubst org-cmp-tag (a b)
  20607. "Compare the string values of categories of strings A and B."
  20608. (let ((ta (car (last (get-text-property 1 'tags a))))
  20609. (tb (car (last (get-text-property 1 'tags b)))))
  20610. (cond ((not ta) +1)
  20611. ((not tb) -1)
  20612. ((string-lessp ta tb) -1)
  20613. ((string-lessp tb ta) +1)
  20614. (t nil))))
  20615. (defsubst org-cmp-time (a b)
  20616. "Compare the time-of-day values of strings A and B."
  20617. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20618. (ta (or (get-text-property 1 'time-of-day a) def))
  20619. (tb (or (get-text-property 1 'time-of-day b) def)))
  20620. (cond ((< ta tb) -1)
  20621. ((< tb ta) +1)
  20622. (t nil))))
  20623. (defun org-entries-lessp (a b)
  20624. "Predicate for sorting agenda entries."
  20625. ;; The following variables will be used when the form is evaluated.
  20626. ;; So even though the compiler complains, keep them.
  20627. (let* ((time-up (org-cmp-time a b))
  20628. (time-down (if time-up (- time-up) nil))
  20629. (priority-up (org-cmp-priority a b))
  20630. (priority-down (if priority-up (- priority-up) nil))
  20631. (category-up (org-cmp-category a b))
  20632. (category-down (if category-up (- category-up) nil))
  20633. (category-keep (if category-up +1 nil))
  20634. (tag-up (org-cmp-tag a b))
  20635. (tag-down (if tag-up (- tag-up) nil)))
  20636. (cdr (assoc
  20637. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20638. '((-1 . t) (1 . nil) (nil . nil))))))
  20639. ;;; Agenda restriction lock
  20640. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20641. "Overlay to mark the headline to which arenda commands are restricted.")
  20642. (org-overlay-put org-agenda-restriction-lock-overlay
  20643. 'face 'org-agenda-restriction-lock)
  20644. (org-overlay-put org-agenda-restriction-lock-overlay
  20645. 'help-echo "Agendas are currently limited to this subtree.")
  20646. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20647. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20648. "Overlay marking the agenda restriction line in speedbar.")
  20649. (org-overlay-put org-speedbar-restriction-lock-overlay
  20650. 'face 'org-agenda-restriction-lock)
  20651. (org-overlay-put org-speedbar-restriction-lock-overlay
  20652. 'help-echo "Agendas are currently limited to this item.")
  20653. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20654. (defun org-agenda-set-restriction-lock (&optional type)
  20655. "Set restriction lock for agenda, to current subtree or file.
  20656. Restriction will be the file if TYPE is `file', or if type is the
  20657. universal prefix '(4), or if the cursor is before the first headline
  20658. in the file. Otherwise, restriction will be to the current subtree."
  20659. (interactive "P")
  20660. (and (equal type '(4)) (setq type 'file))
  20661. (setq type (cond
  20662. (type type)
  20663. ((org-at-heading-p) 'subtree)
  20664. ((condition-case nil (org-back-to-heading t) (error nil))
  20665. 'subtree)
  20666. (t 'file)))
  20667. (if (eq type 'subtree)
  20668. (progn
  20669. (setq org-agenda-restrict t)
  20670. (setq org-agenda-overriding-restriction 'subtree)
  20671. (put 'org-agenda-files 'org-restrict
  20672. (list (buffer-file-name (buffer-base-buffer))))
  20673. (org-back-to-heading t)
  20674. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20675. (move-marker org-agenda-restrict-begin (point))
  20676. (move-marker org-agenda-restrict-end
  20677. (save-excursion (org-end-of-subtree t)))
  20678. (message "Locking agenda restriction to subtree"))
  20679. (put 'org-agenda-files 'org-restrict
  20680. (list (buffer-file-name (buffer-base-buffer))))
  20681. (setq org-agenda-restrict nil)
  20682. (setq org-agenda-overriding-restriction 'file)
  20683. (move-marker org-agenda-restrict-begin nil)
  20684. (move-marker org-agenda-restrict-end nil)
  20685. (message "Locking agenda restriction to file"))
  20686. (setq current-prefix-arg nil)
  20687. (org-agenda-maybe-redo))
  20688. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20689. "Remove the agenda restriction lock."
  20690. (interactive "P")
  20691. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20692. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20693. (setq org-agenda-overriding-restriction nil)
  20694. (setq org-agenda-restrict nil)
  20695. (put 'org-agenda-files 'org-restrict nil)
  20696. (move-marker org-agenda-restrict-begin nil)
  20697. (move-marker org-agenda-restrict-end nil)
  20698. (setq current-prefix-arg nil)
  20699. (message "Agenda restriction lock removed")
  20700. (or noupdate (org-agenda-maybe-redo)))
  20701. (defun org-agenda-maybe-redo ()
  20702. "If there is any window showing the agenda view, update it."
  20703. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20704. (w0 (selected-window)))
  20705. (when w
  20706. (select-window w)
  20707. (org-agenda-redo)
  20708. (select-window w0)
  20709. (if org-agenda-overriding-restriction
  20710. (message "Agenda view shifted to new %s restriction"
  20711. org-agenda-overriding-restriction)
  20712. (message "Agenda restriction lock removed")))))
  20713. ;;; Agenda commands
  20714. (defun org-agenda-check-type (error &rest types)
  20715. "Check if agenda buffer is of allowed type.
  20716. If ERROR is non-nil, throw an error, otherwise just return nil."
  20717. (if (memq org-agenda-type types)
  20718. t
  20719. (if error
  20720. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20721. nil)))
  20722. (defun org-agenda-quit ()
  20723. "Exit agenda by removing the window or the buffer."
  20724. (interactive)
  20725. (let ((buf (current-buffer)))
  20726. (if (not (one-window-p)) (delete-window))
  20727. (kill-buffer buf)
  20728. (org-agenda-reset-markers)
  20729. (org-columns-remove-overlays))
  20730. ;; Maybe restore the pre-agenda window configuration.
  20731. (and org-agenda-restore-windows-after-quit
  20732. (not (eq org-agenda-window-setup 'other-frame))
  20733. org-pre-agenda-window-conf
  20734. (set-window-configuration org-pre-agenda-window-conf)))
  20735. (defun org-agenda-exit ()
  20736. "Exit agenda by removing the window or the buffer.
  20737. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20738. Org-mode buffers visited directly by the user will not be touched."
  20739. (interactive)
  20740. (org-release-buffers org-agenda-new-buffers)
  20741. (setq org-agenda-new-buffers nil)
  20742. (org-agenda-quit))
  20743. (defun org-agenda-execute (arg)
  20744. "Execute another agenda command, keeping same window.\\<global-map>
  20745. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20746. (interactive "P")
  20747. (let ((org-agenda-window-setup 'current-window))
  20748. (org-agenda arg)))
  20749. (defun org-save-all-org-buffers ()
  20750. "Save all Org-mode buffers without user confirmation."
  20751. (interactive)
  20752. (message "Saving all Org-mode buffers...")
  20753. (save-some-buffers t 'org-mode-p)
  20754. (message "Saving all Org-mode buffers... done"))
  20755. (defun org-agenda-redo ()
  20756. "Rebuild Agenda.
  20757. When this is the global TODO list, a prefix argument will be interpreted."
  20758. (interactive)
  20759. (let* ((org-agenda-keep-modes t)
  20760. (line (org-current-line))
  20761. (window-line (- line (org-current-line (window-start))))
  20762. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20763. (message "Rebuilding agenda buffer...")
  20764. (org-let lprops '(eval org-agenda-redo-command))
  20765. (setq org-agenda-undo-list nil
  20766. org-agenda-pending-undo-list nil)
  20767. (message "Rebuilding agenda buffer...done")
  20768. (goto-line line)
  20769. (recenter window-line)))
  20770. (defun org-agenda-manipulate-query-add ()
  20771. "Manipulate the query by adding a search term with positive selection.
  20772. Positive selection means, the term must be matched for selection of an entry."
  20773. (interactive)
  20774. (org-agenda-manipulate-query ?\[))
  20775. (defun org-agenda-manipulate-query-subtract ()
  20776. "Manipulate the query by adding a search term with negative selection.
  20777. Negative selection means, term must not be matched for selection of an entry."
  20778. (interactive)
  20779. (org-agenda-manipulate-query ?\]))
  20780. (defun org-agenda-manipulate-query-add-re ()
  20781. "Manipulate the query by adding a search regexp with positive selection.
  20782. Positive selection means, the regexp must match for selection of an entry."
  20783. (interactive)
  20784. (org-agenda-manipulate-query ?\{))
  20785. (defun org-agenda-manipulate-query-subtract-re ()
  20786. "Manipulate the query by adding a search regexp with negative selection.
  20787. Negative selection means, regexp must not match for selection of an entry."
  20788. (interactive)
  20789. (org-agenda-manipulate-query ?\}))
  20790. (defun org-agenda-manipulate-query (char)
  20791. (cond
  20792. ((eq org-agenda-type 'search)
  20793. (org-add-to-string
  20794. 'org-agenda-query-string
  20795. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20796. (?\{ . " +{}") (?\} . " -{}")))))
  20797. (setq org-agenda-redo-command
  20798. (list 'org-search-view
  20799. (+ (length org-agenda-query-string)
  20800. (if (member char '(?\{ ?\})) 0 1))
  20801. org-agenda-query-string))
  20802. (set-register org-agenda-query-register org-agenda-query-string)
  20803. (org-agenda-redo))
  20804. (t (error "Canot manipulate query for %s-type agenda buffers"
  20805. org-agenda-type))))
  20806. (defun org-add-to-string (var string)
  20807. (set var (concat (symbol-value var) string)))
  20808. (defun org-agenda-goto-date (date)
  20809. "Jump to DATE in agenda."
  20810. (interactive (list (org-read-date)))
  20811. (org-agenda-list nil date))
  20812. (defun org-agenda-goto-today ()
  20813. "Go to today."
  20814. (interactive)
  20815. (org-agenda-check-type t 'timeline 'agenda)
  20816. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20817. (cond
  20818. (tdpos (goto-char tdpos))
  20819. ((eq org-agenda-type 'agenda)
  20820. (let* ((sd (time-to-days
  20821. (time-subtract (current-time)
  20822. (list 0 (* 3600 org-extend-today-until) 0))))
  20823. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20824. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20825. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20826. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20827. (org-agenda-redo)
  20828. (org-agenda-find-same-or-today-or-agenda)))
  20829. (t (error "Cannot find today")))))
  20830. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20831. (goto-char
  20832. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20833. (text-property-any (point-min) (point-max) 'org-today t)
  20834. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20835. (point-min))))
  20836. (defun org-agenda-later (arg)
  20837. "Go forward in time by thee current span.
  20838. With prefix ARG, go forward that many times the current span."
  20839. (interactive "p")
  20840. (org-agenda-check-type t 'agenda)
  20841. (let* ((span org-agenda-span)
  20842. (sd org-starting-day)
  20843. (greg (calendar-gregorian-from-absolute sd))
  20844. (cnt (get-text-property (point) 'org-day-cnt))
  20845. greg2 nd)
  20846. (cond
  20847. ((eq span 'day)
  20848. (setq sd (+ arg sd) nd 1))
  20849. ((eq span 'week)
  20850. (setq sd (+ (* 7 arg) sd) nd 7))
  20851. ((eq span 'month)
  20852. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20853. sd (calendar-absolute-from-gregorian greg2))
  20854. (setcar greg2 (1+ (car greg2)))
  20855. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20856. ((eq span 'year)
  20857. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20858. sd (calendar-absolute-from-gregorian greg2))
  20859. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20860. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20861. (let ((org-agenda-overriding-arguments
  20862. (list (car org-agenda-last-arguments) sd nd t)))
  20863. (org-agenda-redo)
  20864. (org-agenda-find-same-or-today-or-agenda cnt))))
  20865. (defun org-agenda-earlier (arg)
  20866. "Go backward in time by the current span.
  20867. With prefix ARG, go backward that many times the current span."
  20868. (interactive "p")
  20869. (org-agenda-later (- arg)))
  20870. (defun org-agenda-day-view ()
  20871. "Switch to daily view for agenda."
  20872. (interactive)
  20873. (setq org-agenda-ndays 1)
  20874. (org-agenda-change-time-span 'day))
  20875. (defun org-agenda-week-view ()
  20876. "Switch to daily view for agenda."
  20877. (interactive)
  20878. (setq org-agenda-ndays 7)
  20879. (org-agenda-change-time-span 'week))
  20880. (defun org-agenda-month-view ()
  20881. "Switch to daily view for agenda."
  20882. (interactive)
  20883. (org-agenda-change-time-span 'month))
  20884. (defun org-agenda-year-view ()
  20885. "Switch to daily view for agenda."
  20886. (interactive)
  20887. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20888. (org-agenda-change-time-span 'year)
  20889. (error "Abort")))
  20890. (defun org-agenda-change-time-span (span)
  20891. "Change the agenda view to SPAN.
  20892. SPAN may be `day', `week', `month', `year'."
  20893. (org-agenda-check-type t 'agenda)
  20894. (if (equal org-agenda-span span)
  20895. (error "Viewing span is already \"%s\"" span))
  20896. (let* ((sd (or (get-text-property (point) 'day)
  20897. org-starting-day))
  20898. (computed (org-agenda-compute-time-span sd span))
  20899. (org-agenda-overriding-arguments
  20900. (list (car org-agenda-last-arguments)
  20901. (car computed) (cdr computed) t)))
  20902. (org-agenda-redo)
  20903. (org-agenda-find-same-or-today-or-agenda))
  20904. (org-agenda-set-mode-name)
  20905. (message "Switched to %s view" span))
  20906. (defun org-agenda-compute-time-span (sd span)
  20907. "Compute starting date and number of days for agenda.
  20908. SPAN may be `day', `week', `month', `year'. The return value
  20909. is a cons cell with the starting date and the number of days,
  20910. so that the date SD will be in that range."
  20911. (let* ((greg (calendar-gregorian-from-absolute sd))
  20912. nd)
  20913. (cond
  20914. ((eq span 'day)
  20915. (setq nd 1))
  20916. ((eq span 'week)
  20917. (let* ((nt (calendar-day-of-week
  20918. (calendar-gregorian-from-absolute sd)))
  20919. (d (if org-agenda-start-on-weekday
  20920. (- nt org-agenda-start-on-weekday)
  20921. 0)))
  20922. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20923. (setq nd 7)))
  20924. ((eq span 'month)
  20925. (setq sd (calendar-absolute-from-gregorian
  20926. (list (car greg) 1 (nth 2 greg)))
  20927. nd (- (calendar-absolute-from-gregorian
  20928. (list (1+ (car greg)) 1 (nth 2 greg)))
  20929. sd)))
  20930. ((eq span 'year)
  20931. (setq sd (calendar-absolute-from-gregorian
  20932. (list 1 1 (nth 2 greg)))
  20933. nd (- (calendar-absolute-from-gregorian
  20934. (list 1 1 (1+ (nth 2 greg))))
  20935. sd))))
  20936. (cons sd nd)))
  20937. ;; FIXME: does not work if user makes date format that starts with a blank
  20938. (defun org-agenda-next-date-line (&optional arg)
  20939. "Jump to the next line indicating a date in agenda buffer."
  20940. (interactive "p")
  20941. (org-agenda-check-type t 'agenda 'timeline)
  20942. (beginning-of-line 1)
  20943. (if (looking-at "^\\S-") (forward-char 1))
  20944. (if (not (re-search-forward "^\\S-" nil t arg))
  20945. (progn
  20946. (backward-char 1)
  20947. (error "No next date after this line in this buffer")))
  20948. (goto-char (match-beginning 0)))
  20949. (defun org-agenda-previous-date-line (&optional arg)
  20950. "Jump to the previous line indicating a date in agenda buffer."
  20951. (interactive "p")
  20952. (org-agenda-check-type t 'agenda 'timeline)
  20953. (beginning-of-line 1)
  20954. (if (not (re-search-backward "^\\S-" nil t arg))
  20955. (error "No previous date before this line in this buffer")))
  20956. ;; Initialize the highlight
  20957. (defvar org-hl (org-make-overlay 1 1))
  20958. (org-overlay-put org-hl 'face 'highlight)
  20959. (defun org-highlight (begin end &optional buffer)
  20960. "Highlight a region with overlay."
  20961. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20962. org-hl begin end (or buffer (current-buffer))))
  20963. (defun org-unhighlight ()
  20964. "Detach overlay INDEX."
  20965. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20966. ;; FIXME this is currently not used.
  20967. (defun org-highlight-until-next-command (beg end &optional buffer)
  20968. (org-highlight beg end buffer)
  20969. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20970. (defun org-unhighlight-once ()
  20971. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20972. (org-unhighlight))
  20973. (defun org-agenda-follow-mode ()
  20974. "Toggle follow mode in an agenda buffer."
  20975. (interactive)
  20976. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20977. (org-agenda-set-mode-name)
  20978. (message "Follow mode is %s"
  20979. (if org-agenda-follow-mode "on" "off")))
  20980. (defun org-agenda-log-mode ()
  20981. "Toggle log mode in an agenda buffer."
  20982. (interactive)
  20983. (org-agenda-check-type t 'agenda 'timeline)
  20984. (setq org-agenda-show-log (not org-agenda-show-log))
  20985. (org-agenda-set-mode-name)
  20986. (org-agenda-redo)
  20987. (message "Log mode is %s"
  20988. (if org-agenda-show-log "on" "off")))
  20989. (defun org-agenda-toggle-diary ()
  20990. "Toggle diary inclusion in an agenda buffer."
  20991. (interactive)
  20992. (org-agenda-check-type t 'agenda)
  20993. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20994. (org-agenda-redo)
  20995. (org-agenda-set-mode-name)
  20996. (message "Diary inclusion turned %s"
  20997. (if org-agenda-include-diary "on" "off")))
  20998. (defun org-agenda-toggle-time-grid ()
  20999. "Toggle time grid in an agenda buffer."
  21000. (interactive)
  21001. (org-agenda-check-type t 'agenda)
  21002. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  21003. (org-agenda-redo)
  21004. (org-agenda-set-mode-name)
  21005. (message "Time-grid turned %s"
  21006. (if org-agenda-use-time-grid "on" "off")))
  21007. (defun org-agenda-set-mode-name ()
  21008. "Set the mode name to indicate all the small mode settings."
  21009. (setq mode-name
  21010. (concat "Org-Agenda"
  21011. (if (equal org-agenda-ndays 1) " Day" "")
  21012. (if (equal org-agenda-ndays 7) " Week" "")
  21013. (if org-agenda-follow-mode " Follow" "")
  21014. (if org-agenda-include-diary " Diary" "")
  21015. (if org-agenda-use-time-grid " Grid" "")
  21016. (if org-agenda-show-log " Log" "")))
  21017. (force-mode-line-update))
  21018. (defun org-agenda-post-command-hook ()
  21019. (and (eolp) (not (bolp)) (backward-char 1))
  21020. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21021. (if (and org-agenda-follow-mode
  21022. (get-text-property (point) 'org-marker))
  21023. (org-agenda-show)))
  21024. (defun org-agenda-show-priority ()
  21025. "Show the priority of the current item.
  21026. This priority is composed of the main priority given with the [#A] cookies,
  21027. and by additional input from the age of a schedules or deadline entry."
  21028. (interactive)
  21029. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21030. (message "Priority is %d" (if pri pri -1000))))
  21031. (defun org-agenda-show-tags ()
  21032. "Show the tags applicable to the current item."
  21033. (interactive)
  21034. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21035. (if tags
  21036. (message "Tags are :%s:"
  21037. (org-no-properties (mapconcat 'identity tags ":")))
  21038. (message "No tags associated with this line"))))
  21039. (defun org-agenda-goto (&optional highlight)
  21040. "Go to the Org-mode file which contains the item at point."
  21041. (interactive)
  21042. (let* ((marker (or (get-text-property (point) 'org-marker)
  21043. (org-agenda-error)))
  21044. (buffer (marker-buffer marker))
  21045. (pos (marker-position marker)))
  21046. (switch-to-buffer-other-window buffer)
  21047. (widen)
  21048. (goto-char pos)
  21049. (when (org-mode-p)
  21050. (org-show-context 'agenda)
  21051. (save-excursion
  21052. (and (outline-next-heading)
  21053. (org-flag-heading nil)))) ; show the next heading
  21054. (recenter (/ (window-height) 2))
  21055. (run-hooks 'org-agenda-after-show-hook)
  21056. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21057. (defvar org-agenda-after-show-hook nil
  21058. "Normal hook run after an item has been shown from the agenda.
  21059. Point is in the buffer where the item originated.")
  21060. (defun org-agenda-kill ()
  21061. "Kill the entry or subtree belonging to the current agenda entry."
  21062. (interactive)
  21063. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21064. (let* ((marker (or (get-text-property (point) 'org-marker)
  21065. (org-agenda-error)))
  21066. (buffer (marker-buffer marker))
  21067. (pos (marker-position marker))
  21068. (type (get-text-property (point) 'type))
  21069. dbeg dend (n 0) conf)
  21070. (org-with-remote-undo buffer
  21071. (with-current-buffer buffer
  21072. (save-excursion
  21073. (goto-char pos)
  21074. (if (and (org-mode-p) (not (member type '("sexp"))))
  21075. (setq dbeg (progn (org-back-to-heading t) (point))
  21076. dend (org-end-of-subtree t t))
  21077. (setq dbeg (point-at-bol)
  21078. dend (min (point-max) (1+ (point-at-eol)))))
  21079. (goto-char dbeg)
  21080. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21081. (setq conf (or (eq t org-agenda-confirm-kill)
  21082. (and (numberp org-agenda-confirm-kill)
  21083. (> n org-agenda-confirm-kill))))
  21084. (and conf
  21085. (not (y-or-n-p
  21086. (format "Delete entry with %d lines in buffer \"%s\"? "
  21087. n (buffer-name buffer))))
  21088. (error "Abort"))
  21089. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21090. (with-current-buffer buffer (delete-region dbeg dend))
  21091. (message "Agenda item and source killed"))))
  21092. (defun org-agenda-archive ()
  21093. "Kill the entry or subtree belonging to the current agenda entry."
  21094. (interactive)
  21095. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21096. (let* ((marker (or (get-text-property (point) 'org-marker)
  21097. (org-agenda-error)))
  21098. (buffer (marker-buffer marker))
  21099. (pos (marker-position marker)))
  21100. (org-with-remote-undo buffer
  21101. (with-current-buffer buffer
  21102. (if (org-mode-p)
  21103. (save-excursion
  21104. (goto-char pos)
  21105. (org-remove-subtree-entries-from-agenda)
  21106. (org-back-to-heading t)
  21107. (org-archive-subtree))
  21108. (error "Archiving works only in Org-mode files"))))))
  21109. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21110. "Remove all lines in the agenda that correspond to a given subtree.
  21111. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21112. If this information is not given, the function uses the tree at point."
  21113. (let ((buf (or buf (current-buffer))) m p)
  21114. (save-excursion
  21115. (unless (and beg end)
  21116. (org-back-to-heading t)
  21117. (setq beg (point))
  21118. (org-end-of-subtree t)
  21119. (setq end (point)))
  21120. (set-buffer (get-buffer org-agenda-buffer-name))
  21121. (save-excursion
  21122. (goto-char (point-max))
  21123. (beginning-of-line 1)
  21124. (while (not (bobp))
  21125. (when (and (setq m (get-text-property (point) 'org-marker))
  21126. (equal buf (marker-buffer m))
  21127. (setq p (marker-position m))
  21128. (>= p beg)
  21129. (<= p end))
  21130. (let ((inhibit-read-only t))
  21131. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21132. (beginning-of-line 0))))))
  21133. (defun org-agenda-open-link ()
  21134. "Follow the link in the current line, if any."
  21135. (interactive)
  21136. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21137. (save-excursion
  21138. (save-restriction
  21139. (narrow-to-region (point-at-bol) (point-at-eol))
  21140. (org-open-at-point))))
  21141. (defun org-agenda-copy-local-variable (var)
  21142. "Get a variable from a referenced buffer and install it here."
  21143. (let ((m (get-text-property (point) 'org-marker)))
  21144. (when (and m (buffer-live-p (marker-buffer m)))
  21145. (org-set-local var (with-current-buffer (marker-buffer m)
  21146. (symbol-value var))))))
  21147. (defun org-agenda-switch-to (&optional delete-other-windows)
  21148. "Go to the Org-mode file which contains the item at point."
  21149. (interactive)
  21150. (let* ((marker (or (get-text-property (point) 'org-marker)
  21151. (org-agenda-error)))
  21152. (buffer (marker-buffer marker))
  21153. (pos (marker-position marker)))
  21154. (switch-to-buffer buffer)
  21155. (and delete-other-windows (delete-other-windows))
  21156. (widen)
  21157. (goto-char pos)
  21158. (when (org-mode-p)
  21159. (org-show-context 'agenda)
  21160. (save-excursion
  21161. (and (outline-next-heading)
  21162. (org-flag-heading nil)))))) ; show the next heading
  21163. (defun org-agenda-goto-mouse (ev)
  21164. "Go to the Org-mode file which contains the item at the mouse click."
  21165. (interactive "e")
  21166. (mouse-set-point ev)
  21167. (org-agenda-goto))
  21168. (defun org-agenda-show ()
  21169. "Display the Org-mode file which contains the item at point."
  21170. (interactive)
  21171. (let ((win (selected-window)))
  21172. (org-agenda-goto t)
  21173. (select-window win)))
  21174. (defun org-agenda-recenter (arg)
  21175. "Display the Org-mode file which contains the item at point and recenter."
  21176. (interactive "P")
  21177. (let ((win (selected-window)))
  21178. (org-agenda-goto t)
  21179. (recenter arg)
  21180. (select-window win)))
  21181. (defun org-agenda-show-mouse (ev)
  21182. "Display the Org-mode file which contains the item at the mouse click."
  21183. (interactive "e")
  21184. (mouse-set-point ev)
  21185. (org-agenda-show))
  21186. (defun org-agenda-check-no-diary ()
  21187. "Check if the entry is a diary link and abort if yes."
  21188. (if (get-text-property (point) 'org-agenda-diary-link)
  21189. (org-agenda-error)))
  21190. (defun org-agenda-error ()
  21191. (error "Command not allowed in this line"))
  21192. (defun org-agenda-tree-to-indirect-buffer ()
  21193. "Show the subtree corresponding to the current entry in an indirect buffer.
  21194. This calls the command `org-tree-to-indirect-buffer' from the original
  21195. Org-mode buffer.
  21196. With numerical prefix arg ARG, go up to this level and then take that tree.
  21197. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21198. dedicated frame)."
  21199. (interactive)
  21200. (org-agenda-check-no-diary)
  21201. (let* ((marker (or (get-text-property (point) 'org-marker)
  21202. (org-agenda-error)))
  21203. (buffer (marker-buffer marker))
  21204. (pos (marker-position marker)))
  21205. (with-current-buffer buffer
  21206. (save-excursion
  21207. (goto-char pos)
  21208. (call-interactively 'org-tree-to-indirect-buffer)))))
  21209. (defvar org-last-heading-marker (make-marker)
  21210. "Marker pointing to the headline that last changed its TODO state
  21211. by a remote command from the agenda.")
  21212. (defun org-agenda-todo-nextset ()
  21213. "Switch TODO entry to next sequence."
  21214. (interactive)
  21215. (org-agenda-todo 'nextset))
  21216. (defun org-agenda-todo-previousset ()
  21217. "Switch TODO entry to previous sequence."
  21218. (interactive)
  21219. (org-agenda-todo 'previousset))
  21220. (defun org-agenda-todo (&optional arg)
  21221. "Cycle TODO state of line at point, also in Org-mode file.
  21222. This changes the line at point, all other lines in the agenda referring to
  21223. the same tree node, and the headline of the tree node in the Org-mode file."
  21224. (interactive "P")
  21225. (org-agenda-check-no-diary)
  21226. (let* ((col (current-column))
  21227. (marker (or (get-text-property (point) 'org-marker)
  21228. (org-agenda-error)))
  21229. (buffer (marker-buffer marker))
  21230. (pos (marker-position marker))
  21231. (hdmarker (get-text-property (point) 'org-hd-marker))
  21232. (inhibit-read-only t)
  21233. newhead)
  21234. (org-with-remote-undo buffer
  21235. (with-current-buffer buffer
  21236. (widen)
  21237. (goto-char pos)
  21238. (org-show-context 'agenda)
  21239. (save-excursion
  21240. (and (outline-next-heading)
  21241. (org-flag-heading nil))) ; show the next heading
  21242. (org-todo arg)
  21243. (and (bolp) (forward-char 1))
  21244. (setq newhead (org-get-heading))
  21245. (save-excursion
  21246. (org-back-to-heading)
  21247. (move-marker org-last-heading-marker (point))))
  21248. (beginning-of-line 1)
  21249. (save-excursion
  21250. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21251. (move-to-column col))))
  21252. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21253. "Change all lines in the agenda buffer which match HDMARKER.
  21254. The new content of the line will be NEWHEAD (as modified by
  21255. `org-format-agenda-item'). HDMARKER is checked with
  21256. `equal' against all `org-hd-marker' text properties in the file.
  21257. If FIXFACE is non-nil, the face of each item is modified acording to
  21258. the new TODO state."
  21259. (let* ((inhibit-read-only t)
  21260. props m pl undone-face done-face finish new dotime cat tags)
  21261. (save-excursion
  21262. (goto-char (point-max))
  21263. (beginning-of-line 1)
  21264. (while (not finish)
  21265. (setq finish (bobp))
  21266. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21267. (equal m hdmarker))
  21268. (setq props (text-properties-at (point))
  21269. dotime (get-text-property (point) 'dotime)
  21270. cat (get-text-property (point) 'org-category)
  21271. tags (get-text-property (point) 'tags)
  21272. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21273. pl (get-text-property (point) 'prefix-length)
  21274. undone-face (get-text-property (point) 'undone-face)
  21275. done-face (get-text-property (point) 'done-face))
  21276. (move-to-column pl)
  21277. (cond
  21278. ((equal new "")
  21279. (beginning-of-line 1)
  21280. (and (looking-at ".*\n?") (replace-match "")))
  21281. ((looking-at ".*")
  21282. (replace-match new t t)
  21283. (beginning-of-line 1)
  21284. (add-text-properties (point-at-bol) (point-at-eol) props)
  21285. (when fixface
  21286. (add-text-properties
  21287. (point-at-bol) (point-at-eol)
  21288. (list 'face
  21289. (if org-last-todo-state-is-todo
  21290. undone-face done-face))))
  21291. (org-agenda-highlight-todo 'line)
  21292. (beginning-of-line 1))
  21293. (t (error "Line update did not work"))))
  21294. (beginning-of-line 0)))
  21295. (org-finalize-agenda)))
  21296. (defun org-agenda-align-tags (&optional line)
  21297. "Align all tags in agenda items to `org-agenda-tags-column'."
  21298. (let ((inhibit-read-only t) l c)
  21299. (save-excursion
  21300. (goto-char (if line (point-at-bol) (point-min)))
  21301. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21302. (if line (point-at-eol) nil) t)
  21303. (add-text-properties
  21304. (match-beginning 2) (match-end 2)
  21305. (list 'face (delq nil (list 'org-tag (get-text-property
  21306. (match-beginning 2) 'face)))))
  21307. (setq l (- (match-end 2) (match-beginning 2))
  21308. c (if (< org-agenda-tags-column 0)
  21309. (- (abs org-agenda-tags-column) l)
  21310. org-agenda-tags-column))
  21311. (delete-region (match-beginning 1) (match-end 1))
  21312. (goto-char (match-beginning 1))
  21313. (insert (org-add-props
  21314. (make-string (max 1 (- c (current-column))) ?\ )
  21315. (text-properties-at (point))))))))
  21316. (defun org-agenda-priority-up ()
  21317. "Increase the priority of line at point, also in Org-mode file."
  21318. (interactive)
  21319. (org-agenda-priority 'up))
  21320. (defun org-agenda-priority-down ()
  21321. "Decrease the priority of line at point, also in Org-mode file."
  21322. (interactive)
  21323. (org-agenda-priority 'down))
  21324. (defun org-agenda-priority (&optional force-direction)
  21325. "Set the priority of line at point, also in Org-mode file.
  21326. This changes the line at point, all other lines in the agenda referring to
  21327. the same tree node, and the headline of the tree node in the Org-mode file."
  21328. (interactive)
  21329. (org-agenda-check-no-diary)
  21330. (let* ((marker (or (get-text-property (point) 'org-marker)
  21331. (org-agenda-error)))
  21332. (hdmarker (get-text-property (point) 'org-hd-marker))
  21333. (buffer (marker-buffer hdmarker))
  21334. (pos (marker-position hdmarker))
  21335. (inhibit-read-only t)
  21336. newhead)
  21337. (org-with-remote-undo buffer
  21338. (with-current-buffer buffer
  21339. (widen)
  21340. (goto-char pos)
  21341. (org-show-context 'agenda)
  21342. (save-excursion
  21343. (and (outline-next-heading)
  21344. (org-flag-heading nil))) ; show the next heading
  21345. (funcall 'org-priority force-direction)
  21346. (end-of-line 1)
  21347. (setq newhead (org-get-heading)))
  21348. (org-agenda-change-all-lines newhead hdmarker)
  21349. (beginning-of-line 1))))
  21350. (defun org-get-tags-at (&optional pos)
  21351. "Get a list of all headline tags applicable at POS.
  21352. POS defaults to point. If tags are inherited, the list contains
  21353. the targets in the same sequence as the headlines appear, i.e.
  21354. the tags of the current headline come last."
  21355. (interactive)
  21356. (let (tags lastpos)
  21357. (save-excursion
  21358. (save-restriction
  21359. (widen)
  21360. (goto-char (or pos (point)))
  21361. (save-match-data
  21362. (org-back-to-heading t)
  21363. (condition-case nil
  21364. (while (not (equal lastpos (point)))
  21365. (setq lastpos (point))
  21366. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21367. (setq tags (append (org-split-string
  21368. (org-match-string-no-properties 1) ":")
  21369. tags)))
  21370. (or org-use-tag-inheritance (error ""))
  21371. (org-up-heading-all 1))
  21372. (error nil))))
  21373. tags)))
  21374. ;; FIXME: should fix the tags property of the agenda line.
  21375. (defun org-agenda-set-tags ()
  21376. "Set tags for the current headline."
  21377. (interactive)
  21378. (org-agenda-check-no-diary)
  21379. (if (and (org-region-active-p) (interactive-p))
  21380. (call-interactively 'org-change-tag-in-region)
  21381. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21382. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21383. (org-agenda-error)))
  21384. (buffer (marker-buffer hdmarker))
  21385. (pos (marker-position hdmarker))
  21386. (inhibit-read-only t)
  21387. newhead)
  21388. (org-with-remote-undo buffer
  21389. (with-current-buffer buffer
  21390. (widen)
  21391. (goto-char pos)
  21392. (save-excursion
  21393. (org-show-context 'agenda))
  21394. (save-excursion
  21395. (and (outline-next-heading)
  21396. (org-flag-heading nil))) ; show the next heading
  21397. (goto-char pos)
  21398. (call-interactively 'org-set-tags)
  21399. (end-of-line 1)
  21400. (setq newhead (org-get-heading)))
  21401. (org-agenda-change-all-lines newhead hdmarker)
  21402. (beginning-of-line 1)))))
  21403. (defun org-agenda-toggle-archive-tag ()
  21404. "Toggle the archive tag for the current entry."
  21405. (interactive)
  21406. (org-agenda-check-no-diary)
  21407. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21408. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21409. (org-agenda-error)))
  21410. (buffer (marker-buffer hdmarker))
  21411. (pos (marker-position hdmarker))
  21412. (inhibit-read-only t)
  21413. newhead)
  21414. (org-with-remote-undo buffer
  21415. (with-current-buffer buffer
  21416. (widen)
  21417. (goto-char pos)
  21418. (org-show-context 'agenda)
  21419. (save-excursion
  21420. (and (outline-next-heading)
  21421. (org-flag-heading nil))) ; show the next heading
  21422. (call-interactively 'org-toggle-archive-tag)
  21423. (end-of-line 1)
  21424. (setq newhead (org-get-heading)))
  21425. (org-agenda-change-all-lines newhead hdmarker)
  21426. (beginning-of-line 1))))
  21427. (defun org-agenda-date-later (arg &optional what)
  21428. "Change the date of this item to one day later."
  21429. (interactive "p")
  21430. (org-agenda-check-type t 'agenda 'timeline)
  21431. (org-agenda-check-no-diary)
  21432. (let* ((marker (or (get-text-property (point) 'org-marker)
  21433. (org-agenda-error)))
  21434. (buffer (marker-buffer marker))
  21435. (pos (marker-position marker)))
  21436. (org-with-remote-undo buffer
  21437. (with-current-buffer buffer
  21438. (widen)
  21439. (goto-char pos)
  21440. (if (not (org-at-timestamp-p))
  21441. (error "Cannot find time stamp"))
  21442. (org-timestamp-change arg (or what 'day)))
  21443. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21444. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21445. (defun org-agenda-date-earlier (arg &optional what)
  21446. "Change the date of this item to one day earlier."
  21447. (interactive "p")
  21448. (org-agenda-date-later (- arg) what))
  21449. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21450. "Show new date stamp via text properties."
  21451. ;; We use text properties to make this undoable
  21452. (let ((inhibit-read-only t))
  21453. (setq stamp (concat " " prefix " => " stamp))
  21454. (save-excursion
  21455. (goto-char (point-max))
  21456. (while (not (bobp))
  21457. (when (equal marker (get-text-property (point) 'org-marker))
  21458. (move-to-column (- (window-width) (length stamp)) t)
  21459. (if (featurep 'xemacs)
  21460. ;; Use `duplicable' property to trigger undo recording
  21461. (let ((ex (make-extent nil nil))
  21462. (gl (make-glyph stamp)))
  21463. (set-glyph-face gl 'secondary-selection)
  21464. (set-extent-properties
  21465. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21466. (insert-extent ex (1- (point)) (point-at-eol)))
  21467. (add-text-properties
  21468. (1- (point)) (point-at-eol)
  21469. (list 'display (org-add-props stamp nil
  21470. 'face 'secondary-selection))))
  21471. (beginning-of-line 1))
  21472. (beginning-of-line 0)))))
  21473. (defun org-agenda-date-prompt (arg)
  21474. "Change the date of this item. Date is prompted for, with default today.
  21475. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21476. be used to request time specification in the time stamp."
  21477. (interactive "P")
  21478. (org-agenda-check-type t 'agenda 'timeline)
  21479. (org-agenda-check-no-diary)
  21480. (let* ((marker (or (get-text-property (point) 'org-marker)
  21481. (org-agenda-error)))
  21482. (buffer (marker-buffer marker))
  21483. (pos (marker-position marker)))
  21484. (org-with-remote-undo buffer
  21485. (with-current-buffer buffer
  21486. (widen)
  21487. (goto-char pos)
  21488. (if (not (org-at-timestamp-p))
  21489. (error "Cannot find time stamp"))
  21490. (org-time-stamp arg)
  21491. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21492. (defun org-agenda-schedule (arg)
  21493. "Schedule the item at point."
  21494. (interactive "P")
  21495. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21496. (org-agenda-check-no-diary)
  21497. (let* ((marker (or (get-text-property (point) 'org-marker)
  21498. (org-agenda-error)))
  21499. (buffer (marker-buffer marker))
  21500. (pos (marker-position marker))
  21501. (org-insert-labeled-timestamps-at-point nil)
  21502. ts)
  21503. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21504. (set-marker-insertion-type marker t)
  21505. (org-with-remote-undo buffer
  21506. (with-current-buffer buffer
  21507. (widen)
  21508. (goto-char pos)
  21509. (setq ts (org-schedule arg)))
  21510. (org-agenda-show-new-time marker ts "S"))
  21511. (message "Item scheduled for %s" ts)))
  21512. (defun org-agenda-deadline (arg)
  21513. "Schedule the item at point."
  21514. (interactive "P")
  21515. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21516. (org-agenda-check-no-diary)
  21517. (let* ((marker (or (get-text-property (point) 'org-marker)
  21518. (org-agenda-error)))
  21519. (buffer (marker-buffer marker))
  21520. (pos (marker-position marker))
  21521. (org-insert-labeled-timestamps-at-point nil)
  21522. ts)
  21523. (org-with-remote-undo buffer
  21524. (with-current-buffer buffer
  21525. (widen)
  21526. (goto-char pos)
  21527. (setq ts (org-deadline arg)))
  21528. (org-agenda-show-new-time marker ts "S"))
  21529. (message "Deadline for this item set to %s" ts)))
  21530. (defun org-get-heading (&optional no-tags)
  21531. "Return the heading of the current entry, without the stars."
  21532. (save-excursion
  21533. (org-back-to-heading t)
  21534. (if (looking-at
  21535. (if no-tags
  21536. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21537. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21538. (match-string 1) "")))
  21539. (defun org-agenda-clock-in (&optional arg)
  21540. "Start the clock on the currently selected item."
  21541. (interactive "P")
  21542. (org-agenda-check-no-diary)
  21543. (let* ((marker (or (get-text-property (point) 'org-marker)
  21544. (org-agenda-error)))
  21545. (pos (marker-position marker)))
  21546. (org-with-remote-undo (marker-buffer marker)
  21547. (with-current-buffer (marker-buffer marker)
  21548. (widen)
  21549. (goto-char pos)
  21550. (org-clock-in)))))
  21551. (defun org-agenda-clock-out (&optional arg)
  21552. "Stop the currently running clock."
  21553. (interactive "P")
  21554. (unless (marker-buffer org-clock-marker)
  21555. (error "No running clock"))
  21556. (org-with-remote-undo (marker-buffer org-clock-marker)
  21557. (org-clock-out)))
  21558. (defun org-agenda-clock-cancel (&optional arg)
  21559. "Cancel the currently running clock."
  21560. (interactive "P")
  21561. (unless (marker-buffer org-clock-marker)
  21562. (error "No running clock"))
  21563. (org-with-remote-undo (marker-buffer org-clock-marker)
  21564. (org-clock-cancel)))
  21565. (defun org-agenda-diary-entry ()
  21566. "Make a diary entry, like the `i' command from the calendar.
  21567. All the standard commands work: block, weekly etc."
  21568. (interactive)
  21569. (org-agenda-check-type t 'agenda 'timeline)
  21570. (require 'diary-lib)
  21571. (let* ((char (progn
  21572. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21573. (read-char-exclusive)))
  21574. (cmd (cdr (assoc char
  21575. '((?d . insert-diary-entry)
  21576. (?w . insert-weekly-diary-entry)
  21577. (?m . insert-monthly-diary-entry)
  21578. (?y . insert-yearly-diary-entry)
  21579. (?a . insert-anniversary-diary-entry)
  21580. (?b . insert-block-diary-entry)
  21581. (?c . insert-cyclic-diary-entry)))))
  21582. (oldf (symbol-function 'calendar-cursor-to-date))
  21583. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21584. (point (point))
  21585. (mark (or (mark t) (point))))
  21586. (unless cmd
  21587. (error "No command associated with <%c>" char))
  21588. (unless (and (get-text-property point 'day)
  21589. (or (not (equal ?b char))
  21590. (get-text-property mark 'day)))
  21591. (error "Don't know which date to use for diary entry"))
  21592. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21593. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21594. (let ((calendar-mark-ring
  21595. (list (calendar-gregorian-from-absolute
  21596. (or (get-text-property mark 'day)
  21597. (get-text-property point 'day))))))
  21598. (unwind-protect
  21599. (progn
  21600. (fset 'calendar-cursor-to-date
  21601. (lambda (&optional error)
  21602. (calendar-gregorian-from-absolute
  21603. (get-text-property point 'day))))
  21604. (call-interactively cmd))
  21605. (fset 'calendar-cursor-to-date oldf)))))
  21606. (defun org-agenda-execute-calendar-command (cmd)
  21607. "Execute a calendar command from the agenda, with the date associated to
  21608. the cursor position."
  21609. (org-agenda-check-type t 'agenda 'timeline)
  21610. (require 'diary-lib)
  21611. (unless (get-text-property (point) 'day)
  21612. (error "Don't know which date to use for calendar command"))
  21613. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21614. (point (point))
  21615. (date (calendar-gregorian-from-absolute
  21616. (get-text-property point 'day)))
  21617. ;; the following 3 vars are needed in the calendar
  21618. (displayed-day (extract-calendar-day date))
  21619. (displayed-month (extract-calendar-month date))
  21620. (displayed-year (extract-calendar-year date)))
  21621. (unwind-protect
  21622. (progn
  21623. (fset 'calendar-cursor-to-date
  21624. (lambda (&optional error)
  21625. (calendar-gregorian-from-absolute
  21626. (get-text-property point 'day))))
  21627. (call-interactively cmd))
  21628. (fset 'calendar-cursor-to-date oldf))))
  21629. (defun org-agenda-phases-of-moon ()
  21630. "Display the phases of the moon for the 3 months around the cursor date."
  21631. (interactive)
  21632. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21633. (defun org-agenda-holidays ()
  21634. "Display the holidays for the 3 months around the cursor date."
  21635. (interactive)
  21636. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21637. (defun org-agenda-sunrise-sunset (arg)
  21638. "Display sunrise and sunset for the cursor date.
  21639. Latitude and longitude can be specified with the variables
  21640. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21641. argument, latitude and longitude will be prompted for."
  21642. (interactive "P")
  21643. (let ((calendar-longitude (if arg nil calendar-longitude))
  21644. (calendar-latitude (if arg nil calendar-latitude))
  21645. (calendar-location-name
  21646. (if arg "the given coordinates" calendar-location-name)))
  21647. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21648. (defun org-agenda-goto-calendar ()
  21649. "Open the Emacs calendar with the date at the cursor."
  21650. (interactive)
  21651. (org-agenda-check-type t 'agenda 'timeline)
  21652. (let* ((day (or (get-text-property (point) 'day)
  21653. (error "Don't know which date to open in calendar")))
  21654. (date (calendar-gregorian-from-absolute day))
  21655. (calendar-move-hook nil)
  21656. (view-calendar-holidays-initially nil)
  21657. (view-diary-entries-initially nil))
  21658. (calendar)
  21659. (calendar-goto-date date)))
  21660. (defun org-calendar-goto-agenda ()
  21661. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21662. This is a command that has to be installed in `calendar-mode-map'."
  21663. (interactive)
  21664. (org-agenda-list nil (calendar-absolute-from-gregorian
  21665. (calendar-cursor-to-date))
  21666. nil))
  21667. (defun org-agenda-convert-date ()
  21668. (interactive)
  21669. (org-agenda-check-type t 'agenda 'timeline)
  21670. (let ((day (get-text-property (point) 'day))
  21671. date s)
  21672. (unless day
  21673. (error "Don't know which date to convert"))
  21674. (setq date (calendar-gregorian-from-absolute day))
  21675. (setq s (concat
  21676. "Gregorian: " (calendar-date-string date) "\n"
  21677. "ISO: " (calendar-iso-date-string date) "\n"
  21678. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21679. "Julian: " (calendar-julian-date-string date) "\n"
  21680. "Astron. JD: " (calendar-astro-date-string date)
  21681. " (Julian date number at noon UTC)\n"
  21682. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21683. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21684. "French: " (calendar-french-date-string date) "\n"
  21685. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21686. "Mayan: " (calendar-mayan-date-string date) "\n"
  21687. "Coptic: " (calendar-coptic-date-string date) "\n"
  21688. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21689. "Persian: " (calendar-persian-date-string date) "\n"
  21690. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21691. (with-output-to-temp-buffer "*Dates*"
  21692. (princ s))
  21693. (if (fboundp 'fit-window-to-buffer)
  21694. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21695. ;;;; Embedded LaTeX
  21696. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21697. "Keymap for the minor `org-cdlatex-mode'.")
  21698. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21699. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21700. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21701. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21702. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21703. (defvar org-cdlatex-texmathp-advice-is-done nil
  21704. "Flag remembering if we have applied the advice to texmathp already.")
  21705. (define-minor-mode org-cdlatex-mode
  21706. "Toggle the minor `org-cdlatex-mode'.
  21707. This mode supports entering LaTeX environment and math in LaTeX fragments
  21708. in Org-mode.
  21709. \\{org-cdlatex-mode-map}"
  21710. nil " OCDL" nil
  21711. (when org-cdlatex-mode (require 'cdlatex))
  21712. (unless org-cdlatex-texmathp-advice-is-done
  21713. (setq org-cdlatex-texmathp-advice-is-done t)
  21714. (defadvice texmathp (around org-math-always-on activate)
  21715. "Always return t in org-mode buffers.
  21716. This is because we want to insert math symbols without dollars even outside
  21717. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21718. en embedded LaTeX fragement, let texmathp do its job.
  21719. \\[org-cdlatex-mode-map]"
  21720. (interactive)
  21721. (let (p)
  21722. (cond
  21723. ((not (org-mode-p)) ad-do-it)
  21724. ((eq this-command 'cdlatex-math-symbol)
  21725. (setq ad-return-value t
  21726. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21727. (t
  21728. (let ((p (org-inside-LaTeX-fragment-p)))
  21729. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21730. (setq ad-return-value t
  21731. texmathp-why '("Org-mode embedded math" . 0))
  21732. (if p ad-do-it)))))))))
  21733. (defun turn-on-org-cdlatex ()
  21734. "Unconditionally turn on `org-cdlatex-mode'."
  21735. (org-cdlatex-mode 1))
  21736. (defun org-inside-LaTeX-fragment-p ()
  21737. "Test if point is inside a LaTeX fragment.
  21738. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21739. sequence appearing also before point.
  21740. Even though the matchers for math are configurable, this function assumes
  21741. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21742. delimiters are skipped when they have been removed by customization.
  21743. The return value is nil, or a cons cell with the delimiter and
  21744. and the position of this delimiter.
  21745. This function does a reasonably good job, but can locally be fooled by
  21746. for example currency specifications. For example it will assume being in
  21747. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21748. fragments that are properly closed, but during editing, we have to live
  21749. with the uncertainty caused by missing closing delimiters. This function
  21750. looks only before point, not after."
  21751. (catch 'exit
  21752. (let ((pos (point))
  21753. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21754. (lim (progn
  21755. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21756. (point)))
  21757. dd-on str (start 0) m re)
  21758. (goto-char pos)
  21759. (when dodollar
  21760. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21761. re (nth 1 (assoc "$" org-latex-regexps)))
  21762. (while (string-match re str start)
  21763. (cond
  21764. ((= (match-end 0) (length str))
  21765. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21766. ((= (match-end 0) (- (length str) 5))
  21767. (throw 'exit nil))
  21768. (t (setq start (match-end 0))))))
  21769. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21770. (goto-char pos)
  21771. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21772. (and (match-beginning 2) (throw 'exit nil))
  21773. ;; count $$
  21774. (while (re-search-backward "\\$\\$" lim t)
  21775. (setq dd-on (not dd-on)))
  21776. (goto-char pos)
  21777. (if dd-on (cons "$$" m))))))
  21778. (defun org-try-cdlatex-tab ()
  21779. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21780. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21781. - inside a LaTeX fragment, or
  21782. - after the first word in a line, where an abbreviation expansion could
  21783. insert a LaTeX environment."
  21784. (when org-cdlatex-mode
  21785. (cond
  21786. ((save-excursion
  21787. (skip-chars-backward "a-zA-Z0-9*")
  21788. (skip-chars-backward " \t")
  21789. (bolp))
  21790. (cdlatex-tab) t)
  21791. ((org-inside-LaTeX-fragment-p)
  21792. (cdlatex-tab) t)
  21793. (t nil))))
  21794. (defun org-cdlatex-underscore-caret (&optional arg)
  21795. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21796. Revert to the normal definition outside of these fragments."
  21797. (interactive "P")
  21798. (if (org-inside-LaTeX-fragment-p)
  21799. (call-interactively 'cdlatex-sub-superscript)
  21800. (let (org-cdlatex-mode)
  21801. (call-interactively (key-binding (vector last-input-event))))))
  21802. (defun org-cdlatex-math-modify (&optional arg)
  21803. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21804. Revert to the normal definition outside of these fragments."
  21805. (interactive "P")
  21806. (if (org-inside-LaTeX-fragment-p)
  21807. (call-interactively 'cdlatex-math-modify)
  21808. (let (org-cdlatex-mode)
  21809. (call-interactively (key-binding (vector last-input-event))))))
  21810. (defvar org-latex-fragment-image-overlays nil
  21811. "List of overlays carrying the images of latex fragments.")
  21812. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21813. (defun org-remove-latex-fragment-image-overlays ()
  21814. "Remove all overlays with LaTeX fragment images in current buffer."
  21815. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21816. (setq org-latex-fragment-image-overlays nil))
  21817. (defun org-preview-latex-fragment (&optional subtree)
  21818. "Preview the LaTeX fragment at point, or all locally or globally.
  21819. If the cursor is in a LaTeX fragment, create the image and overlay
  21820. it over the source code. If there is no fragment at point, display
  21821. all fragments in the current text, from one headline to the next. With
  21822. prefix SUBTREE, display all fragments in the current subtree. With a
  21823. double prefix `C-u C-u', or when the cursor is before the first headline,
  21824. display all fragments in the buffer.
  21825. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21826. (interactive "P")
  21827. (org-remove-latex-fragment-image-overlays)
  21828. (save-excursion
  21829. (save-restriction
  21830. (let (beg end at msg)
  21831. (cond
  21832. ((or (equal subtree '(16))
  21833. (not (save-excursion
  21834. (re-search-backward (concat "^" outline-regexp) nil t))))
  21835. (setq beg (point-min) end (point-max)
  21836. msg "Creating images for buffer...%s"))
  21837. ((equal subtree '(4))
  21838. (org-back-to-heading)
  21839. (setq beg (point) end (org-end-of-subtree t)
  21840. msg "Creating images for subtree...%s"))
  21841. (t
  21842. (if (setq at (org-inside-LaTeX-fragment-p))
  21843. (goto-char (max (point-min) (- (cdr at) 2)))
  21844. (org-back-to-heading))
  21845. (setq beg (point) end (progn (outline-next-heading) (point))
  21846. msg (if at "Creating image...%s"
  21847. "Creating images for entry...%s"))))
  21848. (message msg "")
  21849. (narrow-to-region beg end)
  21850. (goto-char beg)
  21851. (org-format-latex
  21852. (concat "ltxpng/" (file-name-sans-extension
  21853. (file-name-nondirectory
  21854. buffer-file-name)))
  21855. default-directory 'overlays msg at 'forbuffer)
  21856. (message msg "done. Use `C-c C-c' to remove images.")))))
  21857. (defvar org-latex-regexps
  21858. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21859. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21860. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21861. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21862. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21863. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21864. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21865. "Regular expressions for matching embedded LaTeX.")
  21866. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21867. "Replace LaTeX fragments with links to an image, and produce images."
  21868. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21869. (let* ((prefixnodir (file-name-nondirectory prefix))
  21870. (absprefix (expand-file-name prefix dir))
  21871. (todir (file-name-directory absprefix))
  21872. (opt org-format-latex-options)
  21873. (matchers (plist-get opt :matchers))
  21874. (re-list org-latex-regexps)
  21875. (cnt 0) txt link beg end re e checkdir
  21876. m n block linkfile movefile ov)
  21877. ;; Check if there are old images files with this prefix, and remove them
  21878. (when (file-directory-p todir)
  21879. (mapc 'delete-file
  21880. (directory-files
  21881. todir 'full
  21882. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21883. ;; Check the different regular expressions
  21884. (while (setq e (pop re-list))
  21885. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21886. block (if (nth 3 e) "\n\n" ""))
  21887. (when (member m matchers)
  21888. (goto-char (point-min))
  21889. (while (re-search-forward re nil t)
  21890. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21891. (setq txt (match-string n)
  21892. beg (match-beginning n) end (match-end n)
  21893. cnt (1+ cnt)
  21894. linkfile (format "%s_%04d.png" prefix cnt)
  21895. movefile (format "%s_%04d.png" absprefix cnt)
  21896. link (concat block "[[file:" linkfile "]]" block))
  21897. (if msg (message msg cnt))
  21898. (goto-char beg)
  21899. (unless checkdir ; make sure the directory exists
  21900. (setq checkdir t)
  21901. (or (file-directory-p todir) (make-directory todir)))
  21902. (org-create-formula-image
  21903. txt movefile opt forbuffer)
  21904. (if overlays
  21905. (progn
  21906. (setq ov (org-make-overlay beg end))
  21907. (if (featurep 'xemacs)
  21908. (progn
  21909. (org-overlay-put ov 'invisible t)
  21910. (org-overlay-put
  21911. ov 'end-glyph
  21912. (make-glyph (vector 'png :file movefile))))
  21913. (org-overlay-put
  21914. ov 'display
  21915. (list 'image :type 'png :file movefile :ascent 'center)))
  21916. (push ov org-latex-fragment-image-overlays)
  21917. (goto-char end))
  21918. (delete-region beg end)
  21919. (insert link))))))))
  21920. ;; This function borrows from Ganesh Swami's latex2png.el
  21921. (defun org-create-formula-image (string tofile options buffer)
  21922. (let* ((tmpdir (if (featurep 'xemacs)
  21923. (temp-directory)
  21924. temporary-file-directory))
  21925. (texfilebase (make-temp-name
  21926. (expand-file-name "orgtex" tmpdir)))
  21927. (texfile (concat texfilebase ".tex"))
  21928. (dvifile (concat texfilebase ".dvi"))
  21929. (pngfile (concat texfilebase ".png"))
  21930. (fnh (face-attribute 'default :height nil))
  21931. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21932. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21933. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21934. "Black"))
  21935. (bg (or (plist-get options (if buffer :background :html-background))
  21936. "Transparent")))
  21937. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21938. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21939. (with-temp-file texfile
  21940. (insert org-format-latex-header
  21941. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21942. (let ((dir default-directory))
  21943. (condition-case nil
  21944. (progn
  21945. (cd tmpdir)
  21946. (call-process "latex" nil nil nil texfile))
  21947. (error nil))
  21948. (cd dir))
  21949. (if (not (file-exists-p dvifile))
  21950. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21951. (call-process "dvipng" nil nil nil
  21952. "-E" "-fg" fg "-bg" bg
  21953. "-D" dpi
  21954. ;;"-x" scale "-y" scale
  21955. "-T" "tight"
  21956. "-o" pngfile
  21957. dvifile)
  21958. (if (not (file-exists-p pngfile))
  21959. (progn (message "Failed to create png file from %s" texfile) nil)
  21960. ;; Use the requested file name and clean up
  21961. (copy-file pngfile tofile 'replace)
  21962. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21963. (delete-file (concat texfilebase e)))
  21964. pngfile))))
  21965. (defun org-dvipng-color (attr)
  21966. "Return an rgb color specification for dvipng."
  21967. (apply 'format "rgb %s %s %s"
  21968. (mapcar 'org-normalize-color
  21969. (color-values (face-attribute 'default attr nil)))))
  21970. (defun org-normalize-color (value)
  21971. "Return string to be used as color value for an RGB component."
  21972. (format "%g" (/ value 65535.0)))
  21973. ;;;; Exporting
  21974. ;;; Variables, constants, and parameter plists
  21975. (defconst org-level-max 20)
  21976. (defvar org-export-html-preamble nil
  21977. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21978. (defvar org-export-html-postamble nil
  21979. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21980. (defvar org-export-html-auto-preamble t
  21981. "Should default preamble be inserted? Set by publishing functions.")
  21982. (defvar org-export-html-auto-postamble t
  21983. "Should default postamble be inserted? Set by publishing functions.")
  21984. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21985. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21986. (defconst org-export-plist-vars
  21987. '((:language . org-export-default-language)
  21988. (:customtime . org-display-custom-times)
  21989. (:headline-levels . org-export-headline-levels)
  21990. (:section-numbers . org-export-with-section-numbers)
  21991. (:table-of-contents . org-export-with-toc)
  21992. (:preserve-breaks . org-export-preserve-breaks)
  21993. (:archived-trees . org-export-with-archived-trees)
  21994. (:emphasize . org-export-with-emphasize)
  21995. (:sub-superscript . org-export-with-sub-superscripts)
  21996. (:special-strings . org-export-with-special-strings)
  21997. (:footnotes . org-export-with-footnotes)
  21998. (:drawers . org-export-with-drawers)
  21999. (:tags . org-export-with-tags)
  22000. (:TeX-macros . org-export-with-TeX-macros)
  22001. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  22002. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  22003. (:fixed-width . org-export-with-fixed-width)
  22004. (:timestamps . org-export-with-timestamps)
  22005. (:author-info . org-export-author-info)
  22006. (:time-stamp-file . org-export-time-stamp-file)
  22007. (:tables . org-export-with-tables)
  22008. (:table-auto-headline . org-export-highlight-first-table-line)
  22009. (:style . org-export-html-style)
  22010. (:agenda-style . org-agenda-export-html-style)
  22011. (:convert-org-links . org-export-html-link-org-files-as-html)
  22012. (:inline-images . org-export-html-inline-images)
  22013. (:html-extension . org-export-html-extension)
  22014. (:html-table-tag . org-export-html-table-tag)
  22015. (:expand-quoted-html . org-export-html-expand)
  22016. (:timestamp . org-export-html-with-timestamp)
  22017. (:publishing-directory . org-export-publishing-directory)
  22018. (:preamble . org-export-html-preamble)
  22019. (:postamble . org-export-html-postamble)
  22020. (:auto-preamble . org-export-html-auto-preamble)
  22021. (:auto-postamble . org-export-html-auto-postamble)
  22022. (:author . user-full-name)
  22023. (:email . user-mail-address)))
  22024. (defun org-default-export-plist ()
  22025. "Return the property list with default settings for the export variables."
  22026. (let ((l org-export-plist-vars) rtn e)
  22027. (while (setq e (pop l))
  22028. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22029. rtn))
  22030. (defun org-infile-export-plist ()
  22031. "Return the property list with file-local settings for export."
  22032. (save-excursion
  22033. (save-restriction
  22034. (widen)
  22035. (goto-char 0)
  22036. (let ((re (org-make-options-regexp
  22037. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22038. p key val text options)
  22039. (while (re-search-forward re nil t)
  22040. (setq key (org-match-string-no-properties 1)
  22041. val (org-match-string-no-properties 2))
  22042. (cond
  22043. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22044. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22045. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22046. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22047. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22048. ((string-equal key "TEXT")
  22049. (setq text (if text (concat text "\n" val) val)))
  22050. ((string-equal key "OPTIONS") (setq options val))))
  22051. (setq p (plist-put p :text text))
  22052. (when options
  22053. (let ((op '(("H" . :headline-levels)
  22054. ("num" . :section-numbers)
  22055. ("toc" . :table-of-contents)
  22056. ("\\n" . :preserve-breaks)
  22057. ("@" . :expand-quoted-html)
  22058. (":" . :fixed-width)
  22059. ("|" . :tables)
  22060. ("^" . :sub-superscript)
  22061. ("-" . :special-strings)
  22062. ("f" . :footnotes)
  22063. ("d" . :drawers)
  22064. ("tags" . :tags)
  22065. ("*" . :emphasize)
  22066. ("TeX" . :TeX-macros)
  22067. ("LaTeX" . :LaTeX-fragments)
  22068. ("skip" . :skip-before-1st-heading)
  22069. ("author" . :author-info)
  22070. ("timestamp" . :time-stamp-file)))
  22071. o)
  22072. (while (setq o (pop op))
  22073. (if (string-match (concat (regexp-quote (car o))
  22074. ":\\([^ \t\n\r;,.]*\\)")
  22075. options)
  22076. (setq p (plist-put p (cdr o)
  22077. (car (read-from-string
  22078. (match-string 1 options)))))))))
  22079. p))))
  22080. (defun org-export-directory (type plist)
  22081. (let* ((val (plist-get plist :publishing-directory))
  22082. (dir (if (listp val)
  22083. (or (cdr (assoc type val)) ".")
  22084. val)))
  22085. dir))
  22086. (defun org-skip-comments (lines)
  22087. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22088. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22089. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22090. (case-fold-search nil)
  22091. rtn line level)
  22092. (while (setq line (pop lines))
  22093. (cond
  22094. ((and (string-match re1 line)
  22095. (setq level (- (match-end 1) (match-beginning 1))))
  22096. ;; Beginning of a COMMENT subtree. Skip it.
  22097. (while (and (setq line (pop lines))
  22098. (or (not (string-match re2 line))
  22099. (> (- (match-end 1) (match-beginning 1)) level))))
  22100. (setq lines (cons line lines)))
  22101. ((string-match "^#" line)
  22102. ;; an ordinary comment line
  22103. )
  22104. ((and org-export-table-remove-special-lines
  22105. (string-match "^[ \t]*|" line)
  22106. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22107. (and (string-match "| *<[0-9]+> *|" line)
  22108. (not (string-match "| *[^ <|]" line)))))
  22109. ;; a special table line that should be removed
  22110. )
  22111. (t (setq rtn (cons line rtn)))))
  22112. (nreverse rtn)))
  22113. (defun org-export (&optional arg)
  22114. (interactive)
  22115. (let ((help "[t] insert the export option template
  22116. \[v] limit export to visible part of outline tree
  22117. \[a] export as ASCII
  22118. \[h] export as HTML
  22119. \[H] export as HTML to temporary buffer
  22120. \[R] export region as HTML
  22121. \[b] export as HTML and browse immediately
  22122. \[x] export as XOXO
  22123. \[l] export as LaTeX
  22124. \[L] export as LaTeX to temporary buffer
  22125. \[i] export current file as iCalendar file
  22126. \[I] export all agenda files as iCalendar files
  22127. \[c] export agenda files into combined iCalendar file
  22128. \[F] publish current file
  22129. \[P] publish current project
  22130. \[X] publish... (project will be prompted for)
  22131. \[A] publish all projects")
  22132. (cmds
  22133. '((?t . org-insert-export-options-template)
  22134. (?v . org-export-visible)
  22135. (?a . org-export-as-ascii)
  22136. (?h . org-export-as-html)
  22137. (?b . org-export-as-html-and-open)
  22138. (?H . org-export-as-html-to-buffer)
  22139. (?R . org-export-region-as-html)
  22140. (?x . org-export-as-xoxo)
  22141. (?l . org-export-as-latex)
  22142. (?L . org-export-as-latex-to-buffer)
  22143. (?i . org-export-icalendar-this-file)
  22144. (?I . org-export-icalendar-all-agenda-files)
  22145. (?c . org-export-icalendar-combine-agenda-files)
  22146. (?F . org-publish-current-file)
  22147. (?P . org-publish-current-project)
  22148. (?X . org-publish)
  22149. (?A . org-publish-all)))
  22150. r1 r2 ass)
  22151. (save-window-excursion
  22152. (delete-other-windows)
  22153. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22154. (princ help))
  22155. (message "Select command: ")
  22156. (setq r1 (read-char-exclusive)))
  22157. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22158. (if (setq ass (assq r2 cmds))
  22159. (call-interactively (cdr ass))
  22160. (error "No command associated with key %c" r1))))
  22161. (defconst org-html-entities
  22162. '(("nbsp")
  22163. ("iexcl")
  22164. ("cent")
  22165. ("pound")
  22166. ("curren")
  22167. ("yen")
  22168. ("brvbar")
  22169. ("vert" . "&#124;")
  22170. ("sect")
  22171. ("uml")
  22172. ("copy")
  22173. ("ordf")
  22174. ("laquo")
  22175. ("not")
  22176. ("shy")
  22177. ("reg")
  22178. ("macr")
  22179. ("deg")
  22180. ("plusmn")
  22181. ("sup2")
  22182. ("sup3")
  22183. ("acute")
  22184. ("micro")
  22185. ("para")
  22186. ("middot")
  22187. ("odot"."o")
  22188. ("star"."*")
  22189. ("cedil")
  22190. ("sup1")
  22191. ("ordm")
  22192. ("raquo")
  22193. ("frac14")
  22194. ("frac12")
  22195. ("frac34")
  22196. ("iquest")
  22197. ("Agrave")
  22198. ("Aacute")
  22199. ("Acirc")
  22200. ("Atilde")
  22201. ("Auml")
  22202. ("Aring") ("AA"."&Aring;")
  22203. ("AElig")
  22204. ("Ccedil")
  22205. ("Egrave")
  22206. ("Eacute")
  22207. ("Ecirc")
  22208. ("Euml")
  22209. ("Igrave")
  22210. ("Iacute")
  22211. ("Icirc")
  22212. ("Iuml")
  22213. ("ETH")
  22214. ("Ntilde")
  22215. ("Ograve")
  22216. ("Oacute")
  22217. ("Ocirc")
  22218. ("Otilde")
  22219. ("Ouml")
  22220. ("times")
  22221. ("Oslash")
  22222. ("Ugrave")
  22223. ("Uacute")
  22224. ("Ucirc")
  22225. ("Uuml")
  22226. ("Yacute")
  22227. ("THORN")
  22228. ("szlig")
  22229. ("agrave")
  22230. ("aacute")
  22231. ("acirc")
  22232. ("atilde")
  22233. ("auml")
  22234. ("aring")
  22235. ("aelig")
  22236. ("ccedil")
  22237. ("egrave")
  22238. ("eacute")
  22239. ("ecirc")
  22240. ("euml")
  22241. ("igrave")
  22242. ("iacute")
  22243. ("icirc")
  22244. ("iuml")
  22245. ("eth")
  22246. ("ntilde")
  22247. ("ograve")
  22248. ("oacute")
  22249. ("ocirc")
  22250. ("otilde")
  22251. ("ouml")
  22252. ("divide")
  22253. ("oslash")
  22254. ("ugrave")
  22255. ("uacute")
  22256. ("ucirc")
  22257. ("uuml")
  22258. ("yacute")
  22259. ("thorn")
  22260. ("yuml")
  22261. ("fnof")
  22262. ("Alpha")
  22263. ("Beta")
  22264. ("Gamma")
  22265. ("Delta")
  22266. ("Epsilon")
  22267. ("Zeta")
  22268. ("Eta")
  22269. ("Theta")
  22270. ("Iota")
  22271. ("Kappa")
  22272. ("Lambda")
  22273. ("Mu")
  22274. ("Nu")
  22275. ("Xi")
  22276. ("Omicron")
  22277. ("Pi")
  22278. ("Rho")
  22279. ("Sigma")
  22280. ("Tau")
  22281. ("Upsilon")
  22282. ("Phi")
  22283. ("Chi")
  22284. ("Psi")
  22285. ("Omega")
  22286. ("alpha")
  22287. ("beta")
  22288. ("gamma")
  22289. ("delta")
  22290. ("epsilon")
  22291. ("varepsilon"."&epsilon;")
  22292. ("zeta")
  22293. ("eta")
  22294. ("theta")
  22295. ("iota")
  22296. ("kappa")
  22297. ("lambda")
  22298. ("mu")
  22299. ("nu")
  22300. ("xi")
  22301. ("omicron")
  22302. ("pi")
  22303. ("rho")
  22304. ("sigmaf") ("varsigma"."&sigmaf;")
  22305. ("sigma")
  22306. ("tau")
  22307. ("upsilon")
  22308. ("phi")
  22309. ("chi")
  22310. ("psi")
  22311. ("omega")
  22312. ("thetasym") ("vartheta"."&thetasym;")
  22313. ("upsih")
  22314. ("piv")
  22315. ("bull") ("bullet"."&bull;")
  22316. ("hellip") ("dots"."&hellip;")
  22317. ("prime")
  22318. ("Prime")
  22319. ("oline")
  22320. ("frasl")
  22321. ("weierp")
  22322. ("image")
  22323. ("real")
  22324. ("trade")
  22325. ("alefsym")
  22326. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22327. ("uarr") ("uparrow"."&uarr;")
  22328. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22329. ("darr")("downarrow"."&darr;")
  22330. ("harr") ("leftrightarrow"."&harr;")
  22331. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22332. ("lArr") ("Leftarrow"."&lArr;")
  22333. ("uArr") ("Uparrow"."&uArr;")
  22334. ("rArr") ("Rightarrow"."&rArr;")
  22335. ("dArr") ("Downarrow"."&dArr;")
  22336. ("hArr") ("Leftrightarrow"."&hArr;")
  22337. ("forall")
  22338. ("part") ("partial"."&part;")
  22339. ("exist") ("exists"."&exist;")
  22340. ("empty") ("emptyset"."&empty;")
  22341. ("nabla")
  22342. ("isin") ("in"."&isin;")
  22343. ("notin")
  22344. ("ni")
  22345. ("prod")
  22346. ("sum")
  22347. ("minus")
  22348. ("lowast") ("ast"."&lowast;")
  22349. ("radic")
  22350. ("prop") ("proptp"."&prop;")
  22351. ("infin") ("infty"."&infin;")
  22352. ("ang") ("angle"."&ang;")
  22353. ("and") ("wedge"."&and;")
  22354. ("or") ("vee"."&or;")
  22355. ("cap")
  22356. ("cup")
  22357. ("int")
  22358. ("there4")
  22359. ("sim")
  22360. ("cong") ("simeq"."&cong;")
  22361. ("asymp")("approx"."&asymp;")
  22362. ("ne") ("neq"."&ne;")
  22363. ("equiv")
  22364. ("le")
  22365. ("ge")
  22366. ("sub") ("subset"."&sub;")
  22367. ("sup") ("supset"."&sup;")
  22368. ("nsub")
  22369. ("sube")
  22370. ("supe")
  22371. ("oplus")
  22372. ("otimes")
  22373. ("perp")
  22374. ("sdot") ("cdot"."&sdot;")
  22375. ("lceil")
  22376. ("rceil")
  22377. ("lfloor")
  22378. ("rfloor")
  22379. ("lang")
  22380. ("rang")
  22381. ("loz") ("Diamond"."&loz;")
  22382. ("spades") ("spadesuit"."&spades;")
  22383. ("clubs") ("clubsuit"."&clubs;")
  22384. ("hearts") ("diamondsuit"."&hearts;")
  22385. ("diams") ("diamondsuit"."&diams;")
  22386. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22387. ("quot")
  22388. ("amp")
  22389. ("lt")
  22390. ("gt")
  22391. ("OElig")
  22392. ("oelig")
  22393. ("Scaron")
  22394. ("scaron")
  22395. ("Yuml")
  22396. ("circ")
  22397. ("tilde")
  22398. ("ensp")
  22399. ("emsp")
  22400. ("thinsp")
  22401. ("zwnj")
  22402. ("zwj")
  22403. ("lrm")
  22404. ("rlm")
  22405. ("ndash")
  22406. ("mdash")
  22407. ("lsquo")
  22408. ("rsquo")
  22409. ("sbquo")
  22410. ("ldquo")
  22411. ("rdquo")
  22412. ("bdquo")
  22413. ("dagger")
  22414. ("Dagger")
  22415. ("permil")
  22416. ("lsaquo")
  22417. ("rsaquo")
  22418. ("euro")
  22419. ("arccos"."arccos")
  22420. ("arcsin"."arcsin")
  22421. ("arctan"."arctan")
  22422. ("arg"."arg")
  22423. ("cos"."cos")
  22424. ("cosh"."cosh")
  22425. ("cot"."cot")
  22426. ("coth"."coth")
  22427. ("csc"."csc")
  22428. ("deg"."deg")
  22429. ("det"."det")
  22430. ("dim"."dim")
  22431. ("exp"."exp")
  22432. ("gcd"."gcd")
  22433. ("hom"."hom")
  22434. ("inf"."inf")
  22435. ("ker"."ker")
  22436. ("lg"."lg")
  22437. ("lim"."lim")
  22438. ("liminf"."liminf")
  22439. ("limsup"."limsup")
  22440. ("ln"."ln")
  22441. ("log"."log")
  22442. ("max"."max")
  22443. ("min"."min")
  22444. ("Pr"."Pr")
  22445. ("sec"."sec")
  22446. ("sin"."sin")
  22447. ("sinh"."sinh")
  22448. ("sup"."sup")
  22449. ("tan"."tan")
  22450. ("tanh"."tanh")
  22451. )
  22452. "Entities for TeX->HTML translation.
  22453. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22454. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22455. In that case, \"\\ent\" will be translated to \"&other;\".
  22456. The list contains HTML entities for Latin-1, Greek and other symbols.
  22457. It is supplemented by a number of commonly used TeX macros with appropriate
  22458. translations. There is currently no way for users to extend this.")
  22459. ;;; General functions for all backends
  22460. (defun org-cleaned-string-for-export (string &rest parameters)
  22461. "Cleanup a buffer STRING so that links can be created safely."
  22462. (interactive)
  22463. (let* ((re-radio (and org-target-link-regexp
  22464. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22465. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22466. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22467. (re-archive (concat ":" org-archive-tag ":"))
  22468. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22469. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22470. (htmlp (plist-get parameters :for-html))
  22471. (asciip (plist-get parameters :for-ascii))
  22472. (latexp (plist-get parameters :for-LaTeX))
  22473. (commentsp (plist-get parameters :comments))
  22474. (archived-trees (plist-get parameters :archived-trees))
  22475. (inhibit-read-only t)
  22476. (drawers org-drawers)
  22477. (exp-drawers (plist-get parameters :drawers))
  22478. (outline-regexp "\\*+ ")
  22479. a b xx
  22480. rtn p)
  22481. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22482. (erase-buffer)
  22483. (insert string)
  22484. ;; Remove license-to-kill stuff
  22485. (while (setq p (text-property-any (point-min) (point-max)
  22486. :org-license-to-kill t))
  22487. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22488. (let ((org-inhibit-startup t)) (org-mode))
  22489. (untabify (point-min) (point-max))
  22490. ;; Get rid of drawers
  22491. (unless (eq t exp-drawers)
  22492. (goto-char (point-min))
  22493. (let ((re (concat "^[ \t]*:\\("
  22494. (mapconcat
  22495. 'identity
  22496. (org-delete-all exp-drawers
  22497. (copy-sequence drawers))
  22498. "\\|")
  22499. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22500. (while (re-search-forward re nil t)
  22501. (replace-match ""))))
  22502. ;; Get the correct stuff before the first headline
  22503. (when (plist-get parameters :skip-before-1st-heading)
  22504. (goto-char (point-min))
  22505. (when (re-search-forward "^\\*+[ \t]" nil t)
  22506. (delete-region (point-min) (match-beginning 0))
  22507. (goto-char (point-min))
  22508. (insert "\n")))
  22509. (when (plist-get parameters :add-text)
  22510. (goto-char (point-min))
  22511. (insert (plist-get parameters :add-text) "\n"))
  22512. ;; Get rid of archived trees
  22513. (when (not (eq archived-trees t))
  22514. (goto-char (point-min))
  22515. (while (re-search-forward re-archive nil t)
  22516. (if (not (org-on-heading-p t))
  22517. (org-end-of-subtree t)
  22518. (beginning-of-line 1)
  22519. (setq a (if archived-trees
  22520. (1+ (point-at-eol)) (point))
  22521. b (org-end-of-subtree t))
  22522. (if (> b a) (delete-region a b)))))
  22523. ;; Find targets in comments and move them out of comments,
  22524. ;; but mark them as targets that should be invisible
  22525. (goto-char (point-min))
  22526. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22527. (replace-match "\\1(INVISIBLE)"))
  22528. ;; Protect backend specific stuff, throw away the others.
  22529. (let ((formatters
  22530. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22531. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22532. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22533. fmt)
  22534. (goto-char (point-min))
  22535. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22536. (goto-char (match-end 0))
  22537. (while (not (looking-at "#\\+END_EXAMPLE"))
  22538. (insert ": ")
  22539. (beginning-of-line 2)))
  22540. (goto-char (point-min))
  22541. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22542. (add-text-properties (match-beginning 0) (match-end 0)
  22543. '(org-protected t)))
  22544. (while formatters
  22545. (setq fmt (pop formatters))
  22546. (when (car fmt)
  22547. (goto-char (point-min))
  22548. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22549. ":[ \t]*\\(.*\\)") nil t)
  22550. (replace-match "\\1" t)
  22551. (add-text-properties
  22552. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22553. '(org-protected t))))
  22554. (goto-char (point-min))
  22555. (while (re-search-forward
  22556. (concat "^#\\+"
  22557. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22558. (cadddr fmt) "\\>.*\n?") nil t)
  22559. (if (car fmt)
  22560. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22561. '(org-protected t))
  22562. (delete-region (match-beginning 0) (match-end 0))))))
  22563. ;; Protect quoted subtrees
  22564. (goto-char (point-min))
  22565. (while (re-search-forward re-quote nil t)
  22566. (goto-char (match-beginning 0))
  22567. (end-of-line 1)
  22568. (add-text-properties (point) (org-end-of-subtree t)
  22569. '(org-protected t)))
  22570. ;; Protect verbatim elements
  22571. (goto-char (point-min))
  22572. (while (re-search-forward org-verbatim-re nil t)
  22573. (add-text-properties (match-beginning 4) (match-end 4)
  22574. '(org-protected t))
  22575. (goto-char (1+ (match-end 4))))
  22576. ;; Remove subtrees that are commented
  22577. (goto-char (point-min))
  22578. (while (re-search-forward re-commented nil t)
  22579. (goto-char (match-beginning 0))
  22580. (delete-region (point) (org-end-of-subtree t)))
  22581. ;; Remove special table lines
  22582. (when org-export-table-remove-special-lines
  22583. (goto-char (point-min))
  22584. (while (re-search-forward "^[ \t]*|" nil t)
  22585. (beginning-of-line 1)
  22586. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22587. (and (looking-at ".*?| *<[0-9]+> *|")
  22588. (not (looking-at ".*?| *[^ <|]"))))
  22589. (delete-region (max (point-min) (1- (point-at-bol)))
  22590. (point-at-eol))
  22591. (end-of-line 1))))
  22592. ;; Specific LaTeX stuff
  22593. (when latexp
  22594. (require 'org-export-latex nil)
  22595. (org-export-latex-cleaned-string))
  22596. (when asciip
  22597. (org-export-ascii-clean-string))
  22598. ;; Specific HTML stuff
  22599. (when htmlp
  22600. ;; Convert LaTeX fragments to images
  22601. (when (plist-get parameters :LaTeX-fragments)
  22602. (org-format-latex
  22603. (concat "ltxpng/" (file-name-sans-extension
  22604. (file-name-nondirectory
  22605. org-current-export-file)))
  22606. org-current-export-dir nil "Creating LaTeX image %s"))
  22607. (message "Exporting..."))
  22608. ;; Remove or replace comments
  22609. (goto-char (point-min))
  22610. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22611. (if commentsp
  22612. (progn (add-text-properties
  22613. (match-beginning 0) (match-end 0) '(org-protected t))
  22614. (replace-match (format commentsp (match-string 1)) t t))
  22615. (replace-match "")))
  22616. ;; Find matches for radio targets and turn them into internal links
  22617. (goto-char (point-min))
  22618. (when re-radio
  22619. (while (re-search-forward re-radio nil t)
  22620. (org-if-unprotected
  22621. (replace-match "\\1[[\\2]]"))))
  22622. ;; Find all links that contain a newline and put them into a single line
  22623. (goto-char (point-min))
  22624. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22625. (org-if-unprotected
  22626. (replace-match "\\1 \\3")
  22627. (goto-char (match-beginning 0))))
  22628. ;; Normalize links: Convert angle and plain links into bracket links
  22629. ;; Expand link abbreviations
  22630. (goto-char (point-min))
  22631. (while (re-search-forward re-plain-link nil t)
  22632. (goto-char (1- (match-end 0)))
  22633. (org-if-unprotected
  22634. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22635. ":" (match-string 3) "]]")))
  22636. ;; added 'org-link face to links
  22637. (put-text-property 0 (length s) 'face 'org-link s)
  22638. (replace-match s t t))))
  22639. (goto-char (point-min))
  22640. (while (re-search-forward re-angle-link nil t)
  22641. (goto-char (1- (match-end 0)))
  22642. (org-if-unprotected
  22643. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22644. ":" (match-string 3) "]]")))
  22645. (put-text-property 0 (length s) 'face 'org-link s)
  22646. (replace-match s t t))))
  22647. (goto-char (point-min))
  22648. (while (re-search-forward org-bracket-link-regexp nil t)
  22649. (org-if-unprotected
  22650. (let* ((s (concat "[[" (setq xx (save-match-data
  22651. (org-link-expand-abbrev (match-string 1))))
  22652. "]"
  22653. (if (match-end 3)
  22654. (match-string 2)
  22655. (concat "[" xx "]"))
  22656. "]")))
  22657. (put-text-property 0 (length s) 'face 'org-link s)
  22658. (replace-match s t t))))
  22659. ;; Find multiline emphasis and put them into single line
  22660. (when (plist-get parameters :emph-multiline)
  22661. (goto-char (point-min))
  22662. (while (re-search-forward org-emph-re nil t)
  22663. (if (not (= (char-after (match-beginning 3))
  22664. (char-after (match-beginning 4))))
  22665. (org-if-unprotected
  22666. (subst-char-in-region (match-beginning 0) (match-end 0)
  22667. ?\n ?\ t)
  22668. (goto-char (1- (match-end 0))))
  22669. (goto-char (1+ (match-beginning 0))))))
  22670. (setq rtn (buffer-string)))
  22671. (kill-buffer " org-mode-tmp")
  22672. rtn))
  22673. (defun org-export-grab-title-from-buffer ()
  22674. "Get a title for the current document, from looking at the buffer."
  22675. (let ((inhibit-read-only t))
  22676. (save-excursion
  22677. (goto-char (point-min))
  22678. (let ((end (save-excursion (outline-next-heading) (point))))
  22679. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22680. ;; Mark the line so that it will not be exported as normal text.
  22681. (org-unmodified
  22682. (add-text-properties (match-beginning 0) (match-end 0)
  22683. (list :org-license-to-kill t)))
  22684. ;; Return the title string
  22685. (org-trim (match-string 0)))))))
  22686. (defun org-export-get-title-from-subtree ()
  22687. "Return subtree title and exclude it from export."
  22688. (let (title (m (mark)))
  22689. (save-excursion
  22690. (goto-char (region-beginning))
  22691. (when (and (org-at-heading-p)
  22692. (>= (org-end-of-subtree t t) (region-end)))
  22693. ;; This is a subtree, we take the title from the first heading
  22694. (goto-char (region-beginning))
  22695. (looking-at org-todo-line-regexp)
  22696. (setq title (match-string 3))
  22697. (org-unmodified
  22698. (add-text-properties (point) (1+ (point-at-eol))
  22699. (list :org-license-to-kill t)))))
  22700. title))
  22701. (defun org-solidify-link-text (s &optional alist)
  22702. "Take link text and make a safe target out of it."
  22703. (save-match-data
  22704. (let* ((rtn
  22705. (mapconcat
  22706. 'identity
  22707. (org-split-string s "[ \t\r\n]+") "--"))
  22708. (a (assoc rtn alist)))
  22709. (or (cdr a) rtn))))
  22710. (defun org-get-min-level (lines)
  22711. "Get the minimum level in LINES."
  22712. (let ((re "^\\(\\*+\\) ") l min)
  22713. (catch 'exit
  22714. (while (setq l (pop lines))
  22715. (if (string-match re l)
  22716. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22717. 1)))
  22718. ;; Variable holding the vector with section numbers
  22719. (defvar org-section-numbers (make-vector org-level-max 0))
  22720. (defun org-init-section-numbers ()
  22721. "Initialize the vector for the section numbers."
  22722. (let* ((level -1)
  22723. (numbers (nreverse (org-split-string "" "\\.")))
  22724. (depth (1- (length org-section-numbers)))
  22725. (i depth) number-string)
  22726. (while (>= i 0)
  22727. (if (> i level)
  22728. (aset org-section-numbers i 0)
  22729. (setq number-string (or (car numbers) "0"))
  22730. (if (string-match "\\`[A-Z]\\'" number-string)
  22731. (aset org-section-numbers i
  22732. (- (string-to-char number-string) ?A -1))
  22733. (aset org-section-numbers i (string-to-number number-string)))
  22734. (pop numbers))
  22735. (setq i (1- i)))))
  22736. (defun org-section-number (&optional level)
  22737. "Return a string with the current section number.
  22738. When LEVEL is non-nil, increase section numbers on that level."
  22739. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22740. (when level
  22741. (when (> level -1)
  22742. (aset org-section-numbers
  22743. level (1+ (aref org-section-numbers level))))
  22744. (setq idx (1+ level))
  22745. (while (<= idx depth)
  22746. (if (not (= idx 1))
  22747. (aset org-section-numbers idx 0))
  22748. (setq idx (1+ idx))))
  22749. (setq idx 0)
  22750. (while (<= idx depth)
  22751. (setq n (aref org-section-numbers idx))
  22752. (setq string (concat string (if (not (string= string "")) "." "")
  22753. (int-to-string n)))
  22754. (setq idx (1+ idx)))
  22755. (save-match-data
  22756. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22757. (setq string (replace-match "" t nil string)))
  22758. (if (string-match "\\(\\.0\\)+\\'" string)
  22759. (setq string (replace-match "" t nil string))))
  22760. string))
  22761. ;;; ASCII export
  22762. (defvar org-last-level nil) ; dynamically scoped variable
  22763. (defvar org-min-level nil) ; dynamically scoped variable
  22764. (defvar org-levels-open nil) ; dynamically scoped parameter
  22765. (defvar org-ascii-current-indentation nil) ; For communication
  22766. (defun org-export-as-ascii (arg)
  22767. "Export the outline as a pretty ASCII file.
  22768. If there is an active region, export only the region.
  22769. The prefix ARG specifies how many levels of the outline should become
  22770. underlined headlines. The default is 3."
  22771. (interactive "P")
  22772. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22773. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22774. (org-infile-export-plist)))
  22775. (region-p (org-region-active-p))
  22776. (subtree-p
  22777. (when region-p
  22778. (save-excursion
  22779. (goto-char (region-beginning))
  22780. (and (org-at-heading-p)
  22781. (>= (org-end-of-subtree t t) (region-end))))))
  22782. (custom-times org-display-custom-times)
  22783. (org-ascii-current-indentation '(0 . 0))
  22784. (level 0) line txt
  22785. (umax nil)
  22786. (umax-toc nil)
  22787. (case-fold-search nil)
  22788. (filename (concat (file-name-as-directory
  22789. (org-export-directory :ascii opt-plist))
  22790. (file-name-sans-extension
  22791. (or (and subtree-p
  22792. (org-entry-get (region-beginning)
  22793. "EXPORT_FILE_NAME" t))
  22794. (file-name-nondirectory buffer-file-name)))
  22795. ".txt"))
  22796. (filename (if (equal (file-truename filename)
  22797. (file-truename buffer-file-name))
  22798. (concat filename ".txt")
  22799. filename))
  22800. (buffer (find-file-noselect filename))
  22801. (org-levels-open (make-vector org-level-max nil))
  22802. (odd org-odd-levels-only)
  22803. (date (plist-get opt-plist :date))
  22804. (author (plist-get opt-plist :author))
  22805. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22806. (plist-get opt-plist :title)
  22807. (and (not
  22808. (plist-get opt-plist :skip-before-1st-heading))
  22809. (org-export-grab-title-from-buffer))
  22810. (file-name-sans-extension
  22811. (file-name-nondirectory buffer-file-name))))
  22812. (email (plist-get opt-plist :email))
  22813. (language (plist-get opt-plist :language))
  22814. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22815. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22816. (todo nil)
  22817. (lang-words nil)
  22818. (region
  22819. (buffer-substring
  22820. (if (org-region-active-p) (region-beginning) (point-min))
  22821. (if (org-region-active-p) (region-end) (point-max))))
  22822. (lines (org-split-string
  22823. (org-cleaned-string-for-export
  22824. region
  22825. :for-ascii t
  22826. :skip-before-1st-heading
  22827. (plist-get opt-plist :skip-before-1st-heading)
  22828. :drawers (plist-get opt-plist :drawers)
  22829. :verbatim-multiline t
  22830. :archived-trees
  22831. (plist-get opt-plist :archived-trees)
  22832. :add-text (plist-get opt-plist :text))
  22833. "\n"))
  22834. thetoc have-headings first-heading-pos
  22835. table-open table-buffer)
  22836. (let ((inhibit-read-only t))
  22837. (org-unmodified
  22838. (remove-text-properties (point-min) (point-max)
  22839. '(:org-license-to-kill t))))
  22840. (setq org-min-level (org-get-min-level lines))
  22841. (setq org-last-level org-min-level)
  22842. (org-init-section-numbers)
  22843. (find-file-noselect filename)
  22844. (setq lang-words (or (assoc language org-export-language-setup)
  22845. (assoc "en" org-export-language-setup)))
  22846. (switch-to-buffer-other-window buffer)
  22847. (erase-buffer)
  22848. (fundamental-mode)
  22849. ;; create local variables for all options, to make sure all called
  22850. ;; functions get the correct information
  22851. (mapc (lambda (x)
  22852. (set (make-local-variable (cdr x))
  22853. (plist-get opt-plist (car x))))
  22854. org-export-plist-vars)
  22855. (org-set-local 'org-odd-levels-only odd)
  22856. (setq umax (if arg (prefix-numeric-value arg)
  22857. org-export-headline-levels))
  22858. (setq umax-toc (if (integerp org-export-with-toc)
  22859. (min org-export-with-toc umax)
  22860. umax))
  22861. ;; File header
  22862. (if title (org-insert-centered title ?=))
  22863. (insert "\n")
  22864. (if (and (or author email)
  22865. org-export-author-info)
  22866. (insert (concat (nth 1 lang-words) ": " (or author "")
  22867. (if email (concat " <" email ">") "")
  22868. "\n")))
  22869. (cond
  22870. ((and date (string-match "%" date))
  22871. (setq date (format-time-string date (current-time))))
  22872. (date)
  22873. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22874. (if (and date org-export-time-stamp-file)
  22875. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22876. (insert "\n\n")
  22877. (if org-export-with-toc
  22878. (progn
  22879. (push (concat (nth 3 lang-words) "\n") thetoc)
  22880. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22881. (mapc '(lambda (line)
  22882. (if (string-match org-todo-line-regexp
  22883. line)
  22884. ;; This is a headline
  22885. (progn
  22886. (setq have-headings t)
  22887. (setq level (- (match-end 1) (match-beginning 1))
  22888. level (org-tr-level level)
  22889. txt (match-string 3 line)
  22890. todo
  22891. (or (and org-export-mark-todo-in-toc
  22892. (match-beginning 2)
  22893. (not (member (match-string 2 line)
  22894. org-done-keywords)))
  22895. ; TODO, not DONE
  22896. (and org-export-mark-todo-in-toc
  22897. (= level umax-toc)
  22898. (org-search-todo-below
  22899. line lines level))))
  22900. (setq txt (org-html-expand-for-ascii txt))
  22901. (while (string-match org-bracket-link-regexp txt)
  22902. (setq txt
  22903. (replace-match
  22904. (match-string (if (match-end 2) 3 1) txt)
  22905. t t txt)))
  22906. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22907. (string-match
  22908. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22909. txt))
  22910. (setq txt (replace-match "" t t txt)))
  22911. (if (string-match quote-re0 txt)
  22912. (setq txt (replace-match "" t t txt)))
  22913. (if org-export-with-section-numbers
  22914. (setq txt (concat (org-section-number level)
  22915. " " txt)))
  22916. (if (<= level umax-toc)
  22917. (progn
  22918. (push
  22919. (concat
  22920. (make-string
  22921. (* (max 0 (- level org-min-level)) 4) ?\ )
  22922. (format (if todo "%s (*)\n" "%s\n") txt))
  22923. thetoc)
  22924. (setq org-last-level level))
  22925. ))))
  22926. lines)
  22927. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22928. (org-init-section-numbers)
  22929. (while (setq line (pop lines))
  22930. ;; Remove the quoted HTML tags.
  22931. (setq line (org-html-expand-for-ascii line))
  22932. ;; Remove targets
  22933. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22934. (setq line (replace-match "" t t line)))
  22935. ;; Replace internal links
  22936. (while (string-match org-bracket-link-regexp line)
  22937. (setq line (replace-match
  22938. (if (match-end 3) "[\\3]" "[\\1]")
  22939. t nil line)))
  22940. (when custom-times
  22941. (setq line (org-translate-time line)))
  22942. (cond
  22943. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22944. ;; a Headline
  22945. (setq first-heading-pos (or first-heading-pos (point)))
  22946. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22947. txt (match-string 2 line))
  22948. (org-ascii-level-start level txt umax lines))
  22949. ((and org-export-with-tables
  22950. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22951. (if (not table-open)
  22952. ;; New table starts
  22953. (setq table-open t table-buffer nil))
  22954. ;; Accumulate lines
  22955. (setq table-buffer (cons line table-buffer))
  22956. (when (or (not lines)
  22957. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22958. (car lines))))
  22959. (setq table-open nil
  22960. table-buffer (nreverse table-buffer))
  22961. (insert (mapconcat
  22962. (lambda (x)
  22963. (org-fix-indentation x org-ascii-current-indentation))
  22964. (org-format-table-ascii table-buffer)
  22965. "\n") "\n")))
  22966. (t
  22967. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22968. (if (and org-export-with-fixed-width
  22969. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22970. (setq line (replace-match "\\1" nil nil line)))
  22971. (insert line "\n"))))
  22972. (normal-mode)
  22973. ;; insert the table of contents
  22974. (when thetoc
  22975. (goto-char (point-min))
  22976. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22977. (progn
  22978. (goto-char (match-beginning 0))
  22979. (replace-match ""))
  22980. (goto-char first-heading-pos))
  22981. (mapc 'insert thetoc)
  22982. (or (looking-at "[ \t]*\n[ \t]*\n")
  22983. (insert "\n\n")))
  22984. ;; Convert whitespace place holders
  22985. (goto-char (point-min))
  22986. (let (beg end)
  22987. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22988. (setq end (next-single-property-change beg 'org-whitespace))
  22989. (goto-char beg)
  22990. (delete-region beg end)
  22991. (insert (make-string (- end beg) ?\ ))))
  22992. (save-buffer)
  22993. ;; remove display and invisible chars
  22994. (let (beg end)
  22995. (goto-char (point-min))
  22996. (while (setq beg (next-single-property-change (point) 'display))
  22997. (setq end (next-single-property-change beg 'display))
  22998. (delete-region beg end)
  22999. (goto-char beg)
  23000. (insert "=>"))
  23001. (goto-char (point-min))
  23002. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  23003. (setq end (next-single-property-change beg 'org-cwidth))
  23004. (delete-region beg end)
  23005. (goto-char beg)))
  23006. (goto-char (point-min))))
  23007. (defun org-export-ascii-clean-string ()
  23008. "Do extra work for ASCII export"
  23009. (goto-char (point-min))
  23010. (while (re-search-forward org-verbatim-re nil t)
  23011. (goto-char (match-end 2))
  23012. (backward-delete-char 1) (insert "'")
  23013. (goto-char (match-beginning 2))
  23014. (delete-char 1) (insert "`")
  23015. (goto-char (match-end 2))))
  23016. (defun org-search-todo-below (line lines level)
  23017. "Search the subtree below LINE for any TODO entries."
  23018. (let ((rest (cdr (memq line lines)))
  23019. (re org-todo-line-regexp)
  23020. line lv todo)
  23021. (catch 'exit
  23022. (while (setq line (pop rest))
  23023. (if (string-match re line)
  23024. (progn
  23025. (setq lv (- (match-end 1) (match-beginning 1))
  23026. todo (and (match-beginning 2)
  23027. (not (member (match-string 2 line)
  23028. org-done-keywords))))
  23029. ; TODO, not DONE
  23030. (if (<= lv level) (throw 'exit nil))
  23031. (if todo (throw 'exit t))))))))
  23032. (defun org-html-expand-for-ascii (line)
  23033. "Handle quoted HTML for ASCII export."
  23034. (if org-export-html-expand
  23035. (while (string-match "@<[^<>\n]*>" line)
  23036. ;; We just remove the tags for now.
  23037. (setq line (replace-match "" nil nil line))))
  23038. line)
  23039. (defun org-insert-centered (s &optional underline)
  23040. "Insert the string S centered and underline it with character UNDERLINE."
  23041. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23042. (insert (make-string ind ?\ ) s "\n")
  23043. (if underline
  23044. (insert (make-string ind ?\ )
  23045. (make-string (string-width s) underline)
  23046. "\n"))))
  23047. (defun org-ascii-level-start (level title umax &optional lines)
  23048. "Insert a new level in ASCII export."
  23049. (let (char (n (- level umax 1)) (ind 0))
  23050. (if (> level umax)
  23051. (progn
  23052. (insert (make-string (* 2 n) ?\ )
  23053. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23054. org-export-ascii-bullets))
  23055. " " title "\n")
  23056. ;; find the indentation of the next non-empty line
  23057. (catch 'stop
  23058. (while lines
  23059. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23060. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23061. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23062. (pop lines)))
  23063. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23064. (if (or (not (equal (char-before) ?\n))
  23065. (not (equal (char-before (1- (point))) ?\n)))
  23066. (insert "\n"))
  23067. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23068. (unless org-export-with-tags
  23069. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23070. (setq title (replace-match "" t t title))))
  23071. (if org-export-with-section-numbers
  23072. (setq title (concat (org-section-number level) " " title)))
  23073. (insert title "\n" (make-string (string-width title) char) "\n")
  23074. (setq org-ascii-current-indentation '(0 . 0)))))
  23075. (defun org-export-visible (type arg)
  23076. "Create a copy of the visible part of the current buffer, and export it.
  23077. The copy is created in a temporary buffer and removed after use.
  23078. TYPE is the final key (as a string) that also select the export command in
  23079. the `C-c C-e' export dispatcher.
  23080. As a special case, if the you type SPC at the prompt, the temporary
  23081. org-mode file will not be removed but presented to you so that you can
  23082. continue to use it. The prefix arg ARG is passed through to the exporting
  23083. command."
  23084. (interactive
  23085. (list (progn
  23086. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23087. (read-char-exclusive))
  23088. current-prefix-arg))
  23089. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23090. (error "Invalid export key"))
  23091. (let* ((binding (cdr (assoc type
  23092. '((?a . org-export-as-ascii)
  23093. (?\C-a . org-export-as-ascii)
  23094. (?b . org-export-as-html-and-open)
  23095. (?\C-b . org-export-as-html-and-open)
  23096. (?h . org-export-as-html)
  23097. (?H . org-export-as-html-to-buffer)
  23098. (?R . org-export-region-as-html)
  23099. (?x . org-export-as-xoxo)))))
  23100. (keepp (equal type ?\ ))
  23101. (file buffer-file-name)
  23102. (buffer (get-buffer-create "*Org Export Visible*"))
  23103. s e)
  23104. ;; Need to hack the drawers here.
  23105. (save-excursion
  23106. (goto-char (point-min))
  23107. (while (re-search-forward org-drawer-regexp nil t)
  23108. (goto-char (match-beginning 1))
  23109. (or (org-invisible-p) (org-flag-drawer nil))))
  23110. (with-current-buffer buffer (erase-buffer))
  23111. (save-excursion
  23112. (setq s (goto-char (point-min)))
  23113. (while (not (= (point) (point-max)))
  23114. (goto-char (org-find-invisible))
  23115. (append-to-buffer buffer s (point))
  23116. (setq s (goto-char (org-find-visible))))
  23117. (org-cycle-hide-drawers 'all)
  23118. (goto-char (point-min))
  23119. (unless keepp
  23120. ;; Copy all comment lines to the end, to make sure #+ settings are
  23121. ;; still available for the second export step. Kind of a hack, but
  23122. ;; does do the trick.
  23123. (if (looking-at "#[^\r\n]*")
  23124. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23125. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23126. (append-to-buffer buffer (1+ (match-beginning 0))
  23127. (min (point-max) (1+ (match-end 0))))))
  23128. (set-buffer buffer)
  23129. (let ((buffer-file-name file)
  23130. (org-inhibit-startup t))
  23131. (org-mode)
  23132. (show-all)
  23133. (unless keepp (funcall binding arg))))
  23134. (if (not keepp)
  23135. (kill-buffer buffer)
  23136. (switch-to-buffer-other-window buffer)
  23137. (goto-char (point-min)))))
  23138. (defun org-find-visible ()
  23139. (let ((s (point)))
  23140. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23141. (get-char-property s 'invisible)))
  23142. s))
  23143. (defun org-find-invisible ()
  23144. (let ((s (point)))
  23145. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23146. (not (get-char-property s 'invisible))))
  23147. s))
  23148. ;;; HTML export
  23149. (defun org-get-current-options ()
  23150. "Return a string with current options as keyword options.
  23151. Does include HTML export options as well as TODO and CATEGORY stuff."
  23152. (format
  23153. "#+TITLE: %s
  23154. #+AUTHOR: %s
  23155. #+EMAIL: %s
  23156. #+LANGUAGE: %s
  23157. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23158. #+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
  23159. #+CATEGORY: %s
  23160. #+SEQ_TODO: %s
  23161. #+TYP_TODO: %s
  23162. #+PRIORITIES: %c %c %c
  23163. #+DRAWERS: %s
  23164. #+STARTUP: %s %s %s %s %s
  23165. #+TAGS: %s
  23166. #+ARCHIVE: %s
  23167. #+LINK: %s
  23168. "
  23169. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23170. org-export-headline-levels
  23171. org-export-with-section-numbers
  23172. org-export-with-toc
  23173. org-export-preserve-breaks
  23174. org-export-html-expand
  23175. org-export-with-fixed-width
  23176. org-export-with-tables
  23177. org-export-with-sub-superscripts
  23178. org-export-with-special-strings
  23179. org-export-with-footnotes
  23180. org-export-with-emphasize
  23181. org-export-with-TeX-macros
  23182. org-export-with-LaTeX-fragments
  23183. org-export-skip-text-before-1st-heading
  23184. org-export-with-drawers
  23185. org-export-with-tags
  23186. (file-name-nondirectory buffer-file-name)
  23187. "TODO FEEDBACK VERIFY DONE"
  23188. "Me Jason Marie DONE"
  23189. org-highest-priority org-lowest-priority org-default-priority
  23190. (mapconcat 'identity org-drawers " ")
  23191. (cdr (assoc org-startup-folded
  23192. '((nil . "showall") (t . "overview") (content . "content"))))
  23193. (if org-odd-levels-only "odd" "oddeven")
  23194. (if org-hide-leading-stars "hidestars" "showstars")
  23195. (if org-startup-align-all-tables "align" "noalign")
  23196. (cond ((eq org-log-done t) "logdone")
  23197. ((equal org-log-done 'note) "lognotedone")
  23198. ((not org-log-done) "nologdone"))
  23199. (or (mapconcat (lambda (x)
  23200. (cond
  23201. ((equal '(:startgroup) x) "{")
  23202. ((equal '(:endgroup) x) "}")
  23203. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23204. (t (car x))))
  23205. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23206. org-archive-location
  23207. "org file:~/org/%s.org"
  23208. ))
  23209. (defun org-insert-export-options-template ()
  23210. "Insert into the buffer a template with information for exporting."
  23211. (interactive)
  23212. (if (not (bolp)) (newline))
  23213. (let ((s (org-get-current-options)))
  23214. (and (string-match "#\\+CATEGORY" s)
  23215. (setq s (substring s 0 (match-beginning 0))))
  23216. (insert s)))
  23217. (defun org-toggle-fixed-width-section (arg)
  23218. "Toggle the fixed-width export.
  23219. If there is no active region, the QUOTE keyword at the current headline is
  23220. inserted or removed. When present, it causes the text between this headline
  23221. and the next to be exported as fixed-width text, and unmodified.
  23222. If there is an active region, this command adds or removes a colon as the
  23223. first character of this line. If the first character of a line is a colon,
  23224. this line is also exported in fixed-width font."
  23225. (interactive "P")
  23226. (let* ((cc 0)
  23227. (regionp (org-region-active-p))
  23228. (beg (if regionp (region-beginning) (point)))
  23229. (end (if regionp (region-end)))
  23230. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23231. (case-fold-search nil)
  23232. (re "[ \t]*\\(:\\)")
  23233. off)
  23234. (if regionp
  23235. (save-excursion
  23236. (goto-char beg)
  23237. (setq cc (current-column))
  23238. (beginning-of-line 1)
  23239. (setq off (looking-at re))
  23240. (while (> nlines 0)
  23241. (setq nlines (1- nlines))
  23242. (beginning-of-line 1)
  23243. (cond
  23244. (arg
  23245. (move-to-column cc t)
  23246. (insert ":\n")
  23247. (forward-line -1))
  23248. ((and off (looking-at re))
  23249. (replace-match "" t t nil 1))
  23250. ((not off) (move-to-column cc t) (insert ":")))
  23251. (forward-line 1)))
  23252. (save-excursion
  23253. (org-back-to-heading)
  23254. (if (looking-at (concat outline-regexp
  23255. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23256. (replace-match "" t t nil 1)
  23257. (if (looking-at outline-regexp)
  23258. (progn
  23259. (goto-char (match-end 0))
  23260. (insert org-quote-string " "))))))))
  23261. (defun org-export-as-html-and-open (arg)
  23262. "Export the outline as HTML and immediately open it with a browser.
  23263. If there is an active region, export only the region.
  23264. The prefix ARG specifies how many levels of the outline should become
  23265. headlines. The default is 3. Lower levels will become bulleted lists."
  23266. (interactive "P")
  23267. (org-export-as-html arg 'hidden)
  23268. (org-open-file buffer-file-name))
  23269. (defun org-export-as-html-batch ()
  23270. "Call `org-export-as-html', may be used in batch processing as
  23271. emacs --batch
  23272. --load=$HOME/lib/emacs/org.el
  23273. --eval \"(setq org-export-headline-levels 2)\"
  23274. --visit=MyFile --funcall org-export-as-html-batch"
  23275. (org-export-as-html org-export-headline-levels 'hidden))
  23276. (defun org-export-as-html-to-buffer (arg)
  23277. "Call `org-exort-as-html` with output to a temporary buffer.
  23278. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23279. (interactive "P")
  23280. (org-export-as-html arg nil nil "*Org HTML Export*")
  23281. (switch-to-buffer-other-window "*Org HTML Export*"))
  23282. (defun org-replace-region-by-html (beg end)
  23283. "Assume the current region has org-mode syntax, and convert it to HTML.
  23284. This can be used in any buffer. For example, you could write an
  23285. itemized list in org-mode syntax in an HTML buffer and then use this
  23286. command to convert it."
  23287. (interactive "r")
  23288. (let (reg html buf pop-up-frames)
  23289. (save-window-excursion
  23290. (if (org-mode-p)
  23291. (setq html (org-export-region-as-html
  23292. beg end t 'string))
  23293. (setq reg (buffer-substring beg end)
  23294. buf (get-buffer-create "*Org tmp*"))
  23295. (with-current-buffer buf
  23296. (erase-buffer)
  23297. (insert reg)
  23298. (org-mode)
  23299. (setq html (org-export-region-as-html
  23300. (point-min) (point-max) t 'string)))
  23301. (kill-buffer buf)))
  23302. (delete-region beg end)
  23303. (insert html)))
  23304. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23305. "Convert region from BEG to END in org-mode buffer to HTML.
  23306. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23307. contents, and only produce the region of converted text, useful for
  23308. cut-and-paste operations.
  23309. If BUFFER is a buffer or a string, use/create that buffer as a target
  23310. of the converted HTML. If BUFFER is the symbol `string', return the
  23311. produced HTML as a string and leave not buffer behind. For example,
  23312. a Lisp program could call this function in the following way:
  23313. (setq html (org-export-region-as-html beg end t 'string))
  23314. When called interactively, the output buffer is selected, and shown
  23315. in a window. A non-interactive call will only retunr the buffer."
  23316. (interactive "r\nP")
  23317. (when (interactive-p)
  23318. (setq buffer "*Org HTML Export*"))
  23319. (let ((transient-mark-mode t) (zmacs-regions t)
  23320. rtn)
  23321. (goto-char end)
  23322. (set-mark (point)) ;; to activate the region
  23323. (goto-char beg)
  23324. (setq rtn (org-export-as-html
  23325. nil nil nil
  23326. buffer body-only))
  23327. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23328. (if (and (interactive-p) (bufferp rtn))
  23329. (switch-to-buffer-other-window rtn)
  23330. rtn)))
  23331. (defvar html-table-tag nil) ; dynamically scoped into this.
  23332. (defun org-export-as-html (arg &optional hidden ext-plist
  23333. to-buffer body-only pub-dir)
  23334. "Export the outline as a pretty HTML file.
  23335. If there is an active region, export only the region. The prefix
  23336. ARG specifies how many levels of the outline should become
  23337. headlines. The default is 3. Lower levels will become bulleted
  23338. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23339. EXT-PLIST is a property list with external parameters overriding
  23340. org-mode's default settings, but still inferior to file-local
  23341. settings. When TO-BUFFER is non-nil, create a buffer with that
  23342. name and export to that buffer. If TO-BUFFER is the symbol
  23343. `string', don't leave any buffer behind but just return the
  23344. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23345. the file header and footer, simply return the content of
  23346. <body>...</body>, without even the body tags themselves. When
  23347. PUB-DIR is set, use this as the publishing directory."
  23348. (interactive "P")
  23349. ;; Make sure we have a file name when we need it.
  23350. (when (and (not (or to-buffer body-only))
  23351. (not buffer-file-name))
  23352. (if (buffer-base-buffer)
  23353. (org-set-local 'buffer-file-name
  23354. (with-current-buffer (buffer-base-buffer)
  23355. buffer-file-name))
  23356. (error "Need a file name to be able to export.")))
  23357. (message "Exporting...")
  23358. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23359. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23360. (setq-default org-done-keywords org-done-keywords)
  23361. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23362. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23363. ext-plist
  23364. (org-infile-export-plist)))
  23365. (style (plist-get opt-plist :style))
  23366. (html-extension (plist-get opt-plist :html-extension))
  23367. (link-validate (plist-get opt-plist :link-validation-function))
  23368. valid thetoc have-headings first-heading-pos
  23369. (odd org-odd-levels-only)
  23370. (region-p (org-region-active-p))
  23371. (subtree-p
  23372. (when region-p
  23373. (save-excursion
  23374. (goto-char (region-beginning))
  23375. (and (org-at-heading-p)
  23376. (>= (org-end-of-subtree t t) (region-end))))))
  23377. ;; The following two are dynamically scoped into other
  23378. ;; routines below.
  23379. (org-current-export-dir
  23380. (or pub-dir (org-export-directory :html opt-plist)))
  23381. (org-current-export-file buffer-file-name)
  23382. (level 0) (line "") (origline "") txt todo
  23383. (umax nil)
  23384. (umax-toc nil)
  23385. (filename (if to-buffer nil
  23386. (expand-file-name
  23387. (concat
  23388. (file-name-sans-extension
  23389. (or (and subtree-p
  23390. (org-entry-get (region-beginning)
  23391. "EXPORT_FILE_NAME" t))
  23392. (file-name-nondirectory buffer-file-name)))
  23393. "." html-extension)
  23394. (file-name-as-directory
  23395. (or pub-dir (org-export-directory :html opt-plist))))))
  23396. (current-dir (if buffer-file-name
  23397. (file-name-directory buffer-file-name)
  23398. default-directory))
  23399. (buffer (if to-buffer
  23400. (cond
  23401. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23402. (t (get-buffer-create to-buffer)))
  23403. (find-file-noselect filename)))
  23404. (org-levels-open (make-vector org-level-max nil))
  23405. (date (plist-get opt-plist :date))
  23406. (author (plist-get opt-plist :author))
  23407. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23408. (plist-get opt-plist :title)
  23409. (and (not
  23410. (plist-get opt-plist :skip-before-1st-heading))
  23411. (org-export-grab-title-from-buffer))
  23412. (and buffer-file-name
  23413. (file-name-sans-extension
  23414. (file-name-nondirectory buffer-file-name)))
  23415. "UNTITLED"))
  23416. (html-table-tag (plist-get opt-plist :html-table-tag))
  23417. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23418. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23419. (inquote nil)
  23420. (infixed nil)
  23421. (in-local-list nil)
  23422. (local-list-num nil)
  23423. (local-list-indent nil)
  23424. (llt org-plain-list-ordered-item-terminator)
  23425. (email (plist-get opt-plist :email))
  23426. (language (plist-get opt-plist :language))
  23427. (lang-words nil)
  23428. (target-alist nil) tg
  23429. (head-count 0) cnt
  23430. (start 0)
  23431. (coding-system (and (boundp 'buffer-file-coding-system)
  23432. buffer-file-coding-system))
  23433. (coding-system-for-write (or org-export-html-coding-system
  23434. coding-system))
  23435. (save-buffer-coding-system (or org-export-html-coding-system
  23436. coding-system))
  23437. (charset (and coding-system-for-write
  23438. (fboundp 'coding-system-get)
  23439. (coding-system-get coding-system-for-write
  23440. 'mime-charset)))
  23441. (region
  23442. (buffer-substring
  23443. (if region-p (region-beginning) (point-min))
  23444. (if region-p (region-end) (point-max))))
  23445. (lines
  23446. (org-split-string
  23447. (org-cleaned-string-for-export
  23448. region
  23449. :emph-multiline t
  23450. :for-html t
  23451. :skip-before-1st-heading
  23452. (plist-get opt-plist :skip-before-1st-heading)
  23453. :drawers (plist-get opt-plist :drawers)
  23454. :archived-trees
  23455. (plist-get opt-plist :archived-trees)
  23456. :add-text
  23457. (plist-get opt-plist :text)
  23458. :LaTeX-fragments
  23459. (plist-get opt-plist :LaTeX-fragments))
  23460. "[\r\n]"))
  23461. table-open type
  23462. table-buffer table-orig-buffer
  23463. ind start-is-num starter didclose
  23464. rpl path desc descp desc1 desc2 link
  23465. )
  23466. (let ((inhibit-read-only t))
  23467. (org-unmodified
  23468. (remove-text-properties (point-min) (point-max)
  23469. '(:org-license-to-kill t))))
  23470. (message "Exporting...")
  23471. (setq org-min-level (org-get-min-level lines))
  23472. (setq org-last-level org-min-level)
  23473. (org-init-section-numbers)
  23474. (cond
  23475. ((and date (string-match "%" date))
  23476. (setq date (format-time-string date (current-time))))
  23477. (date)
  23478. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23479. ;; Get the language-dependent settings
  23480. (setq lang-words (or (assoc language org-export-language-setup)
  23481. (assoc "en" org-export-language-setup)))
  23482. ;; Switch to the output buffer
  23483. (set-buffer buffer)
  23484. (let ((inhibit-read-only t)) (erase-buffer))
  23485. (fundamental-mode)
  23486. (and (fboundp 'set-buffer-file-coding-system)
  23487. (set-buffer-file-coding-system coding-system-for-write))
  23488. (let ((case-fold-search nil)
  23489. (org-odd-levels-only odd))
  23490. ;; create local variables for all options, to make sure all called
  23491. ;; functions get the correct information
  23492. (mapc (lambda (x)
  23493. (set (make-local-variable (cdr x))
  23494. (plist-get opt-plist (car x))))
  23495. org-export-plist-vars)
  23496. (setq umax (if arg (prefix-numeric-value arg)
  23497. org-export-headline-levels))
  23498. (setq umax-toc (if (integerp org-export-with-toc)
  23499. (min org-export-with-toc umax)
  23500. umax))
  23501. (unless body-only
  23502. ;; File header
  23503. (insert (format
  23504. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23505. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23506. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23507. lang=\"%s\" xml:lang=\"%s\">
  23508. <head>
  23509. <title>%s</title>
  23510. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23511. <meta name=\"generator\" content=\"Org-mode\"/>
  23512. <meta name=\"generated\" content=\"%s\"/>
  23513. <meta name=\"author\" content=\"%s\"/>
  23514. %s
  23515. </head><body>
  23516. "
  23517. language language (org-html-expand title)
  23518. (or charset "iso-8859-1") date author style))
  23519. (insert (or (plist-get opt-plist :preamble) ""))
  23520. (when (plist-get opt-plist :auto-preamble)
  23521. (if title (insert (format org-export-html-title-format
  23522. (org-html-expand title))))))
  23523. (if (and org-export-with-toc (not body-only))
  23524. (progn
  23525. (push (format "<h%d>%s</h%d>\n"
  23526. org-export-html-toplevel-hlevel
  23527. (nth 3 lang-words)
  23528. org-export-html-toplevel-hlevel)
  23529. thetoc)
  23530. (push "<ul>\n<li>" thetoc)
  23531. (setq lines
  23532. (mapcar '(lambda (line)
  23533. (if (string-match org-todo-line-regexp line)
  23534. ;; This is a headline
  23535. (progn
  23536. (setq have-headings t)
  23537. (setq level (- (match-end 1) (match-beginning 1))
  23538. level (org-tr-level level)
  23539. txt (save-match-data
  23540. (org-html-expand
  23541. (org-export-cleanup-toc-line
  23542. (match-string 3 line))))
  23543. todo
  23544. (or (and org-export-mark-todo-in-toc
  23545. (match-beginning 2)
  23546. (not (member (match-string 2 line)
  23547. org-done-keywords)))
  23548. ; TODO, not DONE
  23549. (and org-export-mark-todo-in-toc
  23550. (= level umax-toc)
  23551. (org-search-todo-below
  23552. line lines level))))
  23553. (if (string-match
  23554. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23555. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23556. (if (string-match quote-re0 txt)
  23557. (setq txt (replace-match "" t t txt)))
  23558. (if org-export-with-section-numbers
  23559. (setq txt (concat (org-section-number level)
  23560. " " txt)))
  23561. (if (<= level (max umax umax-toc))
  23562. (setq head-count (+ head-count 1)))
  23563. (if (<= level umax-toc)
  23564. (progn
  23565. (if (> level org-last-level)
  23566. (progn
  23567. (setq cnt (- level org-last-level))
  23568. (while (>= (setq cnt (1- cnt)) 0)
  23569. (push "\n<ul>\n<li>" thetoc))
  23570. (push "\n" thetoc)))
  23571. (if (< level org-last-level)
  23572. (progn
  23573. (setq cnt (- org-last-level level))
  23574. (while (>= (setq cnt (1- cnt)) 0)
  23575. (push "</li>\n</ul>" thetoc))
  23576. (push "\n" thetoc)))
  23577. ;; Check for targets
  23578. (while (string-match org-target-regexp line)
  23579. (setq tg (match-string 1 line)
  23580. line (replace-match
  23581. (concat "@<span class=\"target\">" tg "@</span> ")
  23582. t t line))
  23583. (push (cons (org-solidify-link-text tg)
  23584. (format "sec-%d" head-count))
  23585. target-alist))
  23586. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23587. (setq txt (replace-match "" t t txt)))
  23588. (push
  23589. (format
  23590. (if todo
  23591. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23592. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23593. head-count txt) thetoc)
  23594. (setq org-last-level level))
  23595. )))
  23596. line)
  23597. lines))
  23598. (while (> org-last-level (1- org-min-level))
  23599. (setq org-last-level (1- org-last-level))
  23600. (push "</li>\n</ul>\n" thetoc))
  23601. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23602. (setq head-count 0)
  23603. (org-init-section-numbers)
  23604. (while (setq line (pop lines) origline line)
  23605. (catch 'nextline
  23606. ;; end of quote section?
  23607. (when (and inquote (string-match "^\\*+ " line))
  23608. (insert "</pre>\n")
  23609. (setq inquote nil))
  23610. ;; inside a quote section?
  23611. (when inquote
  23612. (insert (org-html-protect line) "\n")
  23613. (throw 'nextline nil))
  23614. ;; verbatim lines
  23615. (when (and org-export-with-fixed-width
  23616. (string-match "^[ \t]*:\\(.*\\)" line))
  23617. (when (not infixed)
  23618. (setq infixed t)
  23619. (insert "<pre>\n"))
  23620. (insert (org-html-protect (match-string 1 line)) "\n")
  23621. (when (and lines
  23622. (not (string-match "^[ \t]*\\(:.*\\)"
  23623. (car lines))))
  23624. (setq infixed nil)
  23625. (insert "</pre>\n"))
  23626. (throw 'nextline nil))
  23627. ;; Protected HTML
  23628. (when (get-text-property 0 'org-protected line)
  23629. (let (par)
  23630. (when (re-search-backward
  23631. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23632. (setq par (match-string 1))
  23633. (replace-match "\\2\n"))
  23634. (insert line "\n")
  23635. (while (and lines
  23636. (or (= (length (car lines)) 0)
  23637. (get-text-property 0 'org-protected (car lines))))
  23638. (insert (pop lines) "\n"))
  23639. (and par (insert "<p>\n")))
  23640. (throw 'nextline nil))
  23641. ;; Horizontal line
  23642. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23643. (insert "\n<hr/>\n")
  23644. (throw 'nextline nil))
  23645. ;; make targets to anchors
  23646. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23647. (cond
  23648. ((match-end 2)
  23649. (setq line (replace-match
  23650. (concat "@<a name=\""
  23651. (org-solidify-link-text (match-string 1 line))
  23652. "\">\\nbsp@</a>")
  23653. t t line)))
  23654. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23655. (setq line (replace-match
  23656. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23657. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23658. t t line)))
  23659. (t
  23660. (setq line (replace-match
  23661. (concat "@<a name=\""
  23662. (org-solidify-link-text (match-string 1 line))
  23663. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23664. t t line)))))
  23665. (setq line (org-html-handle-time-stamps line))
  23666. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23667. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23668. ;; Also handle sub_superscripts and checkboxes
  23669. (or (string-match org-table-hline-regexp line)
  23670. (setq line (org-html-expand line)))
  23671. ;; Format the links
  23672. (setq start 0)
  23673. (while (string-match org-bracket-link-analytic-regexp line start)
  23674. (setq start (match-beginning 0))
  23675. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23676. (setq path (match-string 3 line))
  23677. (setq desc1 (if (match-end 5) (match-string 5 line))
  23678. desc2 (if (match-end 2) (concat type ":" path) path)
  23679. descp (and desc1 (not (equal desc1 desc2)))
  23680. desc (or desc1 desc2))
  23681. ;; Make an image out of the description if that is so wanted
  23682. (when (and descp (org-file-image-p desc))
  23683. (save-match-data
  23684. (if (string-match "^file:" desc)
  23685. (setq desc (substring desc (match-end 0)))))
  23686. (setq desc (concat "<img src=\"" desc "\"/>")))
  23687. ;; FIXME: do we need to unescape here somewhere?
  23688. (cond
  23689. ((equal type "internal")
  23690. (setq rpl
  23691. (concat
  23692. "<a href=\"#"
  23693. (org-solidify-link-text
  23694. (save-match-data (org-link-unescape path)) target-alist)
  23695. "\">" desc "</a>")))
  23696. ((member type '("http" "https"))
  23697. ;; standard URL, just check if we need to inline an image
  23698. (if (and (or (eq t org-export-html-inline-images)
  23699. (and org-export-html-inline-images (not descp)))
  23700. (org-file-image-p path))
  23701. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23702. (setq link (concat type ":" path))
  23703. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23704. ((member type '("ftp" "mailto" "news"))
  23705. ;; standard URL
  23706. (setq link (concat type ":" path))
  23707. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23708. ((string= type "file")
  23709. ;; FILE link
  23710. (let* ((filename path)
  23711. (abs-p (file-name-absolute-p filename))
  23712. thefile file-is-image-p search)
  23713. (save-match-data
  23714. (if (string-match "::\\(.*\\)" filename)
  23715. (setq search (match-string 1 filename)
  23716. filename (replace-match "" t nil filename)))
  23717. (setq valid
  23718. (if (functionp link-validate)
  23719. (funcall link-validate filename current-dir)
  23720. t))
  23721. (setq file-is-image-p (org-file-image-p filename))
  23722. (setq thefile (if abs-p (expand-file-name filename) filename))
  23723. (when (and org-export-html-link-org-files-as-html
  23724. (string-match "\\.org$" thefile))
  23725. (setq thefile (concat (substring thefile 0
  23726. (match-beginning 0))
  23727. "." html-extension))
  23728. (if (and search
  23729. ;; make sure this is can be used as target search
  23730. (not (string-match "^[0-9]*$" search))
  23731. (not (string-match "^\\*" search))
  23732. (not (string-match "^/.*/$" search)))
  23733. (setq thefile (concat thefile "#"
  23734. (org-solidify-link-text
  23735. (org-link-unescape search)))))
  23736. (when (string-match "^file:" desc)
  23737. (setq desc (replace-match "" t t desc))
  23738. (if (string-match "\\.org$" desc)
  23739. (setq desc (replace-match "" t t desc))))))
  23740. (setq rpl (if (and file-is-image-p
  23741. (or (eq t org-export-html-inline-images)
  23742. (and org-export-html-inline-images
  23743. (not descp))))
  23744. (concat "<img src=\"" thefile "\"/>")
  23745. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23746. (if (not valid) (setq rpl desc))))
  23747. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23748. (setq rpl (concat "<i>&lt;" type ":"
  23749. (save-match-data (org-link-unescape path))
  23750. "&gt;</i>"))))
  23751. (setq line (replace-match rpl t t line)
  23752. start (+ start (length rpl))))
  23753. ;; TODO items
  23754. (if (and (string-match org-todo-line-regexp line)
  23755. (match-beginning 2))
  23756. (setq line
  23757. (concat (substring line 0 (match-beginning 2))
  23758. "<span class=\""
  23759. (if (member (match-string 2 line)
  23760. org-done-keywords)
  23761. "done" "todo")
  23762. "\">" (match-string 2 line)
  23763. "</span>" (substring line (match-end 2)))))
  23764. ;; Does this contain a reference to a footnote?
  23765. (when org-export-with-footnotes
  23766. (setq start 0)
  23767. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23768. (if (get-text-property (match-beginning 2) 'org-protected line)
  23769. (setq start (match-end 2))
  23770. (let ((n (match-string 2 line)))
  23771. (setq line
  23772. (replace-match
  23773. (format
  23774. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23775. (match-string 1 line) n n n)
  23776. t t line))))))
  23777. (cond
  23778. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23779. ;; This is a headline
  23780. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23781. txt (match-string 2 line))
  23782. (if (string-match quote-re0 txt)
  23783. (setq txt (replace-match "" t t txt)))
  23784. (if (<= level (max umax umax-toc))
  23785. (setq head-count (+ head-count 1)))
  23786. (when in-local-list
  23787. ;; Close any local lists before inserting a new header line
  23788. (while local-list-num
  23789. (org-close-li)
  23790. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23791. (pop local-list-num))
  23792. (setq local-list-indent nil
  23793. in-local-list nil))
  23794. (setq first-heading-pos (or first-heading-pos (point)))
  23795. (org-html-level-start level txt umax
  23796. (and org-export-with-toc (<= level umax))
  23797. head-count)
  23798. ;; QUOTES
  23799. (when (string-match quote-re line)
  23800. (insert "<pre>")
  23801. (setq inquote t)))
  23802. ((and org-export-with-tables
  23803. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23804. (if (not table-open)
  23805. ;; New table starts
  23806. (setq table-open t table-buffer nil table-orig-buffer nil))
  23807. ;; Accumulate lines
  23808. (setq table-buffer (cons line table-buffer)
  23809. table-orig-buffer (cons origline table-orig-buffer))
  23810. (when (or (not lines)
  23811. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23812. (car lines))))
  23813. (setq table-open nil
  23814. table-buffer (nreverse table-buffer)
  23815. table-orig-buffer (nreverse table-orig-buffer))
  23816. (org-close-par-maybe)
  23817. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23818. (t
  23819. ;; Normal lines
  23820. (when (string-match
  23821. (cond
  23822. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23823. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23824. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23825. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23826. line)
  23827. (setq ind (org-get-string-indentation line)
  23828. start-is-num (match-beginning 4)
  23829. starter (if (match-beginning 2)
  23830. (substring (match-string 2 line) 0 -1))
  23831. line (substring line (match-beginning 5)))
  23832. (unless (string-match "[^ \t]" line)
  23833. ;; empty line. Pretend indentation is large.
  23834. (setq ind (if org-empty-line-terminates-plain-lists
  23835. 0
  23836. (1+ (or (car local-list-indent) 1)))))
  23837. (setq didclose nil)
  23838. (while (and in-local-list
  23839. (or (and (= ind (car local-list-indent))
  23840. (not starter))
  23841. (< ind (car local-list-indent))))
  23842. (setq didclose t)
  23843. (org-close-li)
  23844. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23845. (pop local-list-num) (pop local-list-indent)
  23846. (setq in-local-list local-list-indent))
  23847. (cond
  23848. ((and starter
  23849. (or (not in-local-list)
  23850. (> ind (car local-list-indent))))
  23851. ;; Start new (level of) list
  23852. (org-close-par-maybe)
  23853. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23854. (push start-is-num local-list-num)
  23855. (push ind local-list-indent)
  23856. (setq in-local-list t))
  23857. (starter
  23858. ;; continue current list
  23859. (org-close-li)
  23860. (insert "<li>\n"))
  23861. (didclose
  23862. ;; we did close a list, normal text follows: need <p>
  23863. (org-open-par)))
  23864. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23865. (setq line
  23866. (replace-match
  23867. (if (equal (match-string 1 line) "X")
  23868. "<b>[X]</b>"
  23869. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23870. t t line))))
  23871. ;; Empty lines start a new paragraph. If hand-formatted lists
  23872. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23873. ;; also start a new paragraph.
  23874. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23875. ;; Is this the start of a footnote?
  23876. (when org-export-with-footnotes
  23877. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23878. (org-close-par-maybe)
  23879. (let ((n (match-string 1 line)))
  23880. (setq line (replace-match
  23881. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23882. ;; Check if the line break needs to be conserved
  23883. (cond
  23884. ((string-match "\\\\\\\\[ \t]*$" line)
  23885. (setq line (replace-match "<br/>" t t line)))
  23886. (org-export-preserve-breaks
  23887. (setq line (concat line "<br/>"))))
  23888. (insert line "\n")))))
  23889. ;; Properly close all local lists and other lists
  23890. (when inquote (insert "</pre>\n"))
  23891. (when in-local-list
  23892. ;; Close any local lists before inserting a new header line
  23893. (while local-list-num
  23894. (org-close-li)
  23895. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23896. (pop local-list-num))
  23897. (setq local-list-indent nil
  23898. in-local-list nil))
  23899. (org-html-level-start 1 nil umax
  23900. (and org-export-with-toc (<= level umax))
  23901. head-count)
  23902. (unless body-only
  23903. (when (plist-get opt-plist :auto-postamble)
  23904. (insert "<div id=\"postamble\">")
  23905. (when (and org-export-author-info author)
  23906. (insert "<p class=\"author\"> "
  23907. (nth 1 lang-words) ": " author "\n")
  23908. (when email
  23909. (if (listp (split-string email ",+ *"))
  23910. (mapc (lambda(e)
  23911. (insert "<a href=\"mailto:" e "\">&lt;"
  23912. e "&gt;</a>\n"))
  23913. (split-string email ",+ *"))
  23914. (insert "<a href=\"mailto:" email "\">&lt;"
  23915. email "&gt;</a>\n")))
  23916. (insert "</p>\n"))
  23917. (when (and date org-export-time-stamp-file)
  23918. (insert "<p class=\"date\"> "
  23919. (nth 2 lang-words) ": "
  23920. date "</p>\n"))
  23921. (insert "</div>"))
  23922. (if org-export-html-with-timestamp
  23923. (insert org-export-html-html-helper-timestamp))
  23924. (insert (or (plist-get opt-plist :postamble) ""))
  23925. (insert "</body>\n</html>\n"))
  23926. (normal-mode)
  23927. (if (eq major-mode default-major-mode) (html-mode))
  23928. ;; insert the table of contents
  23929. (goto-char (point-min))
  23930. (when thetoc
  23931. (if (or (re-search-forward
  23932. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23933. (re-search-forward
  23934. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23935. (progn
  23936. (goto-char (match-beginning 0))
  23937. (replace-match ""))
  23938. (goto-char first-heading-pos)
  23939. (when (looking-at "\\s-*</p>")
  23940. (goto-char (match-end 0))
  23941. (insert "\n")))
  23942. (insert "<div id=\"table-of-contents\">\n")
  23943. (mapc 'insert thetoc)
  23944. (insert "</div>\n"))
  23945. ;; remove empty paragraphs and lists
  23946. (goto-char (point-min))
  23947. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23948. (replace-match ""))
  23949. (goto-char (point-min))
  23950. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23951. (replace-match ""))
  23952. (goto-char (point-min))
  23953. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23954. (replace-match ""))
  23955. ;; Convert whitespace place holders
  23956. (goto-char (point-min))
  23957. (let (beg end n)
  23958. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23959. (setq n (get-text-property beg 'org-whitespace)
  23960. end (next-single-property-change beg 'org-whitespace))
  23961. (goto-char beg)
  23962. (delete-region beg end)
  23963. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23964. (make-string n ?x)))))
  23965. (or to-buffer (progn (save-buffer) (kill-buffer (current-buffer))))
  23966. (goto-char (point-min))
  23967. (message "Exporting... done")
  23968. (if (eq to-buffer 'string)
  23969. (prog1 (buffer-substring (point-min) (point-max))
  23970. (kill-buffer (current-buffer)))
  23971. (current-buffer)))))
  23972. (defvar org-table-colgroup-info nil)
  23973. (defun org-format-table-ascii (lines)
  23974. "Format a table for ascii export."
  23975. (if (stringp lines)
  23976. (setq lines (org-split-string lines "\n")))
  23977. (if (not (string-match "^[ \t]*|" (car lines)))
  23978. ;; Table made by table.el - test for spanning
  23979. lines
  23980. ;; A normal org table
  23981. ;; Get rid of hlines at beginning and end
  23982. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23983. (setq lines (nreverse lines))
  23984. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23985. (setq lines (nreverse lines))
  23986. (when org-export-table-remove-special-lines
  23987. ;; Check if the table has a marking column. If yes remove the
  23988. ;; column and the special lines
  23989. (setq lines (org-table-clean-before-export lines)))
  23990. ;; Get rid of the vertical lines except for grouping
  23991. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23992. rtn line vl1 start)
  23993. (while (setq line (pop lines))
  23994. (if (string-match org-table-hline-regexp line)
  23995. (and (string-match "|\\(.*\\)|" line)
  23996. (setq line (replace-match " \\1" t nil line)))
  23997. (setq start 0 vl1 vl)
  23998. (while (string-match "|" line start)
  23999. (setq start (match-end 0))
  24000. (or (pop vl1) (setq line (replace-match " " t t line)))))
  24001. (push line rtn))
  24002. (nreverse rtn))))
  24003. (defun org-colgroup-info-to-vline-list (info)
  24004. (let (vl new last)
  24005. (while info
  24006. (setq last new new (pop info))
  24007. (if (or (memq last '(:end :startend))
  24008. (memq new '(:start :startend)))
  24009. (push t vl)
  24010. (push nil vl)))
  24011. (setq vl (nreverse vl))
  24012. (and vl (setcar vl nil))
  24013. vl))
  24014. (defun org-format-table-html (lines olines)
  24015. "Find out which HTML converter to use and return the HTML code."
  24016. (if (stringp lines)
  24017. (setq lines (org-split-string lines "\n")))
  24018. (if (string-match "^[ \t]*|" (car lines))
  24019. ;; A normal org table
  24020. (org-format-org-table-html lines)
  24021. ;; Table made by table.el - test for spanning
  24022. (let* ((hlines (delq nil (mapcar
  24023. (lambda (x)
  24024. (if (string-match "^[ \t]*\\+-" x) x
  24025. nil))
  24026. lines)))
  24027. (first (car hlines))
  24028. (ll (and (string-match "\\S-+" first)
  24029. (match-string 0 first)))
  24030. (re (concat "^[ \t]*" (regexp-quote ll)))
  24031. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24032. hlines))))
  24033. (if (and (not spanning)
  24034. (not org-export-prefer-native-exporter-for-tables))
  24035. ;; We can use my own converter with HTML conversions
  24036. (org-format-table-table-html lines)
  24037. ;; Need to use the code generator in table.el, with the original text.
  24038. (org-format-table-table-html-using-table-generate-source olines)))))
  24039. (defun org-format-org-table-html (lines &optional splice)
  24040. "Format a table into HTML."
  24041. ;; Get rid of hlines at beginning and end
  24042. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24043. (setq lines (nreverse lines))
  24044. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24045. (setq lines (nreverse lines))
  24046. (when org-export-table-remove-special-lines
  24047. ;; Check if the table has a marking column. If yes remove the
  24048. ;; column and the special lines
  24049. (setq lines (org-table-clean-before-export lines)))
  24050. (let ((head (and org-export-highlight-first-table-line
  24051. (delq nil (mapcar
  24052. (lambda (x) (string-match "^[ \t]*|-" x))
  24053. (cdr lines)))))
  24054. (nlines 0) fnum i
  24055. tbopen line fields html gr colgropen)
  24056. (if splice (setq head nil))
  24057. (unless splice (push (if head "<thead>" "<tbody>") html))
  24058. (setq tbopen t)
  24059. (while (setq line (pop lines))
  24060. (catch 'next-line
  24061. (if (string-match "^[ \t]*|-" line)
  24062. (progn
  24063. (unless splice
  24064. (push (if head "</thead>" "</tbody>") html)
  24065. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24066. (setq head nil) ;; head ends here, first time around
  24067. ;; ignore this line
  24068. (throw 'next-line t)))
  24069. ;; Break the line into fields
  24070. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24071. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24072. (setq nlines (1+ nlines) i -1)
  24073. (push (concat "<tr>"
  24074. (mapconcat
  24075. (lambda (x)
  24076. (setq i (1+ i))
  24077. (if (and (< i nlines)
  24078. (string-match org-table-number-regexp x))
  24079. (incf (aref fnum i)))
  24080. (if head
  24081. (concat (car org-export-table-header-tags) x
  24082. (cdr org-export-table-header-tags))
  24083. (concat (car org-export-table-data-tags) x
  24084. (cdr org-export-table-data-tags))))
  24085. fields "")
  24086. "</tr>")
  24087. html)))
  24088. (unless splice (if tbopen (push "</tbody>" html)))
  24089. (unless splice (push "</table>\n" html))
  24090. (setq html (nreverse html))
  24091. (unless splice
  24092. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24093. (push (mapconcat
  24094. (lambda (x)
  24095. (setq gr (pop org-table-colgroup-info))
  24096. (format "%s<col align=\"%s\"></col>%s"
  24097. (if (memq gr '(:start :startend))
  24098. (prog1
  24099. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24100. (setq colgropen t))
  24101. "")
  24102. (if (> (/ (float x) nlines) org-table-number-fraction)
  24103. "right" "left")
  24104. (if (memq gr '(:end :startend))
  24105. (progn (setq colgropen nil) "</colgroup>")
  24106. "")))
  24107. fnum "")
  24108. html)
  24109. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24110. (push html-table-tag html))
  24111. (concat (mapconcat 'identity html "\n") "\n")))
  24112. (defun org-table-clean-before-export (lines)
  24113. "Check if the table has a marking column.
  24114. If yes remove the column and the special lines."
  24115. (setq org-table-colgroup-info nil)
  24116. (if (memq nil
  24117. (mapcar
  24118. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24119. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24120. lines))
  24121. (progn
  24122. (setq org-table-clean-did-remove-column nil)
  24123. (delq nil
  24124. (mapcar
  24125. (lambda (x)
  24126. (cond
  24127. ((string-match "^[ \t]*| */ *|" x)
  24128. (setq org-table-colgroup-info
  24129. (mapcar (lambda (x)
  24130. (cond ((member x '("<" "&lt;")) :start)
  24131. ((member x '(">" "&gt;")) :end)
  24132. ((member x '("<>" "&lt;&gt;")) :startend)
  24133. (t nil)))
  24134. (org-split-string x "[ \t]*|[ \t]*")))
  24135. nil)
  24136. (t x)))
  24137. lines)))
  24138. (setq org-table-clean-did-remove-column t)
  24139. (delq nil
  24140. (mapcar
  24141. (lambda (x)
  24142. (cond
  24143. ((string-match "^[ \t]*| */ *|" x)
  24144. (setq org-table-colgroup-info
  24145. (mapcar (lambda (x)
  24146. (cond ((member x '("<" "&lt;")) :start)
  24147. ((member x '(">" "&gt;")) :end)
  24148. ((member x '("<>" "&lt;&gt;")) :startend)
  24149. (t nil)))
  24150. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24151. nil)
  24152. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24153. nil) ; ignore this line
  24154. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24155. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24156. ;; remove the first column
  24157. (replace-match "\\1|" t nil x))))
  24158. lines))))
  24159. (defun org-format-table-table-html (lines)
  24160. "Format a table generated by table.el into HTML.
  24161. This conversion does *not* use `table-generate-source' from table.el.
  24162. This has the advantage that Org-mode's HTML conversions can be used.
  24163. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24164. (let (line field-buffer
  24165. (head org-export-highlight-first-table-line)
  24166. fields html empty)
  24167. (setq html (concat html-table-tag "\n"))
  24168. (while (setq line (pop lines))
  24169. (setq empty "&nbsp;")
  24170. (catch 'next-line
  24171. (if (string-match "^[ \t]*\\+-" line)
  24172. (progn
  24173. (if field-buffer
  24174. (progn
  24175. (setq
  24176. html
  24177. (concat
  24178. html
  24179. "<tr>"
  24180. (mapconcat
  24181. (lambda (x)
  24182. (if (equal x "") (setq x empty))
  24183. (if head
  24184. (concat (car org-export-table-header-tags) x
  24185. (cdr org-export-table-header-tags))
  24186. (concat (car org-export-table-data-tags) x
  24187. (cdr org-export-table-data-tags))))
  24188. field-buffer "\n")
  24189. "</tr>\n"))
  24190. (setq head nil)
  24191. (setq field-buffer nil)))
  24192. ;; Ignore this line
  24193. (throw 'next-line t)))
  24194. ;; Break the line into fields and store the fields
  24195. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24196. (if field-buffer
  24197. (setq field-buffer (mapcar
  24198. (lambda (x)
  24199. (concat x "<br/>" (pop fields)))
  24200. field-buffer))
  24201. (setq field-buffer fields))))
  24202. (setq html (concat html "</table>\n"))
  24203. html))
  24204. (defun org-format-table-table-html-using-table-generate-source (lines)
  24205. "Format a table into html, using `table-generate-source' from table.el.
  24206. This has the advantage that cell- or row-spanning is allowed.
  24207. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24208. (require 'table)
  24209. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24210. (erase-buffer)
  24211. (insert (mapconcat 'identity lines "\n"))
  24212. (goto-char (point-min))
  24213. (if (not (re-search-forward "|[^+]" nil t))
  24214. (error "Error processing table"))
  24215. (table-recognize-table)
  24216. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24217. (table-generate-source 'html " org-tmp2 ")
  24218. (set-buffer " org-tmp2 ")
  24219. (buffer-substring (point-min) (point-max))))
  24220. (defun org-html-handle-time-stamps (s)
  24221. "Format time stamps in string S, or remove them."
  24222. (catch 'exit
  24223. (let (r b)
  24224. (while (string-match org-maybe-keyword-time-regexp s)
  24225. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24226. ;; never export CLOCK
  24227. (throw 'exit ""))
  24228. (or b (setq b (substring s 0 (match-beginning 0))))
  24229. (if (not org-export-with-timestamps)
  24230. (setq r (concat r (substring s 0 (match-beginning 0)))
  24231. s (substring s (match-end 0)))
  24232. (setq r (concat
  24233. r (substring s 0 (match-beginning 0))
  24234. (if (match-end 1)
  24235. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24236. (match-string 1 s)))
  24237. (format " @<span class=\"timestamp\">%s@</span>"
  24238. (substring
  24239. (org-translate-time (match-string 3 s)) 1 -1)))
  24240. s (substring s (match-end 0)))))
  24241. ;; Line break if line started and ended with time stamp stuff
  24242. (if (not r)
  24243. s
  24244. (setq r (concat r s))
  24245. (unless (string-match "\\S-" (concat b s))
  24246. (setq r (concat r "@<br/>")))
  24247. r))))
  24248. (defun org-html-protect (s)
  24249. ;; convert & to &amp;, < to &lt; and > to &gt;
  24250. (let ((start 0))
  24251. (while (string-match "&" s start)
  24252. (setq s (replace-match "&amp;" t t s)
  24253. start (1+ (match-beginning 0))))
  24254. (while (string-match "<" s)
  24255. (setq s (replace-match "&lt;" t t s)))
  24256. (while (string-match ">" s)
  24257. (setq s (replace-match "&gt;" t t s))))
  24258. s)
  24259. (defun org-export-cleanup-toc-line (s)
  24260. "Remove tags and time staps from lines going into the toc."
  24261. (when (memq org-export-with-tags '(not-in-toc nil))
  24262. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24263. (setq s (replace-match "" t t s))))
  24264. (when org-export-remove-timestamps-from-toc
  24265. (while (string-match org-maybe-keyword-time-regexp s)
  24266. (setq s (replace-match "" t t s))))
  24267. (while (string-match org-bracket-link-regexp s)
  24268. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24269. t t s)))
  24270. s)
  24271. (defun org-html-expand (string)
  24272. "Prepare STRING for HTML export. Applies all active conversions.
  24273. If there are links in the string, don't modify these."
  24274. (let* ((re (concat org-bracket-link-regexp "\\|"
  24275. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24276. m s l res)
  24277. (while (setq m (string-match re string))
  24278. (setq s (substring string 0 m)
  24279. l (match-string 0 string)
  24280. string (substring string (match-end 0)))
  24281. (push (org-html-do-expand s) res)
  24282. (push l res))
  24283. (push (org-html-do-expand string) res)
  24284. (apply 'concat (nreverse res))))
  24285. (defun org-html-do-expand (s)
  24286. "Apply all active conversions to translate special ASCII to HTML."
  24287. (setq s (org-html-protect s))
  24288. (if org-export-html-expand
  24289. (let ((start 0))
  24290. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24291. (setq s (replace-match "<\\1>" t nil s)))))
  24292. (if org-export-with-emphasize
  24293. (setq s (org-export-html-convert-emphasize s)))
  24294. (if org-export-with-special-strings
  24295. (setq s (org-export-html-convert-special-strings s)))
  24296. (if org-export-with-sub-superscripts
  24297. (setq s (org-export-html-convert-sub-super s)))
  24298. (if org-export-with-TeX-macros
  24299. (let ((start 0) wd ass)
  24300. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24301. (if (get-text-property (match-beginning 0) 'org-protected s)
  24302. (setq start (match-end 0))
  24303. (setq wd (match-string 1 s))
  24304. (if (setq ass (assoc wd org-html-entities))
  24305. (setq s (replace-match (or (cdr ass)
  24306. (concat "&" (car ass) ";"))
  24307. t t s))
  24308. (setq start (+ start (length wd))))))))
  24309. s)
  24310. (defun org-create-multibrace-regexp (left right n)
  24311. "Create a regular expression which will match a balanced sexp.
  24312. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24313. as single character strings.
  24314. The regexp returned will match the entire expression including the
  24315. delimiters. It will also define a single group which contains the
  24316. match except for the outermost delimiters. The maximum depth of
  24317. stacked delimiters is N. Escaping delimiters is not possible."
  24318. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24319. (or "\\|")
  24320. (re nothing)
  24321. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24322. (while (> n 1)
  24323. (setq n (1- n)
  24324. re (concat re or next)
  24325. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24326. (concat left "\\(" re "\\)" right)))
  24327. (defvar org-match-substring-regexp
  24328. (concat
  24329. "\\([^\\]\\)\\([_^]\\)\\("
  24330. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24331. "\\|"
  24332. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24333. "\\|"
  24334. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24335. "The regular expression matching a sub- or superscript.")
  24336. (defvar org-match-substring-with-braces-regexp
  24337. (concat
  24338. "\\([^\\]\\)\\([_^]\\)\\("
  24339. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24340. "\\)")
  24341. "The regular expression matching a sub- or superscript, forcing braces.")
  24342. (defconst org-export-html-special-string-regexps
  24343. '(("\\\\-" . "&shy;")
  24344. ("---\\([^-]\\)" . "&mdash;\\1")
  24345. ("--\\([^-]\\)" . "&ndash;\\1")
  24346. ("\\.\\.\\." . "&hellip;"))
  24347. "Regular expressions for special string conversion.")
  24348. (defun org-export-html-convert-special-strings (string)
  24349. "Convert special characters in STRING to HTML."
  24350. (let ((all org-export-html-special-string-regexps)
  24351. e a re rpl start)
  24352. (while (setq a (pop all))
  24353. (setq re (car a) rpl (cdr a) start 0)
  24354. (while (string-match re string start)
  24355. (if (get-text-property (match-beginning 0) 'org-protected string)
  24356. (setq start (match-end 0))
  24357. (setq string (replace-match rpl t nil string)))))
  24358. string))
  24359. (defun org-export-html-convert-sub-super (string)
  24360. "Convert sub- and superscripts in STRING to HTML."
  24361. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24362. (while (string-match org-match-substring-regexp string s)
  24363. (cond
  24364. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24365. ((get-text-property (match-beginning 2) 'org-protected string)
  24366. (setq s (match-end 2)))
  24367. (t
  24368. (setq s (match-end 1)
  24369. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24370. c (or (match-string 8 string)
  24371. (match-string 6 string)
  24372. (match-string 5 string))
  24373. string (replace-match
  24374. (concat (match-string 1 string)
  24375. "<" key ">" c "</" key ">")
  24376. t t string)))))
  24377. (while (string-match "\\\\\\([_^]\\)" string)
  24378. (setq string (replace-match (match-string 1 string) t t string)))
  24379. string))
  24380. (defun org-export-html-convert-emphasize (string)
  24381. "Apply emphasis."
  24382. (let ((s 0) rpl)
  24383. (while (string-match org-emph-re string s)
  24384. (if (not (equal
  24385. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24386. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24387. (setq s (match-beginning 0)
  24388. rpl
  24389. (concat
  24390. (match-string 1 string)
  24391. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24392. (match-string 4 string)
  24393. (nth 3 (assoc (match-string 3 string)
  24394. org-emphasis-alist))
  24395. (match-string 5 string))
  24396. string (replace-match rpl t t string)
  24397. s (+ s (- (length rpl) 2)))
  24398. (setq s (1+ s))))
  24399. string))
  24400. (defvar org-par-open nil)
  24401. (defun org-open-par ()
  24402. "Insert <p>, but first close previous paragraph if any."
  24403. (org-close-par-maybe)
  24404. (insert "\n<p>")
  24405. (setq org-par-open t))
  24406. (defun org-close-par-maybe ()
  24407. "Close paragraph if there is one open."
  24408. (when org-par-open
  24409. (insert "</p>")
  24410. (setq org-par-open nil)))
  24411. (defun org-close-li ()
  24412. "Close <li> if necessary."
  24413. (org-close-par-maybe)
  24414. (insert "</li>\n"))
  24415. (defvar body-only) ; dynamically scoped into this.
  24416. (defun org-html-level-start (level title umax with-toc head-count)
  24417. "Insert a new level in HTML export.
  24418. When TITLE is nil, just close all open levels."
  24419. (org-close-par-maybe)
  24420. (let ((l org-level-max))
  24421. (while (>= l level)
  24422. (if (aref org-levels-open (1- l))
  24423. (progn
  24424. (org-html-level-close l umax)
  24425. (aset org-levels-open (1- l) nil)))
  24426. (setq l (1- l)))
  24427. (when title
  24428. ;; If title is nil, this means this function is called to close
  24429. ;; all levels, so the rest is done only if title is given
  24430. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24431. (setq title (replace-match
  24432. (if org-export-with-tags
  24433. (save-match-data
  24434. (concat
  24435. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24436. (mapconcat 'identity (org-split-string
  24437. (match-string 1 title) ":")
  24438. "&nbsp;")
  24439. "</span>"))
  24440. "")
  24441. t t title)))
  24442. (if (> level umax)
  24443. (progn
  24444. (if (aref org-levels-open (1- level))
  24445. (progn
  24446. (org-close-li)
  24447. (insert "<li>" title "<br/>\n"))
  24448. (aset org-levels-open (1- level) t)
  24449. (org-close-par-maybe)
  24450. (insert "<ul>\n<li>" title "<br/>\n")))
  24451. (aset org-levels-open (1- level) t)
  24452. (if (and org-export-with-section-numbers (not body-only))
  24453. (setq title (concat (org-section-number level) " " title)))
  24454. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24455. (if with-toc
  24456. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24457. level level head-count title level))
  24458. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24459. (org-open-par)))))
  24460. (defun org-html-level-close (level max-outline-level)
  24461. "Terminate one level in HTML export."
  24462. (if (<= level max-outline-level)
  24463. (insert "</div>\n")
  24464. (org-close-li)
  24465. (insert "</ul>\n")))
  24466. ;;; iCalendar export
  24467. ;;;###autoload
  24468. (defun org-export-icalendar-this-file ()
  24469. "Export current file as an iCalendar file.
  24470. The iCalendar file will be located in the same directory as the Org-mode
  24471. file, but with extension `.ics'."
  24472. (interactive)
  24473. (org-export-icalendar nil buffer-file-name))
  24474. ;;;###autoload
  24475. (defun org-export-icalendar-all-agenda-files ()
  24476. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24477. Each iCalendar file will be located in the same directory as the Org-mode
  24478. file, but with extension `.ics'."
  24479. (interactive)
  24480. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24481. ;;;###autoload
  24482. (defun org-export-icalendar-combine-agenda-files ()
  24483. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24484. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24485. (interactive)
  24486. (apply 'org-export-icalendar t (org-agenda-files t)))
  24487. (defun org-export-icalendar (combine &rest files)
  24488. "Create iCalendar files for all elements of FILES.
  24489. If COMBINE is non-nil, combine all calendar entries into a single large
  24490. file and store it under the name `org-combined-agenda-icalendar-file'."
  24491. (save-excursion
  24492. (org-prepare-agenda-buffers files)
  24493. (let* ((dir (org-export-directory
  24494. :ical (list :publishing-directory
  24495. org-export-publishing-directory)))
  24496. file ical-file ical-buffer category started org-agenda-new-buffers)
  24497. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24498. (when combine
  24499. (setq ical-file
  24500. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24501. org-combined-agenda-icalendar-file
  24502. (expand-file-name org-combined-agenda-icalendar-file dir))
  24503. ical-buffer (org-get-agenda-file-buffer ical-file))
  24504. (set-buffer ical-buffer) (erase-buffer))
  24505. (while (setq file (pop files))
  24506. (catch 'nextfile
  24507. (org-check-agenda-file file)
  24508. (set-buffer (org-get-agenda-file-buffer file))
  24509. (unless combine
  24510. (setq ical-file (concat (file-name-as-directory dir)
  24511. (file-name-sans-extension
  24512. (file-name-nondirectory buffer-file-name))
  24513. ".ics"))
  24514. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24515. (with-current-buffer ical-buffer (erase-buffer)))
  24516. (setq category (or org-category
  24517. (file-name-sans-extension
  24518. (file-name-nondirectory buffer-file-name))))
  24519. (if (symbolp category) (setq category (symbol-name category)))
  24520. (let ((standard-output ical-buffer))
  24521. (if combine
  24522. (and (not started) (setq started t)
  24523. (org-start-icalendar-file org-icalendar-combined-name))
  24524. (org-start-icalendar-file category))
  24525. (org-print-icalendar-entries combine)
  24526. (when (or (and combine (not files)) (not combine))
  24527. (org-finish-icalendar-file)
  24528. (set-buffer ical-buffer)
  24529. (save-buffer)
  24530. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24531. (org-release-buffers org-agenda-new-buffers))))
  24532. (defvar org-after-save-iCalendar-file-hook nil
  24533. "Hook run after an iCalendar file has been saved.
  24534. The iCalendar buffer is still current when this hook is run.
  24535. A good way to use this is to tell a desktop calenndar application to re-read
  24536. the iCalendar file.")
  24537. (defun org-print-icalendar-entries (&optional combine)
  24538. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24539. When COMBINE is non nil, add the category to each line."
  24540. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24541. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24542. (dts (org-ical-ts-to-string
  24543. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24544. "DTSTART"))
  24545. hd ts ts2 state status (inc t) pos b sexp rrule
  24546. scheduledp deadlinep tmp pri category entry location summary desc
  24547. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24548. (org-refresh-category-properties)
  24549. (save-excursion
  24550. (goto-char (point-min))
  24551. (while (re-search-forward re1 nil t)
  24552. (catch :skip
  24553. (org-agenda-skip)
  24554. (setq pos (match-beginning 0)
  24555. ts (match-string 0)
  24556. inc t
  24557. hd (org-get-heading)
  24558. summary (org-icalendar-cleanup-string
  24559. (org-entry-get nil "SUMMARY"))
  24560. desc (org-icalendar-cleanup-string
  24561. (or (org-entry-get nil "DESCRIPTION")
  24562. (and org-icalendar-include-body (org-get-entry)))
  24563. t org-icalendar-include-body)
  24564. location (org-icalendar-cleanup-string
  24565. (org-entry-get nil "LOCATION"))
  24566. category (org-get-category))
  24567. (if (looking-at re2)
  24568. (progn
  24569. (goto-char (match-end 0))
  24570. (setq ts2 (match-string 1) inc nil))
  24571. (setq tmp (buffer-substring (max (point-min)
  24572. (- pos org-ds-keyword-length))
  24573. pos)
  24574. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24575. (progn
  24576. (setq inc nil)
  24577. (replace-match "\\1" t nil ts))
  24578. ts)
  24579. deadlinep (string-match org-deadline-regexp tmp)
  24580. scheduledp (string-match org-scheduled-regexp tmp)
  24581. ;; donep (org-entry-is-done-p)
  24582. ))
  24583. (if (or (string-match org-tr-regexp hd)
  24584. (string-match org-ts-regexp hd))
  24585. (setq hd (replace-match "" t t hd)))
  24586. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24587. (setq rrule
  24588. (concat "\nRRULE:FREQ="
  24589. (cdr (assoc
  24590. (match-string 2 ts)
  24591. '(("d" . "DAILY")("w" . "WEEKLY")
  24592. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24593. ";INTERVAL=" (match-string 1 ts)))
  24594. (setq rrule ""))
  24595. (setq summary (or summary hd))
  24596. (if (string-match org-bracket-link-regexp summary)
  24597. (setq summary
  24598. (replace-match (if (match-end 3)
  24599. (match-string 3 summary)
  24600. (match-string 1 summary))
  24601. t t summary)))
  24602. (if deadlinep (setq summary (concat "DL: " summary)))
  24603. (if scheduledp (setq summary (concat "S: " summary)))
  24604. (if (string-match "\\`<%%" ts)
  24605. (with-current-buffer sexp-buffer
  24606. (insert (substring ts 1 -1) " " summary "\n"))
  24607. (princ (format "BEGIN:VEVENT
  24608. %s
  24609. %s%s
  24610. SUMMARY:%s%s%s
  24611. CATEGORIES:%s
  24612. END:VEVENT\n"
  24613. (org-ical-ts-to-string ts "DTSTART")
  24614. (org-ical-ts-to-string ts2 "DTEND" inc)
  24615. rrule summary
  24616. (if (and desc (string-match "\\S-" desc))
  24617. (concat "\nDESCRIPTION: " desc) "")
  24618. (if (and location (string-match "\\S-" location))
  24619. (concat "\nLOCATION: " location) "")
  24620. category)))))
  24621. (when (and org-icalendar-include-sexps
  24622. (condition-case nil (require 'icalendar) (error nil))
  24623. (fboundp 'icalendar-export-region))
  24624. ;; Get all the literal sexps
  24625. (goto-char (point-min))
  24626. (while (re-search-forward "^&?%%(" nil t)
  24627. (catch :skip
  24628. (org-agenda-skip)
  24629. (setq b (match-beginning 0))
  24630. (goto-char (1- (match-end 0)))
  24631. (forward-sexp 1)
  24632. (end-of-line 1)
  24633. (setq sexp (buffer-substring b (point)))
  24634. (with-current-buffer sexp-buffer
  24635. (insert sexp "\n"))
  24636. (princ (org-diary-to-ical-string sexp-buffer)))))
  24637. (when org-icalendar-include-todo
  24638. (goto-char (point-min))
  24639. (while (re-search-forward org-todo-line-regexp nil t)
  24640. (catch :skip
  24641. (org-agenda-skip)
  24642. (setq state (match-string 2))
  24643. (setq status (if (member state org-done-keywords)
  24644. "COMPLETED" "NEEDS-ACTION"))
  24645. (when (and state
  24646. (or (not (member state org-done-keywords))
  24647. (eq org-icalendar-include-todo 'all))
  24648. (not (member org-archive-tag (org-get-tags-at)))
  24649. )
  24650. (setq hd (match-string 3)
  24651. summary (org-icalendar-cleanup-string
  24652. (org-entry-get nil "SUMMARY"))
  24653. desc (org-icalendar-cleanup-string
  24654. (or (org-entry-get nil "DESCRIPTION")
  24655. (and org-icalendar-include-body (org-get-entry)))
  24656. t org-icalendar-include-body)
  24657. location (org-icalendar-cleanup-string
  24658. (org-entry-get nil "LOCATION")))
  24659. (if (string-match org-bracket-link-regexp hd)
  24660. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24661. (match-string 1 hd))
  24662. t t hd)))
  24663. (if (string-match org-priority-regexp hd)
  24664. (setq pri (string-to-char (match-string 2 hd))
  24665. hd (concat (substring hd 0 (match-beginning 1))
  24666. (substring hd (match-end 1))))
  24667. (setq pri org-default-priority))
  24668. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24669. (- org-lowest-priority org-highest-priority))))))
  24670. (princ (format "BEGIN:VTODO
  24671. %s
  24672. SUMMARY:%s%s%s
  24673. CATEGORIES:%s
  24674. SEQUENCE:1
  24675. PRIORITY:%d
  24676. STATUS:%s
  24677. END:VTODO\n"
  24678. dts
  24679. (or summary hd)
  24680. (if (and location (string-match "\\S-" location))
  24681. (concat "\nLOCATION: " location) "")
  24682. (if (and desc (string-match "\\S-" desc))
  24683. (concat "\nDESCRIPTION: " desc) "")
  24684. category pri status)))))))))
  24685. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24686. "Take out stuff and quote what needs to be quoted.
  24687. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24688. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24689. characters."
  24690. (if (not s)
  24691. nil
  24692. (when is-body
  24693. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24694. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24695. (while (string-match re s) (setq s (replace-match "" t t s)))
  24696. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24697. (let ((start 0))
  24698. (while (string-match "\\([,;\\]\\)" s start)
  24699. (setq start (+ (match-beginning 0) 2)
  24700. s (replace-match "\\\\\\1" nil nil s))))
  24701. (when is-body
  24702. (while (string-match "[ \t]*\n[ \t]*" s)
  24703. (setq s (replace-match "\\n" t t s))))
  24704. (setq s (org-trim s))
  24705. (if is-body
  24706. (if maxlength
  24707. (if (and (numberp maxlength)
  24708. (> (length s) maxlength))
  24709. (setq s (substring s 0 maxlength)))))
  24710. s))
  24711. (defun org-get-entry ()
  24712. "Clean-up description string."
  24713. (save-excursion
  24714. (org-back-to-heading t)
  24715. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24716. (defun org-start-icalendar-file (name)
  24717. "Start an iCalendar file by inserting the header."
  24718. (let ((user user-full-name)
  24719. (name (or name "unknown"))
  24720. (timezone (cadr (current-time-zone))))
  24721. (princ
  24722. (format "BEGIN:VCALENDAR
  24723. VERSION:2.0
  24724. X-WR-CALNAME:%s
  24725. PRODID:-//%s//Emacs with Org-mode//EN
  24726. X-WR-TIMEZONE:%s
  24727. CALSCALE:GREGORIAN\n" name user timezone))))
  24728. (defun org-finish-icalendar-file ()
  24729. "Finish an iCalendar file by inserting the END statement."
  24730. (princ "END:VCALENDAR\n"))
  24731. (defun org-ical-ts-to-string (s keyword &optional inc)
  24732. "Take a time string S and convert it to iCalendar format.
  24733. KEYWORD is added in front, to make a complete line like DTSTART....
  24734. When INC is non-nil, increase the hour by two (if time string contains
  24735. a time), or the day by one (if it does not contain a time)."
  24736. (let ((t1 (org-parse-time-string s 'nodefault))
  24737. t2 fmt have-time time)
  24738. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24739. (setq t2 t1 have-time t)
  24740. (setq t2 (org-parse-time-string s)))
  24741. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24742. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24743. (when inc
  24744. (if have-time
  24745. (if org-agenda-default-appointment-duration
  24746. (setq mi (+ org-agenda-default-appointment-duration mi))
  24747. (setq h (+ 2 h)))
  24748. (setq d (1+ d))))
  24749. (setq time (encode-time s mi h d m y)))
  24750. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24751. (concat keyword (format-time-string fmt time))))
  24752. ;;; XOXO export
  24753. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24754. (with-current-buffer buffer
  24755. (apply 'insert output)))
  24756. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24757. (defun org-export-as-xoxo (&optional buffer)
  24758. "Export the org buffer as XOXO.
  24759. The XOXO buffer is named *xoxo-<source buffer name>*"
  24760. (interactive (list (current-buffer)))
  24761. ;; A quickie abstraction
  24762. ;; Output everything as XOXO
  24763. (with-current-buffer (get-buffer buffer)
  24764. (let* ((pos (point))
  24765. (opt-plist (org-combine-plists (org-default-export-plist)
  24766. (org-infile-export-plist)))
  24767. (filename (concat (file-name-as-directory
  24768. (org-export-directory :xoxo opt-plist))
  24769. (file-name-sans-extension
  24770. (file-name-nondirectory buffer-file-name))
  24771. ".html"))
  24772. (out (find-file-noselect filename))
  24773. (last-level 1)
  24774. (hanging-li nil))
  24775. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24776. ;; Check the output buffer is empty.
  24777. (with-current-buffer out (erase-buffer))
  24778. ;; Kick off the output
  24779. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24780. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24781. (let* ((hd (match-string-no-properties 1))
  24782. (level (length hd))
  24783. (text (concat
  24784. (match-string-no-properties 2)
  24785. (save-excursion
  24786. (goto-char (match-end 0))
  24787. (let ((str ""))
  24788. (catch 'loop
  24789. (while 't
  24790. (forward-line)
  24791. (if (looking-at "^[ \t]\\(.*\\)")
  24792. (setq str (concat str (match-string-no-properties 1)))
  24793. (throw 'loop str)))))))))
  24794. ;; Handle level rendering
  24795. (cond
  24796. ((> level last-level)
  24797. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24798. ((< level last-level)
  24799. (dotimes (- (- last-level level) 1)
  24800. (if hanging-li
  24801. (org-export-as-xoxo-insert-into out "</li>\n"))
  24802. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24803. (when hanging-li
  24804. (org-export-as-xoxo-insert-into out "</li>\n")
  24805. (setq hanging-li nil)))
  24806. ((equal level last-level)
  24807. (if hanging-li
  24808. (org-export-as-xoxo-insert-into out "</li>\n")))
  24809. )
  24810. (setq last-level level)
  24811. ;; And output the new li
  24812. (setq hanging-li 't)
  24813. (if (equal ?+ (elt text 0))
  24814. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24815. (org-export-as-xoxo-insert-into out "<li>" text))))
  24816. ;; Finally finish off the ol
  24817. (dotimes (- last-level 1)
  24818. (if hanging-li
  24819. (org-export-as-xoxo-insert-into out "</li>\n"))
  24820. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24821. (goto-char pos)
  24822. ;; Finish the buffer off and clean it up.
  24823. (switch-to-buffer-other-window out)
  24824. (indent-region (point-min) (point-max) nil)
  24825. (save-buffer)
  24826. (goto-char (point-min))
  24827. )))
  24828. ;;;; Key bindings
  24829. ;; Make `C-c C-x' a prefix key
  24830. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24831. ;; TAB key with modifiers
  24832. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24833. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24834. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24835. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24836. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24837. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24838. ;; The following line is necessary under Suse GNU/Linux
  24839. (unless (featurep 'xemacs)
  24840. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24841. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24842. (define-key org-mode-map [backtab] 'org-shifttab)
  24843. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24844. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24845. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24846. ;; Cursor keys with modifiers
  24847. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24848. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24849. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24850. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24851. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24852. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24853. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24854. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24855. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24856. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24857. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24858. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24859. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24860. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24861. ;;; Extra keys for tty access.
  24862. ;; We only set them when really needed because otherwise the
  24863. ;; menus don't show the simple keys
  24864. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24865. (not window-system))
  24866. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24867. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24868. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24869. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24870. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24871. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24872. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24873. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24874. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24875. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24876. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24877. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24878. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24879. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24880. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24881. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24882. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24883. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24884. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24885. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24886. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24887. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24888. ;; All the other keys
  24889. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24890. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24891. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24892. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24893. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24894. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24895. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24896. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24897. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24898. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24899. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24900. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24901. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24902. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24903. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24904. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24905. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24906. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24907. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24908. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24909. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24910. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24911. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24912. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24913. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24914. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24915. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24916. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24917. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24918. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24919. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24920. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24921. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24922. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24923. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24924. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24925. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24926. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24927. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24928. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  24929. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24930. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24931. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24932. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24933. (org-defkey org-mode-map "\C-m" 'org-return)
  24934. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24935. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24936. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24937. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24938. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24939. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24940. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24941. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24942. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24943. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24944. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24945. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24946. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24947. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24948. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24949. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24950. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24951. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24952. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24953. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24954. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24955. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24956. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24957. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24958. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24959. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24960. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24961. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24962. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24963. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24964. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24965. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24966. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24967. (when (featurep 'xemacs)
  24968. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24969. (defsubst org-table-p () (org-at-table-p))
  24970. (defun org-self-insert-command (N)
  24971. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24972. If the cursor is in a table looking at whitespace, the whitespace is
  24973. overwritten, and the table is not marked as requiring realignment."
  24974. (interactive "p")
  24975. (if (and (org-table-p)
  24976. (progn
  24977. ;; check if we blank the field, and if that triggers align
  24978. (and org-table-auto-blank-field
  24979. (member last-command
  24980. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24981. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24982. ;; got extra space, this field does not determine column width
  24983. (let (org-table-may-need-update) (org-table-blank-field))
  24984. ;; no extra space, this field may determine column width
  24985. (org-table-blank-field)))
  24986. t)
  24987. (eq N 1)
  24988. (looking-at "[^|\n]* |"))
  24989. (let (org-table-may-need-update)
  24990. (goto-char (1- (match-end 0)))
  24991. (delete-backward-char 1)
  24992. (goto-char (match-beginning 0))
  24993. (self-insert-command N))
  24994. (setq org-table-may-need-update t)
  24995. (self-insert-command N)
  24996. (org-fix-tags-on-the-fly)))
  24997. (defun org-fix-tags-on-the-fly ()
  24998. (when (and (equal (char-after (point-at-bol)) ?*)
  24999. (org-on-heading-p))
  25000. (org-align-tags-here org-tags-column)))
  25001. (defun org-delete-backward-char (N)
  25002. "Like `delete-backward-char', insert whitespace at field end in tables.
  25003. When deleting backwards, in tables this function will insert whitespace in
  25004. front of the next \"|\" separator, to keep the table aligned. The table will
  25005. still be marked for re-alignment if the field did fill the entire column,
  25006. because, in this case the deletion might narrow the column."
  25007. (interactive "p")
  25008. (if (and (org-table-p)
  25009. (eq N 1)
  25010. (string-match "|" (buffer-substring (point-at-bol) (point)))
  25011. (looking-at ".*?|"))
  25012. (let ((pos (point))
  25013. (noalign (looking-at "[^|\n\r]* |"))
  25014. (c org-table-may-need-update))
  25015. (backward-delete-char N)
  25016. (skip-chars-forward "^|")
  25017. (insert " ")
  25018. (goto-char (1- pos))
  25019. ;; noalign: if there were two spaces at the end, this field
  25020. ;; does not determine the width of the column.
  25021. (if noalign (setq org-table-may-need-update c)))
  25022. (backward-delete-char N)
  25023. (org-fix-tags-on-the-fly)))
  25024. (defun org-delete-char (N)
  25025. "Like `delete-char', but insert whitespace at field end in tables.
  25026. When deleting characters, in tables this function will insert whitespace in
  25027. front of the next \"|\" separator, to keep the table aligned. The table will
  25028. still be marked for re-alignment if the field did fill the entire column,
  25029. because, in this case the deletion might narrow the column."
  25030. (interactive "p")
  25031. (if (and (org-table-p)
  25032. (not (bolp))
  25033. (not (= (char-after) ?|))
  25034. (eq N 1))
  25035. (if (looking-at ".*?|")
  25036. (let ((pos (point))
  25037. (noalign (looking-at "[^|\n\r]* |"))
  25038. (c org-table-may-need-update))
  25039. (replace-match (concat
  25040. (substring (match-string 0) 1 -1)
  25041. " |"))
  25042. (goto-char pos)
  25043. ;; noalign: if there were two spaces at the end, this field
  25044. ;; does not determine the width of the column.
  25045. (if noalign (setq org-table-may-need-update c)))
  25046. (delete-char N))
  25047. (delete-char N)
  25048. (org-fix-tags-on-the-fly)))
  25049. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25050. (put 'org-self-insert-command 'delete-selection t)
  25051. (put 'orgtbl-self-insert-command 'delete-selection t)
  25052. (put 'org-delete-char 'delete-selection 'supersede)
  25053. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25054. ;; Make `flyspell-mode' delay after some commands
  25055. (put 'org-self-insert-command 'flyspell-delayed t)
  25056. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25057. (put 'org-delete-char 'flyspell-delayed t)
  25058. (put 'org-delete-backward-char 'flyspell-delayed t)
  25059. ;; Make pabbrev-mode expand after org-mode commands
  25060. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25061. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25062. ;; How to do this: Measure non-white length of current string
  25063. ;; If equal to column width, we should realign.
  25064. (defun org-remap (map &rest commands)
  25065. "In MAP, remap the functions given in COMMANDS.
  25066. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25067. (let (new old)
  25068. (while commands
  25069. (setq old (pop commands) new (pop commands))
  25070. (if (fboundp 'command-remapping)
  25071. (org-defkey map (vector 'remap old) new)
  25072. (substitute-key-definition old new map global-map)))))
  25073. (when (eq org-enable-table-editor 'optimized)
  25074. ;; If the user wants maximum table support, we need to hijack
  25075. ;; some standard editing functions
  25076. (org-remap org-mode-map
  25077. 'self-insert-command 'org-self-insert-command
  25078. 'delete-char 'org-delete-char
  25079. 'delete-backward-char 'org-delete-backward-char)
  25080. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25081. (defun org-shiftcursor-error ()
  25082. "Throw an error because Shift-Cursor command was applied in wrong context."
  25083. (error "This command is active in special context like tables, headlines or timestamps"))
  25084. (defun org-shifttab (&optional arg)
  25085. "Global visibility cycling or move to previous table field.
  25086. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25087. on context.
  25088. See the individual commands for more information."
  25089. (interactive "P")
  25090. (cond
  25091. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25092. (arg (message "Content view to level: ")
  25093. (org-content (prefix-numeric-value arg))
  25094. (setq org-cycle-global-status 'overview))
  25095. (t (call-interactively 'org-global-cycle))))
  25096. (defun org-shiftmetaleft ()
  25097. "Promote subtree or delete table column.
  25098. Calls `org-promote-subtree', `org-outdent-item',
  25099. or `org-table-delete-column', depending on context.
  25100. See the individual commands for more information."
  25101. (interactive)
  25102. (cond
  25103. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25104. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25105. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25106. (t (org-shiftcursor-error))))
  25107. (defun org-shiftmetaright ()
  25108. "Demote subtree or insert table column.
  25109. Calls `org-demote-subtree', `org-indent-item',
  25110. or `org-table-insert-column', depending on context.
  25111. See the individual commands for more information."
  25112. (interactive)
  25113. (cond
  25114. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25115. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25116. ((org-at-item-p) (call-interactively 'org-indent-item))
  25117. (t (org-shiftcursor-error))))
  25118. (defun org-shiftmetaup (&optional arg)
  25119. "Move subtree up or kill table row.
  25120. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25121. `org-move-item-up' depending on context. See the individual commands
  25122. for more information."
  25123. (interactive "P")
  25124. (cond
  25125. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25126. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25127. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25128. (t (org-shiftcursor-error))))
  25129. (defun org-shiftmetadown (&optional arg)
  25130. "Move subtree down or insert table row.
  25131. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25132. `org-move-item-down', depending on context. See the individual
  25133. commands for more information."
  25134. (interactive "P")
  25135. (cond
  25136. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25137. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25138. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25139. (t (org-shiftcursor-error))))
  25140. (defun org-metaleft (&optional arg)
  25141. "Promote heading or move table column to left.
  25142. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25143. With no specific context, calls the Emacs default `backward-word'.
  25144. See the individual commands for more information."
  25145. (interactive "P")
  25146. (cond
  25147. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25148. ((or (org-on-heading-p) (org-region-active-p))
  25149. (call-interactively 'org-do-promote))
  25150. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25151. (t (call-interactively 'backward-word))))
  25152. (defun org-metaright (&optional arg)
  25153. "Demote subtree or move table column to right.
  25154. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25155. With no specific context, calls the Emacs default `forward-word'.
  25156. See the individual commands for more information."
  25157. (interactive "P")
  25158. (cond
  25159. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25160. ((or (org-on-heading-p) (org-region-active-p))
  25161. (call-interactively 'org-do-demote))
  25162. ((org-at-item-p) (call-interactively 'org-indent-item))
  25163. (t (call-interactively 'forward-word))))
  25164. (defun org-metaup (&optional arg)
  25165. "Move subtree up or move table row up.
  25166. Calls `org-move-subtree-up' or `org-table-move-row' or
  25167. `org-move-item-up', depending on context. See the individual commands
  25168. for more information."
  25169. (interactive "P")
  25170. (cond
  25171. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25172. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25173. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25174. (t (transpose-lines 1) (beginning-of-line -1))))
  25175. (defun org-metadown (&optional arg)
  25176. "Move subtree down or move table row down.
  25177. Calls `org-move-subtree-down' or `org-table-move-row' or
  25178. `org-move-item-down', depending on context. See the individual
  25179. commands for more information."
  25180. (interactive "P")
  25181. (cond
  25182. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25183. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25184. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25185. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25186. (defun org-shiftup (&optional arg)
  25187. "Increase item in timestamp or increase priority of current headline.
  25188. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25189. depending on context. See the individual commands for more information."
  25190. (interactive "P")
  25191. (cond
  25192. ((org-at-timestamp-p t)
  25193. (call-interactively (if org-edit-timestamp-down-means-later
  25194. 'org-timestamp-down 'org-timestamp-up)))
  25195. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25196. ((org-at-item-p) (call-interactively 'org-previous-item))
  25197. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25198. (defun org-shiftdown (&optional arg)
  25199. "Decrease item in timestamp or decrease priority of current headline.
  25200. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25201. depending on context. See the individual commands for more information."
  25202. (interactive "P")
  25203. (cond
  25204. ((org-at-timestamp-p t)
  25205. (call-interactively (if org-edit-timestamp-down-means-later
  25206. 'org-timestamp-up 'org-timestamp-down)))
  25207. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25208. (t (call-interactively 'org-next-item))))
  25209. (defun org-shiftright ()
  25210. "Next TODO keyword or timestamp one day later, depending on context."
  25211. (interactive)
  25212. (cond
  25213. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25214. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25215. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25216. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25217. (t (org-shiftcursor-error))))
  25218. (defun org-shiftleft ()
  25219. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25220. (interactive)
  25221. (cond
  25222. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25223. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25224. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25225. ((org-at-property-p)
  25226. (call-interactively 'org-property-previous-allowed-value))
  25227. (t (org-shiftcursor-error))))
  25228. (defun org-shiftcontrolright ()
  25229. "Switch to next TODO set."
  25230. (interactive)
  25231. (cond
  25232. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25233. (t (org-shiftcursor-error))))
  25234. (defun org-shiftcontrolleft ()
  25235. "Switch to previous TODO set."
  25236. (interactive)
  25237. (cond
  25238. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25239. (t (org-shiftcursor-error))))
  25240. (defun org-ctrl-c-ret ()
  25241. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25242. (interactive)
  25243. (cond
  25244. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25245. (t (call-interactively 'org-insert-heading))))
  25246. (defun org-copy-special ()
  25247. "Copy region in table or copy current subtree.
  25248. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25249. See the individual commands for more information."
  25250. (interactive)
  25251. (call-interactively
  25252. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25253. (defun org-cut-special ()
  25254. "Cut region in table or cut current subtree.
  25255. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25256. See the individual commands for more information."
  25257. (interactive)
  25258. (call-interactively
  25259. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25260. (defun org-paste-special (arg)
  25261. "Paste rectangular region into table, or past subtree relative to level.
  25262. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25263. See the individual commands for more information."
  25264. (interactive "P")
  25265. (if (org-at-table-p)
  25266. (org-table-paste-rectangle)
  25267. (org-paste-subtree arg)))
  25268. (defun org-ctrl-c-ctrl-c (&optional arg)
  25269. "Set tags in headline, or update according to changed information at point.
  25270. This command does many different things, depending on context:
  25271. - If the cursor is in a headline, prompt for tags and insert them
  25272. into the current line, aligned to `org-tags-column'. When called
  25273. with prefix arg, realign all tags in the current buffer.
  25274. - If the cursor is in one of the special #+KEYWORD lines, this
  25275. triggers scanning the buffer for these lines and updating the
  25276. information.
  25277. - If the cursor is inside a table, realign the table. This command
  25278. works even if the automatic table editor has been turned off.
  25279. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25280. the entire table.
  25281. - If the cursor is a the beginning of a dynamic block, update it.
  25282. - If the cursor is inside a table created by the table.el package,
  25283. activate that table.
  25284. - If the current buffer is a remember buffer, close note and file it.
  25285. with a prefix argument, file it without further interaction to the default
  25286. location.
  25287. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25288. links in this buffer.
  25289. - If the cursor is on a numbered item in a plain list, renumber the
  25290. ordered list.
  25291. - If the cursor is on a checkbox, toggle it."
  25292. (interactive "P")
  25293. (let ((org-enable-table-editor t))
  25294. (cond
  25295. ((or org-clock-overlays
  25296. org-occur-highlights
  25297. org-latex-fragment-image-overlays)
  25298. (org-remove-clock-overlays)
  25299. (org-remove-occur-highlights)
  25300. (org-remove-latex-fragment-image-overlays)
  25301. (message "Temporary highlights/overlays removed from current buffer"))
  25302. ((and (local-variable-p 'org-finish-function (current-buffer))
  25303. (fboundp org-finish-function))
  25304. (funcall org-finish-function))
  25305. ((org-at-property-p)
  25306. (call-interactively 'org-property-action))
  25307. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25308. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25309. ((org-at-table.el-p)
  25310. (require 'table)
  25311. (beginning-of-line 1)
  25312. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25313. (call-interactively 'table-recognize-table))
  25314. ((org-at-table-p)
  25315. (org-table-maybe-eval-formula)
  25316. (if arg
  25317. (call-interactively 'org-table-recalculate)
  25318. (org-table-maybe-recalculate-line))
  25319. (call-interactively 'org-table-align))
  25320. ((org-at-item-checkbox-p)
  25321. (call-interactively 'org-toggle-checkbox))
  25322. ((org-at-item-p)
  25323. (call-interactively 'org-maybe-renumber-ordered-list))
  25324. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25325. ;; Dynamic block
  25326. (beginning-of-line 1)
  25327. (org-update-dblock))
  25328. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25329. (cond
  25330. ((equal (match-string 1) "TBLFM")
  25331. ;; Recalculate the table before this line
  25332. (save-excursion
  25333. (beginning-of-line 1)
  25334. (skip-chars-backward " \r\n\t")
  25335. (if (org-at-table-p)
  25336. (org-call-with-arg 'org-table-recalculate t))))
  25337. (t
  25338. (call-interactively 'org-mode-restart))))
  25339. (t (error "C-c C-c can do nothing useful at this location.")))))
  25340. (defun org-mode-restart ()
  25341. "Restart Org-mode, to scan again for special lines.
  25342. Also updates the keyword regular expressions."
  25343. (interactive)
  25344. (let ((org-inhibit-startup t)) (org-mode))
  25345. (message "Org-mode restarted to refresh keyword and special line setup"))
  25346. (defun org-kill-note-or-show-branches ()
  25347. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25348. (interactive)
  25349. (if (not org-finish-function)
  25350. (call-interactively 'show-branches)
  25351. (let ((org-note-abort t))
  25352. (funcall org-finish-function))))
  25353. (defun org-return (&optional indent)
  25354. "Goto next table row or insert a newline.
  25355. Calls `org-table-next-row' or `newline', depending on context.
  25356. See the individual commands for more information."
  25357. (interactive)
  25358. (cond
  25359. ((bobp) (if indent (newline-and-indent) (newline)))
  25360. ((and (org-at-heading-p)
  25361. (looking-at
  25362. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25363. (org-show-entry)
  25364. (end-of-line 1)
  25365. (newline))
  25366. ((org-at-table-p)
  25367. (org-table-justify-field-maybe)
  25368. (call-interactively 'org-table-next-row))
  25369. (t (if indent (newline-and-indent) (newline)))))
  25370. (defun org-return-indent ()
  25371. "Goto next table row or insert a newline and indent.
  25372. Calls `org-table-next-row' or `newline-and-indent', depending on
  25373. context. See the individual commands for more information."
  25374. (interactive)
  25375. (org-return t))
  25376. (defun org-ctrl-c-star ()
  25377. "Compute table, or change heading status of lines.
  25378. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25379. depending on context. This will also turn a plain list item or a normal
  25380. line into a subheading."
  25381. (interactive)
  25382. (cond
  25383. ((org-at-table-p)
  25384. (call-interactively 'org-table-recalculate))
  25385. ((org-region-active-p)
  25386. ;; Convert all lines in region to list items
  25387. (call-interactively 'org-toggle-region-headings))
  25388. ((org-on-heading-p)
  25389. (org-toggle-region-headings (point-at-bol)
  25390. (min (1+ (point-at-eol)) (point-max))))
  25391. ((org-at-item-p)
  25392. ;; Convert to heading
  25393. ;; FIXME: not yet implemented
  25394. )
  25395. (t (org-toggle-region-headings (point-at-bol)
  25396. (min (1+ (point-at-eol)) (point-max))))))
  25397. (defun org-ctrl-c-minus ()
  25398. "Insert separator line in table or modify bullet status of line.
  25399. Also turns a plain line or a region of lines into list items.
  25400. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25401. `org-cycle-list-bullet', depending on context."
  25402. (interactive)
  25403. (cond
  25404. ((org-at-table-p)
  25405. (call-interactively 'org-table-insert-hline))
  25406. ((org-on-heading-p)
  25407. ;; Convert to item
  25408. (save-excursion
  25409. (beginning-of-line 1)
  25410. (if (looking-at "\\*+ ")
  25411. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25412. ((org-region-active-p)
  25413. ;; Convert all lines in region to list items
  25414. (call-interactively 'org-toggle-region-items))
  25415. ((org-in-item-p)
  25416. (call-interactively 'org-cycle-list-bullet))
  25417. (t (org-toggle-region-items (point-at-bol)
  25418. (min (1+ (point-at-eol)) (point-max))))))
  25419. (defun org-toggle-region-items (beg end)
  25420. "Convert all lines in region to list items.
  25421. If the first line is already an item, convert all list items in the region
  25422. to normal lines."
  25423. (interactive "r")
  25424. (let (l2 l)
  25425. (save-excursion
  25426. (goto-char end)
  25427. (setq l2 (org-current-line))
  25428. (goto-char beg)
  25429. (beginning-of-line 1)
  25430. (setq l (1- (org-current-line)))
  25431. (if (org-at-item-p)
  25432. ;; We already have items, de-itemize
  25433. (while (< (setq l (1+ l)) l2)
  25434. (when (org-at-item-p)
  25435. (goto-char (match-beginning 2))
  25436. (delete-region (match-beginning 2) (match-end 2))
  25437. (and (looking-at "[ \t]+") (replace-match "")))
  25438. (beginning-of-line 2))
  25439. (while (< (setq l (1+ l)) l2)
  25440. (unless (org-at-item-p)
  25441. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25442. (replace-match "\\1- \\2")))
  25443. (beginning-of-line 2))))))
  25444. (defun org-toggle-region-headings (beg end)
  25445. "Convert all lines in region to list items.
  25446. If the first line is already an item, convert all list items in the region
  25447. to normal lines."
  25448. (interactive "r")
  25449. (let (l2 l)
  25450. (save-excursion
  25451. (goto-char end)
  25452. (setq l2 (org-current-line))
  25453. (goto-char beg)
  25454. (beginning-of-line 1)
  25455. (setq l (1- (org-current-line)))
  25456. (if (org-on-heading-p)
  25457. ;; We already have headlines, de-star them
  25458. (while (< (setq l (1+ l)) l2)
  25459. (when (org-on-heading-p t)
  25460. (and (looking-at outline-regexp) (replace-match "")))
  25461. (beginning-of-line 2))
  25462. (let* ((stars (save-excursion
  25463. (re-search-backward org-complex-heading-regexp nil t)
  25464. (or (match-string 1) "*")))
  25465. (add-stars (if org-odd-levels-only "**" "*"))
  25466. (rpl (concat stars add-stars " \\2")))
  25467. (while (< (setq l (1+ l)) l2)
  25468. (unless (org-on-heading-p)
  25469. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25470. (replace-match rpl)))
  25471. (beginning-of-line 2)))))))
  25472. (defun org-meta-return (&optional arg)
  25473. "Insert a new heading or wrap a region in a table.
  25474. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25475. See the individual commands for more information."
  25476. (interactive "P")
  25477. (cond
  25478. ((org-at-table-p)
  25479. (call-interactively 'org-table-wrap-region))
  25480. (t (call-interactively 'org-insert-heading))))
  25481. ;;; Menu entries
  25482. ;; Define the Org-mode menus
  25483. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25484. '("Tbl"
  25485. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25486. ["Next Field" org-cycle (org-at-table-p)]
  25487. ["Previous Field" org-shifttab (org-at-table-p)]
  25488. ["Next Row" org-return (org-at-table-p)]
  25489. "--"
  25490. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25491. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25492. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25493. "--"
  25494. ("Column"
  25495. ["Move Column Left" org-metaleft (org-at-table-p)]
  25496. ["Move Column Right" org-metaright (org-at-table-p)]
  25497. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25498. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25499. ("Row"
  25500. ["Move Row Up" org-metaup (org-at-table-p)]
  25501. ["Move Row Down" org-metadown (org-at-table-p)]
  25502. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25503. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25504. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25505. "--"
  25506. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25507. ("Rectangle"
  25508. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25509. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25510. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25511. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25512. "--"
  25513. ("Calculate"
  25514. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25515. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25516. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25517. "--"
  25518. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25519. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25520. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25521. "--"
  25522. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25523. "--"
  25524. ["Sum Column/Rectangle" org-table-sum
  25525. (or (org-at-table-p) (org-region-active-p))]
  25526. ["Which Column?" org-table-current-column (org-at-table-p)])
  25527. ["Debug Formulas"
  25528. org-table-toggle-formula-debugger
  25529. :style toggle :selected org-table-formula-debug]
  25530. ["Show Col/Row Numbers"
  25531. org-table-toggle-coordinate-overlays
  25532. :style toggle :selected org-table-overlay-coordinates]
  25533. "--"
  25534. ["Create" org-table-create (and (not (org-at-table-p))
  25535. org-enable-table-editor)]
  25536. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25537. ["Import from File" org-table-import (not (org-at-table-p))]
  25538. ["Export to File" org-table-export (org-at-table-p)]
  25539. "--"
  25540. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25541. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25542. '("Org"
  25543. ("Show/Hide"
  25544. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25545. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25546. ["Sparse Tree" org-occur t]
  25547. ["Reveal Context" org-reveal t]
  25548. ["Show All" show-all t]
  25549. "--"
  25550. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25551. "--"
  25552. ["New Heading" org-insert-heading t]
  25553. ("Navigate Headings"
  25554. ["Up" outline-up-heading t]
  25555. ["Next" outline-next-visible-heading t]
  25556. ["Previous" outline-previous-visible-heading t]
  25557. ["Next Same Level" outline-forward-same-level t]
  25558. ["Previous Same Level" outline-backward-same-level t]
  25559. "--"
  25560. ["Jump" org-goto t])
  25561. ("Edit Structure"
  25562. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25563. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25564. "--"
  25565. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25566. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25567. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25568. "--"
  25569. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25570. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25571. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25572. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25573. "--"
  25574. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25575. "--"
  25576. ["Convert to odd levels" org-convert-to-odd-levels t]
  25577. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25578. ("Editing"
  25579. ["Emphasis..." org-emphasize t])
  25580. ("Archive"
  25581. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25582. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25583. ; :active t :keys "C-u C-c C-x C-a"]
  25584. ["Sparse trees open ARCHIVE trees"
  25585. (setq org-sparse-tree-open-archived-trees
  25586. (not org-sparse-tree-open-archived-trees))
  25587. :style toggle :selected org-sparse-tree-open-archived-trees]
  25588. ["Cycling opens ARCHIVE trees"
  25589. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25590. :style toggle :selected org-cycle-open-archived-trees]
  25591. ["Agenda includes ARCHIVE trees"
  25592. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25593. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25594. "--"
  25595. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25596. ; ["Check and Move Children" (org-archive-subtree '(4))
  25597. ; :active t :keys "C-u C-c C-x C-s"]
  25598. )
  25599. "--"
  25600. ("TODO Lists"
  25601. ["TODO/DONE/-" org-todo t]
  25602. ("Select keyword"
  25603. ["Next keyword" org-shiftright (org-on-heading-p)]
  25604. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25605. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25606. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25607. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25608. ["Show TODO Tree" org-show-todo-tree t]
  25609. ["Global TODO list" org-todo-list t]
  25610. "--"
  25611. ["Set Priority" org-priority t]
  25612. ["Priority Up" org-shiftup t]
  25613. ["Priority Down" org-shiftdown t])
  25614. ("TAGS and Properties"
  25615. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25616. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25617. "--"
  25618. ["Set property" 'org-set-property t]
  25619. ["Column view of properties" org-columns t]
  25620. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25621. ("Dates and Scheduling"
  25622. ["Timestamp" org-time-stamp t]
  25623. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25624. ("Change Date"
  25625. ["1 Day Later" org-shiftright t]
  25626. ["1 Day Earlier" org-shiftleft t]
  25627. ["1 ... Later" org-shiftup t]
  25628. ["1 ... Earlier" org-shiftdown t])
  25629. ["Compute Time Range" org-evaluate-time-range t]
  25630. ["Schedule Item" org-schedule t]
  25631. ["Deadline" org-deadline t]
  25632. "--"
  25633. ["Custom time format" org-toggle-time-stamp-overlays
  25634. :style radio :selected org-display-custom-times]
  25635. "--"
  25636. ["Goto Calendar" org-goto-calendar t]
  25637. ["Date from Calendar" org-date-from-calendar t])
  25638. ("Logging work"
  25639. ["Clock in" org-clock-in t]
  25640. ["Clock out" org-clock-out t]
  25641. ["Clock cancel" org-clock-cancel t]
  25642. ["Goto running clock" org-clock-goto t]
  25643. ["Display times" org-clock-display t]
  25644. ["Create clock table" org-clock-report t]
  25645. "--"
  25646. ["Record DONE time"
  25647. (progn (setq org-log-done (not org-log-done))
  25648. (message "Switching to %s will %s record a timestamp"
  25649. (car org-done-keywords)
  25650. (if org-log-done "automatically" "not")))
  25651. :style toggle :selected org-log-done])
  25652. "--"
  25653. ["Agenda Command..." org-agenda t]
  25654. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25655. ("File List for Agenda")
  25656. ("Special views current file"
  25657. ["TODO Tree" org-show-todo-tree t]
  25658. ["Check Deadlines" org-check-deadlines t]
  25659. ["Timeline" org-timeline t]
  25660. ["Tags Tree" org-tags-sparse-tree t])
  25661. "--"
  25662. ("Hyperlinks"
  25663. ["Store Link (Global)" org-store-link t]
  25664. ["Insert Link" org-insert-link t]
  25665. ["Follow Link" org-open-at-point t]
  25666. "--"
  25667. ["Next link" org-next-link t]
  25668. ["Previous link" org-previous-link t]
  25669. "--"
  25670. ["Descriptive Links"
  25671. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25672. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25673. ["Literal Links"
  25674. (progn
  25675. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25676. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25677. "--"
  25678. ["Export/Publish..." org-export t]
  25679. ("LaTeX"
  25680. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25681. :selected org-cdlatex-mode]
  25682. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25683. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25684. ["Modify math symbol" org-cdlatex-math-modify
  25685. (org-inside-LaTeX-fragment-p)]
  25686. ["Export LaTeX fragments as images"
  25687. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25688. :style toggle :selected org-export-with-LaTeX-fragments])
  25689. "--"
  25690. ("Documentation"
  25691. ["Show Version" org-version t]
  25692. ["Info Documentation" org-info t])
  25693. ("Customize"
  25694. ["Browse Org Group" org-customize t]
  25695. "--"
  25696. ["Expand This Menu" org-create-customize-menu
  25697. (fboundp 'customize-menu-create)])
  25698. "--"
  25699. ["Refresh setup" org-mode-restart t]
  25700. ))
  25701. (defun org-info (&optional node)
  25702. "Read documentation for Org-mode in the info system.
  25703. With optional NODE, go directly to that node."
  25704. (interactive)
  25705. (require 'info)
  25706. (info (format "(org)%s" (or node ""))))
  25707. (defun org-install-agenda-files-menu ()
  25708. (let ((bl (buffer-list)))
  25709. (save-excursion
  25710. (while bl
  25711. (set-buffer (pop bl))
  25712. (if (org-mode-p) (setq bl nil)))
  25713. (when (org-mode-p)
  25714. (easy-menu-change
  25715. '("Org") "File List for Agenda"
  25716. (append
  25717. (list
  25718. ["Edit File List" (org-edit-agenda-file-list) t]
  25719. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25720. ["Remove Current File from List" org-remove-file t]
  25721. ["Cycle through agenda files" org-cycle-agenda-files t]
  25722. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25723. "--")
  25724. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25725. ;;;; Documentation
  25726. (defun org-customize ()
  25727. "Call the customize function with org as argument."
  25728. (interactive)
  25729. (customize-browse 'org))
  25730. (defun org-create-customize-menu ()
  25731. "Create a full customization menu for Org-mode, insert it into the menu."
  25732. (interactive)
  25733. (if (fboundp 'customize-menu-create)
  25734. (progn
  25735. (easy-menu-change
  25736. '("Org") "Customize"
  25737. `(["Browse Org group" org-customize t]
  25738. "--"
  25739. ,(customize-menu-create 'org)
  25740. ["Set" Custom-set t]
  25741. ["Save" Custom-save t]
  25742. ["Reset to Current" Custom-reset-current t]
  25743. ["Reset to Saved" Custom-reset-saved t]
  25744. ["Reset to Standard Settings" Custom-reset-standard t]))
  25745. (message "\"Org\"-menu now contains full customization menu"))
  25746. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25747. ;;;; Miscellaneous stuff
  25748. ;;; Generally useful functions
  25749. (defun org-context ()
  25750. "Return a list of contexts of the current cursor position.
  25751. If several contexts apply, all are returned.
  25752. Each context entry is a list with a symbol naming the context, and
  25753. two positions indicating start and end of the context. Possible
  25754. contexts are:
  25755. :headline anywhere in a headline
  25756. :headline-stars on the leading stars in a headline
  25757. :todo-keyword on a TODO keyword (including DONE) in a headline
  25758. :tags on the TAGS in a headline
  25759. :priority on the priority cookie in a headline
  25760. :item on the first line of a plain list item
  25761. :item-bullet on the bullet/number of a plain list item
  25762. :checkbox on the checkbox in a plain list item
  25763. :table in an org-mode table
  25764. :table-special on a special filed in a table
  25765. :table-table in a table.el table
  25766. :link on a hyperlink
  25767. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25768. :target on a <<target>>
  25769. :radio-target on a <<<radio-target>>>
  25770. :latex-fragment on a LaTeX fragment
  25771. :latex-preview on a LaTeX fragment with overlayed preview image
  25772. This function expects the position to be visible because it uses font-lock
  25773. faces as a help to recognize the following contexts: :table-special, :link,
  25774. and :keyword."
  25775. (let* ((f (get-text-property (point) 'face))
  25776. (faces (if (listp f) f (list f)))
  25777. (p (point)) clist o)
  25778. ;; First the large context
  25779. (cond
  25780. ((org-on-heading-p t)
  25781. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25782. (when (progn
  25783. (beginning-of-line 1)
  25784. (looking-at org-todo-line-tags-regexp))
  25785. (push (org-point-in-group p 1 :headline-stars) clist)
  25786. (push (org-point-in-group p 2 :todo-keyword) clist)
  25787. (push (org-point-in-group p 4 :tags) clist))
  25788. (goto-char p)
  25789. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25790. (if (looking-at "\\[#[A-Z0-9]\\]")
  25791. (push (org-point-in-group p 0 :priority) clist)))
  25792. ((org-at-item-p)
  25793. (push (org-point-in-group p 2 :item-bullet) clist)
  25794. (push (list :item (point-at-bol)
  25795. (save-excursion (org-end-of-item) (point)))
  25796. clist)
  25797. (and (org-at-item-checkbox-p)
  25798. (push (org-point-in-group p 0 :checkbox) clist)))
  25799. ((org-at-table-p)
  25800. (push (list :table (org-table-begin) (org-table-end)) clist)
  25801. (if (memq 'org-formula faces)
  25802. (push (list :table-special
  25803. (previous-single-property-change p 'face)
  25804. (next-single-property-change p 'face)) clist)))
  25805. ((org-at-table-p 'any)
  25806. (push (list :table-table) clist)))
  25807. (goto-char p)
  25808. ;; Now the small context
  25809. (cond
  25810. ((org-at-timestamp-p)
  25811. (push (org-point-in-group p 0 :timestamp) clist))
  25812. ((memq 'org-link faces)
  25813. (push (list :link
  25814. (previous-single-property-change p 'face)
  25815. (next-single-property-change p 'face)) clist))
  25816. ((memq 'org-special-keyword faces)
  25817. (push (list :keyword
  25818. (previous-single-property-change p 'face)
  25819. (next-single-property-change p 'face)) clist))
  25820. ((org-on-target-p)
  25821. (push (org-point-in-group p 0 :target) clist)
  25822. (goto-char (1- (match-beginning 0)))
  25823. (if (looking-at org-radio-target-regexp)
  25824. (push (org-point-in-group p 0 :radio-target) clist))
  25825. (goto-char p))
  25826. ((setq o (car (delq nil
  25827. (mapcar
  25828. (lambda (x)
  25829. (if (memq x org-latex-fragment-image-overlays) x))
  25830. (org-overlays-at (point))))))
  25831. (push (list :latex-fragment
  25832. (org-overlay-start o) (org-overlay-end o)) clist)
  25833. (push (list :latex-preview
  25834. (org-overlay-start o) (org-overlay-end o)) clist))
  25835. ((org-inside-LaTeX-fragment-p)
  25836. ;; FIXME: positions wrong.
  25837. (push (list :latex-fragment (point) (point)) clist)))
  25838. (setq clist (nreverse (delq nil clist)))
  25839. clist))
  25840. ;; FIXME: Compare with at-regexp-p Do we need both?
  25841. (defun org-in-regexp (re &optional nlines visually)
  25842. "Check if point is inside a match of regexp.
  25843. Normally only the current line is checked, but you can include NLINES extra
  25844. lines both before and after point into the search.
  25845. If VISUALLY is set, require that the cursor is not after the match but
  25846. really on, so that the block visually is on the match."
  25847. (catch 'exit
  25848. (let ((pos (point))
  25849. (eol (point-at-eol (+ 1 (or nlines 0))))
  25850. (inc (if visually 1 0)))
  25851. (save-excursion
  25852. (beginning-of-line (- 1 (or nlines 0)))
  25853. (while (re-search-forward re eol t)
  25854. (if (and (<= (match-beginning 0) pos)
  25855. (>= (+ inc (match-end 0)) pos))
  25856. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25857. (defun org-at-regexp-p (regexp)
  25858. "Is point inside a match of REGEXP in the current line?"
  25859. (catch 'exit
  25860. (save-excursion
  25861. (let ((pos (point)) (end (point-at-eol)))
  25862. (beginning-of-line 1)
  25863. (while (re-search-forward regexp end t)
  25864. (if (and (<= (match-beginning 0) pos)
  25865. (>= (match-end 0) pos))
  25866. (throw 'exit t)))
  25867. nil))))
  25868. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25869. "Call `multi-occur' with buffers for all agenda files."
  25870. (interactive "sOrg-files matching: \np")
  25871. (let* ((files (org-agenda-files))
  25872. (tnames (mapcar 'file-truename files))
  25873. (extra org-agenda-text-search-extra-files)
  25874. f)
  25875. (while (setq f (pop extra))
  25876. (unless (member (file-truename f) tnames)
  25877. (add-to-list 'files f 'append)
  25878. (add-to-list 'tnames (file-truename f) 'append)))
  25879. (multi-occur
  25880. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25881. regexp)))
  25882. (if (boundp 'occur-mode-find-occurrence-hook)
  25883. ;; Emacs 23
  25884. (add-hook 'occur-mode-find-occurrence-hook
  25885. (lambda ()
  25886. (when (org-mode-p)
  25887. (org-reveal))))
  25888. ;; Emacs 22
  25889. (defadvice occur-mode-goto-occurrence
  25890. (after org-occur-reveal activate)
  25891. (and (org-mode-p) (org-reveal)))
  25892. (defadvice occur-mode-goto-occurrence-other-window
  25893. (after org-occur-reveal activate)
  25894. (and (org-mode-p) (org-reveal)))
  25895. (defadvice occur-mode-display-occurrence
  25896. (after org-occur-reveal activate)
  25897. (when (org-mode-p)
  25898. (let ((pos (occur-mode-find-occurrence)))
  25899. (with-current-buffer (marker-buffer pos)
  25900. (save-excursion
  25901. (goto-char pos)
  25902. (org-reveal)))))))
  25903. (defun org-uniquify (list)
  25904. "Remove duplicate elements from LIST."
  25905. (let (res)
  25906. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25907. res))
  25908. (defun org-delete-all (elts list)
  25909. "Remove all elements in ELTS from LIST."
  25910. (while elts
  25911. (setq list (delete (pop elts) list)))
  25912. list)
  25913. (defun org-back-over-empty-lines ()
  25914. "Move backwards over witespace, to the beginning of the first empty line.
  25915. Returns the number o empty lines passed."
  25916. (let ((pos (point)))
  25917. (skip-chars-backward " \t\n\r")
  25918. (beginning-of-line 2)
  25919. (goto-char (min (point) pos))
  25920. (count-lines (point) pos)))
  25921. (defun org-skip-whitespace ()
  25922. (skip-chars-forward " \t\n\r"))
  25923. (defun org-point-in-group (point group &optional context)
  25924. "Check if POINT is in match-group GROUP.
  25925. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25926. match. If the match group does ot exist or point is not inside it,
  25927. return nil."
  25928. (and (match-beginning group)
  25929. (>= point (match-beginning group))
  25930. (<= point (match-end group))
  25931. (if context
  25932. (list context (match-beginning group) (match-end group))
  25933. t)))
  25934. (defun org-switch-to-buffer-other-window (&rest args)
  25935. "Switch to buffer in a second window on the current frame.
  25936. In particular, do not allow pop-up frames."
  25937. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25938. special-display-function)
  25939. (apply 'switch-to-buffer-other-window args)))
  25940. (defun org-combine-plists (&rest plists)
  25941. "Create a single property list from all plists in PLISTS.
  25942. The process starts by copying the first list, and then setting properties
  25943. from the other lists. Settings in the last list are the most significant
  25944. ones and overrule settings in the other lists."
  25945. (let ((rtn (copy-sequence (pop plists)))
  25946. p v ls)
  25947. (while plists
  25948. (setq ls (pop plists))
  25949. (while ls
  25950. (setq p (pop ls) v (pop ls))
  25951. (setq rtn (plist-put rtn p v))))
  25952. rtn))
  25953. (defun org-move-line-down (arg)
  25954. "Move the current line down. With prefix argument, move it past ARG lines."
  25955. (interactive "p")
  25956. (let ((col (current-column))
  25957. beg end pos)
  25958. (beginning-of-line 1) (setq beg (point))
  25959. (beginning-of-line 2) (setq end (point))
  25960. (beginning-of-line (+ 1 arg))
  25961. (setq pos (move-marker (make-marker) (point)))
  25962. (insert (delete-and-extract-region beg end))
  25963. (goto-char pos)
  25964. (move-to-column col)))
  25965. (defun org-move-line-up (arg)
  25966. "Move the current line up. With prefix argument, move it past ARG lines."
  25967. (interactive "p")
  25968. (let ((col (current-column))
  25969. beg end pos)
  25970. (beginning-of-line 1) (setq beg (point))
  25971. (beginning-of-line 2) (setq end (point))
  25972. (beginning-of-line (- arg))
  25973. (setq pos (move-marker (make-marker) (point)))
  25974. (insert (delete-and-extract-region beg end))
  25975. (goto-char pos)
  25976. (move-to-column col)))
  25977. (defun org-replace-escapes (string table)
  25978. "Replace %-escapes in STRING with values in TABLE.
  25979. TABLE is an association list with keys like \"%a\" and string values.
  25980. The sequences in STRING may contain normal field width and padding information,
  25981. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25982. so values can contain further %-escapes if they are define later in TABLE."
  25983. (let ((case-fold-search nil)
  25984. e re rpl)
  25985. (while (setq e (pop table))
  25986. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25987. (while (string-match re string)
  25988. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25989. (cdr e)))
  25990. (setq string (replace-match rpl t t string))))
  25991. string))
  25992. (defun org-sublist (list start end)
  25993. "Return a section of LIST, from START to END.
  25994. Counting starts at 1."
  25995. (let (rtn (c start))
  25996. (setq list (nthcdr (1- start) list))
  25997. (while (and list (<= c end))
  25998. (push (pop list) rtn)
  25999. (setq c (1+ c)))
  26000. (nreverse rtn)))
  26001. (defun org-find-base-buffer-visiting (file)
  26002. "Like `find-buffer-visiting' but alway return the base buffer and
  26003. not an indirect buffer"
  26004. (let ((buf (find-buffer-visiting file)))
  26005. (if buf
  26006. (or (buffer-base-buffer buf) buf)
  26007. nil)))
  26008. (defun org-image-file-name-regexp ()
  26009. "Return regexp matching the file names of images."
  26010. (if (fboundp 'image-file-name-regexp)
  26011. (image-file-name-regexp)
  26012. (let ((image-file-name-extensions
  26013. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  26014. "xbm" "xpm" "pbm" "pgm" "ppm")))
  26015. (concat "\\."
  26016. (regexp-opt (nconc (mapcar 'upcase
  26017. image-file-name-extensions)
  26018. image-file-name-extensions)
  26019. t)
  26020. "\\'"))))
  26021. (defun org-file-image-p (file)
  26022. "Return non-nil if FILE is an image."
  26023. (save-match-data
  26024. (string-match (org-image-file-name-regexp) file)))
  26025. ;;; Paragraph filling stuff.
  26026. ;; We want this to be just right, so use the full arsenal.
  26027. (defun org-indent-line-function ()
  26028. "Indent line like previous, but further if previous was headline or item."
  26029. (interactive)
  26030. (let* ((pos (point))
  26031. (itemp (org-at-item-p))
  26032. column bpos bcol tpos tcol bullet btype bullet-type)
  26033. ;; Find the previous relevant line
  26034. (beginning-of-line 1)
  26035. (cond
  26036. ((looking-at "#") (setq column 0))
  26037. ((looking-at "\\*+ ") (setq column 0))
  26038. (t
  26039. (beginning-of-line 0)
  26040. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  26041. (beginning-of-line 0))
  26042. (cond
  26043. ((looking-at "\\*+[ \t]+")
  26044. (goto-char (match-end 0))
  26045. (setq column (current-column)))
  26046. ((org-in-item-p)
  26047. (org-beginning-of-item)
  26048. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26049. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26050. (setq bpos (match-beginning 1) tpos (match-end 0)
  26051. bcol (progn (goto-char bpos) (current-column))
  26052. tcol (progn (goto-char tpos) (current-column))
  26053. bullet (match-string 1)
  26054. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26055. (if (not itemp)
  26056. (setq column tcol)
  26057. (goto-char pos)
  26058. (beginning-of-line 1)
  26059. (if (looking-at "\\S-")
  26060. (progn
  26061. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26062. (setq bullet (match-string 1)
  26063. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26064. (setq column (if (equal btype bullet-type) bcol tcol)))
  26065. (setq column (org-get-indentation)))))
  26066. (t (setq column (org-get-indentation))))))
  26067. (goto-char pos)
  26068. (if (<= (current-column) (current-indentation))
  26069. (indent-line-to column)
  26070. (save-excursion (indent-line-to column)))
  26071. (setq column (current-column))
  26072. (beginning-of-line 1)
  26073. (if (looking-at
  26074. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26075. (replace-match (concat "\\1" (format org-property-format
  26076. (match-string 2) (match-string 3)))
  26077. t nil))
  26078. (move-to-column column)))
  26079. (defun org-set-autofill-regexps ()
  26080. (interactive)
  26081. ;; In the paragraph separator we include headlines, because filling
  26082. ;; text in a line directly attached to a headline would otherwise
  26083. ;; fill the headline as well.
  26084. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26085. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26086. ;; The paragraph starter includes hand-formatted lists.
  26087. (org-set-local 'paragraph-start
  26088. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26089. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26090. ;; But only if the user has not turned off tables or fixed-width regions
  26091. (org-set-local
  26092. 'auto-fill-inhibit-regexp
  26093. (concat "\\*+ \\|#\\+"
  26094. "\\|[ \t]*" org-keyword-time-regexp
  26095. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26096. (concat
  26097. "\\|[ \t]*["
  26098. (if org-enable-table-editor "|" "")
  26099. (if org-enable-fixed-width-editor ":" "")
  26100. "]"))))
  26101. ;; We use our own fill-paragraph function, to make sure that tables
  26102. ;; and fixed-width regions are not wrapped. That function will pass
  26103. ;; through to `fill-paragraph' when appropriate.
  26104. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26105. ; Adaptive filling: To get full control, first make sure that
  26106. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26107. (org-set-local 'adaptive-fill-regexp "\000")
  26108. (org-set-local 'adaptive-fill-function
  26109. 'org-adaptive-fill-function)
  26110. (org-set-local
  26111. 'align-mode-rules-list
  26112. '((org-in-buffer-settings
  26113. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26114. (modes . '(org-mode))))))
  26115. (defun org-fill-paragraph (&optional justify)
  26116. "Re-align a table, pass through to fill-paragraph if no table."
  26117. (let ((table-p (org-at-table-p))
  26118. (table.el-p (org-at-table.el-p)))
  26119. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26120. (save-excursion (goto-char (point-at-bol))
  26121. (looking-at outline-regexp)))
  26122. t) ; skip headlines
  26123. (table.el-p t) ; skip table.el tables
  26124. (table-p (org-table-align) t) ; align org-mode tables
  26125. (t nil)))) ; call paragraph-fill
  26126. ;; For reference, this is the default value of adaptive-fill-regexp
  26127. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26128. (defun org-adaptive-fill-function ()
  26129. "Return a fill prefix for org-mode files.
  26130. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26131. work correctly."
  26132. (cond ((looking-at "#[ \t]+")
  26133. (match-string 0))
  26134. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26135. (save-excursion
  26136. (goto-char (match-end 0))
  26137. (make-string (current-column) ?\ )))
  26138. (t nil)))
  26139. ;;;; Functions extending outline functionality
  26140. (defun org-beginning-of-line (&optional arg)
  26141. "Go to the beginning of the current line. If that is invisible, continue
  26142. to a visible line beginning. This makes the function of C-a more intuitive.
  26143. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26144. first attempt, and only move to after the tags when the cursor is already
  26145. beyond the end of the headline."
  26146. (interactive "P")
  26147. (let ((pos (point)))
  26148. (beginning-of-line 1)
  26149. (if (bobp)
  26150. nil
  26151. (backward-char 1)
  26152. (if (org-invisible-p)
  26153. (while (and (not (bobp)) (org-invisible-p))
  26154. (backward-char 1)
  26155. (beginning-of-line 1))
  26156. (forward-char 1)))
  26157. (when org-special-ctrl-a/e
  26158. (cond
  26159. ((and (looking-at org-todo-line-regexp)
  26160. (= (char-after (match-end 1)) ?\ ))
  26161. (goto-char
  26162. (if (eq org-special-ctrl-a/e t)
  26163. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26164. ((= pos (point)) (match-beginning 3))
  26165. (t (point)))
  26166. (cond ((> pos (point)) (point))
  26167. ((not (eq last-command this-command)) (point))
  26168. (t (match-beginning 3))))))
  26169. ((org-at-item-p)
  26170. (goto-char
  26171. (if (eq org-special-ctrl-a/e t)
  26172. (cond ((> pos (match-end 4)) (match-end 4))
  26173. ((= pos (point)) (match-end 4))
  26174. (t (point)))
  26175. (cond ((> pos (point)) (point))
  26176. ((not (eq last-command this-command)) (point))
  26177. (t (match-end 4))))))))))
  26178. (defun org-end-of-line (&optional arg)
  26179. "Go to the end of the line.
  26180. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26181. first attempt, and only move to after the tags when the cursor is already
  26182. beyond the end of the headline."
  26183. (interactive "P")
  26184. (if (or (not org-special-ctrl-a/e)
  26185. (not (org-on-heading-p)))
  26186. (end-of-line arg)
  26187. (let ((pos (point)))
  26188. (beginning-of-line 1)
  26189. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26190. (if (eq org-special-ctrl-a/e t)
  26191. (if (or (< pos (match-beginning 1))
  26192. (= pos (match-end 0)))
  26193. (goto-char (match-beginning 1))
  26194. (goto-char (match-end 0)))
  26195. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26196. (goto-char (match-end 0))
  26197. (goto-char (match-beginning 1))))
  26198. (end-of-line arg)))))
  26199. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26200. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26201. (defun org-kill-line (&optional arg)
  26202. "Kill line, to tags or end of line."
  26203. (interactive "P")
  26204. (cond
  26205. ((or (not org-special-ctrl-k)
  26206. (bolp)
  26207. (not (org-on-heading-p)))
  26208. (call-interactively 'kill-line))
  26209. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26210. (kill-region (point) (match-beginning 1))
  26211. (org-set-tags nil t))
  26212. (t (kill-region (point) (point-at-eol)))))
  26213. (define-key org-mode-map "\C-k" 'org-kill-line)
  26214. (defun org-invisible-p ()
  26215. "Check if point is at a character currently not visible."
  26216. ;; Early versions of noutline don't have `outline-invisible-p'.
  26217. (if (fboundp 'outline-invisible-p)
  26218. (outline-invisible-p)
  26219. (get-char-property (point) 'invisible)))
  26220. (defun org-invisible-p2 ()
  26221. "Check if point is at a character currently not visible."
  26222. (save-excursion
  26223. (if (and (eolp) (not (bobp))) (backward-char 1))
  26224. ;; Early versions of noutline don't have `outline-invisible-p'.
  26225. (if (fboundp 'outline-invisible-p)
  26226. (outline-invisible-p)
  26227. (get-char-property (point) 'invisible))))
  26228. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26229. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26230. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26231. (defun org-at-heading-or-item-p ()
  26232. (or (org-on-heading-p) (org-at-item-p)))
  26233. (defun org-on-target-p ()
  26234. (or (org-in-regexp org-radio-target-regexp)
  26235. (org-in-regexp org-target-regexp)))
  26236. (defun org-up-heading-all (arg)
  26237. "Move to the heading line of which the present line is a subheading.
  26238. This function considers both visible and invisible heading lines.
  26239. With argument, move up ARG levels."
  26240. (if (fboundp 'outline-up-heading-all)
  26241. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26242. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26243. (defun org-up-heading-safe ()
  26244. "Move to the heading line of which the present line is a subheading.
  26245. This version will not throw an error. It will return the level of the
  26246. headline found, or nil if no higher level is found."
  26247. (let ((pos (point)) start-level level
  26248. (re (concat "^" outline-regexp)))
  26249. (catch 'exit
  26250. (outline-back-to-heading t)
  26251. (setq start-level (funcall outline-level))
  26252. (if (equal start-level 1) (throw 'exit nil))
  26253. (while (re-search-backward re nil t)
  26254. (setq level (funcall outline-level))
  26255. (if (< level start-level) (throw 'exit level)))
  26256. nil)))
  26257. (defun org-first-sibling-p ()
  26258. "Is this heading the first child of its parents?"
  26259. (interactive)
  26260. (let ((re (concat "^" outline-regexp))
  26261. level l)
  26262. (unless (org-at-heading-p t)
  26263. (error "Not at a heading"))
  26264. (setq level (funcall outline-level))
  26265. (save-excursion
  26266. (if (not (re-search-backward re nil t))
  26267. t
  26268. (setq l (funcall outline-level))
  26269. (< l level)))))
  26270. (defun org-goto-sibling (&optional previous)
  26271. "Goto the next sibling, even if it is invisible.
  26272. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26273. when a sibling was found. When none is found, return nil and don't
  26274. move point."
  26275. (let ((fun (if previous 're-search-backward 're-search-forward))
  26276. (pos (point))
  26277. (re (concat "^" outline-regexp))
  26278. level l)
  26279. (when (condition-case nil (org-back-to-heading t) (error nil))
  26280. (setq level (funcall outline-level))
  26281. (catch 'exit
  26282. (or previous (forward-char 1))
  26283. (while (funcall fun re nil t)
  26284. (setq l (funcall outline-level))
  26285. (when (< l level) (goto-char pos) (throw 'exit nil))
  26286. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26287. (goto-char pos)
  26288. nil))))
  26289. (defun org-show-siblings ()
  26290. "Show all siblings of the current headline."
  26291. (save-excursion
  26292. (while (org-goto-sibling) (org-flag-heading nil)))
  26293. (save-excursion
  26294. (while (org-goto-sibling 'previous)
  26295. (org-flag-heading nil))))
  26296. (defun org-show-hidden-entry ()
  26297. "Show an entry where even the heading is hidden."
  26298. (save-excursion
  26299. (org-show-entry)))
  26300. (defun org-flag-heading (flag &optional entry)
  26301. "Flag the current heading. FLAG non-nil means make invisible.
  26302. When ENTRY is non-nil, show the entire entry."
  26303. (save-excursion
  26304. (org-back-to-heading t)
  26305. ;; Check if we should show the entire entry
  26306. (if entry
  26307. (progn
  26308. (org-show-entry)
  26309. (save-excursion
  26310. (and (outline-next-heading)
  26311. (org-flag-heading nil))))
  26312. (outline-flag-region (max (point-min) (1- (point)))
  26313. (save-excursion (outline-end-of-heading) (point))
  26314. flag))))
  26315. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26316. ;; This is an exact copy of the original function, but it uses
  26317. ;; `org-back-to-heading', to make it work also in invisible
  26318. ;; trees. And is uses an invisible-OK argument.
  26319. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26320. (org-back-to-heading invisible-OK)
  26321. (let ((first t)
  26322. (level (funcall outline-level)))
  26323. (while (and (not (eobp))
  26324. (or first (> (funcall outline-level) level)))
  26325. (setq first nil)
  26326. (outline-next-heading))
  26327. (unless to-heading
  26328. (if (memq (preceding-char) '(?\n ?\^M))
  26329. (progn
  26330. ;; Go to end of line before heading
  26331. (forward-char -1)
  26332. (if (memq (preceding-char) '(?\n ?\^M))
  26333. ;; leave blank line before heading
  26334. (forward-char -1))))))
  26335. (point))
  26336. (defun org-show-subtree ()
  26337. "Show everything after this heading at deeper levels."
  26338. (outline-flag-region
  26339. (point)
  26340. (save-excursion
  26341. (outline-end-of-subtree) (outline-next-heading) (point))
  26342. nil))
  26343. (defun org-show-entry ()
  26344. "Show the body directly following this heading.
  26345. Show the heading too, if it is currently invisible."
  26346. (interactive)
  26347. (save-excursion
  26348. (condition-case nil
  26349. (progn
  26350. (org-back-to-heading t)
  26351. (outline-flag-region
  26352. (max (point-min) (1- (point)))
  26353. (save-excursion
  26354. (re-search-forward
  26355. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26356. (or (match-beginning 1) (point-max)))
  26357. nil))
  26358. (error nil))))
  26359. (defun org-make-options-regexp (kwds)
  26360. "Make a regular expression for keyword lines."
  26361. (concat
  26362. "^"
  26363. "#?[ \t]*\\+\\("
  26364. (mapconcat 'regexp-quote kwds "\\|")
  26365. "\\):[ \t]*"
  26366. "\\(.+\\)"))
  26367. ;; Make isearch reveal the necessary context
  26368. (defun org-isearch-end ()
  26369. "Reveal context after isearch exits."
  26370. (when isearch-success ; only if search was successful
  26371. (if (featurep 'xemacs)
  26372. ;; Under XEmacs, the hook is run in the correct place,
  26373. ;; we directly show the context.
  26374. (org-show-context 'isearch)
  26375. ;; In Emacs the hook runs *before* restoring the overlays.
  26376. ;; So we have to use a one-time post-command-hook to do this.
  26377. ;; (Emacs 22 has a special variable, see function `org-mode')
  26378. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26379. isearch-mode-end-hook-quit)
  26380. ;; Only when the isearch was not quitted.
  26381. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26382. 'append 'local)))))
  26383. (defun org-isearch-post-command ()
  26384. "Remove self from hook, and show context."
  26385. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26386. (org-show-context 'isearch))
  26387. ;;;; Integration with and fixes for other packages
  26388. ;;; Imenu support
  26389. (defvar org-imenu-markers nil
  26390. "All markers currently used by Imenu.")
  26391. (make-variable-buffer-local 'org-imenu-markers)
  26392. (defun org-imenu-new-marker (&optional pos)
  26393. "Return a new marker for use by Imenu, and remember the marker."
  26394. (let ((m (make-marker)))
  26395. (move-marker m (or pos (point)))
  26396. (push m org-imenu-markers)
  26397. m))
  26398. (defun org-imenu-get-tree ()
  26399. "Produce the index for Imenu."
  26400. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26401. (setq org-imenu-markers nil)
  26402. (let* ((n org-imenu-depth)
  26403. (re (concat "^" outline-regexp))
  26404. (subs (make-vector (1+ n) nil))
  26405. (last-level 0)
  26406. m tree level head)
  26407. (save-excursion
  26408. (save-restriction
  26409. (widen)
  26410. (goto-char (point-max))
  26411. (while (re-search-backward re nil t)
  26412. (setq level (org-reduced-level (funcall outline-level)))
  26413. (when (<= level n)
  26414. (looking-at org-complex-heading-regexp)
  26415. (setq head (org-match-string-no-properties 4)
  26416. m (org-imenu-new-marker))
  26417. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26418. (if (>= level last-level)
  26419. (push (cons head m) (aref subs level))
  26420. (push (cons head (aref subs (1+ level))) (aref subs level))
  26421. (loop for i from (1+ level) to n do (aset subs i nil)))
  26422. (setq last-level level)))))
  26423. (aref subs 1)))
  26424. (eval-after-load "imenu"
  26425. '(progn
  26426. (add-hook 'imenu-after-jump-hook
  26427. (lambda () (org-show-context 'org-goto)))))
  26428. ;; Speedbar support
  26429. (defun org-speedbar-set-agenda-restriction ()
  26430. "Restrict future agenda commands to the location at point in speedbar.
  26431. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26432. (interactive)
  26433. (let (p m tp np dir txt w)
  26434. (cond
  26435. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26436. 'org-imenu t))
  26437. (setq m (get-text-property p 'org-imenu-marker))
  26438. (save-excursion
  26439. (save-restriction
  26440. (set-buffer (marker-buffer m))
  26441. (goto-char m)
  26442. (org-agenda-set-restriction-lock 'subtree))))
  26443. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26444. 'speedbar-function 'speedbar-find-file))
  26445. (setq tp (previous-single-property-change
  26446. (1+ p) 'speedbar-function)
  26447. np (next-single-property-change
  26448. tp 'speedbar-function)
  26449. dir (speedbar-line-directory)
  26450. txt (buffer-substring-no-properties (or tp (point-min))
  26451. (or np (point-max))))
  26452. (save-excursion
  26453. (save-restriction
  26454. (set-buffer (find-file-noselect
  26455. (let ((default-directory dir))
  26456. (expand-file-name txt))))
  26457. (unless (org-mode-p)
  26458. (error "Cannot restrict to non-Org-mode file"))
  26459. (org-agenda-set-restriction-lock 'file))))
  26460. (t (error "Don't know how to restrict Org-mode's agenda")))
  26461. (org-move-overlay org-speedbar-restriction-lock-overlay
  26462. (point-at-bol) (point-at-eol))
  26463. (setq current-prefix-arg nil)
  26464. (org-agenda-maybe-redo)))
  26465. (eval-after-load "speedbar"
  26466. '(progn
  26467. (speedbar-add-supported-extension ".org")
  26468. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26469. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26470. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26471. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26472. (add-hook 'speedbar-visiting-tag-hook
  26473. (lambda () (org-show-context 'org-goto)))))
  26474. ;;; Fixes and Hacks
  26475. ;; Make flyspell not check words in links, to not mess up our keymap
  26476. (defun org-mode-flyspell-verify ()
  26477. "Don't let flyspell put overlays at active buttons."
  26478. (not (get-text-property (point) 'keymap)))
  26479. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26480. (eval-after-load "bookmark"
  26481. '(if (boundp 'bookmark-after-jump-hook)
  26482. ;; We can use the hook
  26483. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26484. ;; Hook not available, use advice
  26485. (defadvice bookmark-jump (after org-make-visible activate)
  26486. "Make the position visible."
  26487. (org-bookmark-jump-unhide))))
  26488. (defun org-bookmark-jump-unhide ()
  26489. "Unhide the current position, to show the bookmark location."
  26490. (and (org-mode-p)
  26491. (or (org-invisible-p)
  26492. (save-excursion (goto-char (max (point-min) (1- (point))))
  26493. (org-invisible-p)))
  26494. (org-show-context 'bookmark-jump)))
  26495. ;; Fix a bug in htmlize where there are text properties (face nil)
  26496. (eval-after-load "htmlize"
  26497. '(progn
  26498. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26499. "Make sure there are no nil faces"
  26500. (setq ad-return-value (delq nil ad-return-value)))))
  26501. ;; Make session.el ignore our circular variable
  26502. (eval-after-load "session"
  26503. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26504. ;;;; Experimental code
  26505. (defun org-closed-in-range ()
  26506. "Sparse tree of items closed in a certain time range.
  26507. Still experimental, may disappear in the future."
  26508. (interactive)
  26509. ;; Get the time interval from the user.
  26510. (let* ((time1 (time-to-seconds
  26511. (org-read-date nil 'to-time nil "Starting date: ")))
  26512. (time2 (time-to-seconds
  26513. (org-read-date nil 'to-time nil "End date:")))
  26514. ;; callback function
  26515. (callback (lambda ()
  26516. (let ((time
  26517. (time-to-seconds
  26518. (apply 'encode-time
  26519. (org-parse-time-string
  26520. (match-string 1))))))
  26521. ;; check if time in interval
  26522. (and (>= time time1) (<= time time2))))))
  26523. ;; make tree, check each match with the callback
  26524. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26525. ;;;; Finish up
  26526. (provide 'org)
  26527. (run-hooks 'org-load-hook)
  26528. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26529. ;;; org.el ends here