org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484
  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, to 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. ))
  156. (defun org-load-default-extensions ()
  157. "Load all extensions that are listed in `org-default-extensions'."
  158. (mapc 'require org-default-extensions))
  159. ;; FIXME: Needs a separate group...
  160. (defcustom org-completion-fallback-command 'hippie-expand
  161. "The expansion command called by \\[org-complete] in normal context.
  162. Normal means, no org-mode-specific context."
  163. :group 'org
  164. :type 'function)
  165. (defgroup org-startup nil
  166. "Options concerning startup of Org-mode."
  167. :tag "Org Startup"
  168. :group 'org)
  169. (defcustom org-startup-folded t
  170. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  171. This can also be configured on a per-file basis by adding one of
  172. the following lines anywhere in the buffer:
  173. #+STARTUP: fold
  174. #+STARTUP: nofold
  175. #+STARTUP: content"
  176. :group 'org-startup
  177. :type '(choice
  178. (const :tag "nofold: show all" nil)
  179. (const :tag "fold: overview" t)
  180. (const :tag "content: all headlines" content)))
  181. (defcustom org-startup-truncated t
  182. "Non-nil means, entering Org-mode will set `truncate-lines'.
  183. This is useful since some lines containing links can be very long and
  184. uninteresting. Also tables look terrible when wrapped."
  185. :group 'org-startup
  186. :type 'boolean)
  187. (defcustom org-startup-align-all-tables nil
  188. "Non-nil means, align all tables when visiting a file.
  189. This is useful when the column width in tables is forced with <N> cookies
  190. in table fields. Such tables will look correct only after the first re-align.
  191. This can also be configured on a per-file basis by adding one of
  192. the following lines anywhere in the buffer:
  193. #+STARTUP: align
  194. #+STARTUP: noalign"
  195. :group 'org-startup
  196. :type 'boolean)
  197. (defcustom org-insert-mode-line-in-empty-file nil
  198. "Non-nil means insert the first line setting Org-mode in empty files.
  199. When the function `org-mode' is called interactively in an empty file, this
  200. normally means that the file name does not automatically trigger Org-mode.
  201. To ensure that the file will always be in Org-mode in the future, a
  202. line enforcing Org-mode will be inserted into the buffer, if this option
  203. has been set."
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-replace-disputed-keys nil
  207. "Non-nil means use alternative key bindings for some keys.
  208. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  209. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  210. If you want to use Org-mode together with one of these other modes,
  211. or more generally if you would like to move some Org-mode commands to
  212. other keys, set this variable and configure the keys with the variable
  213. `org-disputed-keys'.
  214. This option is only relevant at load-time of Org-mode, and must be set
  215. *before* org.el is loaded. Changing it requires a restart of Emacs to
  216. become effective."
  217. :group 'org-startup
  218. :type 'boolean)
  219. (if (fboundp 'defvaralias)
  220. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  221. (defcustom org-disputed-keys
  222. '(([(shift up)] . [(meta p)])
  223. ([(shift down)] . [(meta n)])
  224. ([(shift left)] . [(meta -)])
  225. ([(shift right)] . [(meta +)])
  226. ([(control shift right)] . [(meta shift +)])
  227. ([(control shift left)] . [(meta shift -)]))
  228. "Keys for which Org-mode and other modes compete.
  229. This is an alist, cars are the default keys, second element specifies
  230. the alternative to use when `org-replace-disputed-keys' is t.
  231. Keys can be specified in any syntax supported by `define-key'.
  232. The value of this option takes effect only at Org-mode's startup,
  233. therefore you'll have to restart Emacs to apply it after changing."
  234. :group 'org-startup
  235. :type 'alist)
  236. (defun org-key (key)
  237. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  238. Or return the original if not disputed."
  239. (if org-replace-disputed-keys
  240. (let* ((nkey (key-description key))
  241. (x (org-find-if (lambda (x)
  242. (equal (key-description (car x)) nkey))
  243. org-disputed-keys)))
  244. (if x (cdr x) key))
  245. key))
  246. (defun org-find-if (predicate seq)
  247. (catch 'exit
  248. (while seq
  249. (if (funcall predicate (car seq))
  250. (throw 'exit (car seq))
  251. (pop seq)))))
  252. (defun org-defkey (keymap key def)
  253. "Define a key, possibly translated, as returned by `org-key'."
  254. (define-key keymap (org-key key) def))
  255. (defcustom org-ellipsis nil
  256. "The ellipsis to use in the Org-mode outline.
  257. When nil, just use the standard three dots. When a string, use that instead,
  258. When a face, use the standart 3 dots, but with the specified face.
  259. The change affects only Org-mode (which will then use its own display table).
  260. Changing this requires executing `M-x org-mode' in a buffer to become
  261. effective."
  262. :group 'org-startup
  263. :type '(choice (const :tag "Default" nil)
  264. (face :tag "Face" :value org-warning)
  265. (string :tag "String" :value "...#")))
  266. (defvar org-display-table nil
  267. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  268. (defgroup org-keywords nil
  269. "Keywords in Org-mode."
  270. :tag "Org Keywords"
  271. :group 'org)
  272. (defcustom org-deadline-string "DEADLINE:"
  273. "String to mark deadline entries.
  274. A deadline is this string, followed by a time stamp. Should be a word,
  275. terminated by a colon. You can insert a schedule keyword and
  276. a timestamp with \\[org-deadline].
  277. Changes become only effective after restarting Emacs."
  278. :group 'org-keywords
  279. :type 'string)
  280. (defcustom org-scheduled-string "SCHEDULED:"
  281. "String to mark scheduled TODO entries.
  282. A schedule is this string, followed by a time stamp. Should be a word,
  283. terminated by a colon. You can insert a schedule keyword and
  284. a timestamp with \\[org-schedule].
  285. Changes become only effective after restarting Emacs."
  286. :group 'org-keywords
  287. :type 'string)
  288. (defcustom org-closed-string "CLOSED:"
  289. "String used as the prefix for timestamps logging closing a TODO entry."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-clock-string "CLOCK:"
  293. "String used as prefix for timestamps clocking work hours on an item."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-comment-string "COMMENT"
  297. "Entries starting with this keyword will never be exported.
  298. An entry can be toggled between COMMENT and normal with
  299. \\[org-toggle-comment].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-quote-string "QUOTE"
  304. "Entries starting with this keyword will be exported in fixed-width font.
  305. Quoting applies only to the text in the entry following the headline, and does
  306. not extend beyond the next headline, even if that is lower level.
  307. An entry can be toggled between QUOTE and normal with
  308. \\[org-toggle-fixed-width-section]."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defconst org-repeat-re
  312. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  313. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defgroup org-cycle nil
  399. "Options concerning visibility cycling in Org-mode."
  400. :tag "Org Cycle"
  401. :group 'org-structure)
  402. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  403. "Names of drawers. Drawers are not opened by cycling on the headline above.
  404. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  405. this:
  406. :DRAWERNAME:
  407. .....
  408. :END:
  409. The drawer \"PROPERTIES\" is special for capturing properties through
  410. the property API.
  411. Drawers can be defined on the per-file basis with a line like:
  412. #+DRAWERS: HIDDEN STATE PROPERTIES"
  413. :group 'org-structure
  414. :type '(repeat (string :tag "Drawer Name")))
  415. (defcustom org-cycle-global-at-bob nil
  416. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  417. This makes it possible to do global cycling without having to use S-TAB or
  418. C-u TAB. For this special case to work, the first line of the buffer
  419. must not be a headline - it may be empty ot some other text. When used in
  420. this way, `org-cycle-hook' is disables temporarily, to make sure the
  421. cursor stays at the beginning of the buffer.
  422. When this option is nil, don't do anything special at the beginning
  423. of the buffer."
  424. :group 'org-cycle
  425. :type 'boolean)
  426. (defcustom org-cycle-emulate-tab t
  427. "Where should `org-cycle' emulate TAB.
  428. nil Never
  429. white Only in completely white lines
  430. whitestart Only at the beginning of lines, before the first non-white char
  431. t Everywhere except in headlines
  432. exc-hl-bol Everywhere except at the start of a headline
  433. If TAB is used in a place where it does not emulate TAB, the current subtree
  434. visibility is cycled."
  435. :group 'org-cycle
  436. :type '(choice (const :tag "Never" nil)
  437. (const :tag "Only in completely white lines" white)
  438. (const :tag "Before first char in a line" whitestart)
  439. (const :tag "Everywhere except in headlines" t)
  440. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  441. ))
  442. (defcustom org-cycle-separator-lines 2
  443. "Number of empty lines needed to keep an empty line between collapsed trees.
  444. If you leave an empty line between the end of a subtree and the following
  445. headline, this empty line is hidden when the subtree is folded.
  446. Org-mode will leave (exactly) one empty line visible if the number of
  447. empty lines is equal or larger to the number given in this variable.
  448. So the default 2 means, at least 2 empty lines after the end of a subtree
  449. are needed to produce free space between a collapsed subtree and the
  450. following headline.
  451. Special case: when 0, never leave empty lines in collapsed view."
  452. :group 'org-cycle
  453. :type 'integer)
  454. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  455. org-cycle-hide-drawers
  456. org-cycle-show-empty-lines
  457. org-optimize-window-after-visibility-change)
  458. "Hook that is run after `org-cycle' has changed the buffer visibility.
  459. The function(s) in this hook must accept a single argument which indicates
  460. the new state that was set by the most recent `org-cycle' command. The
  461. argument is a symbol. After a global state change, it can have the values
  462. `overview', `content', or `all'. After a local state change, it can have
  463. the values `folded', `children', or `subtree'."
  464. :group 'org-cycle
  465. :type 'hook)
  466. (defgroup org-edit-structure nil
  467. "Options concerning structure editing in Org-mode."
  468. :tag "Org Edit Structure"
  469. :group 'org-structure)
  470. (defcustom org-odd-levels-only nil
  471. "Non-nil means, skip even levels and only use odd levels for the outline.
  472. This has the effect that two stars are being added/taken away in
  473. promotion/demotion commands. It also influences how levels are
  474. handled by the exporters.
  475. Changing it requires restart of `font-lock-mode' to become effective
  476. for fontification also in regions already fontified.
  477. You may also set this on a per-file basis by adding one of the following
  478. lines to the buffer:
  479. #+STARTUP: odd
  480. #+STARTUP: oddeven"
  481. :group 'org-edit-structure
  482. :group 'org-font-lock
  483. :type 'boolean)
  484. (defcustom org-adapt-indentation t
  485. "Non-nil means, adapt indentation when promoting and demoting.
  486. When this is set and the *entire* text in an entry is indented, the
  487. indentation is increased by one space in a demotion command, and
  488. decreased by one in a promotion command. If any line in the entry
  489. body starts at column 0, indentation is not changed at all."
  490. :group 'org-edit-structure
  491. :type 'boolean)
  492. (defcustom org-special-ctrl-a/e nil
  493. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  494. When t, `C-a' will bring back the cursor to the beginning of the
  495. headline text, i.e. after the stars and after a possible TODO keyword.
  496. In an item, this will be the position after the bullet.
  497. When the cursor is already at that position, another `C-a' will bring
  498. it to the beginning of the line.
  499. `C-e' will jump to the end of the headline, ignoring the presence of tags
  500. in the headline. A second `C-e' will then jump to the true end of the
  501. line, after any tags.
  502. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  503. and only a directly following, identical keypress will bring the cursor
  504. to the special positions."
  505. :group 'org-edit-structure
  506. :type '(choice
  507. (const :tag "off" nil)
  508. (const :tag "after bullet first" t)
  509. (const :tag "border first" reversed)))
  510. (if (fboundp 'defvaralias)
  511. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  512. (defcustom org-special-ctrl-k nil
  513. "Non-nil means `C-k' will behave specially in headlines.
  514. When nil, `C-k' will call the default `kill-line' command.
  515. When t, the following will happen while the cursor is in the headline:
  516. - When the cursor is at the beginning of a headline, kill the entire
  517. line and possible the folded subtree below the line.
  518. - When in the middle of the headline text, kill the headline up to the tags.
  519. - When after the headline text, kill the tags."
  520. :group 'org-edit-structure
  521. :type 'boolean)
  522. (defcustom org-M-RET-may-split-line '((default . t))
  523. "Non-nil means, M-RET will split the line at the cursor position.
  524. When nil, it will go to the end of the line before making a
  525. new line.
  526. You may also set this option in a different way for different
  527. contexts. Valid contexts are:
  528. headline when creating a new headline
  529. item when creating a new item
  530. table in a table field
  531. default the value to be used for all contexts not explicitly
  532. customized"
  533. :group 'org-structure
  534. :group 'org-table
  535. :type '(choice
  536. (const :tag "Always" t)
  537. (const :tag "Never" nil)
  538. (repeat :greedy t :tag "Individual contexts"
  539. (cons
  540. (choice :tag "Context"
  541. (const headline)
  542. (const item)
  543. (const table)
  544. (const default))
  545. (boolean)))))
  546. (defcustom org-blank-before-new-entry '((heading . nil)
  547. (plain-list-item . nil))
  548. "Should `org-insert-heading' leave a blank line before new heading/item?
  549. The value is an alist, with `heading' and `plain-list-item' as car,
  550. and a boolean flag as cdr."
  551. :group 'org-edit-structure
  552. :type '(list
  553. (cons (const heading) (boolean))
  554. (cons (const plain-list-item) (boolean))))
  555. (defcustom org-insert-heading-hook nil
  556. "Hook being run after inserting a new heading."
  557. :group 'org-edit-structure
  558. :type 'hook)
  559. (defcustom org-enable-fixed-width-editor t
  560. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  561. This currently only means, they are never auto-wrapped.
  562. When nil, such lines will be treated like ordinary lines.
  563. See also the QUOTE keyword."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-goto-auto-isearch t
  567. "Non-nil means, typing characters in org-goto starts incremental search."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defgroup org-sparse-trees nil
  571. "Options concerning sparse trees in Org-mode."
  572. :tag "Org Sparse Trees"
  573. :group 'org-structure)
  574. (defcustom org-highlight-sparse-tree-matches t
  575. "Non-nil means, highlight all matches that define a sparse tree.
  576. The highlights will automatically disappear the next time the buffer is
  577. changed by an edit command."
  578. :group 'org-sparse-trees
  579. :type 'boolean)
  580. (defcustom org-remove-highlights-with-change t
  581. "Non-nil means, any change to the buffer will remove temporary highlights.
  582. Such highlights are created by `org-occur' and `org-clock-display'.
  583. When nil, `C-c C-c needs to be used to get rid of the highlights.
  584. The highlights created by `org-preview-latex-fragment' always need
  585. `C-c C-c' to be removed."
  586. :group 'org-sparse-trees
  587. :group 'org-time
  588. :type 'boolean)
  589. (defcustom org-occur-hook '(org-first-headline-recenter)
  590. "Hook that is run after `org-occur' has constructed a sparse tree.
  591. This can be used to recenter the window to show as much of the structure
  592. as possible."
  593. :group 'org-sparse-trees
  594. :type 'hook)
  595. (defgroup org-plain-lists nil
  596. "Options concerning plain lists in Org-mode."
  597. :tag "Org Plain lists"
  598. :group 'org-structure)
  599. (defcustom org-cycle-include-plain-lists nil
  600. "Non-nil means, include plain lists into visibility cycling.
  601. This means that during cycling, plain list items will *temporarily* be
  602. interpreted as outline headlines with a level given by 1000+i where i is the
  603. indentation of the bullet. In all other operations, plain list items are
  604. not seen as headlines. For example, you cannot assign a TODO keyword to
  605. such an item."
  606. :group 'org-plain-lists
  607. :type 'boolean)
  608. (defcustom org-plain-list-ordered-item-terminator t
  609. "The character that makes a line with leading number an ordered list item.
  610. Valid values are ?. and ?\). To get both terminators, use t. While
  611. ?. may look nicer, it creates the danger that a line with leading
  612. number may be incorrectly interpreted as an item. ?\) therefore is
  613. the safe choice."
  614. :group 'org-plain-lists
  615. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  616. (const :tag "paren like in \"2)\"" ?\))
  617. (const :tab "both" t)))
  618. (defcustom org-auto-renumber-ordered-lists t
  619. "Non-nil means, automatically renumber ordered plain lists.
  620. Renumbering happens when the sequence have been changed with
  621. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  622. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  623. :group 'org-plain-lists
  624. :type 'boolean)
  625. (defcustom org-provide-checkbox-statistics t
  626. "Non-nil means, update checkbox statistics after insert and toggle.
  627. When this is set, checkbox statistics is updated each time you either insert
  628. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  629. with \\[org-ctrl-c-ctrl-c\\]."
  630. :group 'org-plain-lists
  631. :type 'boolean)
  632. (defgroup org-archive nil
  633. "Options concerning archiving in Org-mode."
  634. :tag "Org Archive"
  635. :group 'org-structure)
  636. (defcustom org-archive-tag "ARCHIVE"
  637. "The tag that marks a subtree as archived.
  638. An archived subtree does not open during visibility cycling, and does
  639. not contribute to the agenda listings.
  640. After changing this, font-lock must be restarted in the relevant buffers to
  641. get the proper fontification."
  642. :group 'org-archive
  643. :group 'org-keywords
  644. :type 'string)
  645. (defcustom org-agenda-skip-archived-trees t
  646. "Non-nil means, the agenda will skip any items located in archived trees.
  647. An archived tree is a tree marked with the tag ARCHIVE."
  648. :group 'org-archive
  649. :group 'org-agenda-skip
  650. :type 'boolean)
  651. (defcustom org-cycle-open-archived-trees nil
  652. "Non-nil means, `org-cycle' will open archived trees.
  653. An archived tree is a tree marked with the tag ARCHIVE.
  654. When nil, archived trees will stay folded. You can still open them with
  655. normal outline commands like `show-all', but not with the cycling commands."
  656. :group 'org-archive
  657. :group 'org-cycle
  658. :type 'boolean)
  659. (defcustom org-sparse-tree-open-archived-trees nil
  660. "Non-nil means sparse tree construction shows matches in archived trees.
  661. When nil, matches in these trees are highlighted, but the trees are kept in
  662. collapsed state."
  663. :group 'org-archive
  664. :group 'org-sparse-trees
  665. :type 'boolean)
  666. (defcustom org-archive-location "%s_archive::"
  667. "The location where subtrees should be archived.
  668. This string consists of two parts, separated by a double-colon.
  669. The first part is a file name - when omitted, archiving happens in the same
  670. file. %s will be replaced by the current file name (without directory part).
  671. Archiving to a different file is useful to keep archived entries from
  672. contributing to the Org-mode Agenda.
  673. The part after the double colon is a headline. The archived entries will be
  674. filed under that headline. When omitted, the subtrees are simply filed away
  675. at the end of the file, as top-level entries.
  676. Here are a few examples:
  677. \"%s_archive::\"
  678. If the current file is Projects.org, archive in file
  679. Projects.org_archive, as top-level trees. This is the default.
  680. \"::* Archived Tasks\"
  681. Archive in the current file, under the top-level headline
  682. \"* Archived Tasks\".
  683. \"~/org/archive.org::\"
  684. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  685. \"basement::** Finished Tasks\"
  686. Archive in file ./basement (relative path), as level 3 trees
  687. below the level 2 heading \"** Finished Tasks\".
  688. You may set this option on a per-file basis by adding to the buffer a
  689. line like
  690. #+ARCHIVE: basement::** Finished Tasks"
  691. :group 'org-archive
  692. :type 'string)
  693. (defcustom org-archive-mark-done t
  694. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  695. This can be a string to set the keyword to use. When t, Org-mode will
  696. use the first keyword in its list that means done."
  697. :group 'org-archive
  698. :type '(choice
  699. (const :tag "No" nil)
  700. (const :tag "Yes" t)
  701. (string :tag "Use this keyword")))
  702. (defcustom org-archive-stamp-time t
  703. "Non-nil means, add a time stamp to entries moved to an archive file.
  704. This variable is obsolete and has no effect anymore, instead add ot remove
  705. `time' from the variablle `org-archive-save-context-info'."
  706. :group 'org-archive
  707. :type 'boolean)
  708. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  709. "Parts of context info that should be stored as properties when archiving.
  710. When a subtree is moved to an archive file, it looses information given by
  711. context, like inherited tags, the category, and possibly also the TODO
  712. state (depending on the variable `org-archive-mark-done').
  713. This variable can be a list of any of the following symbols:
  714. time The time of archiving.
  715. file The file where the entry originates.
  716. itags The local tags, in the headline of the subtree.
  717. ltags The tags the subtree inherits from further up the hierarchy.
  718. todo The pre-archive TODO state.
  719. category The category, taken from file name or #+CATEGORY lines.
  720. olpath The outline path to the item. These are all headlines above
  721. the current item, separated by /, like a file path.
  722. For each symbol present in the list, a property will be created in
  723. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  724. information."
  725. :group 'org-archive
  726. :type '(set :greedy t
  727. (const :tag "Time" time)
  728. (const :tag "File" file)
  729. (const :tag "Category" category)
  730. (const :tag "TODO state" todo)
  731. (const :tag "TODO state" priority)
  732. (const :tag "Inherited tags" itags)
  733. (const :tag "Outline path" olpath)
  734. (const :tag "Local tags" ltags)))
  735. (defgroup org-imenu-and-speedbar nil
  736. "Options concerning imenu and speedbar in Org-mode."
  737. :tag "Org Imenu and Speedbar"
  738. :group 'org-structure)
  739. (defcustom org-imenu-depth 2
  740. "The maximum level for Imenu access to Org-mode headlines.
  741. This also applied for speedbar access."
  742. :group 'org-imenu-and-speedbar
  743. :type 'number)
  744. (defgroup org-table nil
  745. "Options concerning tables in Org-mode."
  746. :tag "Org Table"
  747. :group 'org)
  748. (defcustom org-enable-table-editor 'optimized
  749. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  750. When nil, such lines will be treated like ordinary lines.
  751. When equal to the symbol `optimized', the table editor will be optimized to
  752. do the following:
  753. - Automatic overwrite mode in front of whitespace in table fields.
  754. This makes the structure of the table stay in tact as long as the edited
  755. field does not exceed the column width.
  756. - Minimize the number of realigns. Normally, the table is aligned each time
  757. TAB or RET are pressed to move to another field. With optimization this
  758. happens only if changes to a field might have changed the column width.
  759. Optimization requires replacing the functions `self-insert-command',
  760. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  761. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  762. very good at guessing when a re-align will be necessary, but you can always
  763. force one with \\[org-ctrl-c-ctrl-c].
  764. If you would like to use the optimized version in Org-mode, but the
  765. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  766. This variable can be used to turn on and off the table editor during a session,
  767. but in order to toggle optimization, a restart is required.
  768. See also the variable `org-table-auto-blank-field'."
  769. :group 'org-table
  770. :type '(choice
  771. (const :tag "off" nil)
  772. (const :tag "on" t)
  773. (const :tag "on, optimized" optimized)))
  774. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  775. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  776. In the optimized version, the table editor takes over all simple keys that
  777. normally just insert a character. In tables, the characters are inserted
  778. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  779. for empty fields). Outside tables, the correct binding of the keys is
  780. restored.
  781. The default for this option is t if the optimized version is also used in
  782. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  783. this variable requires a restart of Emacs to become effective."
  784. :group 'org-table
  785. :type 'boolean)
  786. (defcustom orgtbl-radio-table-templates
  787. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  788. % END RECEIVE ORGTBL %n
  789. \\begin{comment}
  790. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  791. | | |
  792. \\end{comment}\n")
  793. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  794. @c END RECEIVE ORGTBL %n
  795. @ignore
  796. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  797. | | |
  798. @end ignore\n")
  799. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  800. <!-- END RECEIVE ORGTBL %n -->
  801. <!--
  802. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  803. | | |
  804. -->\n"))
  805. "Templates for radio tables in different major modes.
  806. All occurrences of %n in a template will be replaced with the name of the
  807. table, obtained by prompting the user."
  808. :group 'org-table
  809. :type '(repeat
  810. (list (symbol :tag "Major mode")
  811. (string :tag "Format"))))
  812. (defgroup org-table-settings nil
  813. "Settings for tables in Org-mode."
  814. :tag "Org Table Settings"
  815. :group 'org-table)
  816. (defcustom org-table-default-size "5x2"
  817. "The default size for newly created tables, Columns x Rows."
  818. :group 'org-table-settings
  819. :type 'string)
  820. (defcustom org-table-number-regexp
  821. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  822. "Regular expression for recognizing numbers in table columns.
  823. If a table column contains mostly numbers, it will be aligned to the
  824. right. If not, it will be aligned to the left.
  825. The default value of this option is a regular expression which allows
  826. anything which looks remotely like a number as used in scientific
  827. context. For example, all of the following will be considered a
  828. number:
  829. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  830. Other options offered by the customize interface are more restrictive."
  831. :group 'org-table-settings
  832. :type '(choice
  833. (const :tag "Positive Integers"
  834. "^[0-9]+$")
  835. (const :tag "Integers"
  836. "^[-+]?[0-9]+$")
  837. (const :tag "Floating Point Numbers"
  838. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  839. (const :tag "Floating Point Number or Integer"
  840. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  841. (const :tag "Exponential, Floating point, Integer"
  842. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  843. (const :tag "Very General Number-Like, including hex"
  844. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  845. (string :tag "Regexp:")))
  846. (defcustom org-table-number-fraction 0.5
  847. "Fraction of numbers in a column required to make the column align right.
  848. In a column all non-white fields are considered. If at least this
  849. fraction of fields is matched by `org-table-number-fraction',
  850. alignment to the right border applies."
  851. :group 'org-table-settings
  852. :type 'number)
  853. (defgroup org-table-editing nil
  854. "Behavior of tables during editing in Org-mode."
  855. :tag "Org Table Editing"
  856. :group 'org-table)
  857. (defcustom org-table-automatic-realign t
  858. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  859. When nil, aligning is only done with \\[org-table-align], or after column
  860. removal/insertion."
  861. :group 'org-table-editing
  862. :type 'boolean)
  863. (defcustom org-table-auto-blank-field t
  864. "Non-nil means, automatically blank table field when starting to type into it.
  865. This only happens when typing immediately after a field motion
  866. command (TAB, S-TAB or RET).
  867. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  868. :group 'org-table-editing
  869. :type 'boolean)
  870. (defcustom org-table-tab-jumps-over-hlines t
  871. "Non-nil means, tab in the last column of a table with jump over a hline.
  872. If a horizontal separator line is following the current line,
  873. `org-table-next-field' can either create a new row before that line, or jump
  874. over the line. When this option is nil, a new line will be created before
  875. this line."
  876. :group 'org-table-editing
  877. :type 'boolean)
  878. (defcustom org-table-tab-recognizes-table.el t
  879. "Non-nil means, TAB will automatically notice a table.el table.
  880. When it sees such a table, it moves point into it and - if necessary -
  881. calls `table-recognize-table'."
  882. :group 'org-table-editing
  883. :type 'boolean)
  884. (defgroup org-table-calculation nil
  885. "Options concerning tables in Org-mode."
  886. :tag "Org Table Calculation"
  887. :group 'org-table)
  888. (defcustom org-table-use-standard-references t
  889. "Should org-mode work with table refrences like B3 instead of @3$2?
  890. Possible values are:
  891. nil never use them
  892. from accept as input, do not present for editing
  893. t: accept as input and present for editing"
  894. :group 'org-table-calculation
  895. :type '(choice
  896. (const :tag "Never, don't even check unser input for them" nil)
  897. (const :tag "Always, both as user input, and when editing" t)
  898. (const :tag "Convert user input, don't offer during editing" 'from)))
  899. (defcustom org-table-copy-increment t
  900. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  901. :group 'org-table-calculation
  902. :type 'boolean)
  903. (defcustom org-calc-default-modes
  904. '(calc-internal-prec 12
  905. calc-float-format (float 5)
  906. calc-angle-mode deg
  907. calc-prefer-frac nil
  908. calc-symbolic-mode nil
  909. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  910. calc-display-working-message t
  911. )
  912. "List with Calc mode settings for use in calc-eval for table formulas.
  913. The list must contain alternating symbols (Calc modes variables and values).
  914. Don't remove any of the default settings, just change the values. Org-mode
  915. relies on the variables to be present in the list."
  916. :group 'org-table-calculation
  917. :type 'plist)
  918. (defcustom org-table-formula-evaluate-inline t
  919. "Non-nil means, TAB and RET evaluate a formula in current table field.
  920. If the current field starts with an equal sign, it is assumed to be a formula
  921. which should be evaluated as described in the manual and in the documentation
  922. string of the command `org-table-eval-formula'. This feature requires the
  923. Emacs calc package.
  924. When this variable is nil, formula calculation is only available through
  925. the command \\[org-table-eval-formula]."
  926. :group 'org-table-calculation
  927. :type 'boolean)
  928. (defcustom org-table-formula-use-constants t
  929. "Non-nil means, interpret constants in formulas in tables.
  930. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  931. by the value given in `org-table-formula-constants', or by a value obtained
  932. from the `constants.el' package."
  933. :group 'org-table-calculation
  934. :type 'boolean)
  935. (defcustom org-table-formula-constants nil
  936. "Alist with constant names and values, for use in table formulas.
  937. The car of each element is a name of a constant, without the `$' before it.
  938. The cdr is the value as a string. For example, if you'd like to use the
  939. speed of light in a formula, you would configure
  940. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  941. and then use it in an equation like `$1*$c'.
  942. Constants can also be defined on a per-file basis using a line like
  943. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  944. :group 'org-table-calculation
  945. :type '(repeat
  946. (cons (string :tag "name")
  947. (string :tag "value"))))
  948. (defvar org-table-formula-constants-local nil
  949. "Local version of `org-table-formula-constants'.")
  950. (make-variable-buffer-local 'org-table-formula-constants-local)
  951. (defcustom org-table-allow-automatic-line-recalculation t
  952. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  953. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  954. :group 'org-table-calculation
  955. :type 'boolean)
  956. (defgroup org-link nil
  957. "Options concerning links in Org-mode."
  958. :tag "Org Link"
  959. :group 'org)
  960. (defvar org-link-abbrev-alist-local nil
  961. "Buffer-local version of `org-link-abbrev-alist', which see.
  962. The value of this is taken from the #+LINK lines.")
  963. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  964. (defcustom org-link-abbrev-alist nil
  965. "Alist of link abbreviations.
  966. The car of each element is a string, to be replaced at the start of a link.
  967. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  968. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  969. [[linkkey:tag][description]]
  970. If REPLACE is a string, the tag will simply be appended to create the link.
  971. If the string contains \"%s\", the tag will be inserted there.
  972. REPLACE may also be a function that will be called with the tag as the
  973. only argument to create the link, which should be returned as a string.
  974. See the manual for examples."
  975. :group 'org-link
  976. :type 'alist)
  977. (defcustom org-descriptive-links t
  978. "Non-nil means, hide link part and only show description of bracket links.
  979. Bracket links are like [[link][descritpion]]. This variable sets the initial
  980. state in new org-mode buffers. The setting can then be toggled on a
  981. per-buffer basis from the Org->Hyperlinks menu."
  982. :group 'org-link
  983. :type 'boolean)
  984. (defcustom org-link-file-path-type 'adaptive
  985. "How the path name in file links should be stored.
  986. Valid values are:
  987. relative Relative to the current directory, i.e. the directory of the file
  988. into which the link is being inserted.
  989. absolute Absolute path, if possible with ~ for home directory.
  990. noabbrev Absolute path, no abbreviation of home directory.
  991. adaptive Use relative path for files in the current directory and sub-
  992. directories of it. For other files, use an absolute path."
  993. :group 'org-link
  994. :type '(choice
  995. (const relative)
  996. (const absolute)
  997. (const noabbrev)
  998. (const adaptive)))
  999. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1000. "Types of links that should be activated in Org-mode files.
  1001. This is a list of symbols, each leading to the activation of a certain link
  1002. type. In principle, it does not hurt to turn on most link types - there may
  1003. be a small gain when turning off unused link types. The types are:
  1004. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1005. angular Links in angular brackes that may contain whitespace like
  1006. <bbdb:Carsten Dominik>.
  1007. plain Plain links in normal text, no whitespace, like http://google.com.
  1008. radio Text that is matched by a radio target, see manual for details.
  1009. tag Tag settings in a headline (link to tag search).
  1010. date Time stamps (link to calendar).
  1011. Changing this variable requires a restart of Emacs to become effective."
  1012. :group 'org-link
  1013. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1014. (const :tag "Angular bracket links (old style)" angular)
  1015. (const :tag "plain text links" plain)
  1016. (const :tag "Radio target matches" radio)
  1017. (const :tag "Tags" tag)
  1018. (const :tag "Tags" target)
  1019. (const :tag "Timestamps" date)))
  1020. (defgroup org-link-store nil
  1021. "Options concerning storing links in Org-mode"
  1022. :tag "Org Store Link"
  1023. :group 'org-link)
  1024. (defcustom org-email-link-description-format "Email %c: %.30s"
  1025. "Format of the description part of a link to an email or usenet message.
  1026. The following %-excapes will be replaced by corresponding information:
  1027. %F full \"From\" field
  1028. %f name, taken from \"From\" field, address if no name
  1029. %T full \"To\" field
  1030. %t first name in \"To\" field, address if no name
  1031. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1032. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1033. %s subject
  1034. %m message-id.
  1035. You may use normal field width specification between the % and the letter.
  1036. This is for example useful to limit the length of the subject.
  1037. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1038. :group 'org-link-store
  1039. :type 'string)
  1040. (defcustom org-from-is-user-regexp
  1041. (let (r1 r2)
  1042. (when (and user-mail-address (not (string= user-mail-address "")))
  1043. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1044. (when (and user-full-name (not (string= user-full-name "")))
  1045. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1046. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1047. "Regexp mached against the \"From:\" header of an email or usenet message.
  1048. It should match if the message is from the user him/herself."
  1049. :group 'org-link-store
  1050. :type 'regexp)
  1051. (defcustom org-context-in-file-links t
  1052. "Non-nil means, file links from `org-store-link' contain context.
  1053. A search string will be added to the file name with :: as separator and
  1054. used to find the context when the link is activated by the command
  1055. `org-open-at-point'.
  1056. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1057. negates this setting for the duration of the command."
  1058. :group 'org-link-store
  1059. :type 'boolean)
  1060. (defcustom org-keep-stored-link-after-insertion nil
  1061. "Non-nil means, keep link in list for entire session.
  1062. The command `org-store-link' adds a link pointing to the current
  1063. location to an internal list. These links accumulate during a session.
  1064. The command `org-insert-link' can be used to insert links into any
  1065. Org-mode file (offering completion for all stored links). When this
  1066. option is nil, every link which has been inserted once using \\[org-insert-link]
  1067. will be removed from the list, to make completing the unused links
  1068. more efficient."
  1069. :group 'org-link-store
  1070. :type 'boolean)
  1071. (defcustom org-usenet-links-prefer-google nil
  1072. "Non-nil means, `org-store-link' will create web links to Google groups.
  1073. When nil, Gnus will be used for such links.
  1074. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1075. negates this setting for the duration of the command."
  1076. :group 'org-link-store
  1077. :type 'boolean)
  1078. (defgroup org-link-follow nil
  1079. "Options concerning following links in Org-mode"
  1080. :tag "Org Follow Link"
  1081. :group 'org-link)
  1082. (defcustom org-tab-follows-link nil
  1083. "Non-nil means, on links TAB will follow the link.
  1084. Needs to be set before org.el is loaded."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defcustom org-return-follows-link nil
  1088. "Non-nil means, on links RET will follow the link.
  1089. Needs to be set before org.el is loaded."
  1090. :group 'org-link-follow
  1091. :type 'boolean)
  1092. (defcustom org-mouse-1-follows-link t
  1093. "Non-nil means, mouse-1 on a link will follow the link.
  1094. A longer mouse click will still set point. Does not wortk on XEmacs.
  1095. Needs to be set before org.el is loaded."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-mark-ring-length 4
  1099. "Number of different positions to be recorded in the ring
  1100. Changing this requires a restart of Emacs to work correctly."
  1101. :group 'org-link-follow
  1102. :type 'interger)
  1103. (defcustom org-link-frame-setup
  1104. '((vm . vm-visit-folder-other-frame)
  1105. (gnus . gnus-other-frame)
  1106. (file . find-file-other-window))
  1107. "Setup the frame configuration for following links.
  1108. When following a link with Emacs, it may often be useful to display
  1109. this link in another window or frame. This variable can be used to
  1110. set this up for the different types of links.
  1111. For VM, use any of
  1112. `vm-visit-folder'
  1113. `vm-visit-folder-other-frame'
  1114. For Gnus, use any of
  1115. `gnus'
  1116. `gnus-other-frame'
  1117. For FILE, use any of
  1118. `find-file'
  1119. `find-file-other-window'
  1120. `find-file-other-frame'
  1121. For the calendar, use the variable `calendar-setup'.
  1122. For BBDB, it is currently only possible to display the matches in
  1123. another window."
  1124. :group 'org-link-follow
  1125. :type '(list
  1126. (cons (const vm)
  1127. (choice
  1128. (const vm-visit-folder)
  1129. (const vm-visit-folder-other-window)
  1130. (const vm-visit-folder-other-frame)))
  1131. (cons (const gnus)
  1132. (choice
  1133. (const gnus)
  1134. (const gnus-other-frame)))
  1135. (cons (const file)
  1136. (choice
  1137. (const find-file)
  1138. (const find-file-other-window)
  1139. (const find-file-other-frame)))))
  1140. (defcustom org-display-internal-link-with-indirect-buffer nil
  1141. "Non-nil means, use indirect buffer to display infile links.
  1142. Activating internal links (from one location in a file to another location
  1143. in the same file) normally just jumps to the location. When the link is
  1144. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1145. another window. When this option is set, the other window actually displays
  1146. an indirect buffer clone of the current buffer, to avoid any visibility
  1147. changes to the current buffer."
  1148. :group 'org-link-follow
  1149. :type 'boolean)
  1150. (defcustom org-open-non-existing-files nil
  1151. "Non-nil means, `org-open-file' will open non-existing files.
  1152. When nil, an error will be generated."
  1153. :group 'org-link-follow
  1154. :type 'boolean)
  1155. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1156. "Function and arguments to call for following mailto links.
  1157. This is a list with the first element being a lisp function, and the
  1158. remaining elements being arguments to the function. In string arguments,
  1159. %a will be replaced by the address, and %s will be replaced by the subject
  1160. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1161. :group 'org-link-follow
  1162. :type '(choice
  1163. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1164. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1165. (const :tag "message-mail" (message-mail "%a" "%s"))
  1166. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1167. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1168. "Non-nil means, ask for confirmation before executing shell links.
  1169. Shell links can be dangerous: just think about a link
  1170. [[shell:rm -rf ~/*][Google Search]]
  1171. This link would show up in your Org-mode document as \"Google Search\",
  1172. but really it would remove your entire home directory.
  1173. Therefore we advise against setting this variable to nil.
  1174. Just change it to `y-or-n-p' of you want to confirm with a
  1175. single keystroke rather than having to type \"yes\"."
  1176. :group 'org-link-follow
  1177. :type '(choice
  1178. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1179. (const :tag "with y-or-n (faster)" y-or-n-p)
  1180. (const :tag "no confirmation (dangerous)" nil)))
  1181. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1182. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1183. Elisp links can be dangerous: just think about a link
  1184. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1185. This link would show up in your Org-mode document as \"Google Search\",
  1186. but really it would remove your entire home directory.
  1187. Therefore we advise against setting this variable to nil.
  1188. Just change it to `y-or-n-p' of you want to confirm with a
  1189. single keystroke rather than having to type \"yes\"."
  1190. :group 'org-link-follow
  1191. :type '(choice
  1192. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1193. (const :tag "with y-or-n (faster)" y-or-n-p)
  1194. (const :tag "no confirmation (dangerous)" nil)))
  1195. (defconst org-file-apps-defaults-gnu
  1196. '((remote . emacs)
  1197. (t . mailcap))
  1198. "Default file applications on a UNIX or GNU/Linux system.
  1199. See `org-file-apps'.")
  1200. (defconst org-file-apps-defaults-macosx
  1201. '((remote . emacs)
  1202. (t . "open %s")
  1203. ("ps" . "gv %s")
  1204. ("ps.gz" . "gv %s")
  1205. ("eps" . "gv %s")
  1206. ("eps.gz" . "gv %s")
  1207. ("dvi" . "xdvi %s")
  1208. ("fig" . "xfig %s"))
  1209. "Default file applications on a MacOS X system.
  1210. The system \"open\" is known as a default, but we use X11 applications
  1211. for some files for which the OS does not have a good default.
  1212. See `org-file-apps'.")
  1213. (defconst org-file-apps-defaults-windowsnt
  1214. (list
  1215. '(remote . emacs)
  1216. (cons t
  1217. (list (if (featurep 'xemacs)
  1218. 'mswindows-shell-execute
  1219. 'w32-shell-execute)
  1220. "open" 'file)))
  1221. "Default file applications on a Windows NT system.
  1222. The system \"open\" is used for most files.
  1223. See `org-file-apps'.")
  1224. (defcustom org-file-apps
  1225. '(
  1226. ("txt" . emacs)
  1227. ("tex" . emacs)
  1228. ("ltx" . emacs)
  1229. ("org" . emacs)
  1230. ("el" . emacs)
  1231. ("bib" . emacs)
  1232. )
  1233. "External applications for opening `file:path' items in a document.
  1234. Org-mode uses system defaults for different file types, but
  1235. you can use this variable to set the application for a given file
  1236. extension. The entries in this list are cons cells where the car identifies
  1237. files and the cdr the corresponding command. Possible values for the
  1238. file identifier are
  1239. \"ext\" A string identifying an extension
  1240. `directory' Matches a directory
  1241. `remote' Matches a remote file, accessible through tramp or efs.
  1242. Remote files most likely should be visited through Emacs
  1243. because external applications cannot handle such paths.
  1244. t Default for all remaining files
  1245. Possible values for the command are:
  1246. `emacs' The file will be visited by the current Emacs process.
  1247. `default' Use the default application for this file type.
  1248. string A command to be executed by a shell; %s will be replaced
  1249. by the path to the file.
  1250. sexp A Lisp form which will be evaluated. The file path will
  1251. be available in the Lisp variable `file'.
  1252. For more examples, see the system specific constants
  1253. `org-file-apps-defaults-macosx'
  1254. `org-file-apps-defaults-windowsnt'
  1255. `org-file-apps-defaults-gnu'."
  1256. :group 'org-link-follow
  1257. :type '(repeat
  1258. (cons (choice :value ""
  1259. (string :tag "Extension")
  1260. (const :tag "Default for unrecognized files" t)
  1261. (const :tag "Remote file" remote)
  1262. (const :tag "Links to a directory" directory))
  1263. (choice :value ""
  1264. (const :tag "Visit with Emacs" emacs)
  1265. (const :tag "Use system default" default)
  1266. (string :tag "Command")
  1267. (sexp :tag "Lisp form")))))
  1268. (defcustom org-mhe-search-all-folders nil
  1269. "Non-nil means, that the search for the mh-message will be extended to
  1270. all folders if the message cannot be found in the folder given in the link.
  1271. Searching all folders is very efficient with one of the search engines
  1272. supported by MH-E, but will be slow with pick."
  1273. :group 'org-link-follow
  1274. :type 'boolean)
  1275. (defgroup org-remember nil
  1276. "Options concerning interaction with remember.el."
  1277. :tag "Org Remember"
  1278. :group 'org)
  1279. (defcustom org-directory "~/org"
  1280. "Directory with org files.
  1281. This directory will be used as default to prompt for org files.
  1282. Used by the hooks for remember.el."
  1283. :group 'org-remember
  1284. :type 'directory)
  1285. (defcustom org-default-notes-file "~/.notes"
  1286. "Default target for storing notes.
  1287. Used by the hooks for remember.el. This can be a string, or nil to mean
  1288. the value of `remember-data-file'.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type '(choice
  1293. (const :tag "Default from remember-data-file" nil)
  1294. file))
  1295. (defcustom org-remember-store-without-prompt t
  1296. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1297. In this case, you need `C-u C-c C-c' to get the prompts for
  1298. note file and headline.
  1299. When this variable is nil, `C-c C-c' give you the prompts, and
  1300. `C-u C-c C-c' trigger the fasttrack."
  1301. :group 'org-remember
  1302. :type 'boolean)
  1303. (defcustom org-remember-interactive-interface 'refile
  1304. "The interface to be used for interactive filing of remember notes.
  1305. This is only used when the interactive mode for selecting a filing
  1306. location is used (see the variable `org-remember-store-without-prompt').
  1307. Allowed vaues are:
  1308. outline The interface shows an outline of the relevant file
  1309. and the correct heading is found by moving through
  1310. the outline or by searching with incremental search.
  1311. outline-path-completion Headlines in the current buffer are offered via
  1312. completion.
  1313. refile Use the refile interface, and offer headlines,
  1314. possibly from different buffers."
  1315. :group 'org-remember
  1316. :type '(choice
  1317. (const :tag "Refile" refile)
  1318. (const :tag "Outline" outline)
  1319. (const :tag "Outline-path-completion" outline-path-completion)))
  1320. (defcustom org-goto-interface 'outline
  1321. "The default interface to be used for `org-goto'.
  1322. Allowed vaues are:
  1323. outline The interface shows an outline of the relevant file
  1324. and the correct heading is found by moving through
  1325. the outline or by searching with incremental search.
  1326. outline-path-completion Headlines in the current buffer are offered via
  1327. completion."
  1328. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1329. :type '(choice
  1330. (const :tag "Outline" outline)
  1331. (const :tag "Outline-path-completion" outline-path-completion)))
  1332. (defcustom org-remember-default-headline ""
  1333. "The headline that should be the default location in the notes file.
  1334. When filing remember notes, the cursor will start at that position.
  1335. You can set this on a per-template basis with the variable
  1336. `org-remember-templates'."
  1337. :group 'org-remember
  1338. :type 'string)
  1339. (defcustom org-remember-templates nil
  1340. "Templates for the creation of remember buffers.
  1341. When nil, just let remember make the buffer.
  1342. When not nil, this is a list of 5-element lists. In each entry, the first
  1343. element is the name of the template, which should be a single short word.
  1344. The second element is a character, a unique key to select this template.
  1345. The third element is the template. The fourth element is optional and can
  1346. specify a destination file for remember items created with this template.
  1347. The default file is given by `org-default-notes-file'. An optional fifth
  1348. element can specify the headline in that file that should be offered
  1349. first when the user is asked to file the entry. The default headline is
  1350. given in the variable `org-remember-default-headline'.
  1351. The template specifies the structure of the remember buffer. It should have
  1352. a first line starting with a star, to act as the org-mode headline.
  1353. Furthermore, the following %-escapes will be replaced with content:
  1354. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1355. A default value and a completion table ca be specified like this:
  1356. %^{prompt|default|completion2|completion3|...}
  1357. %t time stamp, date only
  1358. %T time stamp with date and time
  1359. %u, %U like the above, but inactive time stamps
  1360. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1361. You may define a prompt like %^{Please specify birthday}t
  1362. %n user name (taken from `user-full-name')
  1363. %a annotation, normally the link created with org-store-link
  1364. %i initial content, the region active. If %i is indented,
  1365. the entire inserted text will be indented as well.
  1366. %c content of the clipboard, or current kill ring head
  1367. %^g prompt for tags, with completion on tags in target file
  1368. %^G prompt for tags, with completion all tags in all agenda files
  1369. %:keyword specific information for certain link types, see below
  1370. %[pathname] insert the contents of the file given by `pathname'
  1371. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1372. %! Store this note immediately after filling the template
  1373. %? After completing the template, position cursor here.
  1374. Apart from these general escapes, you can access information specific to the
  1375. link type that is created. For example, calling `remember' in emails or gnus
  1376. will record the author and the subject of the message, which you can access
  1377. with %:author and %:subject, respectively. Here is a complete list of what
  1378. is recorded for each link type.
  1379. Link type | Available information
  1380. -------------------+------------------------------------------------------
  1381. bbdb | %:type %:name %:company
  1382. vm, wl, mh, rmail | %:type %:subject %:message-id
  1383. | %:from %:fromname %:fromaddress
  1384. | %:to %:toname %:toaddress
  1385. | %:fromto (either \"to NAME\" or \"from NAME\")
  1386. gnus | %:group, for messages also all email fields
  1387. w3, w3m | %:type %:url
  1388. info | %:type %:file %:node
  1389. calendar | %:type %:date"
  1390. :group 'org-remember
  1391. :get (lambda (var) ; Make sure all entries have 5 elements
  1392. (mapcar (lambda (x)
  1393. (if (not (stringp (car x))) (setq x (cons "" x)))
  1394. (cond ((= (length x) 4) (append x '("")))
  1395. ((= (length x) 3) (append x '("" "")))
  1396. (t x)))
  1397. (default-value var)))
  1398. :type '(repeat
  1399. :tag "enabled"
  1400. (list :value ("" ?a "\n" nil nil)
  1401. (string :tag "Name")
  1402. (character :tag "Selection Key")
  1403. (string :tag "Template")
  1404. (choice
  1405. (file :tag "Destination file")
  1406. (const :tag "Prompt for file" nil))
  1407. (choice
  1408. (string :tag "Destination headline")
  1409. (const :tag "Selection interface for heading")))))
  1410. (defcustom org-reverse-note-order nil
  1411. "Non-nil means, store new notes at the beginning of a file or entry.
  1412. When nil, new notes will be filed to the end of a file or entry.
  1413. This can also be a list with cons cells of regular expressions that
  1414. are matched against file names, and values."
  1415. :group 'org-remember
  1416. :type '(choice
  1417. (const :tag "Reverse always" t)
  1418. (const :tag "Reverse never" nil)
  1419. (repeat :tag "By file name regexp"
  1420. (cons regexp boolean))))
  1421. (defcustom org-refile-targets nil
  1422. "Targets for refiling entries with \\[org-refile].
  1423. This is list of cons cells. Each cell contains:
  1424. - a specification of the files to be considered, either a list of files,
  1425. or a symbol whose function or value fields will be used to retrieve
  1426. a file name or a list of file names. Nil means, refile to a different
  1427. heading in the current buffer.
  1428. - A specification of how to find candidate refile targets. This may be
  1429. any of
  1430. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1431. This tag has to be present in all target headlines, inheritance will
  1432. not be considered.
  1433. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1434. todo keyword.
  1435. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1436. headlines that are refiling targets.
  1437. - a cons cell (:level . N). Any headline of level N is considered a target.
  1438. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1439. ;; FIXME: what if there are a var and func with same name???
  1440. :group 'org-remember
  1441. :type '(repeat
  1442. (cons
  1443. (choice :value org-agenda-files
  1444. (const :tag "All agenda files" org-agenda-files)
  1445. (const :tag "Current buffer" nil)
  1446. (function) (variable) (file))
  1447. (choice :tag "Identify target headline by"
  1448. (cons :tag "Specific tag" (const :tag) (string))
  1449. (cons :tag "TODO keyword" (const :todo) (string))
  1450. (cons :tag "Regular expression" (const :regexp) (regexp))
  1451. (cons :tag "Level number" (const :level) (integer))
  1452. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1453. (defcustom org-refile-use-outline-path nil
  1454. "Non-nil means, provide refile targets as paths.
  1455. So a level 3 headline will be available as level1/level2/level3.
  1456. When the value is `file', also include the file name (without directory)
  1457. into the path. When `full-file-path', include the full file path."
  1458. :group 'org-remember
  1459. :type '(choice
  1460. (const :tag "Not" nil)
  1461. (const :tag "Yes" t)
  1462. (const :tag "Start with file name" file)
  1463. (const :tag "Start with full file path" full-file-path)))
  1464. (defgroup org-todo nil
  1465. "Options concerning TODO items in Org-mode."
  1466. :tag "Org TODO"
  1467. :group 'org)
  1468. (defgroup org-progress nil
  1469. "Options concerning Progress logging in Org-mode."
  1470. :tag "Org Progress"
  1471. :group 'org-time)
  1472. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1473. "List of TODO entry keyword sequences and their interpretation.
  1474. \\<org-mode-map>This is a list of sequences.
  1475. Each sequence starts with a symbol, either `sequence' or `type',
  1476. indicating if the keywords should be interpreted as a sequence of
  1477. action steps, or as different types of TODO items. The first
  1478. keywords are states requiring action - these states will select a headline
  1479. for inclusion into the global TODO list Org-mode produces. If one of
  1480. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1481. signify that no further action is necessary. If \"|\" is not found,
  1482. the last keyword is treated as the only DONE state of the sequence.
  1483. The command \\[org-todo] cycles an entry through these states, and one
  1484. additional state where no keyword is present. For details about this
  1485. cycling, see the manual.
  1486. TODO keywords and interpretation can also be set on a per-file basis with
  1487. the special #+SEQ_TODO and #+TYP_TODO lines.
  1488. Each keyword can optionally specify a character for fast state selection
  1489. \(in combination with the variable `org-use-fast-todo-selection')
  1490. and specifiers for state change logging, using the same syntax
  1491. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1492. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1493. indicates to record a time stamp each time this state is selected.
  1494. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1495. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1496. entering and when leaving this state.
  1497. For backward compatibility, this variable may also be just a list
  1498. of keywords - in this case the interptetation (sequence or type) will be
  1499. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1500. :group 'org-todo
  1501. :group 'org-keywords
  1502. :type '(choice
  1503. (repeat :tag "Old syntax, just keywords"
  1504. (string :tag "Keyword"))
  1505. (repeat :tag "New syntax"
  1506. (cons
  1507. (choice
  1508. :tag "Interpretation"
  1509. (const :tag "Sequence (cycling hits every state)" sequence)
  1510. (const :tag "Type (cycling directly to DONE)" type))
  1511. (repeat
  1512. (string :tag "Keyword"))))))
  1513. (defvar org-todo-keywords-1 nil
  1514. "All TODO and DONE keywords active in a buffer.")
  1515. (make-variable-buffer-local 'org-todo-keywords-1)
  1516. (defvar org-todo-keywords-for-agenda nil)
  1517. (defvar org-done-keywords-for-agenda nil)
  1518. (defvar org-not-done-keywords nil)
  1519. (make-variable-buffer-local 'org-not-done-keywords)
  1520. (defvar org-done-keywords nil)
  1521. (make-variable-buffer-local 'org-done-keywords)
  1522. (defvar org-todo-heads nil)
  1523. (make-variable-buffer-local 'org-todo-heads)
  1524. (defvar org-todo-sets nil)
  1525. (make-variable-buffer-local 'org-todo-sets)
  1526. (defvar org-todo-log-states nil)
  1527. (make-variable-buffer-local 'org-todo-log-states)
  1528. (defvar org-todo-kwd-alist nil)
  1529. (make-variable-buffer-local 'org-todo-kwd-alist)
  1530. (defvar org-todo-key-alist nil)
  1531. (make-variable-buffer-local 'org-todo-key-alist)
  1532. (defvar org-todo-key-trigger nil)
  1533. (make-variable-buffer-local 'org-todo-key-trigger)
  1534. (defcustom org-todo-interpretation 'sequence
  1535. "Controls how TODO keywords are interpreted.
  1536. This variable is in principle obsolete and is only used for
  1537. backward compatibility, if the interpretation of todo keywords is
  1538. not given already in `org-todo-keywords'. See that variable for
  1539. more information."
  1540. :group 'org-todo
  1541. :group 'org-keywords
  1542. :type '(choice (const sequence)
  1543. (const type)))
  1544. (defcustom org-use-fast-todo-selection 'prefix
  1545. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1546. This variable describes if and under what circumstances the cycling
  1547. mechanism for TODO keywords will be replaced by a single-key, direct
  1548. selection scheme.
  1549. When nil, fast selection is never used.
  1550. When the symbol `prefix', it will be used when `org-todo' is called with
  1551. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1552. in an agenda buffer.
  1553. When t, fast selection is used by default. In this case, the prefix
  1554. argument forces cycling instead.
  1555. In all cases, the special interface is only used if access keys have actually
  1556. been assigned by the user, i.e. if keywords in the configuration are followed
  1557. by a letter in parenthesis, like TODO(t)."
  1558. :group 'org-todo
  1559. :type '(choice
  1560. (const :tag "Never" nil)
  1561. (const :tag "By default" t)
  1562. (const :tag "Only with C-u C-c C-t" prefix)))
  1563. (defcustom org-after-todo-state-change-hook nil
  1564. "Hook which is run after the state of a TODO item was changed.
  1565. The new state (a string with a TODO keyword, or nil) is available in the
  1566. Lisp variable `state'."
  1567. :group 'org-todo
  1568. :type 'hook)
  1569. (defcustom org-log-done nil
  1570. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1571. When equal to the list (done), also prompt for a closing note.
  1572. This can also be configured on a per-file basis by adding one of
  1573. the following lines anywhere in the buffer:
  1574. #+STARTUP: logdone
  1575. #+STARTUP: lognotedone
  1576. #+STARTUP: nologdone"
  1577. :group 'org-todo
  1578. :group 'org-progress
  1579. :type '(choice
  1580. (const :tag "No logging" nil)
  1581. (const :tag "Record CLOSED timestamp" time)
  1582. (const :tag "Record CLOSED timestamp with closing note." note)))
  1583. ;; Normalize old uses of org-log-done.
  1584. (cond
  1585. ((eq org-log-done t) (setq org-log-done 'time))
  1586. ((and (listp org-log-done) (memq 'done org-log-done))
  1587. (setq org-log-done 'note)))
  1588. ;; FIXME: document
  1589. (defcustom org-log-note-clock-out nil
  1590. "Non-nil means, recored a note when clocking out of an item.
  1591. This can also be configured on a per-file basis by adding one of
  1592. the following lines anywhere in the buffer:
  1593. #+STARTUP: lognoteclock-out
  1594. #+STARTUP: nolognoteclock-out"
  1595. :group 'org-todo
  1596. :group 'org-progress
  1597. :type 'boolean)
  1598. (defcustom org-log-done-with-time t
  1599. "Non-nil means, the CLOSED time stamp will contain date and time.
  1600. When nil, only the date will be recorded."
  1601. :group 'org-progress
  1602. :type 'boolean)
  1603. (defcustom org-log-note-headings
  1604. '((done . "CLOSING NOTE %t")
  1605. (state . "State %-12s %t")
  1606. (clock-out . ""))
  1607. "Headings for notes added when clocking out or closing TODO items.
  1608. The value is an alist, with the car being a symbol indicating the note
  1609. context, and the cdr is the heading to be used. The heading may also be the
  1610. empty string.
  1611. %t in the heading will be replaced by a time stamp.
  1612. %s will be replaced by the new TODO state, in double quotes.
  1613. %u will be replaced by the user name.
  1614. %U will be replaced by the full user name."
  1615. :group 'org-todo
  1616. :group 'org-progress
  1617. :type '(list :greedy t
  1618. (cons (const :tag "Heading when closing an item" done) string)
  1619. (cons (const :tag
  1620. "Heading when changing todo state (todo sequence only)"
  1621. state) string)
  1622. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1623. (defcustom org-log-states-order-reversed t
  1624. "Non-nil means, the latest state change note will be directly after heading.
  1625. When nil, the notes will be orderer according to time."
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type 'boolean)
  1629. (defcustom org-log-repeat 'time
  1630. "Non-nil means, record moving through the DONE state when triggering repeat.
  1631. An auto-repeating tasks is immediately switched back to TODO when marked
  1632. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1633. the TODO keyword definition, or recording a cloing note by setting
  1634. `org-log-done', there will be no record of the task moving trhough DONE.
  1635. This variable forces taking a note anyway. Possible values are:
  1636. nil Don't force a record
  1637. time Record a time stamp
  1638. note Record a note
  1639. This option can also be set with on a per-file-basis with
  1640. #+STARTUP: logrepeat
  1641. #+STARTUP: lognoterepeat
  1642. #+STARTUP: nologrepeat
  1643. You can have local logging settings for a subtree by setting the LOGGING
  1644. property to one or more of these keywords."
  1645. :group 'org-todo
  1646. :group 'org-progress
  1647. :type '(choice
  1648. (const :tag "Don't force a record" nil)
  1649. (const :tag "Force recording the DONE state" time)
  1650. (const :tag "Force recording a note with the DONE state" note)))
  1651. (defcustom org-clock-into-drawer 2
  1652. "Should clocking info be wrapped into a drawer?
  1653. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1654. If necessary, the drawer will be created.
  1655. When nil, the drawer will not be created, but used when present.
  1656. When an integer and the number of clocking entries in an item
  1657. reaches or exceeds this number, a drawer will be created."
  1658. :group 'org-todo
  1659. :group 'org-progress
  1660. :type '(choice
  1661. (const :tag "Always" t)
  1662. (const :tag "Only when drawer exists" nil)
  1663. (integer :tag "When at least N clock entries")))
  1664. (defcustom org-clock-out-when-done t
  1665. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1666. Nil means, clock will keep running until stopped explicitly with
  1667. `C-c C-x C-o', or until the clock is started in a different item."
  1668. :group 'org-progress
  1669. :type 'boolean)
  1670. (defcustom org-clock-in-switch-to-state nil
  1671. "Set task to a special todo state while clocking it.
  1672. The value should be the state to which the entry should be switched."
  1673. :group 'org-progress
  1674. :group 'org-todo
  1675. :type '(choice
  1676. (const :tag "Don't force a state" nil)
  1677. (string :tag "State")))
  1678. (defgroup org-priorities nil
  1679. "Priorities in Org-mode."
  1680. :tag "Org Priorities"
  1681. :group 'org-todo)
  1682. (defcustom org-highest-priority ?A
  1683. "The highest priority of TODO items. A character like ?A, ?B etc.
  1684. Must have a smaller ASCII number than `org-lowest-priority'."
  1685. :group 'org-priorities
  1686. :type 'character)
  1687. (defcustom org-lowest-priority ?C
  1688. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1689. Must have a larger ASCII number than `org-highest-priority'."
  1690. :group 'org-priorities
  1691. :type 'character)
  1692. (defcustom org-default-priority ?B
  1693. "The default priority of TODO items.
  1694. This is the priority an item get if no explicit priority is given."
  1695. :group 'org-priorities
  1696. :type 'character)
  1697. (defcustom org-priority-start-cycle-with-default t
  1698. "Non-nil means, start with default priority when starting to cycle.
  1699. When this is nil, the first step in the cycle will be (depending on the
  1700. command used) one higher or lower that the default priority."
  1701. :group 'org-priorities
  1702. :type 'boolean)
  1703. (defgroup org-time nil
  1704. "Options concerning time stamps and deadlines in Org-mode."
  1705. :tag "Org Time"
  1706. :group 'org)
  1707. (defcustom org-insert-labeled-timestamps-at-point nil
  1708. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1709. When nil, these labeled time stamps are forces into the second line of an
  1710. entry, just after the headline. When scheduling from the global TODO list,
  1711. the time stamp will always be forced into the second line."
  1712. :group 'org-time
  1713. :type 'boolean)
  1714. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1715. "Formats for `format-time-string' which are used for time stamps.
  1716. It is not recommended to change this constant.")
  1717. (defcustom org-time-stamp-rounding-minutes 0
  1718. "Number of minutes to round time stamps to upon insertion.
  1719. When zero, insert the time unmodified. Useful rounding numbers
  1720. should be factors of 60, so for example 5, 10, 15.
  1721. When this is not zero, you can still force an exact time-stamp by using
  1722. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1723. :group 'org-time
  1724. :type 'integer)
  1725. (defcustom org-display-custom-times nil
  1726. "Non-nil means, overlay custom formats over all time stamps.
  1727. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1728. To turn this on on a per-file basis, insert anywhere in the file:
  1729. #+STARTUP: customtime"
  1730. :group 'org-time
  1731. :set 'set-default
  1732. :type 'sexp)
  1733. (make-variable-buffer-local 'org-display-custom-times)
  1734. (defcustom org-time-stamp-custom-formats
  1735. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1736. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1737. These are overlayed over the default ISO format if the variable
  1738. `org-display-custom-times' is set. Time like %H:%M should be at the
  1739. end of the second format."
  1740. :group 'org-time
  1741. :type 'sexp)
  1742. (defun org-time-stamp-format (&optional long inactive)
  1743. "Get the right format for a time string."
  1744. (let ((f (if long (cdr org-time-stamp-formats)
  1745. (car org-time-stamp-formats))))
  1746. (if inactive
  1747. (concat "[" (substring f 1 -1) "]")
  1748. f)))
  1749. (defcustom org-read-date-prefer-future t
  1750. "Non-nil means, assume future for incomplete date input from user.
  1751. This affects the following situations:
  1752. 1. The user gives a day, but no month.
  1753. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1754. read this as the third of *next* month. However, if you enter \"17\",
  1755. it will be considered as *this* month.
  1756. 2. The user gives a month but not a year.
  1757. For example, if it is april and you enter \"feb 2\", this will be read
  1758. as feb 2, *next* year. \"May 5\", however, will be this year.
  1759. When this option is nil, the current month and year will always be used
  1760. as defaults."
  1761. :group 'org-time
  1762. :type 'boolean)
  1763. (defcustom org-read-date-display-live t
  1764. "Non-nil means, display current interpretation of date prompt live.
  1765. This display will be in an overlay, in the minibuffer."
  1766. :group 'org-time
  1767. :type 'boolean)
  1768. (defcustom org-read-date-popup-calendar t
  1769. "Non-nil means, pop up a calendar when prompting for a date.
  1770. In the calendar, the date can be selected with mouse-1. However, the
  1771. minibuffer will also be active, and you can simply enter the date as well.
  1772. When nil, only the minibuffer will be available."
  1773. :group 'org-time
  1774. :type 'boolean)
  1775. (if (fboundp 'defvaralias)
  1776. (defvaralias 'org-popup-calendar-for-date-prompt
  1777. 'org-read-date-popup-calendar))
  1778. (defcustom org-extend-today-until 0
  1779. "The hour when your day really ends.
  1780. This has influence for the following applications:
  1781. - When switching the agenda to \"today\". It it is still earlier than
  1782. the time given here, the day recognized as TODAY is actually yesterday.
  1783. - When a date is read from the user and it is still before the time given
  1784. here, the current date and time will be assumed to be yesterday, 23:59.
  1785. FIXME:
  1786. IMPORTANT: This is still a very experimental feature, it may disappear
  1787. again or it may be extended to mean more things."
  1788. :group 'org-time
  1789. :type 'number)
  1790. (defcustom org-edit-timestamp-down-means-later nil
  1791. "Non-nil means, S-down will increase the time in a time stamp.
  1792. When nil, S-up will increase."
  1793. :group 'org-time
  1794. :type 'boolean)
  1795. (defcustom org-calendar-follow-timestamp-change t
  1796. "Non-nil means, make the calendar window follow timestamp changes.
  1797. When a timestamp is modified and the calendar window is visible, it will be
  1798. moved to the new date."
  1799. :group 'org-time
  1800. :type 'boolean)
  1801. (defcustom org-clock-heading-function nil
  1802. "When non-nil, should be a function to create `org-clock-heading'.
  1803. This is the string shown in the mode line when a clock is running.
  1804. The function is called with point at the beginning of the headline."
  1805. :group 'org-time ; FIXME: Should we have a separate group????
  1806. :type 'function)
  1807. (defgroup org-tags nil
  1808. "Options concerning tags in Org-mode."
  1809. :tag "Org Tags"
  1810. :group 'org)
  1811. (defcustom org-tag-alist nil
  1812. "List of tags allowed in Org-mode files.
  1813. When this list is nil, Org-mode will base TAG input on what is already in the
  1814. buffer.
  1815. The value of this variable is an alist, the car of each entry must be a
  1816. keyword as a string, the cdr may be a character that is used to select
  1817. that tag through the fast-tag-selection interface.
  1818. See the manual for details."
  1819. :group 'org-tags
  1820. :type '(repeat
  1821. (choice
  1822. (cons (string :tag "Tag name")
  1823. (character :tag "Access char"))
  1824. (const :tag "Start radio group" (:startgroup))
  1825. (const :tag "End radio group" (:endgroup)))))
  1826. (defcustom org-use-fast-tag-selection 'auto
  1827. "Non-nil means, use fast tag selection scheme.
  1828. This is a special interface to select and deselect tags with single keys.
  1829. When nil, fast selection is never used.
  1830. When the symbol `auto', fast selection is used if and only if selection
  1831. characters for tags have been configured, either through the variable
  1832. `org-tag-alist' or through a #+TAGS line in the buffer.
  1833. When t, fast selection is always used and selection keys are assigned
  1834. automatically if necessary."
  1835. :group 'org-tags
  1836. :type '(choice
  1837. (const :tag "Always" t)
  1838. (const :tag "Never" nil)
  1839. (const :tag "When selection characters are configured" 'auto)))
  1840. (defcustom org-fast-tag-selection-single-key nil
  1841. "Non-nil means, fast tag selection exits after first change.
  1842. When nil, you have to press RET to exit it.
  1843. During fast tag selection, you can toggle this flag with `C-c'.
  1844. This variable can also have the value `expert'. In this case, the window
  1845. displaying the tags menu is not even shown, until you press C-c again."
  1846. :group 'org-tags
  1847. :type '(choice
  1848. (const :tag "No" nil)
  1849. (const :tag "Yes" t)
  1850. (const :tag "Expert" expert)))
  1851. (defvar org-fast-tag-selection-include-todo nil
  1852. "Non-nil means, fast tags selection interface will also offer TODO states.
  1853. This is an undocumented feature, you should not rely on it.")
  1854. (defcustom org-tags-column -80
  1855. "The column to which tags should be indented in a headline.
  1856. If this number is positive, it specifies the column. If it is negative,
  1857. it means that the tags should be flushright to that column. For example,
  1858. -80 works well for a normal 80 character screen."
  1859. :group 'org-tags
  1860. :type 'integer)
  1861. (defcustom org-auto-align-tags t
  1862. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1863. These operations change the length of a headline and therefore shift
  1864. the tags around. With this options turned on, after each such operation
  1865. the tags are again aligned to `org-tags-column'."
  1866. :group 'org-tags
  1867. :type 'boolean)
  1868. (defcustom org-use-tag-inheritance t
  1869. "Non-nil means, tags in levels apply also for sublevels.
  1870. When nil, only the tags directly given in a specific line apply there.
  1871. If you turn off this option, you very likely want to turn on the
  1872. companion option `org-tags-match-list-sublevels'."
  1873. :group 'org-tags
  1874. :type 'boolean)
  1875. (defcustom org-tags-match-list-sublevels nil
  1876. "Non-nil means list also sublevels of headlines matching tag search.
  1877. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1878. the sublevels of a headline matching a tag search often also match
  1879. the same search. Listing all of them can create very long lists.
  1880. Setting this variable to nil causes subtrees of a match to be skipped.
  1881. This option is off by default, because inheritance in on. If you turn
  1882. inheritance off, you very likely want to turn this option on.
  1883. As a special case, if the tag search is restricted to TODO items, the
  1884. value of this variable is ignored and sublevels are always checked, to
  1885. make sure all corresponding TODO items find their way into the list."
  1886. :group 'org-tags
  1887. :type 'boolean)
  1888. (defvar org-tags-history nil
  1889. "History of minibuffer reads for tags.")
  1890. (defvar org-last-tags-completion-table nil
  1891. "The last used completion table for tags.")
  1892. (defvar org-after-tags-change-hook nil
  1893. "Hook that is run after the tags in a line have changed.")
  1894. (defgroup org-properties nil
  1895. "Options concerning properties in Org-mode."
  1896. :tag "Org Properties"
  1897. :group 'org)
  1898. (defcustom org-property-format "%-10s %s"
  1899. "How property key/value pairs should be formatted by `indent-line'.
  1900. When `indent-line' hits a property definition, it will format the line
  1901. according to this format, mainly to make sure that the values are
  1902. lined-up with respect to each other."
  1903. :group 'org-properties
  1904. :type 'string)
  1905. (defcustom org-use-property-inheritance nil
  1906. "Non-nil means, properties apply also for sublevels.
  1907. This setting is only relevant during property searches, not when querying
  1908. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1909. you need to call `org-entry-get' with the inheritance flag.
  1910. Turning this on can cause significant overhead when doing a search, so
  1911. this is turned off by default.
  1912. When nil, only the properties directly given in the current entry count.
  1913. The value may also be a list of properties that shouldhave inheritance.
  1914. However, note that some special properties use inheritance under special
  1915. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1916. and the properties ending in \"_ALL\" when they are used as descriptor
  1917. for valid values of a property."
  1918. :group 'org-properties
  1919. :type '(choice
  1920. (const :tag "Not" nil)
  1921. (const :tag "Always" nil)
  1922. (repeat :tag "Specific properties" (string :tag "Property"))))
  1923. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1924. "The default column format, if no other format has been defined.
  1925. This variable can be set on the per-file basis by inserting a line
  1926. #+COLUMNS: %25ITEM ....."
  1927. :group 'org-properties
  1928. :type 'string)
  1929. (defcustom org-global-properties nil
  1930. "List of property/value pairs that can be inherited by any entry.
  1931. You can set buffer-local values for this by adding lines like
  1932. #+PROPERTY: NAME VALUE"
  1933. :group 'org-properties
  1934. :type '(repeat
  1935. (cons (string :tag "Property")
  1936. (string :tag "Value"))))
  1937. (defvar org-local-properties nil
  1938. "List of property/value pairs that can be inherited by any entry.
  1939. Valid for the current buffer.
  1940. This variable is populated from #+PROPERTY lines.")
  1941. (defgroup org-agenda nil
  1942. "Options concerning agenda views in Org-mode."
  1943. :tag "Org Agenda"
  1944. :group 'org)
  1945. (defvar org-category nil
  1946. "Variable used by org files to set a category for agenda display.
  1947. Such files should use a file variable to set it, for example
  1948. # -*- mode: org; org-category: \"ELisp\"
  1949. or contain a special line
  1950. #+CATEGORY: ELisp
  1951. If the file does not specify a category, then file's base name
  1952. is used instead.")
  1953. (make-variable-buffer-local 'org-category)
  1954. (defcustom org-agenda-files nil
  1955. "The files to be used for agenda display.
  1956. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1957. \\[org-remove-file]. You can also use customize to edit the list.
  1958. If an entry is a directory, all files in that directory that are matched by
  1959. `org-agenda-file-regexp' will be part of the file list.
  1960. If the value of the variable is not a list but a single file name, then
  1961. the list of agenda files is actually stored and maintained in that file, one
  1962. agenda file per line."
  1963. :group 'org-agenda
  1964. :type '(choice
  1965. (repeat :tag "List of files and directories" file)
  1966. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1967. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1968. "Regular expression to match files for `org-agenda-files'.
  1969. If any element in the list in that variable contains a directory instead
  1970. of a normal file, all files in that directory that are matched by this
  1971. regular expression will be included."
  1972. :group 'org-agenda
  1973. :type 'regexp)
  1974. (defcustom org-agenda-skip-unavailable-files nil
  1975. "t means to just skip non-reachable files in `org-agenda-files'.
  1976. Nil means to remove them, after a query, from the list."
  1977. :group 'org-agenda
  1978. :type 'boolean)
  1979. (defcustom org-agenda-multi-occur-extra-files nil
  1980. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1981. The files in `org-agenda-files' are always searched."
  1982. :group 'org-agenda
  1983. :type '(repeat file))
  1984. (defcustom org-agenda-confirm-kill 1
  1985. "When set, remote killing from the agenda buffer needs confirmation.
  1986. When t, a confirmation is always needed. When a number N, confirmation is
  1987. only needed when the text to be killed contains more than N non-white lines."
  1988. :group 'org-agenda
  1989. :type '(choice
  1990. (const :tag "Never" nil)
  1991. (const :tag "Always" t)
  1992. (number :tag "When more than N lines")))
  1993. (defcustom org-calendar-to-agenda-key [?c]
  1994. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1995. The command `org-calendar-goto-agenda' will be bound to this key. The
  1996. default is the character `c' because then `c' can be used to switch back and
  1997. forth between agenda and calendar."
  1998. :group 'org-agenda
  1999. :type 'sexp)
  2000. (defcustom org-agenda-compact-blocks nil
  2001. "Non-nil means, make the block agenda more compact.
  2002. This is done by leaving out unnecessary lines."
  2003. :group 'org-agenda
  2004. :type nil)
  2005. (defgroup org-agenda-export nil
  2006. "Options concerning exporting agenda views in Org-mode."
  2007. :tag "Org Agenda Export"
  2008. :group 'org-agenda)
  2009. (defcustom org-agenda-with-colors t
  2010. "Non-nil means, use colors in agenda views."
  2011. :group 'org-agenda-export
  2012. :type 'boolean)
  2013. (defcustom org-agenda-exporter-settings nil
  2014. "Alist of variable/value pairs that should be active during agenda export.
  2015. This is a good place to set uptions for ps-print and for htmlize."
  2016. :group 'org-agenda-export
  2017. :type '(repeat
  2018. (list
  2019. (variable)
  2020. (sexp :tag "Value"))))
  2021. (defcustom org-agenda-export-html-style ""
  2022. "The style specification for exported HTML Agenda files.
  2023. If this variable contains a string, it will replace the default <style>
  2024. section as produced by `htmlize'.
  2025. Since there are different ways of setting style information, this variable
  2026. needs to contain the full HTML structure to provide a style, including the
  2027. surrounding HTML tags. The style specifications should include definitions
  2028. the fonts used by the agenda, here is an example:
  2029. <style type=\"text/css\">
  2030. p { font-weight: normal; color: gray; }
  2031. .org-agenda-structure {
  2032. font-size: 110%;
  2033. color: #003399;
  2034. font-weight: 600;
  2035. }
  2036. .org-todo {
  2037. color: #cc6666;Week-agenda:
  2038. font-weight: bold;
  2039. }
  2040. .org-done {
  2041. color: #339933;
  2042. }
  2043. .title { text-align: center; }
  2044. .todo, .deadline { color: red; }
  2045. .done { color: green; }
  2046. </style>
  2047. or, if you want to keep the style in a file,
  2048. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2049. As the value of this option simply gets inserted into the HTML <head> header,
  2050. you can \"misuse\" it to also add other text to the header. However,
  2051. <style>...</style> is required, if not present the variable will be ignored."
  2052. :group 'org-agenda-export
  2053. :group 'org-export-html
  2054. :type 'string)
  2055. (defgroup org-agenda-custom-commands nil
  2056. "Options concerning agenda views in Org-mode."
  2057. :tag "Org Agenda Custom Commands"
  2058. :group 'org-agenda)
  2059. (defcustom org-agenda-custom-commands nil
  2060. "Custom commands for the agenda.
  2061. These commands will be offered on the splash screen displayed by the
  2062. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2063. (key desc type match options files)
  2064. key The key (one or more characters as a string) to be associated
  2065. with the command.
  2066. desc A description of the commend, when omitted or nil, a default
  2067. description is built using MATCH.
  2068. type The command type, any of the following symbols:
  2069. agenda The daily/weekly agenda.
  2070. todo Entries with a specific TODO keyword, in all agenda files.
  2071. search Entries containing search words entry or headline.
  2072. tags Tags/Property/TODO match in all agenda files.
  2073. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2074. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2075. tags-tree Sparse tree with all tags matches in *current* file.
  2076. occur-tree Occur sparse tree for *current* file.
  2077. ... A user-defined function.
  2078. match What to search for:
  2079. - a single keyword for TODO keyword searches
  2080. - a tags match expression for tags searches
  2081. - a regular expression for occur searches
  2082. options A list of option settings, similar to that in a let form, so like
  2083. this: ((opt1 val1) (opt2 val2) ...)
  2084. files A list of files file to write the produced agenda buffer to
  2085. with the command `org-store-agenda-views'.
  2086. If a file name ends in \".html\", an HTML version of the buffer
  2087. is written out. If it ends in \".ps\", a postscript version is
  2088. produced. Otherwide, only the plain text is written to the file.
  2089. You can also define a set of commands, to create a composite agenda buffer.
  2090. In this case, an entry looks like this:
  2091. (key desc (cmd1 cmd2 ...) general-options file)
  2092. where
  2093. desc A description string to be displayed in the dispatcher menu.
  2094. cmd An agenda command, similar to the above. However, tree commands
  2095. are no allowed, but instead you can get agenda and global todo list.
  2096. So valid commands for a set are:
  2097. (agenda)
  2098. (alltodo)
  2099. (stuck)
  2100. (todo \"match\" options files)
  2101. (search \"match\" options files)
  2102. (tags \"match\" options files)
  2103. (tags-todo \"match\" options files)
  2104. Each command can carry a list of options, and another set of options can be
  2105. given for the whole set of commands. Individual command options take
  2106. precedence over the general options.
  2107. When using several characters as key to a command, the first characters
  2108. are prefix commands. For the dispatcher to display useful information, you
  2109. should provide a description for the prefix, like
  2110. (setq org-agenda-custom-commands
  2111. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2112. (\"hl\" tags \"+HOME+Lisa\")
  2113. (\"hp\" tags \"+HOME+Peter\")
  2114. (\"hk\" tags \"+HOME+Kim\")))"
  2115. :group 'org-agenda-custom-commands
  2116. :type '(repeat
  2117. (choice :value ("a" "" tags "" nil)
  2118. (list :tag "Single command"
  2119. (string :tag "Access Key(s) ")
  2120. (option (string :tag "Description"))
  2121. (choice
  2122. (const :tag "Agenda" agenda)
  2123. (const :tag "TODO list" alltodo)
  2124. (const :tag "Search words" search)
  2125. (const :tag "Stuck projects" stuck)
  2126. (const :tag "Tags search (all agenda files)" tags)
  2127. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2128. (const :tag "TODO keyword search (all agenda files)" todo)
  2129. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2130. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2131. (const :tag "Occur tree (current buffer)" occur-tree)
  2132. (sexp :tag "Other, user-defined function"))
  2133. (string :tag "Match")
  2134. (repeat :tag "Local options"
  2135. (list (variable :tag "Option") (sexp :tag "Value")))
  2136. (option (repeat :tag "Export" (file :tag "Export to"))))
  2137. (list :tag "Command series, all agenda files"
  2138. (string :tag "Access Key(s)")
  2139. (string :tag "Description ")
  2140. (repeat
  2141. (choice
  2142. (const :tag "Agenda" (agenda))
  2143. (const :tag "TODO list" (alltodo))
  2144. (list :tag "Search words"
  2145. (const :format "" search)
  2146. (string :tag "Match")
  2147. (repeat :tag "Local options"
  2148. (list (variable :tag "Option")
  2149. (sexp :tag "Value"))))
  2150. (const :tag "Stuck projects" (stuck))
  2151. (list :tag "Tags search"
  2152. (const :format "" tags)
  2153. (string :tag "Match")
  2154. (repeat :tag "Local options"
  2155. (list (variable :tag "Option")
  2156. (sexp :tag "Value"))))
  2157. (list :tag "Tags search, TODO entries only"
  2158. (const :format "" tags-todo)
  2159. (string :tag "Match")
  2160. (repeat :tag "Local options"
  2161. (list (variable :tag "Option")
  2162. (sexp :tag "Value"))))
  2163. (list :tag "TODO keyword search"
  2164. (const :format "" todo)
  2165. (string :tag "Match")
  2166. (repeat :tag "Local options"
  2167. (list (variable :tag "Option")
  2168. (sexp :tag "Value"))))
  2169. (list :tag "Other, user-defined function"
  2170. (symbol :tag "function")
  2171. (string :tag "Match")
  2172. (repeat :tag "Local options"
  2173. (list (variable :tag "Option")
  2174. (sexp :tag "Value"))))))
  2175. (repeat :tag "General options"
  2176. (list (variable :tag "Option")
  2177. (sexp :tag "Value")))
  2178. (option (repeat :tag "Export" (file :tag "Export to"))))
  2179. (cons :tag "Prefix key documentation"
  2180. (string :tag "Access Key(s)")
  2181. (string :tag "Description ")))))
  2182. (defcustom org-stuck-projects
  2183. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2184. "How to identify stuck projects.
  2185. This is a list of four items:
  2186. 1. A tags/todo matcher string that is used to identify a project.
  2187. The entire tree below a headline matched by this is considered one project.
  2188. 2. A list of TODO keywords identifying non-stuck projects.
  2189. If the project subtree contains any headline with one of these todo
  2190. keywords, the project is considered to be not stuck. If you specify
  2191. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2192. 3. A list of tags identifying non-stuck projects.
  2193. If the project subtree contains any headline with one of these tags,
  2194. the project is considered to be not stuck. If you specify \"*\" as
  2195. a tag, any tag will mark the project unstuck.
  2196. 4. An arbitrary regular expression matching non-stuck projects.
  2197. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2198. or `C-c a #' to produce the list."
  2199. :group 'org-agenda-custom-commands
  2200. :type '(list
  2201. (string :tag "Tags/TODO match to identify a project")
  2202. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2203. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2204. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2205. (defgroup org-agenda-skip nil
  2206. "Options concerning skipping parts of agenda files."
  2207. :tag "Org Agenda Skip"
  2208. :group 'org-agenda)
  2209. (defcustom org-agenda-todo-list-sublevels t
  2210. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2211. When nil, the sublevels of a TODO entry are not checked, resulting in
  2212. potentially much shorter TODO lists."
  2213. :group 'org-agenda-skip
  2214. :group 'org-todo
  2215. :type 'boolean)
  2216. (defcustom org-agenda-todo-ignore-with-date nil
  2217. "Non-nil means, don't show entries with a date in the global todo list.
  2218. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2219. but don't want them to show up in the TODO list.
  2220. When this is set, it also covers deadlines and scheduled items, the settings
  2221. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2222. will be ignored."
  2223. :group 'org-agenda-skip
  2224. :group 'org-todo
  2225. :type 'boolean)
  2226. (defcustom org-agenda-todo-ignore-scheduled nil
  2227. "Non-nil means, don't show scheduled entries in the global todo list.
  2228. The idea behind this is that by scheduling it, you have already taken care
  2229. of this item.
  2230. See also `org-agenda-todo-ignore-with-date'."
  2231. :group 'org-agenda-skip
  2232. :group 'org-todo
  2233. :type 'boolean)
  2234. (defcustom org-agenda-todo-ignore-deadlines nil
  2235. "Non-nil means, don't show near deadline entries in the global todo list.
  2236. Near means closer than `org-deadline-warning-days' days.
  2237. The idea behind this is that such items will appear in the agenda anyway.
  2238. See also `org-agenda-todo-ignore-with-date'."
  2239. :group 'org-agenda-skip
  2240. :group 'org-todo
  2241. :type 'boolean)
  2242. (defcustom org-agenda-skip-scheduled-if-done nil
  2243. "Non-nil means don't show scheduled items in agenda when they are done.
  2244. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2245. it applies only to the actual date of the scheduling. Warnings about
  2246. an item with a past scheduling dates are always turned off when the item
  2247. is DONE."
  2248. :group 'org-agenda-skip
  2249. :type 'boolean)
  2250. (defcustom org-agenda-skip-deadline-if-done nil
  2251. "Non-nil means don't show deadines when the corresponding item is done.
  2252. When nil, the deadline is still shown and should give you a happy feeling.
  2253. This is relevant for the daily/weekly agenda. And it applied only to the
  2254. actualy date of the deadline. Warnings about approching and past-due
  2255. deadlines are always turned off when the item is DONE."
  2256. :group 'org-agenda-skip
  2257. :type 'boolean)
  2258. (defcustom org-agenda-skip-timestamp-if-done nil
  2259. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2260. :group 'org-agenda-skip
  2261. :type 'boolean)
  2262. (defcustom org-timeline-show-empty-dates 3
  2263. "Non-nil means, `org-timeline' also shows dates without an entry.
  2264. When nil, only the days which actually have entries are shown.
  2265. When t, all days between the first and the last date are shown.
  2266. When an integer, show also empty dates, but if there is a gap of more than
  2267. N days, just insert a special line indicating the size of the gap."
  2268. :group 'org-agenda-skip
  2269. :type '(choice
  2270. (const :tag "None" nil)
  2271. (const :tag "All" t)
  2272. (number :tag "at most")))
  2273. (defgroup org-agenda-startup nil
  2274. "Options concerning initial settings in the Agenda in Org Mode."
  2275. :tag "Org Agenda Startup"
  2276. :group 'org-agenda)
  2277. (defcustom org-finalize-agenda-hook nil
  2278. "Hook run just before displaying an agenda buffer."
  2279. :group 'org-agenda-startup
  2280. :type 'hook)
  2281. (defcustom org-agenda-mouse-1-follows-link nil
  2282. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2283. A longer mouse click will still set point. Does not wortk on XEmacs.
  2284. Needs to be set before org.el is loaded."
  2285. :group 'org-agenda-startup
  2286. :type 'boolean)
  2287. (defcustom org-agenda-start-with-follow-mode nil
  2288. "The initial value of follow-mode in a newly created agenda window."
  2289. :group 'org-agenda-startup
  2290. :type 'boolean)
  2291. (defgroup org-agenda-windows nil
  2292. "Options concerning the windows used by the Agenda in Org Mode."
  2293. :tag "Org Agenda Windows"
  2294. :group 'org-agenda)
  2295. (defcustom org-agenda-window-setup 'reorganize-frame
  2296. "How the agenda buffer should be displayed.
  2297. Possible values for this option are:
  2298. current-window Show agenda in the current window, keeping all other windows.
  2299. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2300. other-window Use `switch-to-buffer-other-window' to display agenda.
  2301. reorganize-frame Show only two windows on the current frame, the current
  2302. window and the agenda.
  2303. See also the variable `org-agenda-restore-windows-after-quit'."
  2304. :group 'org-agenda-windows
  2305. :type '(choice
  2306. (const current-window)
  2307. (const other-frame)
  2308. (const other-window)
  2309. (const reorganize-frame)))
  2310. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2311. "The min and max height of the agenda window as a fraction of frame height.
  2312. The value of the variable is a cons cell with two numbers between 0 and 1.
  2313. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2314. :group 'org-agenda-windows
  2315. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2316. (defcustom org-agenda-restore-windows-after-quit nil
  2317. "Non-nil means, restore window configuration open exiting agenda.
  2318. Before the window configuration is changed for displaying the agenda,
  2319. the current status is recorded. When the agenda is exited with
  2320. `q' or `x' and this option is set, the old state is restored. If
  2321. `org-agenda-window-setup' is `other-frame', the value of this
  2322. option will be ignored.."
  2323. :group 'org-agenda-windows
  2324. :type 'boolean)
  2325. (defcustom org-indirect-buffer-display 'other-window
  2326. "How should indirect tree buffers be displayed?
  2327. This applies to indirect buffers created with the commands
  2328. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2329. Valid values are:
  2330. current-window Display in the current window
  2331. other-window Just display in another window.
  2332. dedicated-frame Create one new frame, and re-use it each time.
  2333. new-frame Make a new frame each time. Note that in this case
  2334. previously-made indirect buffers are kept, and you need to
  2335. kill these buffers yourself."
  2336. :group 'org-structure
  2337. :group 'org-agenda-windows
  2338. :type '(choice
  2339. (const :tag "In current window" current-window)
  2340. (const :tag "In current frame, other window" other-window)
  2341. (const :tag "Each time a new frame" new-frame)
  2342. (const :tag "One dedicated frame" dedicated-frame)))
  2343. (defgroup org-agenda-daily/weekly nil
  2344. "Options concerning the daily/weekly agenda."
  2345. :tag "Org Agenda Daily/Weekly"
  2346. :group 'org-agenda)
  2347. (defcustom org-agenda-ndays 7
  2348. "Number of days to include in overview display.
  2349. Should be 1 or 7."
  2350. :group 'org-agenda-daily/weekly
  2351. :type 'number)
  2352. (defcustom org-agenda-start-on-weekday 1
  2353. "Non-nil means, start the overview always on the specified weekday.
  2354. 0 denotes Sunday, 1 denotes Monday etc.
  2355. When nil, always start on the current day."
  2356. :group 'org-agenda-daily/weekly
  2357. :type '(choice (const :tag "Today" nil)
  2358. (number :tag "Weekday No.")))
  2359. (defcustom org-agenda-show-all-dates t
  2360. "Non-nil means, `org-agenda' shows every day in the selected range.
  2361. When nil, only the days which actually have entries are shown."
  2362. :group 'org-agenda-daily/weekly
  2363. :type 'boolean)
  2364. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2365. "Format string for displaying dates in the agenda.
  2366. Used by the daily/weekly agenda and by the timeline. This should be
  2367. a format string understood by `format-time-string', or a function returning
  2368. the formatted date as a string. The function must take a single argument,
  2369. a calendar-style date list like (month day year)."
  2370. :group 'org-agenda-daily/weekly
  2371. :type '(choice
  2372. (string :tag "Format string")
  2373. (function :tag "Function")))
  2374. (defun org-agenda-format-date-aligned (date)
  2375. "Format a date string for display in the daily/weekly agenda, or timeline.
  2376. This function makes sure that dates are aligned for easy reading."
  2377. (format "%-9s %2d %s %4d"
  2378. (calendar-day-name date)
  2379. (extract-calendar-day date)
  2380. (calendar-month-name (extract-calendar-month date))
  2381. (extract-calendar-year date)))
  2382. (defcustom org-agenda-include-diary nil
  2383. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2384. :group 'org-agenda-daily/weekly
  2385. :type 'boolean)
  2386. (defcustom org-agenda-include-all-todo nil
  2387. "Set means weekly/daily agenda will always contain all TODO entries.
  2388. The TODO entries will be listed at the top of the agenda, before
  2389. the entries for specific days."
  2390. :group 'org-agenda-daily/weekly
  2391. :type 'boolean)
  2392. (defcustom org-agenda-repeating-timestamp-show-all t
  2393. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2394. When nil, only one occurence is shown, either today or the
  2395. nearest into the future."
  2396. :group 'org-agenda-daily/weekly
  2397. :type 'boolean)
  2398. (defcustom org-deadline-warning-days 14
  2399. "No. of days before expiration during which a deadline becomes active.
  2400. This variable governs the display in sparse trees and in the agenda.
  2401. When 0 or negative, it means use this number (the absolute value of it)
  2402. even if a deadline has a different individual lead time specified."
  2403. :group 'org-time
  2404. :group 'org-agenda-daily/weekly
  2405. :type 'number)
  2406. (defcustom org-scheduled-past-days 10000
  2407. "No. of days to continue listing scheduled items that are not marked DONE.
  2408. When an item is scheduled on a date, it shows up in the agenda on this
  2409. day and will be listed until it is marked done for the number of days
  2410. given here."
  2411. :group 'org-agenda-daily/weekly
  2412. :type 'number)
  2413. (defgroup org-agenda-time-grid nil
  2414. "Options concerning the time grid in the Org-mode Agenda."
  2415. :tag "Org Agenda Time Grid"
  2416. :group 'org-agenda)
  2417. (defcustom org-agenda-use-time-grid t
  2418. "Non-nil means, show a time grid in the agenda schedule.
  2419. A time grid is a set of lines for specific times (like every two hours between
  2420. 8:00 and 20:00). The items scheduled for a day at specific times are
  2421. sorted in between these lines.
  2422. For details about when the grid will be shown, and what it will look like, see
  2423. the variable `org-agenda-time-grid'."
  2424. :group 'org-agenda-time-grid
  2425. :type 'boolean)
  2426. (defcustom org-agenda-time-grid
  2427. '((daily today require-timed)
  2428. "----------------"
  2429. (800 1000 1200 1400 1600 1800 2000))
  2430. "The settings for time grid for agenda display.
  2431. This is a list of three items. The first item is again a list. It contains
  2432. symbols specifying conditions when the grid should be displayed:
  2433. daily if the agenda shows a single day
  2434. weekly if the agenda shows an entire week
  2435. today show grid on current date, independent of daily/weekly display
  2436. require-timed show grid only if at least one item has a time specification
  2437. The second item is a string which will be places behing the grid time.
  2438. The third item is a list of integers, indicating the times that should have
  2439. a grid line."
  2440. :group 'org-agenda-time-grid
  2441. :type
  2442. '(list
  2443. (set :greedy t :tag "Grid Display Options"
  2444. (const :tag "Show grid in single day agenda display" daily)
  2445. (const :tag "Show grid in weekly agenda display" weekly)
  2446. (const :tag "Always show grid for today" today)
  2447. (const :tag "Show grid only if any timed entries are present"
  2448. require-timed)
  2449. (const :tag "Skip grid times already present in an entry"
  2450. remove-match))
  2451. (string :tag "Grid String")
  2452. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2453. (defgroup org-agenda-sorting nil
  2454. "Options concerning sorting in the Org-mode Agenda."
  2455. :tag "Org Agenda Sorting"
  2456. :group 'org-agenda)
  2457. (defconst org-sorting-choice
  2458. '(choice
  2459. (const time-up) (const time-down)
  2460. (const category-keep) (const category-up) (const category-down)
  2461. (const tag-down) (const tag-up)
  2462. (const priority-up) (const priority-down))
  2463. "Sorting choices.")
  2464. (defcustom org-agenda-sorting-strategy
  2465. '((agenda time-up category-keep priority-down)
  2466. (todo category-keep priority-down)
  2467. (tags category-keep priority-down)
  2468. (search category-keep))
  2469. "Sorting structure for the agenda items of a single day.
  2470. This is a list of symbols which will be used in sequence to determine
  2471. if an entry should be listed before another entry. The following
  2472. symbols are recognized:
  2473. time-up Put entries with time-of-day indications first, early first
  2474. time-down Put entries with time-of-day indications first, late first
  2475. category-keep Keep the default order of categories, corresponding to the
  2476. sequence in `org-agenda-files'.
  2477. category-up Sort alphabetically by category, A-Z.
  2478. category-down Sort alphabetically by category, Z-A.
  2479. tag-up Sort alphabetically by last tag, A-Z.
  2480. tag-down Sort alphabetically by last tag, Z-A.
  2481. priority-up Sort numerically by priority, high priority last.
  2482. priority-down Sort numerically by priority, high priority first.
  2483. The different possibilities will be tried in sequence, and testing stops
  2484. if one comparison returns a \"not-equal\". For example, the default
  2485. '(time-up category-keep priority-down)
  2486. means: Pull out all entries having a specified time of day and sort them,
  2487. in order to make a time schedule for the current day the first thing in the
  2488. agenda listing for the day. Of the entries without a time indication, keep
  2489. the grouped in categories, don't sort the categories, but keep them in
  2490. the sequence given in `org-agenda-files'. Within each category sort by
  2491. priority.
  2492. Leaving out `category-keep' would mean that items will be sorted across
  2493. categories by priority.
  2494. Instead of a single list, this can also be a set of list for specific
  2495. contents, with a context symbol in the car of the list, any of
  2496. `agenda', `todo', `tags' for the corresponding agenda views."
  2497. :group 'org-agenda-sorting
  2498. :type `(choice
  2499. (repeat :tag "General" ,org-sorting-choice)
  2500. (list :tag "Individually"
  2501. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2502. (repeat ,org-sorting-choice))
  2503. (cons (const :tag "Strategy for TODO lists" todo)
  2504. (repeat ,org-sorting-choice))
  2505. (cons (const :tag "Strategy for Tags matches" tags)
  2506. (repeat ,org-sorting-choice)))))
  2507. (defcustom org-sort-agenda-notime-is-late t
  2508. "Non-nil means, items without time are considered late.
  2509. This is only relevant for sorting. When t, items which have no explicit
  2510. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2511. do have a time. When nil, the default time is before 0:00. You can use this
  2512. option to decide if the schedule for today should come before or after timeless
  2513. agenda entries."
  2514. :group 'org-agenda-sorting
  2515. :type 'boolean)
  2516. (defgroup org-agenda-line-format nil
  2517. "Options concerning the entry prefix in the Org-mode agenda display."
  2518. :tag "Org Agenda Line Format"
  2519. :group 'org-agenda)
  2520. (defcustom org-agenda-prefix-format
  2521. '((agenda . " %-12:c%?-12t% s")
  2522. (timeline . " % s")
  2523. (todo . " %-12:c")
  2524. (tags . " %-12:c")
  2525. (search . " %-12:c"))
  2526. "Format specifications for the prefix of items in the agenda views.
  2527. An alist with four entries, for the different agenda types. The keys to the
  2528. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2529. are format strings.
  2530. This format works similar to a printf format, with the following meaning:
  2531. %c the category of the item, \"Diary\" for entries from the diary, or
  2532. as given by the CATEGORY keyword or derived from the file name.
  2533. %T the *last* tag of the item. Last because inherited tags come
  2534. first in the list.
  2535. %t the time-of-day specification if one applies to the entry, in the
  2536. format HH:MM
  2537. %s Scheduling/Deadline information, a short string
  2538. All specifiers work basically like the standard `%s' of printf, but may
  2539. contain two additional characters: A question mark just after the `%' and
  2540. a whitespace/punctuation character just before the final letter.
  2541. If the first character after `%' is a question mark, the entire field
  2542. will only be included if the corresponding value applies to the
  2543. current entry. This is useful for fields which should have fixed
  2544. width when present, but zero width when absent. For example,
  2545. \"%?-12t\" will result in a 12 character time field if a time of the
  2546. day is specified, but will completely disappear in entries which do
  2547. not contain a time.
  2548. If there is punctuation or whitespace character just before the final
  2549. format letter, this character will be appended to the field value if
  2550. the value is not empty. For example, the format \"%-12:c\" leads to
  2551. \"Diary: \" if the category is \"Diary\". If the category were be
  2552. empty, no additional colon would be interted.
  2553. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2554. - Indent the line with two space characters
  2555. - Give the category in a 12 chars wide field, padded with whitespace on
  2556. the right (because of `-'). Append a colon if there is a category
  2557. (because of `:').
  2558. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2559. time, don't put in an empty field, just skip it (because of '?').
  2560. - Finally, put the scheduling information and append a whitespace.
  2561. As another example, if you don't want the time-of-day of entries in
  2562. the prefix, you could use:
  2563. (setq org-agenda-prefix-format \" %-11:c% s\")
  2564. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2565. `org-agenda-remove-tags'."
  2566. :type '(choice
  2567. (string :tag "General format")
  2568. (list :greedy t :tag "View dependent"
  2569. (cons (const agenda) (string :tag "Format"))
  2570. (cons (const timeline) (string :tag "Format"))
  2571. (cons (const todo) (string :tag "Format"))
  2572. (cons (const tags) (string :tag "Format"))
  2573. (cons (const search) (string :tag "Format"))))
  2574. :group 'org-agenda-line-format)
  2575. (defvar org-prefix-format-compiled nil
  2576. "The compiled version of the most recently used prefix format.
  2577. See the variable `org-agenda-prefix-format'.")
  2578. (defcustom org-agenda-todo-keyword-format "%-1s"
  2579. "Format for the TODO keyword in agenda lines.
  2580. Set this to something like \"%-12s\" if you want all TODO keywords
  2581. to occupy a fixed space in the agenda display."
  2582. :group 'org-agenda-line-format
  2583. :type 'string)
  2584. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2585. "Text preceeding scheduled items in the agenda view.
  2586. This is a list with two strings. The first applies when the item is
  2587. scheduled on the current day. The second applies when it has been scheduled
  2588. previously, it may contain a %d to capture how many days ago the item was
  2589. scheduled."
  2590. :group 'org-agenda-line-format
  2591. :type '(list
  2592. (string :tag "Scheduled today ")
  2593. (string :tag "Scheduled previously")))
  2594. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2595. "Text preceeding deadline items in the agenda view.
  2596. This is a list with two strings. The first applies when the item has its
  2597. deadline on the current day. The second applies when it is in the past or
  2598. in the future, it may contain %d to capture how many days away the deadline
  2599. is (was)."
  2600. :group 'org-agenda-line-format
  2601. :type '(list
  2602. (string :tag "Deadline today ")
  2603. (string :tag "Deadline relative")))
  2604. (defcustom org-agenda-remove-times-when-in-prefix t
  2605. "Non-nil means, remove duplicate time specifications in agenda items.
  2606. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2607. time-of-day specification in a headline or diary entry is extracted and
  2608. placed into the prefix. If this option is non-nil, the original specification
  2609. \(a timestamp or -range, or just a plain time(range) specification like
  2610. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2611. cluttered.
  2612. The option can be t or nil. It may also be the symbol `beg', indicating
  2613. that the time should only be removed what it is located at the beginning of
  2614. the headline/diary entry."
  2615. :group 'org-agenda-line-format
  2616. :type '(choice
  2617. (const :tag "Always" t)
  2618. (const :tag "Never" nil)
  2619. (const :tag "When at beginning of entry" beg)))
  2620. (defcustom org-agenda-default-appointment-duration nil
  2621. "Default duration for appointments that only have a starting time.
  2622. When nil, no duration is specified in such cases.
  2623. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2624. :group 'org-agenda-line-format
  2625. :type '(choice
  2626. (integer :tag "Minutes")
  2627. (const :tag "No default duration")))
  2628. (defcustom org-agenda-remove-tags nil
  2629. "Non-nil means, remove the tags from the headline copy in the agenda.
  2630. When this is the symbol `prefix', only remove tags when
  2631. `org-agenda-prefix-format' contains a `%T' specifier."
  2632. :group 'org-agenda-line-format
  2633. :type '(choice
  2634. (const :tag "Always" t)
  2635. (const :tag "Never" nil)
  2636. (const :tag "When prefix format contains %T" prefix)))
  2637. (if (fboundp 'defvaralias)
  2638. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2639. 'org-agenda-remove-tags))
  2640. (defcustom org-agenda-tags-column -80
  2641. "Shift tags in agenda items to this column.
  2642. If this number is positive, it specifies the column. If it is negative,
  2643. it means that the tags should be flushright to that column. For example,
  2644. -80 works well for a normal 80 character screen."
  2645. :group 'org-agenda-line-format
  2646. :type 'integer)
  2647. (if (fboundp 'defvaralias)
  2648. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2649. (defcustom org-agenda-fontify-priorities t
  2650. "Non-nil means, highlight low and high priorities in agenda.
  2651. When t, the highest priority entries are bold, lowest priority italic.
  2652. This may also be an association list of priority faces. The face may be
  2653. a names face, or a list like `(:background \"Red\")'."
  2654. :group 'org-agenda-line-format
  2655. :type '(choice
  2656. (const :tag "Never" nil)
  2657. (const :tag "Defaults" t)
  2658. (repeat :tag "Specify"
  2659. (list (character :tag "Priority" :value ?A)
  2660. (sexp :tag "face")))))
  2661. (defgroup org-latex nil
  2662. "Options for embedding LaTeX code into Org-mode"
  2663. :tag "Org LaTeX"
  2664. :group 'org)
  2665. (defcustom org-format-latex-options
  2666. '(:foreground default :background default :scale 1.0
  2667. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2668. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2669. "Options for creating images from LaTeX fragments.
  2670. This is a property list with the following properties:
  2671. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2672. `default' means use the forground of the default face.
  2673. :background the background color, or \"Transparent\".
  2674. `default' means use the background of the default face.
  2675. :scale a scaling factor for the size of the images
  2676. :html-foreground, :html-background, :html-scale
  2677. The same numbers for HTML export.
  2678. :matchers a list indicating which matchers should be used to
  2679. find LaTeX fragments. Valid members of this list are:
  2680. \"begin\" find environments
  2681. \"$\" find math expressions surrounded by $...$
  2682. \"$$\" find math expressions surrounded by $$....$$
  2683. \"\\(\" find math expressions surrounded by \\(...\\)
  2684. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2685. :group 'org-latex
  2686. :type 'plist)
  2687. (defcustom org-format-latex-header "\\documentclass{article}
  2688. \\usepackage{fullpage} % do not remove
  2689. \\usepackage{amssymb}
  2690. \\usepackage[usenames]{color}
  2691. \\usepackage{amsmath}
  2692. \\usepackage{latexsym}
  2693. \\usepackage[mathscr]{eucal}
  2694. \\pagestyle{empty} % do not remove"
  2695. "The document header used for processing LaTeX fragments."
  2696. :group 'org-latex
  2697. :type 'string)
  2698. (defgroup org-export nil
  2699. "Options for exporting org-listings."
  2700. :tag "Org Export"
  2701. :group 'org)
  2702. (defgroup org-export-general nil
  2703. "General options for exporting Org-mode files."
  2704. :tag "Org Export General"
  2705. :group 'org-export)
  2706. ;; FIXME
  2707. (defvar org-export-publishing-directory nil)
  2708. (defcustom org-export-with-special-strings t
  2709. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2710. When this option is turned on, these strings will be exported as:
  2711. Org HTML LaTeX
  2712. -----+----------+--------
  2713. \\- &shy; \\-
  2714. -- &ndash; --
  2715. --- &mdash; ---
  2716. ... &hellip; \ldots
  2717. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2718. :group 'org-export-translation
  2719. :type 'boolean)
  2720. (defcustom org-export-language-setup
  2721. '(("en" "Author" "Date" "Table of Contents")
  2722. ("cs" "Autor" "Datum" "Obsah")
  2723. ("da" "Ophavsmand" "Dato" "Indhold")
  2724. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2725. ("es" "Autor" "Fecha" "\xcdndice")
  2726. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2727. ("it" "Autore" "Data" "Indice")
  2728. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2729. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2730. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2731. "Terms used in export text, translated to different languages.
  2732. Use the variable `org-export-default-language' to set the language,
  2733. or use the +OPTION lines for a per-file setting."
  2734. :group 'org-export-general
  2735. :type '(repeat
  2736. (list
  2737. (string :tag "HTML language tag")
  2738. (string :tag "Author")
  2739. (string :tag "Date")
  2740. (string :tag "Table of Contents"))))
  2741. (defcustom org-export-default-language "en"
  2742. "The default language of HTML export, as a string.
  2743. This should have an association in `org-export-language-setup'."
  2744. :group 'org-export-general
  2745. :type 'string)
  2746. (defcustom org-export-skip-text-before-1st-heading t
  2747. "Non-nil means, skip all text before the first headline when exporting.
  2748. When nil, that text is exported as well."
  2749. :group 'org-export-general
  2750. :type 'boolean)
  2751. (defcustom org-export-headline-levels 3
  2752. "The last level which is still exported as a headline.
  2753. Inferior levels will produce itemize lists when exported.
  2754. Note that a numeric prefix argument to an exporter function overrides
  2755. this setting.
  2756. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2757. :group 'org-export-general
  2758. :type 'number)
  2759. (defcustom org-export-with-section-numbers t
  2760. "Non-nil means, add section numbers to headlines when exporting.
  2761. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2762. :group 'org-export-general
  2763. :type 'boolean)
  2764. (defcustom org-export-with-toc t
  2765. "Non-nil means, create a table of contents in exported files.
  2766. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2767. When an integer, include levels up to N in the toc, this may then be
  2768. different from `org-export-headline-levels', but it will not be allowed
  2769. to be larger than the number of headline levels.
  2770. When nil, no table of contents is made.
  2771. Headlines which contain any TODO items will be marked with \"(*)\" in
  2772. ASCII export, and with red color in HTML output, if the option
  2773. `org-export-mark-todo-in-toc' is set.
  2774. In HTML output, the TOC will be clickable.
  2775. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2776. or \"toc:3\"."
  2777. :group 'org-export-general
  2778. :type '(choice
  2779. (const :tag "No Table of Contents" nil)
  2780. (const :tag "Full Table of Contents" t)
  2781. (integer :tag "TOC to level")))
  2782. (defcustom org-export-mark-todo-in-toc nil
  2783. "Non-nil means, mark TOC lines that contain any open TODO items."
  2784. :group 'org-export-general
  2785. :type 'boolean)
  2786. (defcustom org-export-preserve-breaks nil
  2787. "Non-nil means, preserve all line breaks when exporting.
  2788. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2789. export, line breaks will always be preserved, regardless of this variable.
  2790. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2791. :group 'org-export-general
  2792. :type 'boolean)
  2793. (defcustom org-export-with-archived-trees 'headline
  2794. "Whether subtrees with the ARCHIVE tag should be exported.
  2795. This can have three different values
  2796. nil Do not export, pretend this tree is not present
  2797. t Do export the entire tree
  2798. headline Only export the headline, but skip the tree below it."
  2799. :group 'org-export-general
  2800. :group 'org-archive
  2801. :type '(choice
  2802. (const :tag "not at all" nil)
  2803. (const :tag "headline only" 'headline)
  2804. (const :tag "entirely" t)))
  2805. (defcustom org-export-author-info t
  2806. "Non-nil means, insert author name and email into the exported file.
  2807. This option can also be set with the +OPTIONS line,
  2808. e.g. \"author-info:nil\"."
  2809. :group 'org-export-general
  2810. :type 'boolean)
  2811. (defcustom org-export-time-stamp-file t
  2812. "Non-nil means, insert a time stamp into the exported file.
  2813. The time stamp shows when the file was created.
  2814. This option can also be set with the +OPTIONS line,
  2815. e.g. \"timestamp:nil\"."
  2816. :group 'org-export-general
  2817. :type 'boolean)
  2818. (defcustom org-export-with-timestamps t
  2819. "If nil, do not export time stamps and associated keywords."
  2820. :group 'org-export-general
  2821. :type 'boolean)
  2822. (defcustom org-export-remove-timestamps-from-toc t
  2823. "If nil, remove timestamps from the table of contents entries."
  2824. :group 'org-export-general
  2825. :type 'boolean)
  2826. (defcustom org-export-with-tags 'not-in-toc
  2827. "If nil, do not export tags, just remove them from headlines.
  2828. If this is the symbol `not-in-toc', tags will be removed from table of
  2829. contents entries, but still be shown in the headlines of the document.
  2830. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2831. :group 'org-export-general
  2832. :type '(choice
  2833. (const :tag "Off" nil)
  2834. (const :tag "Not in TOC" not-in-toc)
  2835. (const :tag "On" t)))
  2836. (defcustom org-export-with-drawers nil
  2837. "Non-nil means, export with drawers like the property drawer.
  2838. When t, all drawers are exported. This may also be a list of
  2839. drawer names to export."
  2840. :group 'org-export-general
  2841. :type '(choice
  2842. (const :tag "All drawers" t)
  2843. (const :tag "None" nil)
  2844. (repeat :tag "Selected drawers"
  2845. (string :tag "Drawer name"))))
  2846. (defgroup org-export-translation nil
  2847. "Options for translating special ascii sequences for the export backends."
  2848. :tag "Org Export Translation"
  2849. :group 'org-export)
  2850. (defcustom org-export-with-emphasize t
  2851. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2852. If the export target supports emphasizing text, the word will be
  2853. typeset in bold, italic, or underlined, respectively. Works only for
  2854. single words, but you can say: I *really* *mean* *this*.
  2855. Not all export backends support this.
  2856. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2857. :group 'org-export-translation
  2858. :type 'boolean)
  2859. (defcustom org-export-with-footnotes t
  2860. "If nil, export [1] as a footnote marker.
  2861. Lines starting with [1] will be formatted as footnotes.
  2862. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2863. :group 'org-export-translation
  2864. :type 'boolean)
  2865. (defcustom org-export-with-sub-superscripts t
  2866. "Non-nil means, interpret \"_\" and \"^\" for export.
  2867. When this option is turned on, you can use TeX-like syntax for sub- and
  2868. superscripts. Several characters after \"_\" or \"^\" will be
  2869. considered as a single item - so grouping with {} is normally not
  2870. needed. For example, the following things will be parsed as single
  2871. sub- or superscripts.
  2872. 10^24 or 10^tau several digits will be considered 1 item.
  2873. 10^-12 or 10^-tau a leading sign with digits or a word
  2874. x^2-y^3 will be read as x^2 - y^3, because items are
  2875. terminated by almost any nonword/nondigit char.
  2876. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2877. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2878. sub/superscript. If you set this variable to the symbol `{}',
  2879. the braces are *required* in order to trigger interpretations as
  2880. sub/superscript. This can be helpful in documents that need \"_\"
  2881. frequently in plain text.
  2882. Not all export backends support this, but HTML does.
  2883. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2884. :group 'org-export-translation
  2885. :type '(choice
  2886. (const :tag "Always interpret" t)
  2887. (const :tag "Only with braces" {})
  2888. (const :tag "Never interpret" nil)))
  2889. (defcustom org-export-with-special-strings t
  2890. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2891. When this option is turned on, these strings will be exported as:
  2892. \\- : &shy;
  2893. -- : &ndash;
  2894. --- : &mdash;
  2895. Not all export backends support this, but HTML does.
  2896. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2897. :group 'org-export-translation
  2898. :type 'boolean)
  2899. (defcustom org-export-with-TeX-macros t
  2900. "Non-nil means, interpret simple TeX-like macros when exporting.
  2901. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2902. No only real TeX macros will work here, but the standard HTML entities
  2903. for math can be used as macro names as well. For a list of supported
  2904. names in HTML export, see the constant `org-html-entities'.
  2905. Not all export backends support this.
  2906. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2907. :group 'org-export-translation
  2908. :group 'org-export-latex
  2909. :type 'boolean)
  2910. (defcustom org-export-with-LaTeX-fragments nil
  2911. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2912. When set, the exporter will find LaTeX environments if the \\begin line is
  2913. the first non-white thing on a line. It will also find the math delimiters
  2914. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2915. display math.
  2916. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2917. :group 'org-export-translation
  2918. :group 'org-export-latex
  2919. :type 'boolean)
  2920. (defcustom org-export-with-fixed-width t
  2921. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2922. This can be used to have pre-formatted text, fragments of code etc. For
  2923. example:
  2924. : ;; Some Lisp examples
  2925. : (while (defc cnt)
  2926. : (ding))
  2927. will be looking just like this in also HTML. See also the QUOTE keyword.
  2928. Not all export backends support this.
  2929. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2930. :group 'org-export-translation
  2931. :type 'boolean)
  2932. (defcustom org-match-sexp-depth 3
  2933. "Number of stacked braces for sub/superscript matching.
  2934. This has to be set before loading org.el to be effective."
  2935. :group 'org-export-translation
  2936. :type 'integer)
  2937. (defgroup org-export-tables nil
  2938. "Options for exporting tables in Org-mode."
  2939. :tag "Org Export Tables"
  2940. :group 'org-export)
  2941. (defcustom org-export-with-tables t
  2942. "If non-nil, lines starting with \"|\" define a table.
  2943. For example:
  2944. | Name | Address | Birthday |
  2945. |-------------+----------+-----------|
  2946. | Arthur Dent | England | 29.2.2100 |
  2947. Not all export backends support this.
  2948. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2949. :group 'org-export-tables
  2950. :type 'boolean)
  2951. (defcustom org-export-highlight-first-table-line t
  2952. "Non-nil means, highlight the first table line.
  2953. In HTML export, this means use <th> instead of <td>.
  2954. In tables created with table.el, this applies to the first table line.
  2955. In Org-mode tables, all lines before the first horizontal separator
  2956. line will be formatted with <th> tags."
  2957. :group 'org-export-tables
  2958. :type 'boolean)
  2959. (defcustom org-export-table-remove-special-lines t
  2960. "Remove special lines and marking characters in calculating tables.
  2961. This removes the special marking character column from tables that are set
  2962. up for spreadsheet calculations. It also removes the entire lines
  2963. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2964. the values of constants may be useful to have."
  2965. :group 'org-export-tables
  2966. :type 'boolean)
  2967. (defcustom org-export-prefer-native-exporter-for-tables nil
  2968. "Non-nil means, always export tables created with table.el natively.
  2969. Natively means, use the HTML code generator in table.el.
  2970. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2971. the table does not use row- or column-spanning). This has the
  2972. advantage, that the automatic HTML conversions for math symbols and
  2973. sub/superscripts can be applied. Org-mode's HTML generator is also
  2974. much faster."
  2975. :group 'org-export-tables
  2976. :type 'boolean)
  2977. (defgroup org-export-ascii nil
  2978. "Options specific for ASCII export of Org-mode files."
  2979. :tag "Org Export ASCII"
  2980. :group 'org-export)
  2981. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2982. "Characters for underlining headings in ASCII export.
  2983. In the given sequence, these characters will be used for level 1, 2, ..."
  2984. :group 'org-export-ascii
  2985. :type '(repeat character))
  2986. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2987. "Bullet characters for headlines converted to lists in ASCII export.
  2988. The first character is used for the first lest level generated in this
  2989. way, and so on. If there are more levels than characters given here,
  2990. the list will be repeated.
  2991. Note that plain lists will keep the same bullets as the have in the
  2992. Org-mode file."
  2993. :group 'org-export-ascii
  2994. :type '(repeat character))
  2995. (defgroup org-export-xml nil
  2996. "Options specific for XML export of Org-mode files."
  2997. :tag "Org Export XML"
  2998. :group 'org-export)
  2999. (defgroup org-export-html nil
  3000. "Options specific for HTML export of Org-mode files."
  3001. :tag "Org Export HTML"
  3002. :group 'org-export)
  3003. (defcustom org-export-html-coding-system nil
  3004. ""
  3005. :group 'org-export-html
  3006. :type 'coding-system)
  3007. (defcustom org-export-html-extension "html"
  3008. "The extension for exported HTML files."
  3009. :group 'org-export-html
  3010. :type 'string)
  3011. (defcustom org-export-html-style
  3012. "<style type=\"text/css\">
  3013. html {
  3014. font-family: Times, serif;
  3015. font-size: 12pt;
  3016. }
  3017. .title { text-align: center; }
  3018. .todo { color: red; }
  3019. .done { color: green; }
  3020. .timestamp { color: grey }
  3021. .timestamp-kwd { color: CadetBlue }
  3022. .tag { background-color:lightblue; font-weight:normal }
  3023. .target { background-color: lavender; }
  3024. pre {
  3025. border: 1pt solid #AEBDCC;
  3026. background-color: #F3F5F7;
  3027. padding: 5pt;
  3028. font-family: courier, monospace;
  3029. }
  3030. table { border-collapse: collapse; }
  3031. td, th {
  3032. vertical-align: top;
  3033. <!--border: 1pt solid #ADB9CC;-->
  3034. }
  3035. </style>"
  3036. "The default style specification for exported HTML files.
  3037. Since there are different ways of setting style information, this variable
  3038. needs to contain the full HTML structure to provide a style, including the
  3039. surrounding HTML tags. The style specifications should include definitions
  3040. for new classes todo, done, title, and deadline. For example, legal values
  3041. would be:
  3042. <style type=\"text/css\">
  3043. p { font-weight: normal; color: gray; }
  3044. h1 { color: black; }
  3045. .title { text-align: center; }
  3046. .todo, .deadline { color: red; }
  3047. .done { color: green; }
  3048. </style>
  3049. or, if you want to keep the style in a file,
  3050. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3051. As the value of this option simply gets inserted into the HTML <head> header,
  3052. you can \"misuse\" it to add arbitrary text to the header."
  3053. :group 'org-export-html
  3054. :type 'string)
  3055. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3056. "Format for typesetting the document title in HTML export."
  3057. :group 'org-export-html
  3058. :type 'string)
  3059. (defcustom org-export-html-toplevel-hlevel 2
  3060. "The <H> level for level 1 headings in HTML export."
  3061. :group 'org-export-html
  3062. :type 'string)
  3063. (defcustom org-export-html-link-org-files-as-html t
  3064. "Non-nil means, make file links to `file.org' point to `file.html'.
  3065. When org-mode is exporting an org-mode file to HTML, links to
  3066. non-html files are directly put into a href tag in HTML.
  3067. However, links to other Org-mode files (recognized by the
  3068. extension `.org.) should become links to the corresponding html
  3069. file, assuming that the linked org-mode file will also be
  3070. converted to HTML.
  3071. When nil, the links still point to the plain `.org' file."
  3072. :group 'org-export-html
  3073. :type 'boolean)
  3074. (defcustom org-export-html-inline-images 'maybe
  3075. "Non-nil means, inline images into exported HTML pages.
  3076. This is done using an <img> tag. When nil, an anchor with href is used to
  3077. link to the image. If this option is `maybe', then images in links with
  3078. an empty description will be inlined, while images with a description will
  3079. be linked only."
  3080. :group 'org-export-html
  3081. :type '(choice (const :tag "Never" nil)
  3082. (const :tag "Always" t)
  3083. (const :tag "When there is no description" maybe)))
  3084. ;; FIXME: rename
  3085. (defcustom org-export-html-expand t
  3086. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3087. When nil, these tags will be exported as plain text and therefore
  3088. not be interpreted by a browser.
  3089. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3090. :group 'org-export-html
  3091. :type 'boolean)
  3092. (defcustom org-export-html-table-tag
  3093. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3094. "The HTML tag that is used to start a table.
  3095. This must be a <table> tag, but you may change the options like
  3096. borders and spacing."
  3097. :group 'org-export-html
  3098. :type 'string)
  3099. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3100. "The opening tag for table header fields.
  3101. This is customizable so that alignment options can be specified."
  3102. :group 'org-export-tables
  3103. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3104. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3105. "The opening tag for table data fields.
  3106. This is customizable so that alignment options can be specified."
  3107. :group 'org-export-tables
  3108. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3109. (defcustom org-export-html-with-timestamp nil
  3110. "If non-nil, write `org-export-html-html-helper-timestamp'
  3111. into the exported HTML text. Otherwise, the buffer will just be saved
  3112. to a file."
  3113. :group 'org-export-html
  3114. :type 'boolean)
  3115. (defcustom org-export-html-html-helper-timestamp
  3116. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3117. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3118. :group 'org-export-html
  3119. :type 'string)
  3120. (defgroup org-export-icalendar nil
  3121. "Options specific for iCalendar export of Org-mode files."
  3122. :tag "Org Export iCalendar"
  3123. :group 'org-export)
  3124. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3125. "The file name for the iCalendar file covering all agenda files.
  3126. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3127. The file name should be absolute, the file will be overwritten without warning."
  3128. :group 'org-export-icalendar
  3129. :type 'file)
  3130. (defcustom org-icalendar-include-todo nil
  3131. "Non-nil means, export to iCalendar files should also cover TODO items."
  3132. :group 'org-export-icalendar
  3133. :type '(choice
  3134. (const :tag "None" nil)
  3135. (const :tag "Unfinished" t)
  3136. (const :tag "All" all)))
  3137. (defcustom org-icalendar-include-sexps t
  3138. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3139. These are entries like in the diary, but directly in an Org-mode file."
  3140. :group 'org-export-icalendar
  3141. :type 'boolean)
  3142. (defcustom org-icalendar-include-body 100
  3143. "Amount of text below headline to be included in iCalendar export.
  3144. This is a number of characters that should maximally be included.
  3145. Properties, scheduling and clocking lines will always be removed.
  3146. The text will be inserted into the DESCRIPTION field."
  3147. :group 'org-export-icalendar
  3148. :type '(choice
  3149. (const :tag "Nothing" nil)
  3150. (const :tag "Everything" t)
  3151. (integer :tag "Max characters")))
  3152. (defcustom org-icalendar-combined-name "OrgMode"
  3153. "Calendar name for the combined iCalendar representing all agenda files."
  3154. :group 'org-export-icalendar
  3155. :type 'string)
  3156. (defgroup org-font-lock nil
  3157. "Font-lock settings for highlighting in Org-mode."
  3158. :tag "Org Font Lock"
  3159. :group 'org)
  3160. (defcustom org-level-color-stars-only nil
  3161. "Non-nil means fontify only the stars in each headline.
  3162. When nil, the entire headline is fontified.
  3163. Changing it requires restart of `font-lock-mode' to become effective
  3164. also in regions already fontified."
  3165. :group 'org-font-lock
  3166. :type 'boolean)
  3167. (defcustom org-hide-leading-stars nil
  3168. "Non-nil means, hide the first N-1 stars in a headline.
  3169. This works by using the face `org-hide' for these stars. This
  3170. face is white for a light background, and black for a dark
  3171. background. You may have to customize the face `org-hide' to
  3172. make this work.
  3173. Changing it requires restart of `font-lock-mode' to become effective
  3174. also in regions already fontified.
  3175. You may also set this on a per-file basis by adding one of the following
  3176. lines to the buffer:
  3177. #+STARTUP: hidestars
  3178. #+STARTUP: showstars"
  3179. :group 'org-font-lock
  3180. :type 'boolean)
  3181. (defcustom org-fontify-done-headline nil
  3182. "Non-nil means, change the face of a headline if it is marked DONE.
  3183. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3184. When this is non-nil, the headline after the keyword is set to the
  3185. `org-headline-done' as an additional indication."
  3186. :group 'org-font-lock
  3187. :type 'boolean)
  3188. (defcustom org-fontify-emphasized-text t
  3189. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3190. Changing this variable requires a restart of Emacs to take effect."
  3191. :group 'org-font-lock
  3192. :type 'boolean)
  3193. (defcustom org-highlight-latex-fragments-and-specials nil
  3194. "Non-nil means, fontify what is treated specially by the exporters."
  3195. :group 'org-font-lock
  3196. :type 'boolean)
  3197. (defcustom org-hide-emphasis-markers nil
  3198. "Non-nil mean font-lock should hide the emphasis marker characters."
  3199. :group 'org-font-lock
  3200. :type 'boolean)
  3201. (defvar org-emph-re nil
  3202. "Regular expression for matching emphasis.")
  3203. (defvar org-verbatim-re nil
  3204. "Regular expression for matching verbatim text.")
  3205. (defvar org-emphasis-regexp-components) ; defined just below
  3206. (defvar org-emphasis-alist) ; defined just below
  3207. (defun org-set-emph-re (var val)
  3208. "Set variable and compute the emphasis regular expression."
  3209. (set var val)
  3210. (when (and (boundp 'org-emphasis-alist)
  3211. (boundp 'org-emphasis-regexp-components)
  3212. org-emphasis-alist org-emphasis-regexp-components)
  3213. (let* ((e org-emphasis-regexp-components)
  3214. (pre (car e))
  3215. (post (nth 1 e))
  3216. (border (nth 2 e))
  3217. (body (nth 3 e))
  3218. (nl (nth 4 e))
  3219. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3220. (body1 (concat body "*?"))
  3221. (markers (mapconcat 'car org-emphasis-alist ""))
  3222. (vmarkers (mapconcat
  3223. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3224. org-emphasis-alist "")))
  3225. ;; make sure special characters appear at the right position in the class
  3226. (if (string-match "\\^" markers)
  3227. (setq markers (concat (replace-match "" t t markers) "^")))
  3228. (if (string-match "-" markers)
  3229. (setq markers (concat (replace-match "" t t markers) "-")))
  3230. (if (string-match "\\^" vmarkers)
  3231. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3232. (if (string-match "-" vmarkers)
  3233. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3234. (if (> nl 0)
  3235. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3236. (int-to-string nl) "\\}")))
  3237. ;; Make the regexp
  3238. (setq org-emph-re
  3239. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3240. "\\("
  3241. "\\([" markers "]\\)"
  3242. "\\("
  3243. "[^" border "]\\|"
  3244. "[^" border (if (and nil stacked) markers) "]"
  3245. body1
  3246. "[^" border (if (and nil stacked) markers) "]"
  3247. "\\)"
  3248. "\\3\\)"
  3249. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3250. (setq org-verbatim-re
  3251. (concat "\\([" pre "]\\|^\\)"
  3252. "\\("
  3253. "\\([" vmarkers "]\\)"
  3254. "\\("
  3255. "[^" border "]\\|"
  3256. "[^" border "]"
  3257. body1
  3258. "[^" border "]"
  3259. "\\)"
  3260. "\\3\\)"
  3261. "\\([" post "]\\|$\\)")))))
  3262. (defcustom org-emphasis-regexp-components
  3263. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3264. "Components used to build the regular expression for emphasis.
  3265. This is a list with 6 entries. Terminology: In an emphasis string
  3266. like \" *strong word* \", we call the initial space PREMATCH, the final
  3267. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3268. and \"trong wor\" is the body. The different components in this variable
  3269. specify what is allowed/forbidden in each part:
  3270. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3271. post Chars allowed as postmatch. End of line will be allowed too.
  3272. border The chars *forbidden* as border characters.
  3273. body-regexp A regexp like \".\" to match a body character. Don't use
  3274. non-shy groups here, and don't allow newline here.
  3275. newline The maximum number of newlines allowed in an emphasis exp.
  3276. Use customize to modify this, or restart Emacs after changing it."
  3277. :group 'org-font-lock
  3278. :set 'org-set-emph-re
  3279. :type '(list
  3280. (sexp :tag "Allowed chars in pre ")
  3281. (sexp :tag "Allowed chars in post ")
  3282. (sexp :tag "Forbidden chars in border ")
  3283. (sexp :tag "Regexp for body ")
  3284. (integer :tag "number of newlines allowed")
  3285. (option (boolean :tag "Stacking (DISABLED) "))))
  3286. (defcustom org-emphasis-alist
  3287. '(("*" bold "<b>" "</b>")
  3288. ("/" italic "<i>" "</i>")
  3289. ("_" underline "<u>" "</u>")
  3290. ("=" org-code "<code>" "</code>" verbatim)
  3291. ("~" org-verbatim "" "" verbatim)
  3292. ("+" (:strike-through t) "<del>" "</del>")
  3293. )
  3294. "Special syntax for emphasized text.
  3295. Text starting and ending with a special character will be emphasized, for
  3296. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3297. characters, the face to be used by font-lock for highlighting in Org-mode
  3298. Emacs buffers, and the HTML tags to be used for this.
  3299. Use customize to modify this, or restart Emacs after changing it."
  3300. :group 'org-font-lock
  3301. :set 'org-set-emph-re
  3302. :type '(repeat
  3303. (list
  3304. (string :tag "Marker character")
  3305. (choice
  3306. (face :tag "Font-lock-face")
  3307. (plist :tag "Face property list"))
  3308. (string :tag "HTML start tag")
  3309. (string :tag "HTML end tag")
  3310. (option (const verbatim)))))
  3311. ;;; The faces
  3312. (defgroup org-faces nil
  3313. "Faces in Org-mode."
  3314. :tag "Org Faces"
  3315. :group 'org-font-lock)
  3316. (defun org-compatible-face (inherits specs)
  3317. "Make a compatible face specification.
  3318. If INHERITS is an existing face and if the Emacs version supports it,
  3319. just inherit the face. If not, use SPECS to define the face.
  3320. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3321. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3322. to the top of the list. The `min-colors' attribute will be removed from
  3323. any other entries, and any resulting duplicates will be removed entirely."
  3324. (cond
  3325. ((and inherits (facep inherits)
  3326. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3327. ;; In Emacs 23, we use inheritance where possible.
  3328. ;; We only do this in Emacs 23, because only there the outline
  3329. ;; faces have been changed to the original org-mode-level-faces.
  3330. (list (list t :inherit inherits)))
  3331. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3332. ;; These do not understand the `min-colors' attribute.
  3333. (let (r e a)
  3334. (while (setq e (pop specs))
  3335. (cond
  3336. ((memq (car e) '(t default)) (push e r))
  3337. ((setq a (member '(min-colors 8) (car e)))
  3338. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3339. (cdr e)))))
  3340. ((setq a (assq 'min-colors (car e)))
  3341. (setq e (cons (delq a (car e)) (cdr e)))
  3342. (or (assoc (car e) r) (push e r)))
  3343. (t (or (assoc (car e) r) (push e r)))))
  3344. (nreverse r)))
  3345. (t specs)))
  3346. (put 'org-compatible-face 'lisp-indent-function 1)
  3347. (defface org-hide
  3348. '((((background light)) (:foreground "white"))
  3349. (((background dark)) (:foreground "black")))
  3350. "Face used to hide leading stars in headlines.
  3351. The forground color of this face should be equal to the background
  3352. color of the frame."
  3353. :group 'org-faces)
  3354. (defface org-level-1 ;; font-lock-function-name-face
  3355. (org-compatible-face 'outline-1
  3356. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3357. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3358. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3359. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3360. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3361. (t (:bold t))))
  3362. "Face used for level 1 headlines."
  3363. :group 'org-faces)
  3364. (defface org-level-2 ;; font-lock-variable-name-face
  3365. (org-compatible-face 'outline-2
  3366. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3367. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3368. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3369. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3370. (t (:bold t))))
  3371. "Face used for level 2 headlines."
  3372. :group 'org-faces)
  3373. (defface org-level-3 ;; font-lock-keyword-face
  3374. (org-compatible-face 'outline-3
  3375. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3376. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3377. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3378. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3379. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3380. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3381. (t (:bold t))))
  3382. "Face used for level 3 headlines."
  3383. :group 'org-faces)
  3384. (defface org-level-4 ;; font-lock-comment-face
  3385. (org-compatible-face 'outline-4
  3386. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3387. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3388. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3389. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3390. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3391. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3392. (t (:bold t))))
  3393. "Face used for level 4 headlines."
  3394. :group 'org-faces)
  3395. (defface org-level-5 ;; font-lock-type-face
  3396. (org-compatible-face 'outline-5
  3397. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3398. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3399. (((class color) (min-colors 8)) (:foreground "green"))))
  3400. "Face used for level 5 headlines."
  3401. :group 'org-faces)
  3402. (defface org-level-6 ;; font-lock-constant-face
  3403. (org-compatible-face 'outline-6
  3404. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3405. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3406. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3407. "Face used for level 6 headlines."
  3408. :group 'org-faces)
  3409. (defface org-level-7 ;; font-lock-builtin-face
  3410. (org-compatible-face 'outline-7
  3411. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3412. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3413. (((class color) (min-colors 8)) (:foreground "blue"))))
  3414. "Face used for level 7 headlines."
  3415. :group 'org-faces)
  3416. (defface org-level-8 ;; font-lock-string-face
  3417. (org-compatible-face 'outline-8
  3418. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3419. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3420. (((class color) (min-colors 8)) (:foreground "green"))))
  3421. "Face used for level 8 headlines."
  3422. :group 'org-faces)
  3423. (defface org-special-keyword ;; font-lock-string-face
  3424. (org-compatible-face nil
  3425. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3426. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3427. (t (:italic t))))
  3428. "Face used for special keywords."
  3429. :group 'org-faces)
  3430. (defface org-drawer ;; font-lock-function-name-face
  3431. (org-compatible-face nil
  3432. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3433. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3434. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3435. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3436. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3437. (t (:bold t))))
  3438. "Face used for drawers."
  3439. :group 'org-faces)
  3440. (defface org-property-value nil
  3441. "Face used for the value of a property."
  3442. :group 'org-faces)
  3443. (defface org-column
  3444. (org-compatible-face nil
  3445. '((((class color) (min-colors 16) (background light))
  3446. (:background "grey90"))
  3447. (((class color) (min-colors 16) (background dark))
  3448. (:background "grey30"))
  3449. (((class color) (min-colors 8))
  3450. (:background "cyan" :foreground "black"))
  3451. (t (:inverse-video t))))
  3452. "Face for column display of entry properties."
  3453. :group 'org-faces)
  3454. (when (fboundp 'set-face-attribute)
  3455. ;; Make sure that a fixed-width face is used when we have a column table.
  3456. (set-face-attribute 'org-column nil
  3457. :height (face-attribute 'default :height)
  3458. :family (face-attribute 'default :family)))
  3459. (defface org-warning
  3460. (org-compatible-face 'font-lock-warning-face
  3461. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3462. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3463. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3464. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3465. (t (:bold t))))
  3466. "Face for deadlines and TODO keywords."
  3467. :group 'org-faces)
  3468. (defface org-archived ; similar to shadow
  3469. (org-compatible-face 'shadow
  3470. '((((class color grayscale) (min-colors 88) (background light))
  3471. (:foreground "grey50"))
  3472. (((class color grayscale) (min-colors 88) (background dark))
  3473. (:foreground "grey70"))
  3474. (((class color) (min-colors 8) (background light))
  3475. (:foreground "green"))
  3476. (((class color) (min-colors 8) (background dark))
  3477. (:foreground "yellow"))))
  3478. "Face for headline with the ARCHIVE tag."
  3479. :group 'org-faces)
  3480. (defface org-link
  3481. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3482. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3483. (t (:underline t)))
  3484. "Face for links."
  3485. :group 'org-faces)
  3486. (defface org-ellipsis
  3487. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3488. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3489. (t (:strike-through t)))
  3490. "Face for the ellipsis in folded text."
  3491. :group 'org-faces)
  3492. (defface org-target
  3493. '((((class color) (background light)) (:underline t))
  3494. (((class color) (background dark)) (:underline t))
  3495. (t (:underline t)))
  3496. "Face for links."
  3497. :group 'org-faces)
  3498. (defface org-date
  3499. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3500. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3501. (t (:underline t)))
  3502. "Face for links."
  3503. :group 'org-faces)
  3504. (defface org-sexp-date
  3505. '((((class color) (background light)) (:foreground "Purple"))
  3506. (((class color) (background dark)) (:foreground "Cyan"))
  3507. (t (:underline t)))
  3508. "Face for links."
  3509. :group 'org-faces)
  3510. (defface org-tag
  3511. '((t (:bold t)))
  3512. "Face for tags."
  3513. :group 'org-faces)
  3514. (defface org-todo ; font-lock-warning-face
  3515. (org-compatible-face nil
  3516. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3517. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3518. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3519. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3520. (t (:inverse-video t :bold t))))
  3521. "Face for TODO keywords."
  3522. :group 'org-faces)
  3523. (defface org-done ;; font-lock-type-face
  3524. (org-compatible-face nil
  3525. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3526. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3527. (((class color) (min-colors 8)) (:foreground "green"))
  3528. (t (:bold t))))
  3529. "Face used for todo keywords that indicate DONE items."
  3530. :group 'org-faces)
  3531. (defface org-headline-done ;; font-lock-string-face
  3532. (org-compatible-face nil
  3533. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3534. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3535. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3536. "Face used to indicate that a headline is DONE.
  3537. This face is only used if `org-fontify-done-headline' is set. If applies
  3538. to the part of the headline after the DONE keyword."
  3539. :group 'org-faces)
  3540. (defcustom org-todo-keyword-faces nil
  3541. "Faces for specific TODO keywords.
  3542. This is a list of cons cells, with TODO keywords in the car
  3543. and faces in the cdr. The face can be a symbol, or a property
  3544. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3545. :group 'org-faces
  3546. :group 'org-todo
  3547. :type '(repeat
  3548. (cons
  3549. (string :tag "keyword")
  3550. (sexp :tag "face"))))
  3551. (defface org-table ;; font-lock-function-name-face
  3552. (org-compatible-face nil
  3553. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3554. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3555. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3556. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3557. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3558. (((class color) (min-colors 8) (background dark)))))
  3559. "Face used for tables."
  3560. :group 'org-faces)
  3561. (defface org-formula
  3562. (org-compatible-face nil
  3563. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3564. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3565. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3566. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3567. (t (:bold t :italic t))))
  3568. "Face for formulas."
  3569. :group 'org-faces)
  3570. (defface org-code
  3571. (org-compatible-face nil
  3572. '((((class color grayscale) (min-colors 88) (background light))
  3573. (:foreground "grey50"))
  3574. (((class color grayscale) (min-colors 88) (background dark))
  3575. (:foreground "grey70"))
  3576. (((class color) (min-colors 8) (background light))
  3577. (:foreground "green"))
  3578. (((class color) (min-colors 8) (background dark))
  3579. (:foreground "yellow"))))
  3580. "Face for fixed-with text like code snippets."
  3581. :group 'org-faces
  3582. :version "22.1")
  3583. (defface org-verbatim
  3584. (org-compatible-face nil
  3585. '((((class color grayscale) (min-colors 88) (background light))
  3586. (:foreground "grey50" :underline t))
  3587. (((class color grayscale) (min-colors 88) (background dark))
  3588. (:foreground "grey70" :underline t))
  3589. (((class color) (min-colors 8) (background light))
  3590. (:foreground "green" :underline t))
  3591. (((class color) (min-colors 8) (background dark))
  3592. (:foreground "yellow" :underline t))))
  3593. "Face for fixed-with text like code snippets."
  3594. :group 'org-faces
  3595. :version "22.1")
  3596. (defface org-agenda-structure ;; font-lock-function-name-face
  3597. (org-compatible-face nil
  3598. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3599. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3600. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3601. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3602. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3603. (t (:bold t))))
  3604. "Face used in agenda for captions and dates."
  3605. :group 'org-faces)
  3606. (defface org-scheduled-today
  3607. (org-compatible-face nil
  3608. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3609. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3610. (((class color) (min-colors 8)) (:foreground "green"))
  3611. (t (:bold t :italic t))))
  3612. "Face for items scheduled for a certain day."
  3613. :group 'org-faces)
  3614. (defface org-scheduled-previously
  3615. (org-compatible-face nil
  3616. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3617. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3618. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3619. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3620. (t (:bold t))))
  3621. "Face for items scheduled previously, and not yet done."
  3622. :group 'org-faces)
  3623. (defface org-upcoming-deadline
  3624. (org-compatible-face nil
  3625. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3626. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3627. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3628. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3629. (t (:bold t))))
  3630. "Face for items scheduled previously, and not yet done."
  3631. :group 'org-faces)
  3632. (defcustom org-agenda-deadline-faces
  3633. '((1.0 . org-warning)
  3634. (0.5 . org-upcoming-deadline)
  3635. (0.0 . default))
  3636. "Faces for showing deadlines in the agenda.
  3637. This is a list of cons cells. The cdr of each cell is a face to be used,
  3638. and it can also just be like '(:foreground \"yellow\").
  3639. Each car is a fraction of the head-warning time that must have passed for
  3640. this the face in the cdr to be used for display. The numbers must be
  3641. given in descending order. The head-warning time is normally taken
  3642. from `org-deadline-warning-days', but can also be specified in the deadline
  3643. timestamp itself, like this:
  3644. DEADLINE: <2007-08-13 Mon -8d>
  3645. You may use d for days, w for weeks, m for months and y for years. Months
  3646. and years will only be treated in an approximate fashion (30.4 days for a
  3647. month and 365.24 days for a year)."
  3648. :group 'org-faces
  3649. :group 'org-agenda-daily/weekly
  3650. :type '(repeat
  3651. (cons
  3652. (number :tag "Fraction of head-warning time passed")
  3653. (sexp :tag "Face"))))
  3654. ;; FIXME: this is not a good face yet.
  3655. (defface org-agenda-restriction-lock
  3656. (org-compatible-face nil
  3657. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3658. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3659. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3660. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3661. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3662. (t (:inverse-video t))))
  3663. "Face for showing the agenda restriction lock."
  3664. :group 'org-faces)
  3665. (defface org-time-grid ;; font-lock-variable-name-face
  3666. (org-compatible-face nil
  3667. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3668. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3669. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3670. "Face used for time grids."
  3671. :group 'org-faces)
  3672. (defconst org-level-faces
  3673. '(org-level-1 org-level-2 org-level-3 org-level-4
  3674. org-level-5 org-level-6 org-level-7 org-level-8
  3675. ))
  3676. (defcustom org-n-level-faces (length org-level-faces)
  3677. "The number of different faces to be used for headlines.
  3678. Org-mode defines 8 different headline faces, so this can be at most 8.
  3679. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3680. :type 'number
  3681. :group 'org-faces)
  3682. ;;; Functions and variables from ther packages
  3683. ;; Declared here to avoid compiler warnings
  3684. (eval-and-compile
  3685. (unless (fboundp 'declare-function)
  3686. (defmacro declare-function (fn file &optional arglist fileonly))))
  3687. ;; XEmacs only
  3688. (defvar outline-mode-menu-heading)
  3689. (defvar outline-mode-menu-show)
  3690. (defvar outline-mode-menu-hide)
  3691. (defvar zmacs-regions) ; XEmacs regions
  3692. ;; Emacs only
  3693. (defvar mark-active)
  3694. ;; Various packages
  3695. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3696. (declare-function add-to-diary-list "diary-lib"
  3697. (date string specifier &optional marker globcolor literal))
  3698. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3699. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3700. (declare-function Info-goto-node "info" (nodename &optional fork))
  3701. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3702. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3703. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3704. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3705. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3706. (declare-function bbdb-record-name "ext:bbdb" (record))
  3707. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3708. (declare-function bibtex-generate-autokey "bibtex" ())
  3709. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3710. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3711. (defvar calc-embedded-close-formula)
  3712. (defvar calc-embedded-open-formula)
  3713. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3714. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3715. (declare-function calendar-check-holidays "holidays" (date))
  3716. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3717. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3718. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3719. (declare-function calendar-forward-day "cal-move" (arg))
  3720. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3721. (declare-function calendar-goto-date "cal-move" (date))
  3722. (declare-function calendar-goto-today "cal-move" ())
  3723. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3724. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3725. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3726. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3727. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3728. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3729. (defvar calendar-mode-map)
  3730. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3731. (declare-function cdlatex-tab "ext:cdlatex" ())
  3732. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3733. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3734. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3735. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3736. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3737. (defvar font-lock-unfontify-region-function)
  3738. (declare-function gnus-article-show-summary "gnus-art" ())
  3739. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3740. (defvar gnus-other-frame-object)
  3741. (defvar gnus-group-name)
  3742. (defvar gnus-article-current)
  3743. (defvar Info-current-file)
  3744. (defvar Info-current-node)
  3745. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3746. (declare-function mh-find-path "mh-utils" ())
  3747. (declare-function mh-get-header-field "mh-utils" (field))
  3748. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3749. (declare-function mh-header-display "mh-show" ())
  3750. (declare-function mh-index-previous-folder "mh-search" ())
  3751. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3752. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3753. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3754. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3755. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3756. (declare-function mh-show-header-display "mh-show" t t)
  3757. (declare-function mh-show-msg "mh-show" (msg))
  3758. (declare-function mh-show-show "mh-show" t t)
  3759. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3760. (defvar mh-progs)
  3761. (defvar mh-current-folder)
  3762. (defvar mh-show-folder-buffer)
  3763. (defvar mh-index-folder)
  3764. (defvar mh-searcher)
  3765. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3766. (declare-function parse-time-string "parse-time" (string))
  3767. (declare-function remember "remember" (&optional initial))
  3768. (declare-function remember-buffer-desc "remember" ())
  3769. (declare-function remember-finalize "remember" ())
  3770. (defvar remember-save-after-remembering)
  3771. (defvar remember-data-file)
  3772. (defvar remember-register)
  3773. (defvar remember-buffer)
  3774. (defvar remember-handler-functions)
  3775. (defvar remember-annotation-functions)
  3776. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3777. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3778. (declare-function rmail-what-message "rmail" ())
  3779. (defvar rmail-current-message)
  3780. (defvar texmathp-why)
  3781. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3782. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3783. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3784. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3785. (declare-function vm-isearch-update "ext:vm-search" ())
  3786. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3787. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3788. (declare-function vm-su-subject "ext:vm-summary" (m))
  3789. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3790. (defvar vm-message-pointer)
  3791. (defvar vm-folder-directory)
  3792. (defvar w3m-current-url)
  3793. (defvar w3m-current-title)
  3794. ;; backward compatibility to old version of wl
  3795. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3796. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3797. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3798. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3799. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3800. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3801. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3802. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3803. (defvar wl-summary-buffer-elmo-folder)
  3804. (defvar wl-summary-buffer-folder-name)
  3805. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3806. (defvar org-latex-regexps)
  3807. (defvar constants-unit-system)
  3808. ;;; Variables for pre-computed regular expressions, all buffer local
  3809. (defvar org-drawer-regexp nil
  3810. "Matches first line of a hidden block.")
  3811. (make-variable-buffer-local 'org-drawer-regexp)
  3812. (defvar org-todo-regexp nil
  3813. "Matches any of the TODO state keywords.")
  3814. (make-variable-buffer-local 'org-todo-regexp)
  3815. (defvar org-not-done-regexp nil
  3816. "Matches any of the TODO state keywords except the last one.")
  3817. (make-variable-buffer-local 'org-not-done-regexp)
  3818. (defvar org-todo-line-regexp nil
  3819. "Matches a headline and puts TODO state into group 2 if present.")
  3820. (make-variable-buffer-local 'org-todo-line-regexp)
  3821. (defvar org-complex-heading-regexp nil
  3822. "Matches a headline and puts everything into groups:
  3823. group 1: the stars
  3824. group 2: The todo keyword, maybe
  3825. group 3: Priority cookie
  3826. group 4: True headline
  3827. group 5: Tags")
  3828. (make-variable-buffer-local 'org-complex-heading-regexp)
  3829. (defvar org-todo-line-tags-regexp nil
  3830. "Matches a headline and puts TODO state into group 2 if present.
  3831. Also put tags into group 4 if tags are present.")
  3832. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3833. (defvar org-nl-done-regexp nil
  3834. "Matches newline followed by a headline with the DONE keyword.")
  3835. (make-variable-buffer-local 'org-nl-done-regexp)
  3836. (defvar org-looking-at-done-regexp nil
  3837. "Matches the DONE keyword a point.")
  3838. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3839. (defvar org-ds-keyword-length 12
  3840. "Maximum length of the Deadline and SCHEDULED keywords.")
  3841. (make-variable-buffer-local 'org-ds-keyword-length)
  3842. (defvar org-deadline-regexp nil
  3843. "Matches the DEADLINE keyword.")
  3844. (make-variable-buffer-local 'org-deadline-regexp)
  3845. (defvar org-deadline-time-regexp nil
  3846. "Matches the DEADLINE keyword together with a time stamp.")
  3847. (make-variable-buffer-local 'org-deadline-time-regexp)
  3848. (defvar org-deadline-line-regexp nil
  3849. "Matches the DEADLINE keyword and the rest of the line.")
  3850. (make-variable-buffer-local 'org-deadline-line-regexp)
  3851. (defvar org-scheduled-regexp nil
  3852. "Matches the SCHEDULED keyword.")
  3853. (make-variable-buffer-local 'org-scheduled-regexp)
  3854. (defvar org-scheduled-time-regexp nil
  3855. "Matches the SCHEDULED keyword together with a time stamp.")
  3856. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3857. (defvar org-closed-time-regexp nil
  3858. "Matches the CLOSED keyword together with a time stamp.")
  3859. (make-variable-buffer-local 'org-closed-time-regexp)
  3860. (defvar org-keyword-time-regexp nil
  3861. "Matches any of the 4 keywords, together with the time stamp.")
  3862. (make-variable-buffer-local 'org-keyword-time-regexp)
  3863. (defvar org-keyword-time-not-clock-regexp nil
  3864. "Matches any of the 3 keywords, together with the time stamp.")
  3865. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3866. (defvar org-maybe-keyword-time-regexp nil
  3867. "Matches a timestamp, possibly preceeded by a keyword.")
  3868. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3869. (defvar org-planning-or-clock-line-re nil
  3870. "Matches a line with planning or clock info.")
  3871. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3872. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3873. rear-nonsticky t mouse-map t fontified t)
  3874. "Properties to remove when a string without properties is wanted.")
  3875. (defsubst org-match-string-no-properties (num &optional string)
  3876. (if (featurep 'xemacs)
  3877. (let ((s (match-string num string)))
  3878. (remove-text-properties 0 (length s) org-rm-props s)
  3879. s)
  3880. (match-string-no-properties num string)))
  3881. (defsubst org-no-properties (s)
  3882. (if (fboundp 'set-text-properties)
  3883. (set-text-properties 0 (length s) nil s)
  3884. (remove-text-properties 0 (length s) org-rm-props s))
  3885. s)
  3886. (defsubst org-get-alist-option (option key)
  3887. (cond ((eq key t) t)
  3888. ((eq option t) t)
  3889. ((assoc key option) (cdr (assoc key option)))
  3890. (t (cdr (assq 'default option)))))
  3891. (defsubst org-inhibit-invisibility ()
  3892. "Modified `buffer-invisibility-spec' for Emacs 21.
  3893. Some ops with invisible text do not work correctly on Emacs 21. For these
  3894. we turn off invisibility temporarily. Use this in a `let' form."
  3895. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3896. (defsubst org-set-local (var value)
  3897. "Make VAR local in current buffer and set it to VALUE."
  3898. (set (make-variable-buffer-local var) value))
  3899. (defsubst org-mode-p ()
  3900. "Check if the current buffer is in Org-mode."
  3901. (eq major-mode 'org-mode))
  3902. (defsubst org-last (list)
  3903. "Return the last element of LIST."
  3904. (car (last list)))
  3905. (defun org-let (list &rest body)
  3906. (eval (cons 'let (cons list body))))
  3907. (put 'org-let 'lisp-indent-function 1)
  3908. (defun org-let2 (list1 list2 &rest body)
  3909. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3910. (put 'org-let2 'lisp-indent-function 2)
  3911. (defconst org-startup-options
  3912. '(("fold" org-startup-folded t)
  3913. ("overview" org-startup-folded t)
  3914. ("nofold" org-startup-folded nil)
  3915. ("showall" org-startup-folded nil)
  3916. ("content" org-startup-folded content)
  3917. ("hidestars" org-hide-leading-stars t)
  3918. ("showstars" org-hide-leading-stars nil)
  3919. ("odd" org-odd-levels-only t)
  3920. ("oddeven" org-odd-levels-only nil)
  3921. ("align" org-startup-align-all-tables t)
  3922. ("noalign" org-startup-align-all-tables nil)
  3923. ("customtime" org-display-custom-times t)
  3924. ("logdone" org-log-done time)
  3925. ("lognotedone" org-log-done note)
  3926. ("nologdone" org-log-done nil)
  3927. ("lognoteclock-out" org-log-note-clock-out t)
  3928. ("nolognoteclock-out" org-log-note-clock-out nil)
  3929. ("logrepeat" org-log-repeat state)
  3930. ("lognoterepeat" org-log-repeat note)
  3931. ("nologrepeat" org-log-repeat nil)
  3932. ("constcgs" constants-unit-system cgs)
  3933. ("constSI" constants-unit-system SI))
  3934. "Variable associated with STARTUP options for org-mode.
  3935. Each element is a list of three items: The startup options as written
  3936. in the #+STARTUP line, the corresponding variable, and the value to
  3937. set this variable to if the option is found. An optional forth element PUSH
  3938. means to push this value onto the list in the variable.")
  3939. (defun org-set-regexps-and-options ()
  3940. "Precompute regular expressions for current buffer."
  3941. (when (org-mode-p)
  3942. (org-set-local 'org-todo-kwd-alist nil)
  3943. (org-set-local 'org-todo-key-alist nil)
  3944. (org-set-local 'org-todo-key-trigger nil)
  3945. (org-set-local 'org-todo-keywords-1 nil)
  3946. (org-set-local 'org-done-keywords nil)
  3947. (org-set-local 'org-todo-heads nil)
  3948. (org-set-local 'org-todo-sets nil)
  3949. (org-set-local 'org-todo-log-states nil)
  3950. (let ((re (org-make-options-regexp
  3951. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3952. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3953. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3954. (splitre "[ \t]+")
  3955. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3956. tail sep kws1 prio props drawers)
  3957. (save-excursion
  3958. (save-restriction
  3959. (widen)
  3960. (goto-char (point-min))
  3961. (while (re-search-forward re nil t)
  3962. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3963. (cond
  3964. ((equal key "CATEGORY")
  3965. (if (string-match "[ \t]+$" value)
  3966. (setq value (replace-match "" t t value)))
  3967. (setq cat value))
  3968. ((member key '("SEQ_TODO" "TODO"))
  3969. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3970. ((equal key "TYP_TODO")
  3971. (push (cons 'type (org-split-string value splitre)) kwds))
  3972. ((equal key "TAGS")
  3973. (setq tags (append tags (org-split-string value splitre))))
  3974. ((equal key "COLUMNS")
  3975. (org-set-local 'org-columns-default-format value))
  3976. ((equal key "LINK")
  3977. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3978. (push (cons (match-string 1 value)
  3979. (org-trim (match-string 2 value)))
  3980. links)))
  3981. ((equal key "PRIORITIES")
  3982. (setq prio (org-split-string value " +")))
  3983. ((equal key "PROPERTY")
  3984. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3985. (push (cons (match-string 1 value) (match-string 2 value))
  3986. props)))
  3987. ((equal key "DRAWERS")
  3988. (setq drawers (org-split-string value splitre)))
  3989. ((equal key "CONSTANTS")
  3990. (setq const (append const (org-split-string value splitre))))
  3991. ((equal key "STARTUP")
  3992. (let ((opts (org-split-string value splitre))
  3993. l var val)
  3994. (while (setq l (pop opts))
  3995. (when (setq l (assoc l org-startup-options))
  3996. (setq var (nth 1 l) val (nth 2 l))
  3997. (if (not (nth 3 l))
  3998. (set (make-local-variable var) val)
  3999. (if (not (listp (symbol-value var)))
  4000. (set (make-local-variable var) nil))
  4001. (set (make-local-variable var) (symbol-value var))
  4002. (add-to-list var val))))))
  4003. ((equal key "ARCHIVE")
  4004. (string-match " *$" value)
  4005. (setq arch (replace-match "" t t value))
  4006. (remove-text-properties 0 (length arch)
  4007. '(face t fontified t) arch)))
  4008. )))
  4009. (when cat
  4010. (org-set-local 'org-category (intern cat))
  4011. (push (cons "CATEGORY" cat) props))
  4012. (when prio
  4013. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4014. (setq prio (mapcar 'string-to-char prio))
  4015. (org-set-local 'org-highest-priority (nth 0 prio))
  4016. (org-set-local 'org-lowest-priority (nth 1 prio))
  4017. (org-set-local 'org-default-priority (nth 2 prio)))
  4018. (and props (org-set-local 'org-local-properties (nreverse props)))
  4019. (and drawers (org-set-local 'org-drawers drawers))
  4020. (and arch (org-set-local 'org-archive-location arch))
  4021. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4022. ;; Process the TODO keywords
  4023. (unless kwds
  4024. ;; Use the global values as if they had been given locally.
  4025. (setq kwds (default-value 'org-todo-keywords))
  4026. (if (stringp (car kwds))
  4027. (setq kwds (list (cons org-todo-interpretation
  4028. (default-value 'org-todo-keywords)))))
  4029. (setq kwds (reverse kwds)))
  4030. (setq kwds (nreverse kwds))
  4031. (let (inter kws kw)
  4032. (while (setq kws (pop kwds))
  4033. (setq inter (pop kws) sep (member "|" kws)
  4034. kws0 (delete "|" (copy-sequence kws))
  4035. kwsa nil
  4036. kws1 (mapcar
  4037. (lambda (x)
  4038. ;; 1 2
  4039. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4040. (progn
  4041. (setq kw (match-string 1 x)
  4042. key (and (match-end 2) (match-string 2 x))
  4043. log (org-extract-log-state-settings x))
  4044. (push (cons kw (and key (string-to-char key))) kwsa)
  4045. (and log (push log org-todo-log-states))
  4046. kw)
  4047. (error "Invalid TODO keyword %s" x)))
  4048. kws0)
  4049. kwsa (if kwsa (append '((:startgroup))
  4050. (nreverse kwsa)
  4051. '((:endgroup))))
  4052. hw (car kws1)
  4053. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4054. tail (list inter hw (car dws) (org-last dws)))
  4055. (add-to-list 'org-todo-heads hw 'append)
  4056. (push kws1 org-todo-sets)
  4057. (setq org-done-keywords (append org-done-keywords dws nil))
  4058. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4059. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4060. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4061. (setq org-todo-sets (nreverse org-todo-sets)
  4062. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4063. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4064. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4065. ;; Process the constants
  4066. (when const
  4067. (let (e cst)
  4068. (while (setq e (pop const))
  4069. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4070. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4071. (setq org-table-formula-constants-local cst)))
  4072. ;; Process the tags.
  4073. (when tags
  4074. (let (e tgs)
  4075. (while (setq e (pop tags))
  4076. (cond
  4077. ((equal e "{") (push '(:startgroup) tgs))
  4078. ((equal e "}") (push '(:endgroup) tgs))
  4079. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4080. (push (cons (match-string 1 e)
  4081. (string-to-char (match-string 2 e)))
  4082. tgs))
  4083. (t (push (list e) tgs))))
  4084. (org-set-local 'org-tag-alist nil)
  4085. (while (setq e (pop tgs))
  4086. (or (and (stringp (car e))
  4087. (assoc (car e) org-tag-alist))
  4088. (push e org-tag-alist))))))
  4089. ;; Compute the regular expressions and other local variables
  4090. (if (not org-done-keywords)
  4091. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4092. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4093. (length org-scheduled-string)))
  4094. org-drawer-regexp
  4095. (concat "^[ \t]*:\\("
  4096. (mapconcat 'regexp-quote org-drawers "\\|")
  4097. "\\):[ \t]*$")
  4098. org-not-done-keywords
  4099. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4100. org-todo-regexp
  4101. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4102. "\\|") "\\)\\>")
  4103. org-not-done-regexp
  4104. (concat "\\<\\("
  4105. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4106. "\\)\\>")
  4107. org-todo-line-regexp
  4108. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4109. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4110. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4111. org-complex-heading-regexp
  4112. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4113. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4114. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4115. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4116. org-nl-done-regexp
  4117. (concat "\n\\*+[ \t]+"
  4118. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4119. "\\)" "\\>")
  4120. org-todo-line-tags-regexp
  4121. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4122. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4123. (org-re
  4124. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4125. org-looking-at-done-regexp
  4126. (concat "^" "\\(?:"
  4127. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4128. "\\>")
  4129. org-deadline-regexp (concat "\\<" org-deadline-string)
  4130. org-deadline-time-regexp
  4131. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4132. org-deadline-line-regexp
  4133. (concat "\\<\\(" org-deadline-string "\\).*")
  4134. org-scheduled-regexp
  4135. (concat "\\<" org-scheduled-string)
  4136. org-scheduled-time-regexp
  4137. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4138. org-closed-time-regexp
  4139. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4140. org-keyword-time-regexp
  4141. (concat "\\<\\(" org-scheduled-string
  4142. "\\|" org-deadline-string
  4143. "\\|" org-closed-string
  4144. "\\|" org-clock-string "\\)"
  4145. " *[[<]\\([^]>]+\\)[]>]")
  4146. org-keyword-time-not-clock-regexp
  4147. (concat "\\<\\(" org-scheduled-string
  4148. "\\|" org-deadline-string
  4149. "\\|" org-closed-string
  4150. "\\)"
  4151. " *[[<]\\([^]>]+\\)[]>]")
  4152. org-maybe-keyword-time-regexp
  4153. (concat "\\(\\<\\(" org-scheduled-string
  4154. "\\|" org-deadline-string
  4155. "\\|" org-closed-string
  4156. "\\|" org-clock-string "\\)\\)?"
  4157. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4158. org-planning-or-clock-line-re
  4159. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4160. "\\|" org-deadline-string
  4161. "\\|" org-closed-string "\\|" org-clock-string
  4162. "\\)\\>\\)")
  4163. )
  4164. (org-compute-latex-and-specials-regexp)
  4165. (org-set-font-lock-defaults)))
  4166. (defun org-extract-log-state-settings (x)
  4167. "Extract the log state setting from a TODO keyword string.
  4168. This will extract info from a string like \"WAIT(w@/!)\"."
  4169. (let (kw key log1 log2)
  4170. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4171. (setq kw (match-string 1 x)
  4172. key (and (match-end 2) (match-string 2 x))
  4173. log1 (and (match-end 3) (match-string 3 x))
  4174. log2 (and (match-end 4) (match-string 4 x)))
  4175. (and (or log1 log2)
  4176. (list kw
  4177. (and log1 (if (equal log1 "!") 'time 'note))
  4178. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4179. (defun org-remove-keyword-keys (list)
  4180. "Remove a pair of parenthesis at the end of each string in LIST."
  4181. (mapcar (lambda (x)
  4182. (if (string-match "(.*)$" x)
  4183. (substring x 0 (match-beginning 0))
  4184. x))
  4185. list))
  4186. ;; FIXME: this could be done much better, using second characters etc.
  4187. (defun org-assign-fast-keys (alist)
  4188. "Assign fast keys to a keyword-key alist.
  4189. Respect keys that are already there."
  4190. (let (new e k c c1 c2 (char ?a))
  4191. (while (setq e (pop alist))
  4192. (cond
  4193. ((equal e '(:startgroup)) (push e new))
  4194. ((equal e '(:endgroup)) (push e new))
  4195. (t
  4196. (setq k (car e) c2 nil)
  4197. (if (cdr e)
  4198. (setq c (cdr e))
  4199. ;; automatically assign a character.
  4200. (setq c1 (string-to-char
  4201. (downcase (substring
  4202. k (if (= (string-to-char k) ?@) 1 0)))))
  4203. (if (or (rassoc c1 new) (rassoc c1 alist))
  4204. (while (or (rassoc char new) (rassoc char alist))
  4205. (setq char (1+ char)))
  4206. (setq c2 c1))
  4207. (setq c (or c2 char)))
  4208. (push (cons k c) new))))
  4209. (nreverse new)))
  4210. ;;; Some variables ujsed in various places
  4211. (defvar org-window-configuration nil
  4212. "Used in various places to store a window configuration.")
  4213. (defvar org-finish-function nil
  4214. "Function to be called when `C-c C-c' is used.
  4215. This is for getting out of special buffers like remember.")
  4216. ;; FIXME: Occasionally check by commenting these, to make sure
  4217. ;; no other functions uses these, forgetting to let-bind them.
  4218. (defvar entry)
  4219. (defvar state)
  4220. (defvar last-state)
  4221. (defvar date)
  4222. (defvar description)
  4223. ;; Defined somewhere in this file, but used before definition.
  4224. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4225. (defvar org-agenda-buffer-name)
  4226. (defvar org-agenda-undo-list)
  4227. (defvar org-agenda-pending-undo-list)
  4228. (defvar org-agenda-overriding-header)
  4229. (defvar orgtbl-mode)
  4230. (defvar org-html-entities)
  4231. (defvar org-struct-menu)
  4232. (defvar org-org-menu)
  4233. (defvar org-tbl-menu)
  4234. (defvar org-agenda-keymap)
  4235. ;;;; Emacs/XEmacs compatibility
  4236. ;; Overlay compatibility functions
  4237. (defun org-make-overlay (beg end &optional buffer)
  4238. (if (featurep 'xemacs)
  4239. (make-extent beg end buffer)
  4240. (make-overlay beg end buffer)))
  4241. (defun org-delete-overlay (ovl)
  4242. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4243. (defun org-detach-overlay (ovl)
  4244. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4245. (defun org-move-overlay (ovl beg end &optional buffer)
  4246. (if (featurep 'xemacs)
  4247. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4248. (move-overlay ovl beg end buffer)))
  4249. (defun org-overlay-put (ovl prop value)
  4250. (if (featurep 'xemacs)
  4251. (set-extent-property ovl prop value)
  4252. (overlay-put ovl prop value)))
  4253. (defun org-overlay-display (ovl text &optional face evap)
  4254. "Make overlay OVL display TEXT with face FACE."
  4255. (if (featurep 'xemacs)
  4256. (let ((gl (make-glyph text)))
  4257. (and face (set-glyph-face gl face))
  4258. (set-extent-property ovl 'invisible t)
  4259. (set-extent-property ovl 'end-glyph gl))
  4260. (overlay-put ovl 'display text)
  4261. (if face (overlay-put ovl 'face face))
  4262. (if evap (overlay-put ovl 'evaporate t))))
  4263. (defun org-overlay-before-string (ovl text &optional face evap)
  4264. "Make overlay OVL display TEXT with face FACE."
  4265. (if (featurep 'xemacs)
  4266. (let ((gl (make-glyph text)))
  4267. (and face (set-glyph-face gl face))
  4268. (set-extent-property ovl 'begin-glyph gl))
  4269. (if face (org-add-props text nil 'face face))
  4270. (overlay-put ovl 'before-string text)
  4271. (if evap (overlay-put ovl 'evaporate t))))
  4272. (defun org-overlay-get (ovl prop)
  4273. (if (featurep 'xemacs)
  4274. (extent-property ovl prop)
  4275. (overlay-get ovl prop)))
  4276. (defun org-overlays-at (pos)
  4277. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4278. (defun org-overlays-in (&optional start end)
  4279. (if (featurep 'xemacs)
  4280. (extent-list nil start end)
  4281. (overlays-in start end)))
  4282. (defun org-overlay-start (o)
  4283. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4284. (defun org-overlay-end (o)
  4285. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4286. (defun org-find-overlays (prop &optional pos delete)
  4287. "Find all overlays specifying PROP at POS or point.
  4288. If DELETE is non-nil, delete all those overlays."
  4289. (let ((overlays (org-overlays-at (or pos (point))))
  4290. ov found)
  4291. (while (setq ov (pop overlays))
  4292. (if (org-overlay-get ov prop)
  4293. (if delete (org-delete-overlay ov) (push ov found))))
  4294. found))
  4295. ;; Region compatibility
  4296. (defun org-add-hook (hook function &optional append local)
  4297. "Add-hook, compatible with both Emacsen."
  4298. (if (and local (featurep 'xemacs))
  4299. (add-local-hook hook function append)
  4300. (add-hook hook function append local)))
  4301. (defvar org-ignore-region nil
  4302. "To temporarily disable the active region.")
  4303. (defun org-region-active-p ()
  4304. "Is `transient-mark-mode' on and the region active?
  4305. Works on both Emacs and XEmacs."
  4306. (if org-ignore-region
  4307. nil
  4308. (if (featurep 'xemacs)
  4309. (and zmacs-regions (region-active-p))
  4310. (if (fboundp 'use-region-p)
  4311. (use-region-p)
  4312. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4313. ;; Invisibility compatibility
  4314. (defun org-add-to-invisibility-spec (arg)
  4315. "Add elements to `buffer-invisibility-spec'.
  4316. See documentation for `buffer-invisibility-spec' for the kind of elements
  4317. that can be added."
  4318. (cond
  4319. ((fboundp 'add-to-invisibility-spec)
  4320. (add-to-invisibility-spec arg))
  4321. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4322. (setq buffer-invisibility-spec (list arg)))
  4323. (t
  4324. (setq buffer-invisibility-spec
  4325. (cons arg buffer-invisibility-spec)))))
  4326. (defun org-remove-from-invisibility-spec (arg)
  4327. "Remove elements from `buffer-invisibility-spec'."
  4328. (if (fboundp 'remove-from-invisibility-spec)
  4329. (remove-from-invisibility-spec arg)
  4330. (if (consp buffer-invisibility-spec)
  4331. (setq buffer-invisibility-spec
  4332. (delete arg buffer-invisibility-spec)))))
  4333. (defun org-in-invisibility-spec-p (arg)
  4334. "Is ARG a member of `buffer-invisibility-spec'?"
  4335. (if (consp buffer-invisibility-spec)
  4336. (member arg buffer-invisibility-spec)
  4337. nil))
  4338. ;;;; Define the Org-mode
  4339. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4340. (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."))
  4341. ;; We use a before-change function to check if a table might need
  4342. ;; an update.
  4343. (defvar org-table-may-need-update t
  4344. "Indicates that a table might need an update.
  4345. This variable is set by `org-before-change-function'.
  4346. `org-table-align' sets it back to nil.")
  4347. (defvar org-mode-map)
  4348. (defvar org-mode-hook nil)
  4349. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4350. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4351. (defvar org-table-buffer-is-an nil)
  4352. (defconst org-outline-regexp "\\*+ ")
  4353. ;;;###autoload
  4354. (define-derived-mode org-mode outline-mode "Org"
  4355. "Outline-based notes management and organizer, alias
  4356. \"Carsten's outline-mode for keeping track of everything.\"
  4357. Org-mode develops organizational tasks around a NOTES file which
  4358. contains information about projects as plain text. Org-mode is
  4359. implemented on top of outline-mode, which is ideal to keep the content
  4360. of large files well structured. It supports ToDo items, deadlines and
  4361. time stamps, which magically appear in the diary listing of the Emacs
  4362. calendar. Tables are easily created with a built-in table editor.
  4363. Plain text URL-like links connect to websites, emails (VM), Usenet
  4364. messages (Gnus), BBDB entries, and any files related to the project.
  4365. For printing and sharing of notes, an Org-mode file (or a part of it)
  4366. can be exported as a structured ASCII or HTML file.
  4367. The following commands are available:
  4368. \\{org-mode-map}"
  4369. ;; Get rid of Outline menus, they are not needed
  4370. ;; Need to do this here because define-derived-mode sets up
  4371. ;; the keymap so late. Still, it is a waste to call this each time
  4372. ;; we switch another buffer into org-mode.
  4373. (if (featurep 'xemacs)
  4374. (when (boundp 'outline-mode-menu-heading)
  4375. ;; Assume this is Greg's port, it used easymenu
  4376. (easy-menu-remove outline-mode-menu-heading)
  4377. (easy-menu-remove outline-mode-menu-show)
  4378. (easy-menu-remove outline-mode-menu-hide))
  4379. (define-key org-mode-map [menu-bar headings] 'undefined)
  4380. (define-key org-mode-map [menu-bar hide] 'undefined)
  4381. (define-key org-mode-map [menu-bar show] 'undefined))
  4382. (easy-menu-add org-org-menu)
  4383. (easy-menu-add org-tbl-menu)
  4384. (org-install-agenda-files-menu)
  4385. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4386. (org-add-to-invisibility-spec '(org-cwidth))
  4387. (when (featurep 'xemacs)
  4388. (org-set-local 'line-move-ignore-invisible t))
  4389. (org-set-local 'outline-regexp org-outline-regexp)
  4390. (org-set-local 'outline-level 'org-outline-level)
  4391. (when (and org-ellipsis
  4392. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4393. (fboundp 'make-glyph-code))
  4394. (unless org-display-table
  4395. (setq org-display-table (make-display-table)))
  4396. (set-display-table-slot
  4397. org-display-table 4
  4398. (vconcat (mapcar
  4399. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4400. org-ellipsis)))
  4401. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4402. (setq buffer-display-table org-display-table))
  4403. (org-set-regexps-and-options)
  4404. ;; Calc embedded
  4405. (org-set-local 'calc-embedded-open-mode "# ")
  4406. (modify-syntax-entry ?# "<")
  4407. (modify-syntax-entry ?@ "w")
  4408. (if org-startup-truncated (setq truncate-lines t))
  4409. (org-set-local 'font-lock-unfontify-region-function
  4410. 'org-unfontify-region)
  4411. ;; Activate before-change-function
  4412. (org-set-local 'org-table-may-need-update t)
  4413. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4414. 'local)
  4415. ;; Check for running clock before killing a buffer
  4416. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4417. ;; Paragraphs and auto-filling
  4418. (org-set-autofill-regexps)
  4419. (setq indent-line-function 'org-indent-line-function)
  4420. (org-update-radio-target-regexp)
  4421. ;; Comment characters
  4422. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4423. (org-set-local 'comment-padding " ")
  4424. ;; Align options lines
  4425. (org-set-local
  4426. 'align-mode-rules-list
  4427. '((org-in-buffer-settings
  4428. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4429. (modes . '(org-mode)))))
  4430. ;; Imenu
  4431. (org-set-local 'imenu-create-index-function
  4432. 'org-imenu-get-tree)
  4433. ;; Make isearch reveal context
  4434. (if (or (featurep 'xemacs)
  4435. (not (boundp 'outline-isearch-open-invisible-function)))
  4436. ;; Emacs 21 and XEmacs make use of the hook
  4437. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4438. ;; Emacs 22 deals with this through a special variable
  4439. (org-set-local 'outline-isearch-open-invisible-function
  4440. (lambda (&rest ignore) (org-show-context 'isearch))))
  4441. ;; If empty file that did not turn on org-mode automatically, make it to.
  4442. (if (and org-insert-mode-line-in-empty-file
  4443. (interactive-p)
  4444. (= (point-min) (point-max)))
  4445. (insert "# -*- mode: org -*-\n\n"))
  4446. (unless org-inhibit-startup
  4447. (when org-startup-align-all-tables
  4448. (let ((bmp (buffer-modified-p)))
  4449. (org-table-map-tables 'org-table-align)
  4450. (set-buffer-modified-p bmp)))
  4451. (org-cycle-hide-drawers 'all)
  4452. (cond
  4453. ((eq org-startup-folded t)
  4454. (org-cycle '(4)))
  4455. ((eq org-startup-folded 'content)
  4456. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4457. (org-cycle '(4)) (org-cycle '(4)))))))
  4458. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4459. (defsubst org-call-with-arg (command arg)
  4460. "Call COMMAND interactively, but pretend prefix are was ARG."
  4461. (let ((current-prefix-arg arg)) (call-interactively command)))
  4462. (defsubst org-current-line (&optional pos)
  4463. (save-excursion
  4464. (and pos (goto-char pos))
  4465. ;; works also in narrowed buffer, because we start at 1, not point-min
  4466. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4467. (defun org-current-time ()
  4468. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4469. (if (> org-time-stamp-rounding-minutes 0)
  4470. (let ((r org-time-stamp-rounding-minutes)
  4471. (time (decode-time)))
  4472. (apply 'encode-time
  4473. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4474. (nthcdr 2 time))))
  4475. (current-time)))
  4476. (defun org-add-props (string plist &rest props)
  4477. "Add text properties to entire string, from beginning to end.
  4478. PLIST may be a list of properties, PROPS are individual properties and values
  4479. that will be added to PLIST. Returns the string that was modified."
  4480. (add-text-properties
  4481. 0 (length string) (if props (append plist props) plist) string)
  4482. string)
  4483. (put 'org-add-props 'lisp-indent-function 2)
  4484. ;;;; Font-Lock stuff, including the activators
  4485. (defvar org-mouse-map (make-sparse-keymap))
  4486. (org-defkey org-mouse-map
  4487. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4488. (org-defkey org-mouse-map
  4489. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4490. (when org-mouse-1-follows-link
  4491. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4492. (when org-tab-follows-link
  4493. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4494. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4495. (when org-return-follows-link
  4496. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4497. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4498. (require 'font-lock)
  4499. (defconst org-non-link-chars "]\t\n\r<>")
  4500. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4501. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4502. (defvar org-link-re-with-space nil
  4503. "Matches a link with spaces, optional angular brackets around it.")
  4504. (defvar org-link-re-with-space2 nil
  4505. "Matches a link with spaces, optional angular brackets around it.")
  4506. (defvar org-angle-link-re nil
  4507. "Matches link with angular brackets, spaces are allowed.")
  4508. (defvar org-plain-link-re nil
  4509. "Matches plain link, without spaces.")
  4510. (defvar org-bracket-link-regexp nil
  4511. "Matches a link in double brackets.")
  4512. (defvar org-bracket-link-analytic-regexp nil
  4513. "Regular expression used to analyze links.
  4514. Here is what the match groups contain after a match:
  4515. 1: http:
  4516. 2: http
  4517. 3: path
  4518. 4: [desc]
  4519. 5: desc")
  4520. (defvar org-any-link-re nil
  4521. "Regular expression matching any link.")
  4522. (defun org-make-link-regexps ()
  4523. "Update the link regular expressions.
  4524. This should be called after the variable `org-link-types' has changed."
  4525. (setq org-link-re-with-space
  4526. (concat
  4527. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4528. "\\([^" org-non-link-chars " ]"
  4529. "[^" org-non-link-chars "]*"
  4530. "[^" org-non-link-chars " ]\\)>?")
  4531. org-link-re-with-space2
  4532. (concat
  4533. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4534. "\\([^" org-non-link-chars " ]"
  4535. "[^]\t\n\r]*"
  4536. "[^" org-non-link-chars " ]\\)>?")
  4537. org-angle-link-re
  4538. (concat
  4539. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4540. "\\([^" org-non-link-chars " ]"
  4541. "[^" org-non-link-chars "]*"
  4542. "\\)>")
  4543. org-plain-link-re
  4544. (concat
  4545. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4546. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4547. org-bracket-link-regexp
  4548. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4549. org-bracket-link-analytic-regexp
  4550. (concat
  4551. "\\[\\["
  4552. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4553. "\\([^]]+\\)"
  4554. "\\]"
  4555. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4556. "\\]")
  4557. org-any-link-re
  4558. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4559. org-angle-link-re "\\)\\|\\("
  4560. org-plain-link-re "\\)")))
  4561. (org-make-link-regexps)
  4562. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4563. "Regular expression for fast time stamp matching.")
  4564. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4565. "Regular expression for fast time stamp matching.")
  4566. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4567. "Regular expression matching time strings for analysis.
  4568. This one does not require the space after the date.")
  4569. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4570. "Regular expression matching time strings for analysis.")
  4571. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4572. "Regular expression matching time stamps, with groups.")
  4573. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4574. "Regular expression matching time stamps (also [..]), with groups.")
  4575. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4576. "Regular expression matching a time stamp range.")
  4577. (defconst org-tr-regexp-both
  4578. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4579. "Regular expression matching a time stamp range.")
  4580. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4581. org-ts-regexp "\\)?")
  4582. "Regular expression matching a time stamp or time stamp range.")
  4583. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4584. org-ts-regexp-both "\\)?")
  4585. "Regular expression matching a time stamp or time stamp range.
  4586. The time stamps may be either active or inactive.")
  4587. (defvar org-emph-face nil)
  4588. (defun org-do-emphasis-faces (limit)
  4589. "Run through the buffer and add overlays to links."
  4590. (let (rtn)
  4591. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4592. (if (not (= (char-after (match-beginning 3))
  4593. (char-after (match-beginning 4))))
  4594. (progn
  4595. (setq rtn t)
  4596. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4597. 'face
  4598. (nth 1 (assoc (match-string 3)
  4599. org-emphasis-alist)))
  4600. (add-text-properties (match-beginning 2) (match-end 2)
  4601. '(font-lock-multiline t))
  4602. (when org-hide-emphasis-markers
  4603. (add-text-properties (match-end 4) (match-beginning 5)
  4604. '(invisible org-link))
  4605. (add-text-properties (match-beginning 3) (match-end 3)
  4606. '(invisible org-link)))))
  4607. (backward-char 1))
  4608. rtn))
  4609. (defun org-emphasize (&optional char)
  4610. "Insert or change an emphasis, i.e. a font like bold or italic.
  4611. If there is an active region, change that region to a new emphasis.
  4612. If there is no region, just insert the marker characters and position
  4613. the cursor between them.
  4614. CHAR should be either the marker character, or the first character of the
  4615. HTML tag associated with that emphasis. If CHAR is a space, the means
  4616. to remove the emphasis of the selected region.
  4617. If char is not given (for example in an interactive call) it
  4618. will be prompted for."
  4619. (interactive)
  4620. (let ((eal org-emphasis-alist) e det
  4621. (erc org-emphasis-regexp-components)
  4622. (prompt "")
  4623. (string "") beg end move tag c s)
  4624. (if (org-region-active-p)
  4625. (setq beg (region-beginning) end (region-end)
  4626. string (buffer-substring beg end))
  4627. (setq move t))
  4628. (while (setq e (pop eal))
  4629. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4630. c (aref tag 0))
  4631. (push (cons c (string-to-char (car e))) det)
  4632. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4633. (substring tag 1)))))
  4634. (unless char
  4635. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4636. (setq char (read-char-exclusive)))
  4637. (setq char (or (cdr (assoc char det)) char))
  4638. (if (equal char ?\ )
  4639. (setq s "" move nil)
  4640. (unless (assoc (char-to-string char) org-emphasis-alist)
  4641. (error "No such emphasis marker: \"%c\"" char))
  4642. (setq s (char-to-string char)))
  4643. (while (and (> (length string) 1)
  4644. (equal (substring string 0 1) (substring string -1))
  4645. (assoc (substring string 0 1) org-emphasis-alist))
  4646. (setq string (substring string 1 -1)))
  4647. (setq string (concat s string s))
  4648. (if beg (delete-region beg end))
  4649. (unless (or (bolp)
  4650. (string-match (concat "[" (nth 0 erc) "\n]")
  4651. (char-to-string (char-before (point)))))
  4652. (insert " "))
  4653. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4654. (char-to-string (char-after (point))))
  4655. (insert " ") (backward-char 1))
  4656. (insert string)
  4657. (and move (backward-char 1))))
  4658. (defconst org-nonsticky-props
  4659. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4660. (defun org-activate-plain-links (limit)
  4661. "Run through the buffer and add overlays to links."
  4662. (catch 'exit
  4663. (let (f)
  4664. (while (re-search-forward org-plain-link-re limit t)
  4665. (setq f (get-text-property (match-beginning 0) 'face))
  4666. (if (or (eq f 'org-tag)
  4667. (and (listp f) (memq 'org-tag f)))
  4668. nil
  4669. (add-text-properties (match-beginning 0) (match-end 0)
  4670. (list 'mouse-face 'highlight
  4671. 'rear-nonsticky org-nonsticky-props
  4672. 'keymap org-mouse-map
  4673. ))
  4674. (throw 'exit t))))))
  4675. (defun org-activate-code (limit)
  4676. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4677. (unless (get-text-property (match-beginning 1) 'face)
  4678. (remove-text-properties (match-beginning 0) (match-end 0)
  4679. '(display t invisible t intangible t))
  4680. t)))
  4681. (defun org-activate-angle-links (limit)
  4682. "Run through the buffer and add overlays to links."
  4683. (if (re-search-forward org-angle-link-re limit t)
  4684. (progn
  4685. (add-text-properties (match-beginning 0) (match-end 0)
  4686. (list 'mouse-face 'highlight
  4687. 'rear-nonsticky org-nonsticky-props
  4688. 'keymap org-mouse-map
  4689. ))
  4690. t)))
  4691. (defmacro org-maybe-intangible (props)
  4692. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4693. In emacs 21, invisible text is not avoided by the command loop, so the
  4694. intangible property is needed to make sure point skips this text.
  4695. In Emacs 22, this is not necessary. The intangible text property has
  4696. led to problems with flyspell. These problems are fixed in flyspell.el,
  4697. but we still avoid setting the property in Emacs 22 and later.
  4698. We use a macro so that the test can happen at compilation time."
  4699. (if (< emacs-major-version 22)
  4700. `(append '(intangible t) ,props)
  4701. props))
  4702. (defun org-activate-bracket-links (limit)
  4703. "Run through the buffer and add overlays to bracketed links."
  4704. (if (re-search-forward org-bracket-link-regexp limit t)
  4705. (let* ((help (concat "LINK: "
  4706. (org-match-string-no-properties 1)))
  4707. ;; FIXME: above we should remove the escapes.
  4708. ;; but that requires another match, protecting match data,
  4709. ;; a lot of overhead for font-lock.
  4710. (ip (org-maybe-intangible
  4711. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4712. 'keymap org-mouse-map 'mouse-face 'highlight
  4713. 'font-lock-multiline t 'help-echo help)))
  4714. (vp (list 'rear-nonsticky org-nonsticky-props
  4715. 'keymap org-mouse-map 'mouse-face 'highlight
  4716. ' font-lock-multiline t 'help-echo help)))
  4717. ;; We need to remove the invisible property here. Table narrowing
  4718. ;; may have made some of this invisible.
  4719. (remove-text-properties (match-beginning 0) (match-end 0)
  4720. '(invisible nil))
  4721. (if (match-end 3)
  4722. (progn
  4723. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4724. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4725. (add-text-properties (match-end 3) (match-end 0) ip))
  4726. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4727. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4728. (add-text-properties (match-end 1) (match-end 0) ip))
  4729. t)))
  4730. (defun org-activate-dates (limit)
  4731. "Run through the buffer and add overlays to dates."
  4732. (if (re-search-forward org-tsr-regexp-both limit t)
  4733. (progn
  4734. (add-text-properties (match-beginning 0) (match-end 0)
  4735. (list 'mouse-face 'highlight
  4736. 'rear-nonsticky org-nonsticky-props
  4737. 'keymap org-mouse-map))
  4738. (when org-display-custom-times
  4739. (if (match-end 3)
  4740. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4741. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4742. t)))
  4743. (defvar org-target-link-regexp nil
  4744. "Regular expression matching radio targets in plain text.")
  4745. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4746. "Regular expression matching a link target.")
  4747. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4748. "Regular expression matching a radio target.")
  4749. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4750. "Regular expression matching any target.")
  4751. (defun org-activate-target-links (limit)
  4752. "Run through the buffer and add overlays to target matches."
  4753. (when org-target-link-regexp
  4754. (let ((case-fold-search t))
  4755. (if (re-search-forward org-target-link-regexp limit t)
  4756. (progn
  4757. (add-text-properties (match-beginning 0) (match-end 0)
  4758. (list 'mouse-face 'highlight
  4759. 'rear-nonsticky org-nonsticky-props
  4760. 'keymap org-mouse-map
  4761. 'help-echo "Radio target link"
  4762. 'org-linked-text t))
  4763. t)))))
  4764. (defun org-update-radio-target-regexp ()
  4765. "Find all radio targets in this file and update the regular expression."
  4766. (interactive)
  4767. (when (memq 'radio org-activate-links)
  4768. (setq org-target-link-regexp
  4769. (org-make-target-link-regexp (org-all-targets 'radio)))
  4770. (org-restart-font-lock)))
  4771. (defun org-hide-wide-columns (limit)
  4772. (let (s e)
  4773. (setq s (text-property-any (point) (or limit (point-max))
  4774. 'org-cwidth t))
  4775. (when s
  4776. (setq e (next-single-property-change s 'org-cwidth))
  4777. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4778. (goto-char e)
  4779. t)))
  4780. (defvar org-latex-and-specials-regexp nil
  4781. "Regular expression for highlighting export special stuff.")
  4782. (defvar org-match-substring-regexp)
  4783. (defvar org-match-substring-with-braces-regexp)
  4784. (defvar org-export-html-special-string-regexps)
  4785. (defun org-compute-latex-and-specials-regexp ()
  4786. "Compute regular expression for stuff treated specially by exporters."
  4787. (if (not org-highlight-latex-fragments-and-specials)
  4788. (org-set-local 'org-latex-and-specials-regexp nil)
  4789. (let*
  4790. ((matchers (plist-get org-format-latex-options :matchers))
  4791. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4792. org-latex-regexps)))
  4793. (options (org-combine-plists (org-default-export-plist)
  4794. (org-infile-export-plist)))
  4795. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4796. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4797. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4798. (org-export-html-expand (plist-get options :expand-quoted-html))
  4799. (org-export-with-special-strings (plist-get options :special-strings))
  4800. (re-sub
  4801. (cond
  4802. ((equal org-export-with-sub-superscripts '{})
  4803. (list org-match-substring-with-braces-regexp))
  4804. (org-export-with-sub-superscripts
  4805. (list org-match-substring-regexp))
  4806. (t nil)))
  4807. (re-latex
  4808. (if org-export-with-LaTeX-fragments
  4809. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4810. (re-macros
  4811. (if org-export-with-TeX-macros
  4812. (list (concat "\\\\"
  4813. (regexp-opt
  4814. (append (mapcar 'car org-html-entities)
  4815. (if (boundp 'org-latex-entities)
  4816. org-latex-entities nil))
  4817. 'words))) ; FIXME
  4818. ))
  4819. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4820. (re-special (if org-export-with-special-strings
  4821. (mapcar (lambda (x) (car x))
  4822. org-export-html-special-string-regexps)))
  4823. (re-rest
  4824. (delq nil
  4825. (list
  4826. (if org-export-html-expand "@<[^>\n]+>")
  4827. ))))
  4828. (org-set-local
  4829. 'org-latex-and-specials-regexp
  4830. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4831. re-rest) "\\|")))))
  4832. (defface org-latex-and-export-specials
  4833. (let ((font (cond ((assq :inherit custom-face-attributes)
  4834. '(:inherit underline))
  4835. (t '(:underline t)))))
  4836. `((((class grayscale) (background light))
  4837. (:foreground "DimGray" ,@font))
  4838. (((class grayscale) (background dark))
  4839. (:foreground "LightGray" ,@font))
  4840. (((class color) (background light))
  4841. (:foreground "SaddleBrown"))
  4842. (((class color) (background dark))
  4843. (:foreground "burlywood"))
  4844. (t (,@font))))
  4845. "Face used to highlight math latex and other special exporter stuff."
  4846. :group 'org-faces)
  4847. (defun org-do-latex-and-special-faces (limit)
  4848. "Run through the buffer and add overlays to links."
  4849. (when org-latex-and-specials-regexp
  4850. (let (rtn d)
  4851. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4852. limit t))
  4853. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4854. 'face))
  4855. '(org-code org-verbatim underline)))
  4856. (progn
  4857. (setq rtn t
  4858. d (cond ((member (char-after (1+ (match-beginning 0)))
  4859. '(?_ ?^)) 1)
  4860. (t 0)))
  4861. (font-lock-prepend-text-property
  4862. (+ d (match-beginning 0)) (match-end 0)
  4863. 'face 'org-latex-and-export-specials)
  4864. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4865. '(font-lock-multiline t)))))
  4866. rtn)))
  4867. (defun org-restart-font-lock ()
  4868. "Restart font-lock-mode, to force refontification."
  4869. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4870. (font-lock-mode -1)
  4871. (font-lock-mode 1)))
  4872. (defun org-all-targets (&optional radio)
  4873. "Return a list of all targets in this file.
  4874. With optional argument RADIO, only find radio targets."
  4875. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4876. rtn)
  4877. (save-excursion
  4878. (goto-char (point-min))
  4879. (while (re-search-forward re nil t)
  4880. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4881. rtn)))
  4882. (defun org-make-target-link-regexp (targets)
  4883. "Make regular expression matching all strings in TARGETS.
  4884. The regular expression finds the targets also if there is a line break
  4885. between words."
  4886. (and targets
  4887. (concat
  4888. "\\<\\("
  4889. (mapconcat
  4890. (lambda (x)
  4891. (while (string-match " +" x)
  4892. (setq x (replace-match "\\s-+" t t x)))
  4893. x)
  4894. targets
  4895. "\\|")
  4896. "\\)\\>")))
  4897. (defun org-activate-tags (limit)
  4898. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4899. (progn
  4900. (add-text-properties (match-beginning 1) (match-end 1)
  4901. (list 'mouse-face 'highlight
  4902. 'rear-nonsticky org-nonsticky-props
  4903. 'keymap org-mouse-map))
  4904. t)))
  4905. (defun org-outline-level ()
  4906. (save-excursion
  4907. (looking-at outline-regexp)
  4908. (if (match-beginning 1)
  4909. (+ (org-get-string-indentation (match-string 1)) 1000)
  4910. (1- (- (match-end 0) (match-beginning 0))))))
  4911. (defvar org-font-lock-keywords nil)
  4912. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4913. "Regular expression matching a property line.")
  4914. (defun org-set-font-lock-defaults ()
  4915. (let* ((em org-fontify-emphasized-text)
  4916. (lk org-activate-links)
  4917. (org-font-lock-extra-keywords
  4918. (list
  4919. ;; Headlines
  4920. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4921. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4922. ;; Table lines
  4923. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4924. (1 'org-table t))
  4925. ;; Table internals
  4926. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4927. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4928. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4929. ;; Drawers
  4930. (list org-drawer-regexp '(0 'org-special-keyword t))
  4931. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4932. ;; Properties
  4933. (list org-property-re
  4934. '(1 'org-special-keyword t)
  4935. '(3 'org-property-value t))
  4936. (if org-format-transports-properties-p
  4937. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4938. ;; Links
  4939. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4940. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4941. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4942. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4943. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4944. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4945. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4946. '(org-hide-wide-columns (0 nil append))
  4947. ;; TODO lines
  4948. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4949. '(1 (org-get-todo-face 1) t))
  4950. ;; DONE
  4951. (if org-fontify-done-headline
  4952. (list (concat "^[*]+ +\\<\\("
  4953. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4954. "\\)\\(.*\\)")
  4955. '(2 'org-headline-done t))
  4956. nil)
  4957. ;; Priorities
  4958. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4959. ;; Special keywords
  4960. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4961. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4962. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4963. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4964. ;; Emphasis
  4965. (if em
  4966. (if (featurep 'xemacs)
  4967. '(org-do-emphasis-faces (0 nil append))
  4968. '(org-do-emphasis-faces)))
  4969. ;; Checkboxes
  4970. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4971. 2 'bold prepend)
  4972. (if org-provide-checkbox-statistics
  4973. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4974. (0 (org-get-checkbox-statistics-face) t)))
  4975. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4976. '(1 'org-archived prepend))
  4977. ;; Specials
  4978. '(org-do-latex-and-special-faces)
  4979. ;; Code
  4980. '(org-activate-code (1 'org-code t))
  4981. ;; COMMENT
  4982. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4983. "\\|" org-quote-string "\\)\\>")
  4984. '(1 'org-special-keyword t))
  4985. '("^#.*" (0 'font-lock-comment-face t))
  4986. )))
  4987. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4988. ;; Now set the full font-lock-keywords
  4989. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4990. (org-set-local 'font-lock-defaults
  4991. '(org-font-lock-keywords t nil nil backward-paragraph))
  4992. (kill-local-variable 'font-lock-keywords) nil))
  4993. (defvar org-m nil)
  4994. (defvar org-l nil)
  4995. (defvar org-f nil)
  4996. (defun org-get-level-face (n)
  4997. "Get the right face for match N in font-lock matching of healdines."
  4998. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4999. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5000. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5001. (cond
  5002. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5003. ((eq n 2) org-f)
  5004. (t (if org-level-color-stars-only nil org-f))))
  5005. (defun org-get-todo-face (kwd)
  5006. "Get the right face for a TODO keyword KWD.
  5007. If KWD is a number, get the corresponding match group."
  5008. (if (numberp kwd) (setq kwd (match-string kwd)))
  5009. (or (cdr (assoc kwd org-todo-keyword-faces))
  5010. (and (member kwd org-done-keywords) 'org-done)
  5011. 'org-todo))
  5012. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5013. "Remove fontification and activation overlays from links."
  5014. (font-lock-default-unfontify-region beg end)
  5015. (let* ((buffer-undo-list t)
  5016. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5017. (inhibit-modification-hooks t)
  5018. deactivate-mark buffer-file-name buffer-file-truename)
  5019. (remove-text-properties beg end
  5020. '(mouse-face t keymap t org-linked-text t
  5021. invisible t intangible t))))
  5022. ;;;; Visibility cycling, including org-goto and indirect buffer
  5023. ;;; Cycling
  5024. (defvar org-cycle-global-status nil)
  5025. (make-variable-buffer-local 'org-cycle-global-status)
  5026. (defvar org-cycle-subtree-status nil)
  5027. (make-variable-buffer-local 'org-cycle-subtree-status)
  5028. ;;;###autoload
  5029. (defun org-cycle (&optional arg)
  5030. "Visibility cycling for Org-mode.
  5031. - When this function is called with a prefix argument, rotate the entire
  5032. buffer through 3 states (global cycling)
  5033. 1. OVERVIEW: Show only top-level headlines.
  5034. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5035. 3. SHOW ALL: Show everything.
  5036. - When point is at the beginning of a headline, rotate the subtree started
  5037. by this line through 3 different states (local cycling)
  5038. 1. FOLDED: Only the main headline is shown.
  5039. 2. CHILDREN: The main headline and the direct children are shown.
  5040. From this state, you can move to one of the children
  5041. and zoom in further.
  5042. 3. SUBTREE: Show the entire subtree, including body text.
  5043. - When there is a numeric prefix, go up to a heading with level ARG, do
  5044. a `show-subtree' and return to the previous cursor position. If ARG
  5045. is negative, go up that many levels.
  5046. - When point is not at the beginning of a headline, execute
  5047. `indent-relative', like TAB normally does. See the option
  5048. `org-cycle-emulate-tab' for details.
  5049. - Special case: if point is at the beginning of the buffer and there is
  5050. no headline in line 1, this function will act as if called with prefix arg.
  5051. But only if also the variable `org-cycle-global-at-bob' is t."
  5052. (interactive "P")
  5053. (let* ((outline-regexp
  5054. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5055. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5056. outline-regexp))
  5057. (bob-special (and org-cycle-global-at-bob (bobp)
  5058. (not (looking-at outline-regexp))))
  5059. (org-cycle-hook
  5060. (if bob-special
  5061. (delq 'org-optimize-window-after-visibility-change
  5062. (copy-sequence org-cycle-hook))
  5063. org-cycle-hook))
  5064. (pos (point)))
  5065. (if (or bob-special (equal arg '(4)))
  5066. ;; special case: use global cycling
  5067. (setq arg t))
  5068. (cond
  5069. ((org-at-table-p 'any)
  5070. ;; Enter the table or move to the next field in the table
  5071. (or (org-table-recognize-table.el)
  5072. (progn
  5073. (if arg (org-table-edit-field t)
  5074. (org-table-justify-field-maybe)
  5075. (call-interactively 'org-table-next-field)))))
  5076. ((eq arg t) ;; Global cycling
  5077. (cond
  5078. ((and (eq last-command this-command)
  5079. (eq org-cycle-global-status 'overview))
  5080. ;; We just created the overview - now do table of contents
  5081. ;; This can be slow in very large buffers, so indicate action
  5082. (message "CONTENTS...")
  5083. (org-content)
  5084. (message "CONTENTS...done")
  5085. (setq org-cycle-global-status 'contents)
  5086. (run-hook-with-args 'org-cycle-hook 'contents))
  5087. ((and (eq last-command this-command)
  5088. (eq org-cycle-global-status 'contents))
  5089. ;; We just showed the table of contents - now show everything
  5090. (show-all)
  5091. (message "SHOW ALL")
  5092. (setq org-cycle-global-status 'all)
  5093. (run-hook-with-args 'org-cycle-hook 'all))
  5094. (t
  5095. ;; Default action: go to overview
  5096. (org-overview)
  5097. (message "OVERVIEW")
  5098. (setq org-cycle-global-status 'overview)
  5099. (run-hook-with-args 'org-cycle-hook 'overview))))
  5100. ((and org-drawers org-drawer-regexp
  5101. (save-excursion
  5102. (beginning-of-line 1)
  5103. (looking-at org-drawer-regexp)))
  5104. ;; Toggle block visibility
  5105. (org-flag-drawer
  5106. (not (get-char-property (match-end 0) 'invisible))))
  5107. ((integerp arg)
  5108. ;; Show-subtree, ARG levels up from here.
  5109. (save-excursion
  5110. (org-back-to-heading)
  5111. (outline-up-heading (if (< arg 0) (- arg)
  5112. (- (funcall outline-level) arg)))
  5113. (org-show-subtree)))
  5114. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5115. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5116. ;; At a heading: rotate between three different views
  5117. (org-back-to-heading)
  5118. (let ((goal-column 0) eoh eol eos)
  5119. ;; First, some boundaries
  5120. (save-excursion
  5121. (org-back-to-heading)
  5122. (save-excursion
  5123. (beginning-of-line 2)
  5124. (while (and (not (eobp)) ;; this is like `next-line'
  5125. (get-char-property (1- (point)) 'invisible))
  5126. (beginning-of-line 2)) (setq eol (point)))
  5127. (outline-end-of-heading) (setq eoh (point))
  5128. (org-end-of-subtree t)
  5129. (unless (eobp)
  5130. (skip-chars-forward " \t\n")
  5131. (beginning-of-line 1) ; in case this is an item
  5132. )
  5133. (setq eos (1- (point))))
  5134. ;; Find out what to do next and set `this-command'
  5135. (cond
  5136. ((= eos eoh)
  5137. ;; Nothing is hidden behind this heading
  5138. (message "EMPTY ENTRY")
  5139. (setq org-cycle-subtree-status nil)
  5140. (save-excursion
  5141. (goto-char eos)
  5142. (outline-next-heading)
  5143. (if (org-invisible-p) (org-flag-heading nil))))
  5144. ((or (>= eol eos)
  5145. (not (string-match "\\S-" (buffer-substring eol eos))))
  5146. ;; Entire subtree is hidden in one line: open it
  5147. (org-show-entry)
  5148. (show-children)
  5149. (message "CHILDREN")
  5150. (save-excursion
  5151. (goto-char eos)
  5152. (outline-next-heading)
  5153. (if (org-invisible-p) (org-flag-heading nil)))
  5154. (setq org-cycle-subtree-status 'children)
  5155. (run-hook-with-args 'org-cycle-hook 'children))
  5156. ((and (eq last-command this-command)
  5157. (eq org-cycle-subtree-status 'children))
  5158. ;; We just showed the children, now show everything.
  5159. (org-show-subtree)
  5160. (message "SUBTREE")
  5161. (setq org-cycle-subtree-status 'subtree)
  5162. (run-hook-with-args 'org-cycle-hook 'subtree))
  5163. (t
  5164. ;; Default action: hide the subtree.
  5165. (hide-subtree)
  5166. (message "FOLDED")
  5167. (setq org-cycle-subtree-status 'folded)
  5168. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5169. ;; TAB emulation
  5170. (buffer-read-only (org-back-to-heading))
  5171. ((org-try-cdlatex-tab))
  5172. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5173. (or (not (bolp))
  5174. (not (looking-at outline-regexp))))
  5175. (call-interactively (global-key-binding "\t")))
  5176. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5177. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5178. (or (and (eq org-cycle-emulate-tab 'white)
  5179. (= (match-end 0) (point-at-eol)))
  5180. (and (eq org-cycle-emulate-tab 'whitestart)
  5181. (>= (match-end 0) pos))))
  5182. t
  5183. (eq org-cycle-emulate-tab t))
  5184. ; (if (and (looking-at "[ \n\r\t]")
  5185. ; (string-match "^[ \t]*$" (buffer-substring
  5186. ; (point-at-bol) (point))))
  5187. ; (progn
  5188. ; (beginning-of-line 1)
  5189. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5190. (call-interactively (global-key-binding "\t")))
  5191. (t (save-excursion
  5192. (org-back-to-heading)
  5193. (org-cycle))))))
  5194. ;;;###autoload
  5195. (defun org-global-cycle (&optional arg)
  5196. "Cycle the global visibility. For details see `org-cycle'."
  5197. (interactive "P")
  5198. (let ((org-cycle-include-plain-lists
  5199. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5200. (if (integerp arg)
  5201. (progn
  5202. (show-all)
  5203. (hide-sublevels arg)
  5204. (setq org-cycle-global-status 'contents))
  5205. (org-cycle '(4)))))
  5206. (defun org-overview ()
  5207. "Switch to overview mode, shoing only top-level headlines.
  5208. Really, this shows all headlines with level equal or greater than the level
  5209. of the first headline in the buffer. This is important, because if the
  5210. first headline is not level one, then (hide-sublevels 1) gives confusing
  5211. results."
  5212. (interactive)
  5213. (let ((level (save-excursion
  5214. (goto-char (point-min))
  5215. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5216. (progn
  5217. (goto-char (match-beginning 0))
  5218. (funcall outline-level))))))
  5219. (and level (hide-sublevels level))))
  5220. (defun org-content (&optional arg)
  5221. "Show all headlines in the buffer, like a table of contents.
  5222. With numerical argument N, show content up to level N."
  5223. (interactive "P")
  5224. (save-excursion
  5225. ;; Visit all headings and show their offspring
  5226. (and (integerp arg) (org-overview))
  5227. (goto-char (point-max))
  5228. (catch 'exit
  5229. (while (and (progn (condition-case nil
  5230. (outline-previous-visible-heading 1)
  5231. (error (goto-char (point-min))))
  5232. t)
  5233. (looking-at outline-regexp))
  5234. (if (integerp arg)
  5235. (show-children (1- arg))
  5236. (show-branches))
  5237. (if (bobp) (throw 'exit nil))))))
  5238. (defun org-optimize-window-after-visibility-change (state)
  5239. "Adjust the window after a change in outline visibility.
  5240. This function is the default value of the hook `org-cycle-hook'."
  5241. (when (get-buffer-window (current-buffer))
  5242. (cond
  5243. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5244. ; ((eq state 'overview) (org-beginning-of-line))
  5245. ((eq state 'content) nil)
  5246. ((eq state 'all) nil)
  5247. ((eq state 'folded) nil)
  5248. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5249. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5250. (defun org-compact-display-after-subtree-move ()
  5251. (let (beg end)
  5252. (save-excursion
  5253. (if (org-up-heading-safe)
  5254. (progn
  5255. (hide-subtree)
  5256. (show-entry)
  5257. (show-children)
  5258. (org-cycle-show-empty-lines 'children)
  5259. (org-cycle-hide-drawers 'children))
  5260. (org-overview)))))
  5261. (defun org-cycle-show-empty-lines (state)
  5262. "Show empty lines above all visible headlines.
  5263. The region to be covered depends on STATE when called through
  5264. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5265. entire buffer covered. Note that an empty line is only shown if there
  5266. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5267. (when (> org-cycle-separator-lines 0)
  5268. (save-excursion
  5269. (let* ((n org-cycle-separator-lines)
  5270. (re (cond
  5271. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5272. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5273. (t (let ((ns (number-to-string (- n 2))))
  5274. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5275. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5276. beg end)
  5277. (cond
  5278. ((memq state '(overview contents t))
  5279. (setq beg (point-min) end (point-max)))
  5280. ((memq state '(children folded))
  5281. (setq beg (point) end (progn (org-end-of-subtree t t)
  5282. (beginning-of-line 2)
  5283. (point)))))
  5284. (when beg
  5285. (goto-char beg)
  5286. (while (re-search-forward re end t)
  5287. (if (not (get-char-property (match-end 1) 'invisible))
  5288. (outline-flag-region
  5289. (match-beginning 1) (match-end 1) nil)))))))
  5290. ;; Never hide empty lines at the end of the file.
  5291. (save-excursion
  5292. (goto-char (point-max))
  5293. (outline-previous-heading)
  5294. (outline-end-of-heading)
  5295. (if (and (looking-at "[ \t\n]+")
  5296. (= (match-end 0) (point-max)))
  5297. (outline-flag-region (point) (match-end 0) nil))))
  5298. (defun org-subtree-end-visible-p ()
  5299. "Is the end of the current subtree visible?"
  5300. (pos-visible-in-window-p
  5301. (save-excursion (org-end-of-subtree t) (point))))
  5302. (defun org-first-headline-recenter (&optional N)
  5303. "Move cursor to the first headline and recenter the headline.
  5304. Optional argument N means, put the headline into the Nth line of the window."
  5305. (goto-char (point-min))
  5306. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5307. (beginning-of-line)
  5308. (recenter (prefix-numeric-value N))))
  5309. ;;; Org-goto
  5310. (defvar org-goto-window-configuration nil)
  5311. (defvar org-goto-marker nil)
  5312. (defvar org-goto-map
  5313. (let ((map (make-sparse-keymap)))
  5314. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5315. (while (setq cmd (pop cmds))
  5316. (substitute-key-definition cmd cmd map global-map)))
  5317. (suppress-keymap map)
  5318. (org-defkey map "\C-m" 'org-goto-ret)
  5319. (org-defkey map [(return)] 'org-goto-ret)
  5320. (org-defkey map [(left)] 'org-goto-left)
  5321. (org-defkey map [(right)] 'org-goto-right)
  5322. (org-defkey map [(control ?g)] 'org-goto-quit)
  5323. (org-defkey map "\C-i" 'org-cycle)
  5324. (org-defkey map [(tab)] 'org-cycle)
  5325. (org-defkey map [(down)] 'outline-next-visible-heading)
  5326. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5327. (if org-goto-auto-isearch
  5328. (if (fboundp 'define-key-after)
  5329. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5330. nil)
  5331. (org-defkey map "q" 'org-goto-quit)
  5332. (org-defkey map "n" 'outline-next-visible-heading)
  5333. (org-defkey map "p" 'outline-previous-visible-heading)
  5334. (org-defkey map "f" 'outline-forward-same-level)
  5335. (org-defkey map "b" 'outline-backward-same-level)
  5336. (org-defkey map "u" 'outline-up-heading))
  5337. (org-defkey map "/" 'org-occur)
  5338. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5339. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5340. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5341. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5342. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5343. map))
  5344. (defconst org-goto-help
  5345. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5346. RET=jump to location [Q]uit and return to previous location
  5347. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5348. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5349. (defun org-goto (&optional alternative-interface)
  5350. "Look up a different location in the current file, keeping current visibility.
  5351. When you want look-up or go to a different location in a document, the
  5352. fastest way is often to fold the entire buffer and then dive into the tree.
  5353. This method has the disadvantage, that the previous location will be folded,
  5354. which may not be what you want.
  5355. This command works around this by showing a copy of the current buffer
  5356. in an indirect buffer, in overview mode. You can dive into the tree in
  5357. that copy, use org-occur and incremental search to find a location.
  5358. When pressing RET or `Q', the command returns to the original buffer in
  5359. which the visibility is still unchanged. After RET is will also jump to
  5360. the location selected in the indirect buffer and expose the
  5361. the headline hierarchy above."
  5362. (interactive "P")
  5363. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5364. (org-refile-use-outline-path t)
  5365. (interface
  5366. (if (not alternative-interface)
  5367. org-goto-interface
  5368. (if (eq org-goto-interface 'outline)
  5369. 'outline-path-completion
  5370. 'outline)))
  5371. (org-goto-start-pos (point))
  5372. (selected-point
  5373. (if (eq interface 'outline)
  5374. (car (org-get-location (current-buffer) org-goto-help))
  5375. (nth 3 (org-refile-get-location "Goto: ")))))
  5376. (if selected-point
  5377. (progn
  5378. (org-mark-ring-push org-goto-start-pos)
  5379. (goto-char selected-point)
  5380. (if (or (org-invisible-p) (org-invisible-p2))
  5381. (org-show-context 'org-goto)))
  5382. (message "Quit"))))
  5383. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5384. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5385. (defvar org-goto-local-auto-isearch-map) ; defined below
  5386. (defun org-get-location (buf help)
  5387. "Let the user select a location in the Org-mode buffer BUF.
  5388. This function uses a recursive edit. It returns the selected position
  5389. or nil."
  5390. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5391. (isearch-hide-immediately nil)
  5392. (isearch-search-fun-function
  5393. (lambda () 'org-goto-local-search-forward-headings))
  5394. (org-goto-selected-point org-goto-exit-command))
  5395. (save-excursion
  5396. (save-window-excursion
  5397. (delete-other-windows)
  5398. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5399. (switch-to-buffer
  5400. (condition-case nil
  5401. (make-indirect-buffer (current-buffer) "*org-goto*")
  5402. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5403. (with-output-to-temp-buffer "*Help*"
  5404. (princ help))
  5405. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5406. (setq buffer-read-only nil)
  5407. (let ((org-startup-truncated t)
  5408. (org-startup-folded nil)
  5409. (org-startup-align-all-tables nil))
  5410. (org-mode)
  5411. (org-overview))
  5412. (setq buffer-read-only t)
  5413. (if (and (boundp 'org-goto-start-pos)
  5414. (integer-or-marker-p org-goto-start-pos))
  5415. (let ((org-show-hierarchy-above t)
  5416. (org-show-siblings t)
  5417. (org-show-following-heading t))
  5418. (goto-char org-goto-start-pos)
  5419. (and (org-invisible-p) (org-show-context)))
  5420. (goto-char (point-min)))
  5421. (org-beginning-of-line)
  5422. (message "Select location and press RET")
  5423. (use-local-map org-goto-map)
  5424. (recursive-edit)
  5425. ))
  5426. (kill-buffer "*org-goto*")
  5427. (cons org-goto-selected-point org-goto-exit-command)))
  5428. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5429. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5430. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5431. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5432. (defun org-goto-local-search-forward-headings (string bound noerror)
  5433. "Search and make sure that anu matches are in headlines."
  5434. (catch 'return
  5435. (while (search-forward string bound noerror)
  5436. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5437. (and (member :headline context)
  5438. (not (member :tags context))))
  5439. (throw 'return (point))))))
  5440. (defun org-goto-local-auto-isearch ()
  5441. "Start isearch."
  5442. (interactive)
  5443. (goto-char (point-min))
  5444. (let ((keys (this-command-keys)))
  5445. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5446. (isearch-mode t)
  5447. (isearch-process-search-char (string-to-char keys)))))
  5448. (defun org-goto-ret (&optional arg)
  5449. "Finish `org-goto' by going to the new location."
  5450. (interactive "P")
  5451. (setq org-goto-selected-point (point)
  5452. org-goto-exit-command 'return)
  5453. (throw 'exit nil))
  5454. (defun org-goto-left ()
  5455. "Finish `org-goto' by going to the new location."
  5456. (interactive)
  5457. (if (org-on-heading-p)
  5458. (progn
  5459. (beginning-of-line 1)
  5460. (setq org-goto-selected-point (point)
  5461. org-goto-exit-command 'left)
  5462. (throw 'exit nil))
  5463. (error "Not on a heading")))
  5464. (defun org-goto-right ()
  5465. "Finish `org-goto' by going to the new location."
  5466. (interactive)
  5467. (if (org-on-heading-p)
  5468. (progn
  5469. (setq org-goto-selected-point (point)
  5470. org-goto-exit-command 'right)
  5471. (throw 'exit nil))
  5472. (error "Not on a heading")))
  5473. (defun org-goto-quit ()
  5474. "Finish `org-goto' without cursor motion."
  5475. (interactive)
  5476. (setq org-goto-selected-point nil)
  5477. (setq org-goto-exit-command 'quit)
  5478. (throw 'exit nil))
  5479. ;;; Indirect buffer display of subtrees
  5480. (defvar org-indirect-dedicated-frame nil
  5481. "This is the frame being used for indirect tree display.")
  5482. (defvar org-last-indirect-buffer nil)
  5483. (defun org-tree-to-indirect-buffer (&optional arg)
  5484. "Create indirect buffer and narrow it to current subtree.
  5485. With numerical prefix ARG, go up to this level and then take that tree.
  5486. If ARG is negative, go up that many levels.
  5487. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5488. indirect buffer previously made with this command, to avoid proliferation of
  5489. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5490. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5491. is kept so that you can work with several indirect buffers at the same time.
  5492. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5493. requests that a new frame be made for the new buffer, so that the dedicated
  5494. frame is not changed."
  5495. (interactive "P")
  5496. (let ((cbuf (current-buffer))
  5497. (cwin (selected-window))
  5498. (pos (point))
  5499. beg end level heading ibuf)
  5500. (save-excursion
  5501. (org-back-to-heading t)
  5502. (when (numberp arg)
  5503. (setq level (org-outline-level))
  5504. (if (< arg 0) (setq arg (+ level arg)))
  5505. (while (> (setq level (org-outline-level)) arg)
  5506. (outline-up-heading 1 t)))
  5507. (setq beg (point)
  5508. heading (org-get-heading))
  5509. (org-end-of-subtree t) (setq end (point)))
  5510. (if (and (buffer-live-p org-last-indirect-buffer)
  5511. (not (eq org-indirect-buffer-display 'new-frame))
  5512. (not arg))
  5513. (kill-buffer org-last-indirect-buffer))
  5514. (setq ibuf (org-get-indirect-buffer cbuf)
  5515. org-last-indirect-buffer ibuf)
  5516. (cond
  5517. ((or (eq org-indirect-buffer-display 'new-frame)
  5518. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5519. (select-frame (make-frame))
  5520. (delete-other-windows)
  5521. (switch-to-buffer ibuf)
  5522. (org-set-frame-title heading))
  5523. ((eq org-indirect-buffer-display 'dedicated-frame)
  5524. (raise-frame
  5525. (select-frame (or (and org-indirect-dedicated-frame
  5526. (frame-live-p org-indirect-dedicated-frame)
  5527. org-indirect-dedicated-frame)
  5528. (setq org-indirect-dedicated-frame (make-frame)))))
  5529. (delete-other-windows)
  5530. (switch-to-buffer ibuf)
  5531. (org-set-frame-title (concat "Indirect: " heading)))
  5532. ((eq org-indirect-buffer-display 'current-window)
  5533. (switch-to-buffer ibuf))
  5534. ((eq org-indirect-buffer-display 'other-window)
  5535. (pop-to-buffer ibuf))
  5536. (t (error "Invalid value.")))
  5537. (if (featurep 'xemacs)
  5538. (save-excursion (org-mode) (turn-on-font-lock)))
  5539. (narrow-to-region beg end)
  5540. (show-all)
  5541. (goto-char pos)
  5542. (and (window-live-p cwin) (select-window cwin))))
  5543. (defun org-get-indirect-buffer (&optional buffer)
  5544. (setq buffer (or buffer (current-buffer)))
  5545. (let ((n 1) (base (buffer-name buffer)) bname)
  5546. (while (buffer-live-p
  5547. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5548. (setq n (1+ n)))
  5549. (condition-case nil
  5550. (make-indirect-buffer buffer bname 'clone)
  5551. (error (make-indirect-buffer buffer bname)))))
  5552. (defun org-set-frame-title (title)
  5553. "Set the title of the current frame to the string TITLE."
  5554. ;; FIXME: how to name a single frame in XEmacs???
  5555. (unless (featurep 'xemacs)
  5556. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5557. ;;;; Structure editing
  5558. ;;; Inserting headlines
  5559. (defun org-insert-heading (&optional force-heading)
  5560. "Insert a new heading or item with same depth at point.
  5561. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5562. If point is at the beginning of a headline, insert a sibling before the
  5563. current headline. If point is not at the beginning, do not split the line,
  5564. but create the new hedline after the current line."
  5565. (interactive "P")
  5566. (if (= (buffer-size) 0)
  5567. (insert "\n* ")
  5568. (when (or force-heading (not (org-insert-item)))
  5569. (let* ((head (save-excursion
  5570. (condition-case nil
  5571. (progn
  5572. (org-back-to-heading)
  5573. (match-string 0))
  5574. (error "*"))))
  5575. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5576. pos)
  5577. (cond
  5578. ((and (org-on-heading-p) (bolp)
  5579. (or (bobp)
  5580. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5581. ;; insert before the current line
  5582. (open-line (if blank 2 1)))
  5583. ((and (bolp)
  5584. (or (bobp)
  5585. (save-excursion
  5586. (backward-char 1) (not (org-invisible-p)))))
  5587. ;; insert right here
  5588. nil)
  5589. (t
  5590. ; ;; in the middle of the line
  5591. ; (org-show-entry)
  5592. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5593. ; (if (and
  5594. ; (org-on-heading-p)
  5595. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5596. ; ;; protect the tags
  5597. ;; (let ((tags (match-string 2)) pos)
  5598. ; (delete-region (match-beginning 1) (match-end 1))
  5599. ; (setq pos (point-at-bol))
  5600. ; (newline (if blank 2 1))
  5601. ; (save-excursion
  5602. ; (goto-char pos)
  5603. ; (end-of-line 1)
  5604. ; (insert " " tags)
  5605. ; (org-set-tags nil 'align)))
  5606. ; (newline (if blank 2 1)))
  5607. ; (newline (if blank 2 1))))
  5608. ;; in the middle of the line
  5609. (org-show-entry)
  5610. (let ((split
  5611. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5612. tags pos)
  5613. (if (org-on-heading-p)
  5614. (progn
  5615. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5616. (setq tags (and (match-end 2) (match-string 2)))
  5617. (and (match-end 1)
  5618. (delete-region (match-beginning 1) (match-end 1)))
  5619. (setq pos (point-at-bol))
  5620. (or split (end-of-line 1))
  5621. (delete-horizontal-space)
  5622. (newline (if blank 2 1))
  5623. (when tags
  5624. (save-excursion
  5625. (goto-char pos)
  5626. (end-of-line 1)
  5627. (insert " " tags)
  5628. (org-set-tags nil 'align))))
  5629. (or split (end-of-line 1))
  5630. (newline (if blank 2 1))))))
  5631. (insert head) (just-one-space)
  5632. (setq pos (point))
  5633. (end-of-line 1)
  5634. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5635. (run-hooks 'org-insert-heading-hook)))))
  5636. (defun org-insert-heading-after-current ()
  5637. "Insert a new heading with same level as current, after current subtree."
  5638. (interactive)
  5639. (org-back-to-heading)
  5640. (org-insert-heading)
  5641. (org-move-subtree-down)
  5642. (end-of-line 1))
  5643. (defun org-insert-todo-heading (arg)
  5644. "Insert a new heading with the same level and TODO state as current heading.
  5645. If the heading has no TODO state, or if the state is DONE, use the first
  5646. state (TODO by default). Also with prefix arg, force first state."
  5647. (interactive "P")
  5648. (when (not (org-insert-item 'checkbox))
  5649. (org-insert-heading)
  5650. (save-excursion
  5651. (org-back-to-heading)
  5652. (outline-previous-heading)
  5653. (looking-at org-todo-line-regexp))
  5654. (if (or arg
  5655. (not (match-beginning 2))
  5656. (member (match-string 2) org-done-keywords))
  5657. (insert (car org-todo-keywords-1) " ")
  5658. (insert (match-string 2) " "))))
  5659. (defun org-insert-subheading (arg)
  5660. "Insert a new subheading and demote it.
  5661. Works for outline headings and for plain lists alike."
  5662. (interactive "P")
  5663. (org-insert-heading arg)
  5664. (cond
  5665. ((org-on-heading-p) (org-do-demote))
  5666. ((org-at-item-p) (org-indent-item 1))))
  5667. (defun org-insert-todo-subheading (arg)
  5668. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5669. Works for outline headings and for plain lists alike."
  5670. (interactive "P")
  5671. (org-insert-todo-heading arg)
  5672. (cond
  5673. ((org-on-heading-p) (org-do-demote))
  5674. ((org-at-item-p) (org-indent-item 1))))
  5675. ;;; Promotion and Demotion
  5676. (defun org-promote-subtree ()
  5677. "Promote the entire subtree.
  5678. See also `org-promote'."
  5679. (interactive)
  5680. (save-excursion
  5681. (org-map-tree 'org-promote))
  5682. (org-fix-position-after-promote))
  5683. (defun org-demote-subtree ()
  5684. "Demote the entire subtree. See `org-demote'.
  5685. See also `org-promote'."
  5686. (interactive)
  5687. (save-excursion
  5688. (org-map-tree 'org-demote))
  5689. (org-fix-position-after-promote))
  5690. (defun org-do-promote ()
  5691. "Promote the current heading higher up the tree.
  5692. If the region is active in `transient-mark-mode', promote all headings
  5693. in the region."
  5694. (interactive)
  5695. (save-excursion
  5696. (if (org-region-active-p)
  5697. (org-map-region 'org-promote (region-beginning) (region-end))
  5698. (org-promote)))
  5699. (org-fix-position-after-promote))
  5700. (defun org-do-demote ()
  5701. "Demote the current heading lower down the tree.
  5702. If the region is active in `transient-mark-mode', demote all headings
  5703. in the region."
  5704. (interactive)
  5705. (save-excursion
  5706. (if (org-region-active-p)
  5707. (org-map-region 'org-demote (region-beginning) (region-end))
  5708. (org-demote)))
  5709. (org-fix-position-after-promote))
  5710. (defun org-fix-position-after-promote ()
  5711. "Make sure that after pro/demotion cursor position is right."
  5712. (let ((pos (point)))
  5713. (when (save-excursion
  5714. (beginning-of-line 1)
  5715. (looking-at org-todo-line-regexp)
  5716. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5717. (cond ((eobp) (insert " "))
  5718. ((eolp) (insert " "))
  5719. ((equal (char-after) ?\ ) (forward-char 1))))))
  5720. (defun org-reduced-level (l)
  5721. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5722. (defun org-get-legal-level (level &optional change)
  5723. "Rectify a level change under the influence of `org-odd-levels-only'
  5724. LEVEL is a current level, CHANGE is by how much the level should be
  5725. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5726. even level numbers will become the next higher odd number."
  5727. (if org-odd-levels-only
  5728. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5729. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5730. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5731. (max 1 (+ level change))))
  5732. (defun org-promote ()
  5733. "Promote the current heading higher up the tree.
  5734. If the region is active in `transient-mark-mode', promote all headings
  5735. in the region."
  5736. (org-back-to-heading t)
  5737. (let* ((level (save-match-data (funcall outline-level)))
  5738. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5739. (diff (abs (- level (length up-head) -1))))
  5740. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5741. (replace-match up-head nil t)
  5742. ;; Fixup tag positioning
  5743. (and org-auto-align-tags (org-set-tags nil t))
  5744. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5745. (defun org-demote ()
  5746. "Demote the current heading lower down the tree.
  5747. If the region is active in `transient-mark-mode', demote all headings
  5748. in the region."
  5749. (org-back-to-heading t)
  5750. (let* ((level (save-match-data (funcall outline-level)))
  5751. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5752. (diff (abs (- level (length down-head) -1))))
  5753. (replace-match down-head nil t)
  5754. ;; Fixup tag positioning
  5755. (and org-auto-align-tags (org-set-tags nil t))
  5756. (if org-adapt-indentation (org-fixup-indentation diff))))
  5757. (defun org-map-tree (fun)
  5758. "Call FUN for every heading underneath the current one."
  5759. (org-back-to-heading)
  5760. (let ((level (funcall outline-level)))
  5761. (save-excursion
  5762. (funcall fun)
  5763. (while (and (progn
  5764. (outline-next-heading)
  5765. (> (funcall outline-level) level))
  5766. (not (eobp)))
  5767. (funcall fun)))))
  5768. (defun org-map-region (fun beg end)
  5769. "Call FUN for every heading between BEG and END."
  5770. (let ((org-ignore-region t))
  5771. (save-excursion
  5772. (setq end (copy-marker end))
  5773. (goto-char beg)
  5774. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5775. (< (point) end))
  5776. (funcall fun))
  5777. (while (and (progn
  5778. (outline-next-heading)
  5779. (< (point) end))
  5780. (not (eobp)))
  5781. (funcall fun)))))
  5782. (defun org-fixup-indentation (diff)
  5783. "Change the indentation in the current entry by DIFF
  5784. However, if any line in the current entry has no indentation, or if it
  5785. would end up with no indentation after the change, nothing at all is done."
  5786. (save-excursion
  5787. (let ((end (save-excursion (outline-next-heading)
  5788. (point-marker)))
  5789. (prohibit (if (> diff 0)
  5790. "^\\S-"
  5791. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5792. col)
  5793. (unless (save-excursion (end-of-line 1)
  5794. (re-search-forward prohibit end t))
  5795. (while (and (< (point) end)
  5796. (re-search-forward "^[ \t]+" end t))
  5797. (goto-char (match-end 0))
  5798. (setq col (current-column))
  5799. (if (< diff 0) (replace-match ""))
  5800. (indent-to (+ diff col))))
  5801. (move-marker end nil))))
  5802. (defun org-convert-to-odd-levels ()
  5803. "Convert an org-mode file with all levels allowed to one with odd levels.
  5804. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5805. level 5 etc."
  5806. (interactive)
  5807. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5808. (let ((org-odd-levels-only nil) n)
  5809. (save-excursion
  5810. (goto-char (point-min))
  5811. (while (re-search-forward "^\\*\\*+ " nil t)
  5812. (setq n (- (length (match-string 0)) 2))
  5813. (while (>= (setq n (1- n)) 0)
  5814. (org-demote))
  5815. (end-of-line 1))))))
  5816. (defun org-convert-to-oddeven-levels ()
  5817. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5818. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5819. section with an even level, conversion would destroy the structure of the file. An error
  5820. is signaled in this case."
  5821. (interactive)
  5822. (goto-char (point-min))
  5823. ;; First check if there are no even levels
  5824. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5825. (org-show-context t)
  5826. (error "Not all levels are odd in this file. Conversion not possible."))
  5827. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5828. (let ((org-odd-levels-only nil) n)
  5829. (save-excursion
  5830. (goto-char (point-min))
  5831. (while (re-search-forward "^\\*\\*+ " nil t)
  5832. (setq n (/ (1- (length (match-string 0))) 2))
  5833. (while (>= (setq n (1- n)) 0)
  5834. (org-promote))
  5835. (end-of-line 1))))))
  5836. (defun org-tr-level (n)
  5837. "Make N odd if required."
  5838. (if org-odd-levels-only (1+ (/ n 2)) n))
  5839. ;;; Vertical tree motion, cutting and pasting of subtrees
  5840. (defun org-move-subtree-up (&optional arg)
  5841. "Move the current subtree up past ARG headlines of the same level."
  5842. (interactive "p")
  5843. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5844. (defun org-move-subtree-down (&optional arg)
  5845. "Move the current subtree down past ARG headlines of the same level."
  5846. (interactive "p")
  5847. (setq arg (prefix-numeric-value arg))
  5848. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5849. 'outline-get-last-sibling))
  5850. (ins-point (make-marker))
  5851. (cnt (abs arg))
  5852. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5853. ;; Select the tree
  5854. (org-back-to-heading)
  5855. (setq beg0 (point))
  5856. (save-excursion
  5857. (setq ne-beg (org-back-over-empty-lines))
  5858. (setq beg (point)))
  5859. (save-match-data
  5860. (save-excursion (outline-end-of-heading)
  5861. (setq folded (org-invisible-p)))
  5862. (outline-end-of-subtree))
  5863. (outline-next-heading)
  5864. (setq ne-end (org-back-over-empty-lines))
  5865. (setq end (point))
  5866. (goto-char beg0)
  5867. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5868. ;; include less whitespace
  5869. (save-excursion
  5870. (goto-char beg)
  5871. (forward-line (- ne-beg ne-end))
  5872. (setq beg (point))))
  5873. ;; Find insertion point, with error handling
  5874. (while (> cnt 0)
  5875. (or (and (funcall movfunc) (looking-at outline-regexp))
  5876. (progn (goto-char beg0)
  5877. (error "Cannot move past superior level or buffer limit")))
  5878. (setq cnt (1- cnt)))
  5879. (if (> arg 0)
  5880. ;; Moving forward - still need to move over subtree
  5881. (progn (org-end-of-subtree t t)
  5882. (save-excursion
  5883. (org-back-over-empty-lines)
  5884. (or (bolp) (newline)))))
  5885. (setq ne-ins (org-back-over-empty-lines))
  5886. (move-marker ins-point (point))
  5887. (setq txt (buffer-substring beg end))
  5888. (delete-region beg end)
  5889. (outline-flag-region (1- beg) beg nil)
  5890. (outline-flag-region (1- (point)) (point) nil)
  5891. (insert txt)
  5892. (or (bolp) (insert "\n"))
  5893. (setq ins-end (point))
  5894. (goto-char ins-point)
  5895. (org-skip-whitespace)
  5896. (when (and (< arg 0)
  5897. (org-first-sibling-p)
  5898. (> ne-ins ne-beg))
  5899. ;; Move whitespace back to beginning
  5900. (save-excursion
  5901. (goto-char ins-end)
  5902. (let ((kill-whole-line t))
  5903. (kill-line (- ne-ins ne-beg)) (point)))
  5904. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5905. (move-marker ins-point nil)
  5906. (org-compact-display-after-subtree-move)
  5907. (unless folded
  5908. (org-show-entry)
  5909. (show-children)
  5910. (org-cycle-hide-drawers 'children))))
  5911. (defvar org-subtree-clip ""
  5912. "Clipboard for cut and paste of subtrees.
  5913. This is actually only a copy of the kill, because we use the normal kill
  5914. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5915. (defvar org-subtree-clip-folded nil
  5916. "Was the last copied subtree folded?
  5917. This is used to fold the tree back after pasting.")
  5918. (defun org-cut-subtree (&optional n)
  5919. "Cut the current subtree into the clipboard.
  5920. With prefix arg N, cut this many sequential subtrees.
  5921. This is a short-hand for marking the subtree and then cutting it."
  5922. (interactive "p")
  5923. (org-copy-subtree n 'cut))
  5924. (defun org-copy-subtree (&optional n cut)
  5925. "Cut the current subtree into the clipboard.
  5926. With prefix arg N, cut this many sequential subtrees.
  5927. This is a short-hand for marking the subtree and then copying it.
  5928. If CUT is non-nil, actually cut the subtree."
  5929. (interactive "p")
  5930. (let (beg end folded (beg0 (point)))
  5931. (if (interactive-p)
  5932. (org-back-to-heading nil) ; take what looks like a subtree
  5933. (org-back-to-heading t)) ; take what is really there
  5934. (org-back-over-empty-lines)
  5935. (setq beg (point))
  5936. (skip-chars-forward " \t\r\n")
  5937. (save-match-data
  5938. (save-excursion (outline-end-of-heading)
  5939. (setq folded (org-invisible-p)))
  5940. (condition-case nil
  5941. (outline-forward-same-level (1- n))
  5942. (error nil))
  5943. (org-end-of-subtree t t))
  5944. (org-back-over-empty-lines)
  5945. (setq end (point))
  5946. (goto-char beg0)
  5947. (when (> end beg)
  5948. (setq org-subtree-clip-folded folded)
  5949. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5950. (setq org-subtree-clip (current-kill 0))
  5951. (message "%s: Subtree(s) with %d characters"
  5952. (if cut "Cut" "Copied")
  5953. (length org-subtree-clip)))))
  5954. (defun org-paste-subtree (&optional level tree)
  5955. "Paste the clipboard as a subtree, with modification of headline level.
  5956. The entire subtree is promoted or demoted in order to match a new headline
  5957. level. By default, the new level is derived from the visible headings
  5958. before and after the insertion point, and taken to be the inferior headline
  5959. level of the two. So if the previous visible heading is level 3 and the
  5960. next is level 4 (or vice versa), level 4 will be used for insertion.
  5961. This makes sure that the subtree remains an independent subtree and does
  5962. not swallow low level entries.
  5963. You can also force a different level, either by using a numeric prefix
  5964. argument, or by inserting the heading marker by hand. For example, if the
  5965. cursor is after \"*****\", then the tree will be shifted to level 5.
  5966. If you want to insert the tree as is, just use \\[yank].
  5967. If optional TREE is given, use this text instead of the kill ring."
  5968. (interactive "P")
  5969. (unless (org-kill-is-subtree-p tree)
  5970. (error "%s"
  5971. (substitute-command-keys
  5972. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5973. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5974. (^re (concat "^\\(" outline-regexp "\\)"))
  5975. (re (concat "\\(" outline-regexp "\\)"))
  5976. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5977. (old-level (if (string-match ^re txt)
  5978. (- (match-end 0) (match-beginning 0) 1)
  5979. -1))
  5980. (force-level (cond (level (prefix-numeric-value level))
  5981. ((string-match
  5982. ^re_ (buffer-substring (point-at-bol) (point)))
  5983. (- (match-end 1) (match-beginning 1)))
  5984. (t nil)))
  5985. (previous-level (save-excursion
  5986. (condition-case nil
  5987. (progn
  5988. (outline-previous-visible-heading 1)
  5989. (if (looking-at re)
  5990. (- (match-end 0) (match-beginning 0) 1)
  5991. 1))
  5992. (error 1))))
  5993. (next-level (save-excursion
  5994. (condition-case nil
  5995. (progn
  5996. (or (looking-at outline-regexp)
  5997. (outline-next-visible-heading 1))
  5998. (if (looking-at re)
  5999. (- (match-end 0) (match-beginning 0) 1)
  6000. 1))
  6001. (error 1))))
  6002. (new-level (or force-level (max previous-level next-level)))
  6003. (shift (if (or (= old-level -1)
  6004. (= new-level -1)
  6005. (= old-level new-level))
  6006. 0
  6007. (- new-level old-level)))
  6008. (delta (if (> shift 0) -1 1))
  6009. (func (if (> shift 0) 'org-demote 'org-promote))
  6010. (org-odd-levels-only nil)
  6011. beg end)
  6012. ;; Remove the forced level indicator
  6013. (if force-level
  6014. (delete-region (point-at-bol) (point)))
  6015. ;; Paste
  6016. (beginning-of-line 1)
  6017. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6018. (setq beg (point))
  6019. (insert-before-markers txt) ;; FIXME: correct fix????
  6020. (unless (string-match "\n\\'" txt) (insert "\n"))
  6021. (setq end (point))
  6022. (goto-char beg)
  6023. (skip-chars-forward " \t\n\r")
  6024. (setq beg (point))
  6025. ;; Shift if necessary
  6026. (unless (= shift 0)
  6027. (save-restriction
  6028. (narrow-to-region beg end)
  6029. (while (not (= shift 0))
  6030. (org-map-region func (point-min) (point-max))
  6031. (setq shift (+ delta shift)))
  6032. (goto-char (point-min))))
  6033. (when (interactive-p)
  6034. (message "Clipboard pasted as level %d subtree" new-level))
  6035. (if (and kill-ring
  6036. (eq org-subtree-clip (current-kill 0))
  6037. org-subtree-clip-folded)
  6038. ;; The tree was folded before it was killed/copied
  6039. (hide-subtree))))
  6040. (defun org-kill-is-subtree-p (&optional txt)
  6041. "Check if the current kill is an outline subtree, or a set of trees.
  6042. Returns nil if kill does not start with a headline, or if the first
  6043. headline level is not the largest headline level in the tree.
  6044. So this will actually accept several entries of equal levels as well,
  6045. which is OK for `org-paste-subtree'.
  6046. If optional TXT is given, check this string instead of the current kill."
  6047. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6048. (start-level (and kill
  6049. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6050. org-outline-regexp "\\)")
  6051. kill)
  6052. (- (match-end 2) (match-beginning 2) 1)))
  6053. (re (concat "^" org-outline-regexp))
  6054. (start (1+ (match-beginning 2))))
  6055. (if (not start-level)
  6056. (progn
  6057. nil) ;; does not even start with a heading
  6058. (catch 'exit
  6059. (while (setq start (string-match re kill (1+ start)))
  6060. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6061. (throw 'exit nil)))
  6062. t))))
  6063. (defun org-narrow-to-subtree ()
  6064. "Narrow buffer to the current subtree."
  6065. (interactive)
  6066. (save-excursion
  6067. (save-match-data
  6068. (narrow-to-region
  6069. (progn (org-back-to-heading) (point))
  6070. (progn (org-end-of-subtree t t) (point))))))
  6071. ;;; Outline Sorting
  6072. (defun org-sort (with-case)
  6073. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6074. Optional argument WITH-CASE means sort case-sensitively."
  6075. (interactive "P")
  6076. (if (org-at-table-p)
  6077. (org-call-with-arg 'org-table-sort-lines with-case)
  6078. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6079. (defvar org-priority-regexp) ; defined later in the file
  6080. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6081. "Sort entries on a certain level of an outline tree.
  6082. If there is an active region, the entries in the region are sorted.
  6083. Else, if the cursor is before the first entry, sort the top-level items.
  6084. Else, the children of the entry at point are sorted.
  6085. Sorting can be alphabetically, numerically, and by date/time as given by
  6086. the first time stamp in the entry. The command prompts for the sorting
  6087. type unless it has been given to the function through the SORTING-TYPE
  6088. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6089. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6090. called with point at the beginning of the record. It must return either
  6091. a string or a number that should serve as the sorting key for that record.
  6092. Comparing entries ignores case by default. However, with an optional argument
  6093. WITH-CASE, the sorting considers case as well."
  6094. (interactive "P")
  6095. (let ((case-func (if with-case 'identity 'downcase))
  6096. start beg end stars re re2
  6097. txt what tmp plain-list-p)
  6098. ;; Find beginning and end of region to sort
  6099. (cond
  6100. ((org-region-active-p)
  6101. ;; we will sort the region
  6102. (setq end (region-end)
  6103. what "region")
  6104. (goto-char (region-beginning))
  6105. (if (not (org-on-heading-p)) (outline-next-heading))
  6106. (setq start (point)))
  6107. ((org-at-item-p)
  6108. ;; we will sort this plain list
  6109. (org-beginning-of-item-list) (setq start (point))
  6110. (org-end-of-item-list) (setq end (point))
  6111. (goto-char start)
  6112. (setq plain-list-p t
  6113. what "plain list"))
  6114. ((or (org-on-heading-p)
  6115. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6116. ;; we will sort the children of the current headline
  6117. (org-back-to-heading)
  6118. (setq start (point)
  6119. end (progn (org-end-of-subtree t t)
  6120. (org-back-over-empty-lines)
  6121. (point))
  6122. what "children")
  6123. (goto-char start)
  6124. (show-subtree)
  6125. (outline-next-heading))
  6126. (t
  6127. ;; we will sort the top-level entries in this file
  6128. (goto-char (point-min))
  6129. (or (org-on-heading-p) (outline-next-heading))
  6130. (setq start (point) end (point-max) what "top-level")
  6131. (goto-char start)
  6132. (show-all)))
  6133. (setq beg (point))
  6134. (if (>= beg end) (error "Nothing to sort"))
  6135. (unless plain-list-p
  6136. (looking-at "\\(\\*+\\)")
  6137. (setq stars (match-string 1)
  6138. re (concat "^" (regexp-quote stars) " +")
  6139. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6140. txt (buffer-substring beg end))
  6141. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6142. (if (and (not (equal stars "*")) (string-match re2 txt))
  6143. (error "Region to sort contains a level above the first entry")))
  6144. (unless sorting-type
  6145. (message
  6146. (if plain-list-p
  6147. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6148. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6149. what)
  6150. (setq sorting-type (read-char-exclusive))
  6151. (and (= (downcase sorting-type) ?f)
  6152. (setq getkey-func
  6153. (completing-read "Sort using function: "
  6154. obarray 'fboundp t nil nil))
  6155. (setq getkey-func (intern getkey-func)))
  6156. (and (= (downcase sorting-type) ?r)
  6157. (setq property
  6158. (completing-read "Property: "
  6159. (mapcar 'list (org-buffer-property-keys t))
  6160. nil t))))
  6161. (message "Sorting entries...")
  6162. (save-restriction
  6163. (narrow-to-region start end)
  6164. (let ((dcst (downcase sorting-type))
  6165. (now (current-time)))
  6166. (sort-subr
  6167. (/= dcst sorting-type)
  6168. ;; This function moves to the beginning character of the "record" to
  6169. ;; be sorted.
  6170. (if plain-list-p
  6171. (lambda nil
  6172. (if (org-at-item-p) t (goto-char (point-max))))
  6173. (lambda nil
  6174. (if (re-search-forward re nil t)
  6175. (goto-char (match-beginning 0))
  6176. (goto-char (point-max)))))
  6177. ;; This function moves to the last character of the "record" being
  6178. ;; sorted.
  6179. (if plain-list-p
  6180. 'org-end-of-item
  6181. (lambda nil
  6182. (save-match-data
  6183. (condition-case nil
  6184. (outline-forward-same-level 1)
  6185. (error
  6186. (goto-char (point-max)))))))
  6187. ;; This function returns the value that gets sorted against.
  6188. (if plain-list-p
  6189. (lambda nil
  6190. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6191. (cond
  6192. ((= dcst ?n)
  6193. (string-to-number (buffer-substring (match-end 0)
  6194. (point-at-eol))))
  6195. ((= dcst ?a)
  6196. (buffer-substring (match-end 0) (point-at-eol)))
  6197. ((= dcst ?t)
  6198. (if (re-search-forward org-ts-regexp
  6199. (point-at-eol) t)
  6200. (org-time-string-to-time (match-string 0))
  6201. now))
  6202. ((= dcst ?f)
  6203. (if getkey-func
  6204. (progn
  6205. (setq tmp (funcall getkey-func))
  6206. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6207. tmp)
  6208. (error "Invalid key function `%s'" getkey-func)))
  6209. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6210. (lambda nil
  6211. (cond
  6212. ((= dcst ?n)
  6213. (if (looking-at outline-regexp)
  6214. (string-to-number (buffer-substring (match-end 0)
  6215. (point-at-eol)))
  6216. nil))
  6217. ((= dcst ?a)
  6218. (funcall case-func (buffer-substring (point-at-bol)
  6219. (point-at-eol))))
  6220. ((= dcst ?t)
  6221. (if (re-search-forward org-ts-regexp
  6222. (save-excursion
  6223. (forward-line 2)
  6224. (point)) t)
  6225. (org-time-string-to-time (match-string 0))
  6226. now))
  6227. ((= dcst ?p)
  6228. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6229. (string-to-char (match-string 2))
  6230. org-default-priority))
  6231. ((= dcst ?r)
  6232. (or (org-entry-get nil property) ""))
  6233. ((= dcst ?f)
  6234. (if getkey-func
  6235. (progn
  6236. (setq tmp (funcall getkey-func))
  6237. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6238. tmp)
  6239. (error "Invalid key function `%s'" getkey-func)))
  6240. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6241. nil
  6242. (cond
  6243. ((= dcst ?a) 'string<)
  6244. ((= dcst ?t) 'time-less-p)
  6245. (t nil)))))
  6246. (message "Sorting entries...done")))
  6247. (defun org-do-sort (table what &optional with-case sorting-type)
  6248. "Sort TABLE of WHAT according to SORTING-TYPE.
  6249. The user will be prompted for the SORTING-TYPE if the call to this
  6250. function does not specify it. WHAT is only for the prompt, to indicate
  6251. what is being sorted. The sorting key will be extracted from
  6252. the car of the elements of the table.
  6253. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6254. (unless sorting-type
  6255. (message
  6256. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6257. what)
  6258. (setq sorting-type (read-char-exclusive)))
  6259. (let ((dcst (downcase sorting-type))
  6260. extractfun comparefun)
  6261. ;; Define the appropriate functions
  6262. (cond
  6263. ((= dcst ?n)
  6264. (setq extractfun 'string-to-number
  6265. comparefun (if (= dcst sorting-type) '< '>)))
  6266. ((= dcst ?a)
  6267. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6268. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6269. comparefun (if (= dcst sorting-type)
  6270. 'string<
  6271. (lambda (a b) (and (not (string< a b))
  6272. (not (string= a b)))))))
  6273. ((= dcst ?t)
  6274. (setq extractfun
  6275. (lambda (x)
  6276. (if (string-match org-ts-regexp x)
  6277. (time-to-seconds
  6278. (org-time-string-to-time (match-string 0 x)))
  6279. 0))
  6280. comparefun (if (= dcst sorting-type) '< '>)))
  6281. (t (error "Invalid sorting type `%c'" sorting-type)))
  6282. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6283. table)
  6284. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6285. ;;;; Plain list items, including checkboxes
  6286. ;;; Plain list items
  6287. (defun org-at-item-p ()
  6288. "Is point in a line starting a hand-formatted item?"
  6289. (let ((llt org-plain-list-ordered-item-terminator))
  6290. (save-excursion
  6291. (goto-char (point-at-bol))
  6292. (looking-at
  6293. (cond
  6294. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6295. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6296. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6297. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6298. (defun org-in-item-p ()
  6299. "It the cursor inside a plain list item.
  6300. Does not have to be the first line."
  6301. (save-excursion
  6302. (condition-case nil
  6303. (progn
  6304. (org-beginning-of-item)
  6305. (org-at-item-p)
  6306. t)
  6307. (error nil))))
  6308. (defun org-insert-item (&optional checkbox)
  6309. "Insert a new item at the current level.
  6310. Return t when things worked, nil when we are not in an item."
  6311. (when (save-excursion
  6312. (condition-case nil
  6313. (progn
  6314. (org-beginning-of-item)
  6315. (org-at-item-p)
  6316. (if (org-invisible-p) (error "Invisible item"))
  6317. t)
  6318. (error nil)))
  6319. (let* ((bul (match-string 0))
  6320. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6321. (match-end 0)))
  6322. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6323. pos)
  6324. (cond
  6325. ((and (org-at-item-p) (<= (point) eow))
  6326. ;; before the bullet
  6327. (beginning-of-line 1)
  6328. (open-line (if blank 2 1)))
  6329. ((<= (point) eow)
  6330. (beginning-of-line 1))
  6331. (t
  6332. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6333. (end-of-line 1)
  6334. (delete-horizontal-space))
  6335. (newline (if blank 2 1))))
  6336. (insert bul (if checkbox "[ ]" ""))
  6337. (just-one-space)
  6338. (setq pos (point))
  6339. (end-of-line 1)
  6340. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6341. (org-maybe-renumber-ordered-list)
  6342. (and checkbox (org-update-checkbox-count-maybe))
  6343. t))
  6344. ;;; Checkboxes
  6345. (defun org-at-item-checkbox-p ()
  6346. "Is point at a line starting a plain-list item with a checklet?"
  6347. (and (org-at-item-p)
  6348. (save-excursion
  6349. (goto-char (match-end 0))
  6350. (skip-chars-forward " \t")
  6351. (looking-at "\\[[- X]\\]"))))
  6352. (defun org-toggle-checkbox (&optional arg)
  6353. "Toggle the checkbox in the current line."
  6354. (interactive "P")
  6355. (catch 'exit
  6356. (let (beg end status (firstnew 'unknown))
  6357. (cond
  6358. ((org-region-active-p)
  6359. (setq beg (region-beginning) end (region-end)))
  6360. ((org-on-heading-p)
  6361. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6362. ((org-at-item-checkbox-p)
  6363. (let ((pos (point)))
  6364. (replace-match
  6365. (cond (arg "[-]")
  6366. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6367. (t "[ ]"))
  6368. t t)
  6369. (goto-char pos))
  6370. (throw 'exit t))
  6371. (t (error "Not at a checkbox or heading, and no active region")))
  6372. (save-excursion
  6373. (goto-char beg)
  6374. (while (< (point) end)
  6375. (when (org-at-item-checkbox-p)
  6376. (setq status (equal (match-string 0) "[X]"))
  6377. (when (eq firstnew 'unknown)
  6378. (setq firstnew (not status)))
  6379. (replace-match
  6380. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6381. (beginning-of-line 2)))))
  6382. (org-update-checkbox-count-maybe))
  6383. (defun org-update-checkbox-count-maybe ()
  6384. "Update checkbox statistics unless turned off by user."
  6385. (when org-provide-checkbox-statistics
  6386. (org-update-checkbox-count)))
  6387. (defun org-update-checkbox-count (&optional all)
  6388. "Update the checkbox statistics in the current section.
  6389. This will find all statistic cookies like [57%] and [6/12] and update them
  6390. with the current numbers. With optional prefix argument ALL, do this for
  6391. the whole buffer."
  6392. (interactive "P")
  6393. (save-excursion
  6394. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6395. (beg (condition-case nil
  6396. (progn (outline-back-to-heading) (point))
  6397. (error (point-min))))
  6398. (end (move-marker (make-marker)
  6399. (progn (outline-next-heading) (point))))
  6400. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6401. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6402. (re-find (concat re "\\|" re-box))
  6403. beg-cookie end-cookie is-percent c-on c-off lim
  6404. eline curr-ind next-ind continue-from startsearch
  6405. (cstat 0)
  6406. )
  6407. (when all
  6408. (goto-char (point-min))
  6409. (outline-next-heading)
  6410. (setq beg (point) end (point-max)))
  6411. (goto-char end)
  6412. ;; find each statistic cookie
  6413. (while (re-search-backward re-find beg t)
  6414. (setq beg-cookie (match-beginning 1)
  6415. end-cookie (match-end 1)
  6416. cstat (+ cstat (if end-cookie 1 0))
  6417. startsearch (point-at-eol)
  6418. continue-from (point-at-bol)
  6419. is-percent (match-beginning 2)
  6420. lim (cond
  6421. ((org-on-heading-p) (outline-next-heading) (point))
  6422. ((org-at-item-p) (org-end-of-item) (point))
  6423. (t nil))
  6424. c-on 0
  6425. c-off 0)
  6426. (when lim
  6427. ;; find first checkbox for this cookie and gather
  6428. ;; statistics from all that are at this indentation level
  6429. (goto-char startsearch)
  6430. (if (re-search-forward re-box lim t)
  6431. (progn
  6432. (org-beginning-of-item)
  6433. (setq curr-ind (org-get-indentation))
  6434. (setq next-ind curr-ind)
  6435. (while (= curr-ind next-ind)
  6436. (save-excursion (end-of-line) (setq eline (point)))
  6437. (if (re-search-forward re-box eline t)
  6438. (if (member (match-string 2) '("[ ]" "[-]"))
  6439. (setq c-off (1+ c-off))
  6440. (setq c-on (1+ c-on))
  6441. )
  6442. )
  6443. (org-end-of-item)
  6444. (setq next-ind (org-get-indentation))
  6445. )))
  6446. (goto-char continue-from)
  6447. ;; update cookie
  6448. (when end-cookie
  6449. (delete-region beg-cookie end-cookie)
  6450. (goto-char beg-cookie)
  6451. (insert
  6452. (if is-percent
  6453. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6454. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6455. ;; update items checkbox if it has one
  6456. (when (org-at-item-p)
  6457. (org-beginning-of-item)
  6458. (when (and (> (+ c-on c-off) 0)
  6459. (re-search-forward re-box (point-at-eol) t))
  6460. (setq beg-cookie (match-beginning 2)
  6461. end-cookie (match-end 2))
  6462. (delete-region beg-cookie end-cookie)
  6463. (goto-char beg-cookie)
  6464. (cond ((= c-off 0) (insert "[X]"))
  6465. ((= c-on 0) (insert "[ ]"))
  6466. (t (insert "[-]")))
  6467. )))
  6468. (goto-char continue-from))
  6469. (when (interactive-p)
  6470. (message "Checkbox satistics updated %s (%d places)"
  6471. (if all "in entire file" "in current outline entry") cstat)))))
  6472. (defun org-get-checkbox-statistics-face ()
  6473. "Select the face for checkbox statistics.
  6474. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6475. it will be `org-todo'."
  6476. (if (match-end 1)
  6477. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6478. (if (and (> (match-end 2) (match-beginning 2))
  6479. (equal (match-string 2) (match-string 3)))
  6480. 'org-done
  6481. 'org-todo)))
  6482. (defun org-get-indentation (&optional line)
  6483. "Get the indentation of the current line, interpreting tabs.
  6484. When LINE is given, assume it represents a line and compute its indentation."
  6485. (if line
  6486. (if (string-match "^ *" (org-remove-tabs line))
  6487. (match-end 0))
  6488. (save-excursion
  6489. (beginning-of-line 1)
  6490. (skip-chars-forward " \t")
  6491. (current-column))))
  6492. (defun org-remove-tabs (s &optional width)
  6493. "Replace tabulators in S with spaces.
  6494. Assumes that s is a single line, starting in column 0."
  6495. (setq width (or width tab-width))
  6496. (while (string-match "\t" s)
  6497. (setq s (replace-match
  6498. (make-string
  6499. (- (* width (/ (+ (match-beginning 0) width) width))
  6500. (match-beginning 0)) ?\ )
  6501. t t s)))
  6502. s)
  6503. (defun org-fix-indentation (line ind)
  6504. "Fix indentation in LINE.
  6505. IND is a cons cell with target and minimum indentation.
  6506. If the current indenation in LINE is smaller than the minimum,
  6507. leave it alone. If it is larger than ind, set it to the target."
  6508. (let* ((l (org-remove-tabs line))
  6509. (i (org-get-indentation l))
  6510. (i1 (car ind)) (i2 (cdr ind)))
  6511. (if (>= i i2) (setq l (substring line i2)))
  6512. (if (> i1 0)
  6513. (concat (make-string i1 ?\ ) l)
  6514. l)))
  6515. (defcustom org-empty-line-terminates-plain-lists nil
  6516. "Non-nil means, an empty line ends all plain list levels.
  6517. When nil, empty lines are part of the preceeding item."
  6518. :group 'org-plain-lists
  6519. :type 'boolean)
  6520. (defun org-beginning-of-item ()
  6521. "Go to the beginning of the current hand-formatted item.
  6522. If the cursor is not in an item, throw an error."
  6523. (interactive)
  6524. (let ((pos (point))
  6525. (limit (save-excursion
  6526. (condition-case nil
  6527. (progn
  6528. (org-back-to-heading)
  6529. (beginning-of-line 2) (point))
  6530. (error (point-min)))))
  6531. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6532. ind ind1)
  6533. (if (org-at-item-p)
  6534. (beginning-of-line 1)
  6535. (beginning-of-line 1)
  6536. (skip-chars-forward " \t")
  6537. (setq ind (current-column))
  6538. (if (catch 'exit
  6539. (while t
  6540. (beginning-of-line 0)
  6541. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6542. (if (looking-at "[ \t]*$")
  6543. (setq ind1 ind-empty)
  6544. (skip-chars-forward " \t")
  6545. (setq ind1 (current-column)))
  6546. (if (< ind1 ind)
  6547. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6548. nil
  6549. (goto-char pos)
  6550. (error "Not in an item")))))
  6551. (defun org-end-of-item ()
  6552. "Go to the end of the current hand-formatted item.
  6553. If the cursor is not in an item, throw an error."
  6554. (interactive)
  6555. (let* ((pos (point))
  6556. ind1
  6557. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6558. (limit (save-excursion (outline-next-heading) (point)))
  6559. (ind (save-excursion
  6560. (org-beginning-of-item)
  6561. (skip-chars-forward " \t")
  6562. (current-column)))
  6563. (end (catch 'exit
  6564. (while t
  6565. (beginning-of-line 2)
  6566. (if (eobp) (throw 'exit (point)))
  6567. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6568. (if (looking-at "[ \t]*$")
  6569. (setq ind1 ind-empty)
  6570. (skip-chars-forward " \t")
  6571. (setq ind1 (current-column)))
  6572. (if (<= ind1 ind)
  6573. (throw 'exit (point-at-bol)))))))
  6574. (if end
  6575. (goto-char end)
  6576. (goto-char pos)
  6577. (error "Not in an item"))))
  6578. (defun org-next-item ()
  6579. "Move to the beginning of the next item in the current plain list.
  6580. Error if not at a plain list, or if this is the last item in the list."
  6581. (interactive)
  6582. (let (ind ind1 (pos (point)))
  6583. (org-beginning-of-item)
  6584. (setq ind (org-get-indentation))
  6585. (org-end-of-item)
  6586. (setq ind1 (org-get-indentation))
  6587. (unless (and (org-at-item-p) (= ind ind1))
  6588. (goto-char pos)
  6589. (error "On last item"))))
  6590. (defun org-previous-item ()
  6591. "Move to the beginning of the previous item in the current plain list.
  6592. Error if not at a plain list, or if this is the first item in the list."
  6593. (interactive)
  6594. (let (beg ind ind1 (pos (point)))
  6595. (org-beginning-of-item)
  6596. (setq beg (point))
  6597. (setq ind (org-get-indentation))
  6598. (goto-char beg)
  6599. (catch 'exit
  6600. (while t
  6601. (beginning-of-line 0)
  6602. (if (looking-at "[ \t]*$")
  6603. nil
  6604. (if (<= (setq ind1 (org-get-indentation)) ind)
  6605. (throw 'exit t)))))
  6606. (condition-case nil
  6607. (if (or (not (org-at-item-p))
  6608. (< ind1 (1- ind)))
  6609. (error "")
  6610. (org-beginning-of-item))
  6611. (error (goto-char pos)
  6612. (error "On first item")))))
  6613. (defun org-first-list-item-p ()
  6614. "Is this heading the item in a plain list?"
  6615. (unless (org-at-item-p)
  6616. (error "Not at a plain list item"))
  6617. (org-beginning-of-item)
  6618. (= (point) (save-excursion (org-beginning-of-item-list))))
  6619. (defun org-move-item-down ()
  6620. "Move the plain list item at point down, i.e. swap with following item.
  6621. Subitems (items with larger indentation) are considered part of the item,
  6622. so this really moves item trees."
  6623. (interactive)
  6624. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6625. (org-beginning-of-item)
  6626. (setq beg0 (point))
  6627. (save-excursion
  6628. (setq ne-beg (org-back-over-empty-lines))
  6629. (setq beg (point)))
  6630. (goto-char beg0)
  6631. (setq ind (org-get-indentation))
  6632. (org-end-of-item)
  6633. (setq end0 (point))
  6634. (setq ind1 (org-get-indentation))
  6635. (setq ne-end (org-back-over-empty-lines))
  6636. (setq end (point))
  6637. (goto-char beg0)
  6638. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6639. ;; include less whitespace
  6640. (save-excursion
  6641. (goto-char beg)
  6642. (forward-line (- ne-beg ne-end))
  6643. (setq beg (point))))
  6644. (goto-char end0)
  6645. (if (and (org-at-item-p) (= ind ind1))
  6646. (progn
  6647. (org-end-of-item)
  6648. (org-back-over-empty-lines)
  6649. (setq txt (buffer-substring beg end))
  6650. (save-excursion
  6651. (delete-region beg end))
  6652. (setq pos (point))
  6653. (insert txt)
  6654. (goto-char pos) (org-skip-whitespace)
  6655. (org-maybe-renumber-ordered-list))
  6656. (goto-char pos)
  6657. (error "Cannot move this item further down"))))
  6658. (defun org-move-item-up (arg)
  6659. "Move the plain list item at point up, i.e. swap with previous item.
  6660. Subitems (items with larger indentation) are considered part of the item,
  6661. so this really moves item trees."
  6662. (interactive "p")
  6663. (let (beg beg0 end ind ind1 (pos (point)) txt
  6664. ne-beg ne-ins ins-end)
  6665. (org-beginning-of-item)
  6666. (setq beg0 (point))
  6667. (setq ind (org-get-indentation))
  6668. (save-excursion
  6669. (setq ne-beg (org-back-over-empty-lines))
  6670. (setq beg (point)))
  6671. (goto-char beg0)
  6672. (org-end-of-item)
  6673. (setq end (point))
  6674. (goto-char beg0)
  6675. (catch 'exit
  6676. (while t
  6677. (beginning-of-line 0)
  6678. (if (looking-at "[ \t]*$")
  6679. (if org-empty-line-terminates-plain-lists
  6680. (progn
  6681. (goto-char pos)
  6682. (error "Cannot move this item further up"))
  6683. nil)
  6684. (if (<= (setq ind1 (org-get-indentation)) ind)
  6685. (throw 'exit t)))))
  6686. (condition-case nil
  6687. (org-beginning-of-item)
  6688. (error (goto-char beg)
  6689. (error "Cannot move this item further up")))
  6690. (setq ind1 (org-get-indentation))
  6691. (if (and (org-at-item-p) (= ind ind1))
  6692. (progn
  6693. (setq ne-ins (org-back-over-empty-lines))
  6694. (setq txt (buffer-substring beg end))
  6695. (save-excursion
  6696. (delete-region beg end))
  6697. (setq pos (point))
  6698. (insert txt)
  6699. (setq ins-end (point))
  6700. (goto-char pos) (org-skip-whitespace)
  6701. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6702. ;; Move whitespace back to beginning
  6703. (save-excursion
  6704. (goto-char ins-end)
  6705. (let ((kill-whole-line t))
  6706. (kill-line (- ne-ins ne-beg)) (point)))
  6707. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6708. (org-maybe-renumber-ordered-list))
  6709. (goto-char pos)
  6710. (error "Cannot move this item further up"))))
  6711. (defun org-maybe-renumber-ordered-list ()
  6712. "Renumber the ordered list at point if setup allows it.
  6713. This tests the user option `org-auto-renumber-ordered-lists' before
  6714. doing the renumbering."
  6715. (interactive)
  6716. (when (and org-auto-renumber-ordered-lists
  6717. (org-at-item-p))
  6718. (if (match-beginning 3)
  6719. (org-renumber-ordered-list 1)
  6720. (org-fix-bullet-type))))
  6721. (defun org-maybe-renumber-ordered-list-safe ()
  6722. (condition-case nil
  6723. (save-excursion
  6724. (org-maybe-renumber-ordered-list))
  6725. (error nil)))
  6726. (defun org-cycle-list-bullet (&optional which)
  6727. "Cycle through the different itemize/enumerate bullets.
  6728. This cycle the entire list level through the sequence:
  6729. `-' -> `+' -> `*' -> `1.' -> `1)'
  6730. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6731. 0 meand `-', 1 means `+' etc."
  6732. (interactive "P")
  6733. (org-preserve-lc
  6734. (org-beginning-of-item-list)
  6735. (org-at-item-p)
  6736. (beginning-of-line 1)
  6737. (let ((current (match-string 0))
  6738. (prevp (eq which 'previous))
  6739. new)
  6740. (setq new (cond
  6741. ((and (numberp which)
  6742. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6743. ((string-match "-" current) (if prevp "1)" "+"))
  6744. ((string-match "\\+" current)
  6745. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6746. ((string-match "\\*" current) (if prevp "+" "1."))
  6747. ((string-match "\\." current) (if prevp "*" "1)"))
  6748. ((string-match ")" current) (if prevp "1." "-"))
  6749. (t (error "This should not happen"))))
  6750. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6751. (org-fix-bullet-type)
  6752. (org-maybe-renumber-ordered-list))))
  6753. (defun org-get-string-indentation (s)
  6754. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6755. (let ((n -1) (i 0) (w tab-width) c)
  6756. (catch 'exit
  6757. (while (< (setq n (1+ n)) (length s))
  6758. (setq c (aref s n))
  6759. (cond ((= c ?\ ) (setq i (1+ i)))
  6760. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6761. (t (throw 'exit t)))))
  6762. i))
  6763. (defun org-renumber-ordered-list (arg)
  6764. "Renumber an ordered plain list.
  6765. Cursor needs to be in the first line of an item, the line that starts
  6766. with something like \"1.\" or \"2)\"."
  6767. (interactive "p")
  6768. (unless (and (org-at-item-p)
  6769. (match-beginning 3))
  6770. (error "This is not an ordered list"))
  6771. (let ((line (org-current-line))
  6772. (col (current-column))
  6773. (ind (org-get-string-indentation
  6774. (buffer-substring (point-at-bol) (match-beginning 3))))
  6775. ;; (term (substring (match-string 3) -1))
  6776. ind1 (n (1- arg))
  6777. fmt)
  6778. ;; find where this list begins
  6779. (org-beginning-of-item-list)
  6780. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6781. (setq fmt (concat "%d" (match-string 1)))
  6782. (beginning-of-line 0)
  6783. ;; walk forward and replace these numbers
  6784. (catch 'exit
  6785. (while t
  6786. (catch 'next
  6787. (beginning-of-line 2)
  6788. (if (eobp) (throw 'exit nil))
  6789. (if (looking-at "[ \t]*$") (throw 'next nil))
  6790. (skip-chars-forward " \t") (setq ind1 (current-column))
  6791. (if (> ind1 ind) (throw 'next t))
  6792. (if (< ind1 ind) (throw 'exit t))
  6793. (if (not (org-at-item-p)) (throw 'exit nil))
  6794. (delete-region (match-beginning 2) (match-end 2))
  6795. (goto-char (match-beginning 2))
  6796. (insert (format fmt (setq n (1+ n)))))))
  6797. (goto-line line)
  6798. (move-to-column col)))
  6799. (defun org-fix-bullet-type ()
  6800. "Make sure all items in this list have the same bullet as the firsst item."
  6801. (interactive)
  6802. (unless (org-at-item-p) (error "This is not a list"))
  6803. (let ((line (org-current-line))
  6804. (col (current-column))
  6805. (ind (current-indentation))
  6806. ind1 bullet)
  6807. ;; find where this list begins
  6808. (org-beginning-of-item-list)
  6809. (beginning-of-line 1)
  6810. ;; find out what the bullet type is
  6811. (looking-at "[ \t]*\\(\\S-+\\)")
  6812. (setq bullet (match-string 1))
  6813. ;; walk forward and replace these numbers
  6814. (beginning-of-line 0)
  6815. (catch 'exit
  6816. (while t
  6817. (catch 'next
  6818. (beginning-of-line 2)
  6819. (if (eobp) (throw 'exit nil))
  6820. (if (looking-at "[ \t]*$") (throw 'next nil))
  6821. (skip-chars-forward " \t") (setq ind1 (current-column))
  6822. (if (> ind1 ind) (throw 'next t))
  6823. (if (< ind1 ind) (throw 'exit t))
  6824. (if (not (org-at-item-p)) (throw 'exit nil))
  6825. (skip-chars-forward " \t")
  6826. (looking-at "\\S-+")
  6827. (replace-match bullet))))
  6828. (goto-line line)
  6829. (move-to-column col)
  6830. (if (string-match "[0-9]" bullet)
  6831. (org-renumber-ordered-list 1))))
  6832. (defun org-beginning-of-item-list ()
  6833. "Go to the beginning of the current item list.
  6834. I.e. to the first item in this list."
  6835. (interactive)
  6836. (org-beginning-of-item)
  6837. (let ((pos (point-at-bol))
  6838. (ind (org-get-indentation))
  6839. ind1)
  6840. ;; find where this list begins
  6841. (catch 'exit
  6842. (while t
  6843. (catch 'next
  6844. (beginning-of-line 0)
  6845. (if (looking-at "[ \t]*$")
  6846. (throw (if (bobp) 'exit 'next) t))
  6847. (skip-chars-forward " \t") (setq ind1 (current-column))
  6848. (if (or (< ind1 ind)
  6849. (and (= ind1 ind)
  6850. (not (org-at-item-p)))
  6851. (bobp))
  6852. (throw 'exit t)
  6853. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6854. (goto-char pos)))
  6855. (defun org-end-of-item-list ()
  6856. "Go to the end of the current item list.
  6857. I.e. to the text after the last item."
  6858. (interactive)
  6859. (org-beginning-of-item)
  6860. (let ((pos (point-at-bol))
  6861. (ind (org-get-indentation))
  6862. ind1)
  6863. ;; find where this list begins
  6864. (catch 'exit
  6865. (while t
  6866. (catch 'next
  6867. (beginning-of-line 2)
  6868. (if (looking-at "[ \t]*$")
  6869. (throw (if (eobp) 'exit 'next) t))
  6870. (skip-chars-forward " \t") (setq ind1 (current-column))
  6871. (if (or (< ind1 ind)
  6872. (and (= ind1 ind)
  6873. (not (org-at-item-p)))
  6874. (eobp))
  6875. (progn
  6876. (setq pos (point-at-bol))
  6877. (throw 'exit t))))))
  6878. (goto-char pos)))
  6879. (defvar org-last-indent-begin-marker (make-marker))
  6880. (defvar org-last-indent-end-marker (make-marker))
  6881. (defun org-outdent-item (arg)
  6882. "Outdent a local list item."
  6883. (interactive "p")
  6884. (org-indent-item (- arg)))
  6885. (defun org-indent-item (arg)
  6886. "Indent a local list item."
  6887. (interactive "p")
  6888. (unless (org-at-item-p)
  6889. (error "Not on an item"))
  6890. (save-excursion
  6891. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6892. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6893. (setq beg org-last-indent-begin-marker
  6894. end org-last-indent-end-marker)
  6895. (org-beginning-of-item)
  6896. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6897. (org-end-of-item)
  6898. (setq end (move-marker org-last-indent-end-marker (point))))
  6899. (goto-char beg)
  6900. (setq tmp (org-item-indent-positions)
  6901. ind (car tmp)
  6902. ind-down (nth 2 tmp)
  6903. ind-up (nth 1 tmp)
  6904. delta (if (> arg 0)
  6905. (if ind-down (- ind-down ind) 2)
  6906. (if ind-up (- ind-up ind) -2)))
  6907. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6908. (while (< (point) end)
  6909. (beginning-of-line 1)
  6910. (skip-chars-forward " \t") (setq ind1 (current-column))
  6911. (delete-region (point-at-bol) (point))
  6912. (or (eolp) (indent-to-column (+ ind1 delta)))
  6913. (beginning-of-line 2))))
  6914. (org-fix-bullet-type)
  6915. (org-maybe-renumber-ordered-list-safe)
  6916. (save-excursion
  6917. (beginning-of-line 0)
  6918. (condition-case nil (org-beginning-of-item) (error nil))
  6919. (org-maybe-renumber-ordered-list-safe)))
  6920. (defun org-item-indent-positions ()
  6921. "Return indentation for plain list items.
  6922. This returns a list with three values: The current indentation, the
  6923. parent indentation and the indentation a child should habe.
  6924. Assumes cursor in item line."
  6925. (let* ((bolpos (point-at-bol))
  6926. (ind (org-get-indentation))
  6927. ind-down ind-up pos)
  6928. (save-excursion
  6929. (org-beginning-of-item-list)
  6930. (skip-chars-backward "\n\r \t")
  6931. (when (org-in-item-p)
  6932. (org-beginning-of-item)
  6933. (setq ind-up (org-get-indentation))))
  6934. (setq pos (point))
  6935. (save-excursion
  6936. (cond
  6937. ((and (condition-case nil (progn (org-previous-item) t)
  6938. (error nil))
  6939. (or (forward-char 1) t)
  6940. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6941. (setq ind-down (org-get-indentation)))
  6942. ((and (goto-char pos)
  6943. (org-at-item-p))
  6944. (goto-char (match-end 0))
  6945. (skip-chars-forward " \t")
  6946. (setq ind-down (current-column)))))
  6947. (list ind ind-up ind-down)))
  6948. ;;; The orgstruct minor mode
  6949. ;; Define a minor mode which can be used in other modes in order to
  6950. ;; integrate the org-mode structure editing commands.
  6951. ;; This is really a hack, because the org-mode structure commands use
  6952. ;; keys which normally belong to the major mode. Here is how it
  6953. ;; works: The minor mode defines all the keys necessary to operate the
  6954. ;; structure commands, but wraps the commands into a function which
  6955. ;; tests if the cursor is currently at a headline or a plain list
  6956. ;; item. If that is the case, the structure command is used,
  6957. ;; temporarily setting many Org-mode variables like regular
  6958. ;; expressions for filling etc. However, when any of those keys is
  6959. ;; used at a different location, function uses `key-binding' to look
  6960. ;; up if the key has an associated command in another currently active
  6961. ;; keymap (minor modes, major mode, global), and executes that
  6962. ;; command. There might be problems if any of the keys is otherwise
  6963. ;; used as a prefix key.
  6964. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6965. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6966. ;; addresses this by checking explicitly for both bindings.
  6967. (defvar orgstruct-mode-map (make-sparse-keymap)
  6968. "Keymap for the minor `orgstruct-mode'.")
  6969. (defvar org-local-vars nil
  6970. "List of local variables, for use by `orgstruct-mode'")
  6971. ;;;###autoload
  6972. (define-minor-mode orgstruct-mode
  6973. "Toggle the minor more `orgstruct-mode'.
  6974. This mode is for using Org-mode structure commands in other modes.
  6975. The following key behave as if Org-mode was active, if the cursor
  6976. is on a headline, or on a plain list item (both in the definition
  6977. of Org-mode).
  6978. M-up Move entry/item up
  6979. M-down Move entry/item down
  6980. M-left Promote
  6981. M-right Demote
  6982. M-S-up Move entry/item up
  6983. M-S-down Move entry/item down
  6984. M-S-left Promote subtree
  6985. M-S-right Demote subtree
  6986. M-q Fill paragraph and items like in Org-mode
  6987. C-c ^ Sort entries
  6988. C-c - Cycle list bullet
  6989. TAB Cycle item visibility
  6990. M-RET Insert new heading/item
  6991. S-M-RET Insert new TODO heading / Chekbox item
  6992. C-c C-c Set tags / toggle checkbox"
  6993. nil " OrgStruct" nil
  6994. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6995. ;;;###autoload
  6996. (defun turn-on-orgstruct ()
  6997. "Unconditionally turn on `orgstruct-mode'."
  6998. (orgstruct-mode 1))
  6999. ;;;###autoload
  7000. (defun turn-on-orgstruct++ ()
  7001. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7002. In addition to setting orgstruct-mode, this also exports all indentation and
  7003. autofilling variables from org-mode into the buffer. Note that turning
  7004. off orgstruct-mode will *not* remove these additional settings."
  7005. (orgstruct-mode 1)
  7006. (let (var val)
  7007. (mapc
  7008. (lambda (x)
  7009. (when (string-match
  7010. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7011. (symbol-name (car x)))
  7012. (setq var (car x) val (nth 1 x))
  7013. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7014. org-local-vars)))
  7015. (defun orgstruct-error ()
  7016. "Error when there is no default binding for a structure key."
  7017. (interactive)
  7018. (error "This key has no function outside structure elements"))
  7019. (defun orgstruct-setup ()
  7020. "Setup orgstruct keymaps."
  7021. (let ((nfunc 0)
  7022. (bindings
  7023. (list
  7024. '([(meta up)] org-metaup)
  7025. '([(meta down)] org-metadown)
  7026. '([(meta left)] org-metaleft)
  7027. '([(meta right)] org-metaright)
  7028. '([(meta shift up)] org-shiftmetaup)
  7029. '([(meta shift down)] org-shiftmetadown)
  7030. '([(meta shift left)] org-shiftmetaleft)
  7031. '([(meta shift right)] org-shiftmetaright)
  7032. '([(shift up)] org-shiftup)
  7033. '([(shift down)] org-shiftdown)
  7034. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7035. '("\M-q" fill-paragraph)
  7036. '("\C-c^" org-sort)
  7037. '("\C-c-" org-cycle-list-bullet)))
  7038. elt key fun cmd)
  7039. (while (setq elt (pop bindings))
  7040. (setq nfunc (1+ nfunc))
  7041. (setq key (org-key (car elt))
  7042. fun (nth 1 elt)
  7043. cmd (orgstruct-make-binding fun nfunc key))
  7044. (org-defkey orgstruct-mode-map key cmd))
  7045. ;; Special treatment needed for TAB and RET
  7046. (org-defkey orgstruct-mode-map [(tab)]
  7047. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7048. (org-defkey orgstruct-mode-map "\C-i"
  7049. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7050. (org-defkey orgstruct-mode-map "\M-\C-m"
  7051. (orgstruct-make-binding 'org-insert-heading 105
  7052. "\M-\C-m" [(meta return)]))
  7053. (org-defkey orgstruct-mode-map [(meta return)]
  7054. (orgstruct-make-binding 'org-insert-heading 106
  7055. [(meta return)] "\M-\C-m"))
  7056. (org-defkey orgstruct-mode-map [(shift meta return)]
  7057. (orgstruct-make-binding 'org-insert-todo-heading 107
  7058. [(meta return)] "\M-\C-m"))
  7059. (unless org-local-vars
  7060. (setq org-local-vars (org-get-local-variables)))
  7061. t))
  7062. (defun orgstruct-make-binding (fun n &rest keys)
  7063. "Create a function for binding in the structure minor mode.
  7064. FUN is the command to call inside a table. N is used to create a unique
  7065. command name. KEYS are keys that should be checked in for a command
  7066. to execute outside of tables."
  7067. (eval
  7068. (list 'defun
  7069. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7070. '(arg)
  7071. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7072. "Outside of structure, run the binding of `"
  7073. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7074. "'.")
  7075. '(interactive "p")
  7076. (list 'if
  7077. '(org-context-p 'headline 'item)
  7078. (list 'org-run-like-in-org-mode (list 'quote fun))
  7079. (list 'let '(orgstruct-mode)
  7080. (list 'call-interactively
  7081. (append '(or)
  7082. (mapcar (lambda (k)
  7083. (list 'key-binding k))
  7084. keys)
  7085. '('orgstruct-error))))))))
  7086. (defun org-context-p (&rest contexts)
  7087. "Check if local context is and of CONTEXTS.
  7088. Possible values in the list of contexts are `table', `headline', and `item'."
  7089. (let ((pos (point)))
  7090. (goto-char (point-at-bol))
  7091. (prog1 (or (and (memq 'table contexts)
  7092. (looking-at "[ \t]*|"))
  7093. (and (memq 'headline contexts)
  7094. (looking-at "\\*+"))
  7095. (and (memq 'item contexts)
  7096. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7097. (goto-char pos))))
  7098. (defun org-get-local-variables ()
  7099. "Return a list of all local variables in an org-mode buffer."
  7100. (let (varlist)
  7101. (with-current-buffer (get-buffer-create "*Org tmp*")
  7102. (erase-buffer)
  7103. (org-mode)
  7104. (setq varlist (buffer-local-variables)))
  7105. (kill-buffer "*Org tmp*")
  7106. (delq nil
  7107. (mapcar
  7108. (lambda (x)
  7109. (setq x
  7110. (if (symbolp x)
  7111. (list x)
  7112. (list (car x) (list 'quote (cdr x)))))
  7113. (if (string-match
  7114. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7115. (symbol-name (car x)))
  7116. x nil))
  7117. varlist))))
  7118. ;;;###autoload
  7119. (defun org-run-like-in-org-mode (cmd)
  7120. (unless org-local-vars
  7121. (setq org-local-vars (org-get-local-variables)))
  7122. (eval (list 'let org-local-vars
  7123. (list 'call-interactively (list 'quote cmd)))))
  7124. ;;;; Archiving
  7125. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7126. (defun org-archive-subtree (&optional find-done)
  7127. "Move the current subtree to the archive.
  7128. The archive can be a certain top-level heading in the current file, or in
  7129. a different file. The tree will be moved to that location, the subtree
  7130. heading be marked DONE, and the current time will be added.
  7131. When called with prefix argument FIND-DONE, find whole trees without any
  7132. open TODO items and archive them (after getting confirmation from the user).
  7133. If the cursor is not at a headline when this comand is called, try all level
  7134. 1 trees. If the cursor is on a headline, only try the direct children of
  7135. this heading."
  7136. (interactive "P")
  7137. (if find-done
  7138. (org-archive-all-done)
  7139. ;; Save all relevant TODO keyword-relatex variables
  7140. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7141. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7142. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7143. (tr-org-done-keywords org-done-keywords)
  7144. (tr-org-todo-regexp org-todo-regexp)
  7145. (tr-org-todo-line-regexp org-todo-line-regexp)
  7146. (tr-org-odd-levels-only org-odd-levels-only)
  7147. (this-buffer (current-buffer))
  7148. (org-archive-location org-archive-location)
  7149. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7150. ;; start of variables that will be used for saving context
  7151. ;; The compiler complains about them - keep them anyway!
  7152. (file (abbreviate-file-name (buffer-file-name)))
  7153. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7154. (time (format-time-string
  7155. (substring (cdr org-time-stamp-formats) 1 -1)
  7156. (current-time)))
  7157. afile heading buffer level newfile-p
  7158. category todo priority
  7159. ;; start of variables that will be used for savind context
  7160. ltags itags prop)
  7161. ;; Try to find a local archive location
  7162. (save-excursion
  7163. (save-restriction
  7164. (widen)
  7165. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7166. (if (and prop (string-match "\\S-" prop))
  7167. (setq org-archive-location prop)
  7168. (if (or (re-search-backward re nil t)
  7169. (re-search-forward re nil t))
  7170. (setq org-archive-location (match-string 1))))))
  7171. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7172. (progn
  7173. (setq afile (format (match-string 1 org-archive-location)
  7174. (file-name-nondirectory buffer-file-name))
  7175. heading (match-string 2 org-archive-location)))
  7176. (error "Invalid `org-archive-location'"))
  7177. (if (> (length afile) 0)
  7178. (setq newfile-p (not (file-exists-p afile))
  7179. buffer (find-file-noselect afile))
  7180. (setq buffer (current-buffer)))
  7181. (unless buffer
  7182. (error "Cannot access file \"%s\"" afile))
  7183. (if (and (> (length heading) 0)
  7184. (string-match "^\\*+" heading))
  7185. (setq level (match-end 0))
  7186. (setq heading nil level 0))
  7187. (save-excursion
  7188. (org-back-to-heading t)
  7189. ;; Get context information that will be lost by moving the tree
  7190. (org-refresh-category-properties)
  7191. (setq category (org-get-category)
  7192. todo (and (looking-at org-todo-line-regexp)
  7193. (match-string 2))
  7194. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7195. ltags (org-get-tags)
  7196. itags (org-delete-all ltags (org-get-tags-at)))
  7197. (setq ltags (mapconcat 'identity ltags " ")
  7198. itags (mapconcat 'identity itags " "))
  7199. ;; We first only copy, in case something goes wrong
  7200. ;; we need to protect this-command, to avoid kill-region sets it,
  7201. ;; which would lead to duplication of subtrees
  7202. (let (this-command) (org-copy-subtree))
  7203. (set-buffer buffer)
  7204. ;; Enforce org-mode for the archive buffer
  7205. (if (not (org-mode-p))
  7206. ;; Force the mode for future visits.
  7207. (let ((org-insert-mode-line-in-empty-file t)
  7208. (org-inhibit-startup t))
  7209. (call-interactively 'org-mode)))
  7210. (when newfile-p
  7211. (goto-char (point-max))
  7212. (insert (format "\nArchived entries from file %s\n\n"
  7213. (buffer-file-name this-buffer))))
  7214. ;; Force the TODO keywords of the original buffer
  7215. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7216. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7217. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7218. (org-done-keywords tr-org-done-keywords)
  7219. (org-todo-regexp tr-org-todo-regexp)
  7220. (org-todo-line-regexp tr-org-todo-line-regexp)
  7221. (org-odd-levels-only
  7222. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7223. org-odd-levels-only
  7224. tr-org-odd-levels-only)))
  7225. (goto-char (point-min))
  7226. (if heading
  7227. (progn
  7228. (if (re-search-forward
  7229. (concat "^" (regexp-quote heading)
  7230. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7231. nil t)
  7232. (goto-char (match-end 0))
  7233. ;; Heading not found, just insert it at the end
  7234. (goto-char (point-max))
  7235. (or (bolp) (insert "\n"))
  7236. (insert "\n" heading "\n")
  7237. (end-of-line 0))
  7238. ;; Make the subtree visible
  7239. (show-subtree)
  7240. (org-end-of-subtree t)
  7241. (skip-chars-backward " \t\r\n")
  7242. (and (looking-at "[ \t\r\n]*")
  7243. (replace-match "\n\n")))
  7244. ;; No specific heading, just go to end of file.
  7245. (goto-char (point-max)) (insert "\n"))
  7246. ;; Paste
  7247. (org-paste-subtree (org-get-legal-level level 1))
  7248. ;; Mark the entry as done
  7249. (when (and org-archive-mark-done
  7250. (looking-at org-todo-line-regexp)
  7251. (or (not (match-end 2))
  7252. (not (member (match-string 2) org-done-keywords))))
  7253. (let (org-log-done org-todo-log-states)
  7254. (org-todo
  7255. (car (or (member org-archive-mark-done org-done-keywords)
  7256. org-done-keywords)))))
  7257. ;; Add the context info
  7258. (when org-archive-save-context-info
  7259. (let ((l org-archive-save-context-info) e n v)
  7260. (while (setq e (pop l))
  7261. (when (and (setq v (symbol-value e))
  7262. (stringp v) (string-match "\\S-" v))
  7263. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7264. (org-entry-put (point) n v)))))
  7265. ;; Save the buffer, if it is not the same buffer.
  7266. (if (not (eq this-buffer buffer)) (save-buffer))))
  7267. ;; Here we are back in the original buffer. Everything seems to have
  7268. ;; worked. So now cut the tree and finish up.
  7269. (let (this-command) (org-cut-subtree))
  7270. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7271. (message "Subtree archived %s"
  7272. (if (eq this-buffer buffer)
  7273. (concat "under heading: " heading)
  7274. (concat "in file: " (abbreviate-file-name afile)))))))
  7275. (defun org-refresh-category-properties ()
  7276. "Refresh category text properties in teh buffer."
  7277. (let ((def-cat (cond
  7278. ((null org-category)
  7279. (if buffer-file-name
  7280. (file-name-sans-extension
  7281. (file-name-nondirectory buffer-file-name))
  7282. "???"))
  7283. ((symbolp org-category) (symbol-name org-category))
  7284. (t org-category)))
  7285. beg end cat pos optionp)
  7286. (org-unmodified
  7287. (save-excursion
  7288. (save-restriction
  7289. (widen)
  7290. (goto-char (point-min))
  7291. (put-text-property (point) (point-max) 'org-category def-cat)
  7292. (while (re-search-forward
  7293. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7294. (setq pos (match-end 0)
  7295. optionp (equal (char-after (match-beginning 0)) ?#)
  7296. cat (org-trim (match-string 2)))
  7297. (if optionp
  7298. (setq beg (point-at-bol) end (point-max))
  7299. (org-back-to-heading t)
  7300. (setq beg (point) end (org-end-of-subtree t t)))
  7301. (put-text-property beg end 'org-category cat)
  7302. (goto-char pos)))))))
  7303. (defun org-archive-all-done (&optional tag)
  7304. "Archive sublevels of the current tree without open TODO items.
  7305. If the cursor is not on a headline, try all level 1 trees. If
  7306. it is on a headline, try all direct children.
  7307. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7308. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7309. (rea (concat ".*:" org-archive-tag ":"))
  7310. (begm (make-marker))
  7311. (endm (make-marker))
  7312. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7313. "Move subtree to archive (no open TODO items)? "))
  7314. beg end (cntarch 0))
  7315. (if (org-on-heading-p)
  7316. (progn
  7317. (setq re1 (concat "^" (regexp-quote
  7318. (make-string
  7319. (1+ (- (match-end 0) (match-beginning 0) 1))
  7320. ?*))
  7321. " "))
  7322. (move-marker begm (point))
  7323. (move-marker endm (org-end-of-subtree t)))
  7324. (setq re1 "^* ")
  7325. (move-marker begm (point-min))
  7326. (move-marker endm (point-max)))
  7327. (save-excursion
  7328. (goto-char begm)
  7329. (while (re-search-forward re1 endm t)
  7330. (setq beg (match-beginning 0)
  7331. end (save-excursion (org-end-of-subtree t) (point)))
  7332. (goto-char beg)
  7333. (if (re-search-forward re end t)
  7334. (goto-char end)
  7335. (goto-char beg)
  7336. (if (and (or (not tag) (not (looking-at rea)))
  7337. (y-or-n-p question))
  7338. (progn
  7339. (if tag
  7340. (org-toggle-tag org-archive-tag 'on)
  7341. (org-archive-subtree))
  7342. (setq cntarch (1+ cntarch)))
  7343. (goto-char end)))))
  7344. (message "%d trees archived" cntarch)))
  7345. (defun org-cycle-hide-drawers (state)
  7346. "Re-hide all drawers after a visibility state change."
  7347. (when (and (org-mode-p)
  7348. (not (memq state '(overview folded))))
  7349. (save-excursion
  7350. (let* ((globalp (memq state '(contents all)))
  7351. (beg (if globalp (point-min) (point)))
  7352. (end (if globalp (point-max) (org-end-of-subtree t))))
  7353. (goto-char beg)
  7354. (while (re-search-forward org-drawer-regexp end t)
  7355. (org-flag-drawer t))))))
  7356. (defun org-flag-drawer (flag)
  7357. (save-excursion
  7358. (beginning-of-line 1)
  7359. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7360. (let ((b (match-end 0))
  7361. (outline-regexp org-outline-regexp))
  7362. (if (re-search-forward
  7363. "^[ \t]*:END:"
  7364. (save-excursion (outline-next-heading) (point)) t)
  7365. (outline-flag-region b (point-at-eol) flag)
  7366. (error ":END: line missing"))))))
  7367. (defun org-cycle-hide-archived-subtrees (state)
  7368. "Re-hide all archived subtrees after a visibility state change."
  7369. (when (and (not org-cycle-open-archived-trees)
  7370. (not (memq state '(overview folded))))
  7371. (save-excursion
  7372. (let* ((globalp (memq state '(contents all)))
  7373. (beg (if globalp (point-min) (point)))
  7374. (end (if globalp (point-max) (org-end-of-subtree t))))
  7375. (org-hide-archived-subtrees beg end)
  7376. (goto-char beg)
  7377. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7378. (message "%s" (substitute-command-keys
  7379. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7380. (defun org-force-cycle-archived ()
  7381. "Cycle subtree even if it is archived."
  7382. (interactive)
  7383. (setq this-command 'org-cycle)
  7384. (let ((org-cycle-open-archived-trees t))
  7385. (call-interactively 'org-cycle)))
  7386. (defun org-hide-archived-subtrees (beg end)
  7387. "Re-hide all archived subtrees after a visibility state change."
  7388. (save-excursion
  7389. (let* ((re (concat ":" org-archive-tag ":")))
  7390. (goto-char beg)
  7391. (while (re-search-forward re end t)
  7392. (and (org-on-heading-p) (hide-subtree))
  7393. (org-end-of-subtree t)))))
  7394. (defun org-toggle-tag (tag &optional onoff)
  7395. "Toggle the tag TAG for the current line.
  7396. If ONOFF is `on' or `off', don't toggle but set to this state."
  7397. (unless (org-on-heading-p t) (error "Not on headling"))
  7398. (let (res current)
  7399. (save-excursion
  7400. (beginning-of-line)
  7401. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7402. (point-at-eol) t)
  7403. (progn
  7404. (setq current (match-string 1))
  7405. (replace-match ""))
  7406. (setq current ""))
  7407. (setq current (nreverse (org-split-string current ":")))
  7408. (cond
  7409. ((eq onoff 'on)
  7410. (setq res t)
  7411. (or (member tag current) (push tag current)))
  7412. ((eq onoff 'off)
  7413. (or (not (member tag current)) (setq current (delete tag current))))
  7414. (t (if (member tag current)
  7415. (setq current (delete tag current))
  7416. (setq res t)
  7417. (push tag current))))
  7418. (end-of-line 1)
  7419. (if current
  7420. (progn
  7421. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7422. (org-set-tags nil t))
  7423. (delete-horizontal-space))
  7424. (run-hooks 'org-after-tags-change-hook))
  7425. res))
  7426. (defun org-toggle-archive-tag (&optional arg)
  7427. "Toggle the archive tag for the current headline.
  7428. With prefix ARG, check all children of current headline and offer tagging
  7429. the children that do not contain any open TODO items."
  7430. (interactive "P")
  7431. (if arg
  7432. (org-archive-all-done 'tag)
  7433. (let (set)
  7434. (save-excursion
  7435. (org-back-to-heading t)
  7436. (setq set (org-toggle-tag org-archive-tag))
  7437. (when set (hide-subtree)))
  7438. (and set (beginning-of-line 1))
  7439. (message "Subtree %s" (if set "archived" "unarchived")))))
  7440. ;;;; Tables
  7441. ;;; The table editor
  7442. ;; Watch out: Here we are talking about two different kind of tables.
  7443. ;; Most of the code is for the tables created with the Org-mode table editor.
  7444. ;; Sometimes, we talk about tables created and edited with the table.el
  7445. ;; Emacs package. We call the former org-type tables, and the latter
  7446. ;; table.el-type tables.
  7447. (defun org-before-change-function (beg end)
  7448. "Every change indicates that a table might need an update."
  7449. (setq org-table-may-need-update t))
  7450. (defconst org-table-line-regexp "^[ \t]*|"
  7451. "Detects an org-type table line.")
  7452. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7453. "Detects an org-type table line.")
  7454. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7455. "Detects a table line marked for automatic recalculation.")
  7456. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7457. "Detects a table line marked for automatic recalculation.")
  7458. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7459. "Detects a table line marked for automatic recalculation.")
  7460. (defconst org-table-hline-regexp "^[ \t]*|-"
  7461. "Detects an org-type table hline.")
  7462. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7463. "Detects a table-type table hline.")
  7464. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7465. "Detects an org-type or table-type table.")
  7466. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7467. "Searching from within a table (any type) this finds the first line
  7468. outside the table.")
  7469. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7470. "Searching from within a table (any type) this finds the first line
  7471. outside the table.")
  7472. (defvar org-table-last-highlighted-reference nil)
  7473. (defvar org-table-formula-history nil)
  7474. (defvar org-table-column-names nil
  7475. "Alist with column names, derived from the `!' line.")
  7476. (defvar org-table-column-name-regexp nil
  7477. "Regular expression matching the current column names.")
  7478. (defvar org-table-local-parameters nil
  7479. "Alist with parameter names, derived from the `$' line.")
  7480. (defvar org-table-named-field-locations nil
  7481. "Alist with locations of named fields.")
  7482. (defvar org-table-current-line-types nil
  7483. "Table row types, non-nil only for the duration of a comand.")
  7484. (defvar org-table-current-begin-line nil
  7485. "Table begin line, non-nil only for the duration of a comand.")
  7486. (defvar org-table-current-begin-pos nil
  7487. "Table begin position, non-nil only for the duration of a comand.")
  7488. (defvar org-table-dlines nil
  7489. "Vector of data line line numbers in the current table.")
  7490. (defvar org-table-hlines nil
  7491. "Vector of hline line numbers in the current table.")
  7492. (defconst org-table-range-regexp
  7493. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7494. ;; 1 2 3 4 5
  7495. "Regular expression for matching ranges in formulas.")
  7496. (defconst org-table-range-regexp2
  7497. (concat
  7498. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7499. "\\.\\."
  7500. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7501. "Match a range for reference display.")
  7502. (defconst org-table-translate-regexp
  7503. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7504. "Match a reference that needs translation, for reference display.")
  7505. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7506. (defun org-table-create-with-table.el ()
  7507. "Use the table.el package to insert a new table.
  7508. If there is already a table at point, convert between Org-mode tables
  7509. and table.el tables."
  7510. (interactive)
  7511. (require 'table)
  7512. (cond
  7513. ((org-at-table.el-p)
  7514. (if (y-or-n-p "Convert table to Org-mode table? ")
  7515. (org-table-convert)))
  7516. ((org-at-table-p)
  7517. (if (y-or-n-p "Convert table to table.el table? ")
  7518. (org-table-convert)))
  7519. (t (call-interactively 'table-insert))))
  7520. (defun org-table-create-or-convert-from-region (arg)
  7521. "Convert region to table, or create an empty table.
  7522. If there is an active region, convert it to a table, using the function
  7523. `org-table-convert-region'. See the documentation of that function
  7524. to learn how the prefix argument is interpreted to determine the field
  7525. separator.
  7526. If there is no such region, create an empty table with `org-table-create'."
  7527. (interactive "P")
  7528. (if (org-region-active-p)
  7529. (org-table-convert-region (region-beginning) (region-end) arg)
  7530. (org-table-create arg)))
  7531. (defun org-table-create (&optional size)
  7532. "Query for a size and insert a table skeleton.
  7533. SIZE is a string Columns x Rows like for example \"3x2\"."
  7534. (interactive "P")
  7535. (unless size
  7536. (setq size (read-string
  7537. (concat "Table size Columns x Rows [e.g. "
  7538. org-table-default-size "]: ")
  7539. "" nil org-table-default-size)))
  7540. (let* ((pos (point))
  7541. (indent (make-string (current-column) ?\ ))
  7542. (split (org-split-string size " *x *"))
  7543. (rows (string-to-number (nth 1 split)))
  7544. (columns (string-to-number (car split)))
  7545. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7546. "\n")))
  7547. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7548. (point-at-bol) (point)))
  7549. (beginning-of-line 1)
  7550. (newline))
  7551. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7552. (dotimes (i rows) (insert line))
  7553. (goto-char pos)
  7554. (if (> rows 1)
  7555. ;; Insert a hline after the first row.
  7556. (progn
  7557. (end-of-line 1)
  7558. (insert "\n|-")
  7559. (goto-char pos)))
  7560. (org-table-align)))
  7561. (defun org-table-convert-region (beg0 end0 &optional separator)
  7562. "Convert region to a table.
  7563. The region goes from BEG0 to END0, but these borders will be moved
  7564. slightly, to make sure a beginning of line in the first line is included.
  7565. SEPARATOR specifies the field separator in the lines. It can have the
  7566. following values:
  7567. '(4) Use the comma as a field separator
  7568. '(16) Use a TAB as field separator
  7569. integer When a number, use that many spaces as field separator
  7570. nil When nil, the command tries to be smart and figure out the
  7571. separator in the following way:
  7572. - when each line contains a TAB, assume TAB-separated material
  7573. - when each line contains a comme, assume CSV material
  7574. - else, assume one or more SPACE charcters as separator."
  7575. (interactive "rP")
  7576. (let* ((beg (min beg0 end0))
  7577. (end (max beg0 end0))
  7578. re)
  7579. (goto-char beg)
  7580. (beginning-of-line 1)
  7581. (setq beg (move-marker (make-marker) (point)))
  7582. (goto-char end)
  7583. (if (bolp) (backward-char 1) (end-of-line 1))
  7584. (setq end (move-marker (make-marker) (point)))
  7585. ;; Get the right field separator
  7586. (unless separator
  7587. (goto-char beg)
  7588. (setq separator
  7589. (cond
  7590. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7591. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7592. (t 1))))
  7593. (setq re (cond
  7594. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7595. ((equal separator '(16)) "^\\|\t")
  7596. ((integerp separator)
  7597. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7598. (t (error "This should not happen"))))
  7599. (goto-char beg)
  7600. (while (re-search-forward re end t)
  7601. (replace-match "| " t t))
  7602. (goto-char beg)
  7603. (insert " ")
  7604. (org-table-align)))
  7605. (defun org-table-import (file arg)
  7606. "Import FILE as a table.
  7607. The file is assumed to be tab-separated. Such files can be produced by most
  7608. spreadsheet and database applications. If no tabs (at least one per line)
  7609. are found, lines will be split on whitespace into fields."
  7610. (interactive "f\nP")
  7611. (or (bolp) (newline))
  7612. (let ((beg (point))
  7613. (pm (point-max)))
  7614. (insert-file-contents file)
  7615. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7616. (defun org-table-export ()
  7617. "Export table as a tab-separated file.
  7618. Such a file can be imported into a spreadsheet program like Excel."
  7619. (interactive)
  7620. (let* ((beg (org-table-begin))
  7621. (end (org-table-end))
  7622. (table (buffer-substring beg end))
  7623. (file (read-file-name "Export table to: "))
  7624. buf)
  7625. (unless (or (not (file-exists-p file))
  7626. (y-or-n-p (format "Overwrite file %s? " file)))
  7627. (error "Abort"))
  7628. (with-current-buffer (find-file-noselect file)
  7629. (setq buf (current-buffer))
  7630. (erase-buffer)
  7631. (fundamental-mode)
  7632. (insert table)
  7633. (goto-char (point-min))
  7634. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7635. (replace-match "" t t)
  7636. (end-of-line 1))
  7637. (goto-char (point-min))
  7638. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7639. (replace-match "" t t)
  7640. (goto-char (min (1+ (point)) (point-max))))
  7641. (goto-char (point-min))
  7642. (while (re-search-forward "^-[-+]*$" nil t)
  7643. (replace-match "")
  7644. (if (looking-at "\n")
  7645. (delete-char 1)))
  7646. (goto-char (point-min))
  7647. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7648. (replace-match "\t" t t))
  7649. (save-buffer))
  7650. (kill-buffer buf)))
  7651. (defvar org-table-aligned-begin-marker (make-marker)
  7652. "Marker at the beginning of the table last aligned.
  7653. Used to check if cursor still is in that table, to minimize realignment.")
  7654. (defvar org-table-aligned-end-marker (make-marker)
  7655. "Marker at the end of the table last aligned.
  7656. Used to check if cursor still is in that table, to minimize realignment.")
  7657. (defvar org-table-last-alignment nil
  7658. "List of flags for flushright alignment, from the last re-alignment.
  7659. This is being used to correctly align a single field after TAB or RET.")
  7660. (defvar org-table-last-column-widths nil
  7661. "List of max width of fields in each column.
  7662. This is being used to correctly align a single field after TAB or RET.")
  7663. (defvar org-table-overlay-coordinates nil
  7664. "Overlay coordinates after each align of a table.")
  7665. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7666. (defvar org-last-recalc-line nil)
  7667. (defconst org-narrow-column-arrow "=>"
  7668. "Used as display property in narrowed table columns.")
  7669. (defun org-table-align ()
  7670. "Align the table at point by aligning all vertical bars."
  7671. (interactive)
  7672. (let* (
  7673. ;; Limits of table
  7674. (beg (org-table-begin))
  7675. (end (org-table-end))
  7676. ;; Current cursor position
  7677. (linepos (org-current-line))
  7678. (colpos (org-table-current-column))
  7679. (winstart (window-start))
  7680. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7681. lines (new "") lengths l typenums ty fields maxfields i
  7682. column
  7683. (indent "") cnt frac
  7684. rfmt hfmt
  7685. (spaces '(1 . 1))
  7686. (sp1 (car spaces))
  7687. (sp2 (cdr spaces))
  7688. (rfmt1 (concat
  7689. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7690. (hfmt1 (concat
  7691. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7692. emptystrings links dates emph narrow fmax f1 len c e)
  7693. (untabify beg end)
  7694. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7695. ;; Check if we have links or dates
  7696. (goto-char beg)
  7697. (setq links (re-search-forward org-bracket-link-regexp end t))
  7698. (goto-char beg)
  7699. (setq emph (and org-hide-emphasis-markers
  7700. (re-search-forward org-emph-re end t)))
  7701. (goto-char beg)
  7702. (setq dates (and org-display-custom-times
  7703. (re-search-forward org-ts-regexp-both end t)))
  7704. ;; Make sure the link properties are right
  7705. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7706. ;; Make sure the date properties are right
  7707. (when dates (goto-char beg) (while (org-activate-dates end)))
  7708. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7709. ;; Check if we are narrowing any columns
  7710. (goto-char beg)
  7711. (setq narrow (and org-format-transports-properties-p
  7712. (re-search-forward "<[0-9]+>" end t)))
  7713. ;; Get the rows
  7714. (setq lines (org-split-string
  7715. (buffer-substring beg end) "\n"))
  7716. ;; Store the indentation of the first line
  7717. (if (string-match "^ *" (car lines))
  7718. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7719. ;; Mark the hlines by setting the corresponding element to nil
  7720. ;; At the same time, we remove trailing space.
  7721. (setq lines (mapcar (lambda (l)
  7722. (if (string-match "^ *|-" l)
  7723. nil
  7724. (if (string-match "[ \t]+$" l)
  7725. (substring l 0 (match-beginning 0))
  7726. l)))
  7727. lines))
  7728. ;; Get the data fields by splitting the lines.
  7729. (setq fields (mapcar
  7730. (lambda (l)
  7731. (org-split-string l " *| *"))
  7732. (delq nil (copy-sequence lines))))
  7733. ;; How many fields in the longest line?
  7734. (condition-case nil
  7735. (setq maxfields (apply 'max (mapcar 'length fields)))
  7736. (error
  7737. (kill-region beg end)
  7738. (org-table-create org-table-default-size)
  7739. (error "Empty table - created default table")))
  7740. ;; A list of empty strings to fill any short rows on output
  7741. (setq emptystrings (make-list maxfields ""))
  7742. ;; Check for special formatting.
  7743. (setq i -1)
  7744. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7745. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7746. ;; Check if there is an explicit width specified
  7747. (when narrow
  7748. (setq c column fmax nil)
  7749. (while c
  7750. (setq e (pop c))
  7751. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7752. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7753. ;; Find fields that are wider than fmax, and shorten them
  7754. (when fmax
  7755. (loop for xx in column do
  7756. (when (and (stringp xx)
  7757. (> (org-string-width xx) fmax))
  7758. (org-add-props xx nil
  7759. 'help-echo
  7760. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7761. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7762. (unless (> f1 1)
  7763. (error "Cannot narrow field starting with wide link \"%s\""
  7764. (match-string 0 xx)))
  7765. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7766. (add-text-properties (- f1 2) f1
  7767. (list 'display org-narrow-column-arrow)
  7768. xx)))))
  7769. ;; Get the maximum width for each column
  7770. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7771. ;; Get the fraction of numbers, to decide about alignment of the column
  7772. (setq cnt 0 frac 0.0)
  7773. (loop for x in column do
  7774. (if (equal x "")
  7775. nil
  7776. (setq frac ( / (+ (* frac cnt)
  7777. (if (string-match org-table-number-regexp x) 1 0))
  7778. (setq cnt (1+ cnt))))))
  7779. (push (>= frac org-table-number-fraction) typenums))
  7780. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7781. ;; Store the alignment of this table, for later editing of single fields
  7782. (setq org-table-last-alignment typenums
  7783. org-table-last-column-widths lengths)
  7784. ;; With invisible characters, `format' does not get the field width right
  7785. ;; So we need to make these fields wide by hand.
  7786. (when (or links emph)
  7787. (loop for i from 0 upto (1- maxfields) do
  7788. (setq len (nth i lengths))
  7789. (loop for j from 0 upto (1- (length fields)) do
  7790. (setq c (nthcdr i (car (nthcdr j fields))))
  7791. (if (and (stringp (car c))
  7792. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7793. ; (string-match org-bracket-link-regexp (car c))
  7794. (< (org-string-width (car c)) len))
  7795. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7796. ;; Compute the formats needed for output of the table
  7797. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7798. (while (setq l (pop lengths))
  7799. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7800. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7801. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7802. (setq rfmt (concat rfmt "\n")
  7803. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7804. (setq new (mapconcat
  7805. (lambda (l)
  7806. (if l (apply 'format rfmt
  7807. (append (pop fields) emptystrings))
  7808. hfmt))
  7809. lines ""))
  7810. ;; Replace the old one
  7811. (delete-region beg end)
  7812. (move-marker end nil)
  7813. (move-marker org-table-aligned-begin-marker (point))
  7814. (insert new)
  7815. (move-marker org-table-aligned-end-marker (point))
  7816. (when (and orgtbl-mode (not (org-mode-p)))
  7817. (goto-char org-table-aligned-begin-marker)
  7818. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7819. ;; Try to move to the old location
  7820. (goto-line winstartline)
  7821. (setq winstart (point-at-bol))
  7822. (goto-line linepos)
  7823. (set-window-start (selected-window) winstart 'noforce)
  7824. (org-table-goto-column colpos)
  7825. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7826. (setq org-table-may-need-update nil)
  7827. ))
  7828. (defun org-string-width (s)
  7829. "Compute width of string, ignoring invisible characters.
  7830. This ignores character with invisibility property `org-link', and also
  7831. characters with property `org-cwidth', because these will become invisible
  7832. upon the next fontification round."
  7833. (let (b l)
  7834. (when (or (eq t buffer-invisibility-spec)
  7835. (assq 'org-link buffer-invisibility-spec))
  7836. (while (setq b (text-property-any 0 (length s)
  7837. 'invisible 'org-link s))
  7838. (setq s (concat (substring s 0 b)
  7839. (substring s (or (next-single-property-change
  7840. b 'invisible s) (length s)))))))
  7841. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7842. (setq s (concat (substring s 0 b)
  7843. (substring s (or (next-single-property-change
  7844. b 'org-cwidth s) (length s))))))
  7845. (setq l (string-width s) b -1)
  7846. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7847. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7848. l))
  7849. (defun org-table-begin (&optional table-type)
  7850. "Find the beginning of the table and return its position.
  7851. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7852. (save-excursion
  7853. (if (not (re-search-backward
  7854. (if table-type org-table-any-border-regexp
  7855. org-table-border-regexp)
  7856. nil t))
  7857. (progn (goto-char (point-min)) (point))
  7858. (goto-char (match-beginning 0))
  7859. (beginning-of-line 2)
  7860. (point))))
  7861. (defun org-table-end (&optional table-type)
  7862. "Find the end of the table and return its position.
  7863. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7864. (save-excursion
  7865. (if (not (re-search-forward
  7866. (if table-type org-table-any-border-regexp
  7867. org-table-border-regexp)
  7868. nil t))
  7869. (goto-char (point-max))
  7870. (goto-char (match-beginning 0)))
  7871. (point-marker)))
  7872. (defun org-table-justify-field-maybe (&optional new)
  7873. "Justify the current field, text to left, number to right.
  7874. Optional argument NEW may specify text to replace the current field content."
  7875. (cond
  7876. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7877. ((org-at-table-hline-p))
  7878. ((and (not new)
  7879. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7880. (current-buffer)))
  7881. (< (point) org-table-aligned-begin-marker)
  7882. (>= (point) org-table-aligned-end-marker)))
  7883. ;; This is not the same table, force a full re-align
  7884. (setq org-table-may-need-update t))
  7885. (t ;; realign the current field, based on previous full realign
  7886. (let* ((pos (point)) s
  7887. (col (org-table-current-column))
  7888. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7889. l f n o e)
  7890. (when (> col 0)
  7891. (skip-chars-backward "^|\n")
  7892. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7893. (progn
  7894. (setq s (match-string 1)
  7895. o (match-string 0)
  7896. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7897. e (not (= (match-beginning 2) (match-end 2))))
  7898. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7899. l (if e "|" (setq org-table-may-need-update t) ""))
  7900. n (format f s))
  7901. (if new
  7902. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7903. (setq n (format f new))
  7904. (setq n (concat new "|") org-table-may-need-update t)))
  7905. (or (equal n o)
  7906. (let (org-table-may-need-update)
  7907. (replace-match n t t))))
  7908. (setq org-table-may-need-update t))
  7909. (goto-char pos))))))
  7910. (defun org-table-next-field ()
  7911. "Go to the next field in the current table, creating new lines as needed.
  7912. Before doing so, re-align the table if necessary."
  7913. (interactive)
  7914. (org-table-maybe-eval-formula)
  7915. (org-table-maybe-recalculate-line)
  7916. (if (and org-table-automatic-realign
  7917. org-table-may-need-update)
  7918. (org-table-align))
  7919. (let ((end (org-table-end)))
  7920. (if (org-at-table-hline-p)
  7921. (end-of-line 1))
  7922. (condition-case nil
  7923. (progn
  7924. (re-search-forward "|" end)
  7925. (if (looking-at "[ \t]*$")
  7926. (re-search-forward "|" end))
  7927. (if (and (looking-at "-")
  7928. org-table-tab-jumps-over-hlines
  7929. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7930. (goto-char (match-beginning 1)))
  7931. (if (looking-at "-")
  7932. (progn
  7933. (beginning-of-line 0)
  7934. (org-table-insert-row 'below))
  7935. (if (looking-at " ") (forward-char 1))))
  7936. (error
  7937. (org-table-insert-row 'below)))))
  7938. (defun org-table-previous-field ()
  7939. "Go to the previous field in the table.
  7940. Before doing so, re-align the table if necessary."
  7941. (interactive)
  7942. (org-table-justify-field-maybe)
  7943. (org-table-maybe-recalculate-line)
  7944. (if (and org-table-automatic-realign
  7945. org-table-may-need-update)
  7946. (org-table-align))
  7947. (if (org-at-table-hline-p)
  7948. (end-of-line 1))
  7949. (re-search-backward "|" (org-table-begin))
  7950. (re-search-backward "|" (org-table-begin))
  7951. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7952. (re-search-backward "|" (org-table-begin)))
  7953. (if (looking-at "| ?")
  7954. (goto-char (match-end 0))))
  7955. (defun org-table-next-row ()
  7956. "Go to the next row (same column) in the current table.
  7957. Before doing so, re-align the table if necessary."
  7958. (interactive)
  7959. (org-table-maybe-eval-formula)
  7960. (org-table-maybe-recalculate-line)
  7961. (if (or (looking-at "[ \t]*$")
  7962. (save-excursion (skip-chars-backward " \t") (bolp)))
  7963. (newline)
  7964. (if (and org-table-automatic-realign
  7965. org-table-may-need-update)
  7966. (org-table-align))
  7967. (let ((col (org-table-current-column)))
  7968. (beginning-of-line 2)
  7969. (if (or (not (org-at-table-p))
  7970. (org-at-table-hline-p))
  7971. (progn
  7972. (beginning-of-line 0)
  7973. (org-table-insert-row 'below)))
  7974. (org-table-goto-column col)
  7975. (skip-chars-backward "^|\n\r")
  7976. (if (looking-at " ") (forward-char 1)))))
  7977. (defun org-table-copy-down (n)
  7978. "Copy a field down in the current column.
  7979. If the field at the cursor is empty, copy into it the content of the nearest
  7980. non-empty field above. With argument N, use the Nth non-empty field.
  7981. If the current field is not empty, it is copied down to the next row, and
  7982. the cursor is moved with it. Therefore, repeating this command causes the
  7983. column to be filled row-by-row.
  7984. If the variable `org-table-copy-increment' is non-nil and the field is an
  7985. integer or a timestamp, it will be incremented while copying. In the case of
  7986. a timestamp, if the cursor is on the year, change the year. If it is on the
  7987. month or the day, change that. Point will stay on the current date field
  7988. in order to easily repeat the interval."
  7989. (interactive "p")
  7990. (let* ((colpos (org-table-current-column))
  7991. (col (current-column))
  7992. (field (org-table-get-field))
  7993. (non-empty (string-match "[^ \t]" field))
  7994. (beg (org-table-begin))
  7995. txt)
  7996. (org-table-check-inside-data-field)
  7997. (if non-empty
  7998. (progn
  7999. (setq txt (org-trim field))
  8000. (org-table-next-row)
  8001. (org-table-blank-field))
  8002. (save-excursion
  8003. (setq txt
  8004. (catch 'exit
  8005. (while (progn (beginning-of-line 1)
  8006. (re-search-backward org-table-dataline-regexp
  8007. beg t))
  8008. (org-table-goto-column colpos t)
  8009. (if (and (looking-at
  8010. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8011. (= (setq n (1- n)) 0))
  8012. (throw 'exit (match-string 1))))))))
  8013. (if txt
  8014. (progn
  8015. (if (and org-table-copy-increment
  8016. (string-match "^[0-9]+$" txt))
  8017. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8018. (insert txt)
  8019. (move-to-column col)
  8020. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8021. (org-timestamp-up 1)
  8022. (org-table-maybe-recalculate-line))
  8023. (org-table-align)
  8024. (move-to-column col))
  8025. (error "No non-empty field found"))))
  8026. (defun org-table-check-inside-data-field ()
  8027. "Is point inside a table data field?
  8028. I.e. not on a hline or before the first or after the last column?
  8029. This actually throws an error, so it aborts the current command."
  8030. (if (or (not (org-at-table-p))
  8031. (= (org-table-current-column) 0)
  8032. (org-at-table-hline-p)
  8033. (looking-at "[ \t]*$"))
  8034. (error "Not in table data field")))
  8035. (defvar org-table-clip nil
  8036. "Clipboard for table regions.")
  8037. (defun org-table-blank-field ()
  8038. "Blank the current table field or active region."
  8039. (interactive)
  8040. (org-table-check-inside-data-field)
  8041. (if (and (interactive-p) (org-region-active-p))
  8042. (let (org-table-clip)
  8043. (org-table-cut-region (region-beginning) (region-end)))
  8044. (skip-chars-backward "^|")
  8045. (backward-char 1)
  8046. (if (looking-at "|[^|\n]+")
  8047. (let* ((pos (match-beginning 0))
  8048. (match (match-string 0))
  8049. (len (org-string-width match)))
  8050. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8051. (goto-char (+ 2 pos))
  8052. (substring match 1)))))
  8053. (defun org-table-get-field (&optional n replace)
  8054. "Return the value of the field in column N of current row.
  8055. N defaults to current field.
  8056. If REPLACE is a string, replace field with this value. The return value
  8057. is always the old value."
  8058. (and n (org-table-goto-column n))
  8059. (skip-chars-backward "^|\n")
  8060. (backward-char 1)
  8061. (if (looking-at "|[^|\r\n]*")
  8062. (let* ((pos (match-beginning 0))
  8063. (val (buffer-substring (1+ pos) (match-end 0))))
  8064. (if replace
  8065. (replace-match (concat "|" replace) t t))
  8066. (goto-char (min (point-at-eol) (+ 2 pos)))
  8067. val)
  8068. (forward-char 1) ""))
  8069. (defun org-table-field-info (arg)
  8070. "Show info about the current field, and highlight any reference at point."
  8071. (interactive "P")
  8072. (org-table-get-specials)
  8073. (save-excursion
  8074. (let* ((pos (point))
  8075. (col (org-table-current-column))
  8076. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8077. (name (car (rassoc (list (org-current-line) col)
  8078. org-table-named-field-locations)))
  8079. (eql (org-table-get-stored-formulas))
  8080. (dline (org-table-current-dline))
  8081. (ref (format "@%d$%d" dline col))
  8082. (ref1 (org-table-convert-refs-to-an ref))
  8083. (fequation (or (assoc name eql) (assoc ref eql)))
  8084. (cequation (assoc (int-to-string col) eql))
  8085. (eqn (or fequation cequation)))
  8086. (goto-char pos)
  8087. (condition-case nil
  8088. (org-table-show-reference 'local)
  8089. (error nil))
  8090. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8091. dline col
  8092. (if cname (concat " or $" cname) "")
  8093. dline col ref1
  8094. (if name (concat " or $" name) "")
  8095. ;; FIXME: formula info not correct if special table line
  8096. (if eqn
  8097. (concat ", formula: "
  8098. (org-table-formula-to-user
  8099. (concat
  8100. (if (string-match "^[$@]"(car eqn)) "" "$")
  8101. (car eqn) "=" (cdr eqn))))
  8102. "")))))
  8103. (defun org-table-current-column ()
  8104. "Find out which column we are in.
  8105. When called interactively, column is also displayed in echo area."
  8106. (interactive)
  8107. (if (interactive-p) (org-table-check-inside-data-field))
  8108. (save-excursion
  8109. (let ((cnt 0) (pos (point)))
  8110. (beginning-of-line 1)
  8111. (while (search-forward "|" pos t)
  8112. (setq cnt (1+ cnt)))
  8113. (if (interactive-p) (message "This is table column %d" cnt))
  8114. cnt)))
  8115. (defun org-table-current-dline ()
  8116. "Find out what table data line we are in.
  8117. Only datalins count for this."
  8118. (interactive)
  8119. (if (interactive-p) (org-table-check-inside-data-field))
  8120. (save-excursion
  8121. (let ((cnt 0) (pos (point)))
  8122. (goto-char (org-table-begin))
  8123. (while (<= (point) pos)
  8124. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8125. (beginning-of-line 2))
  8126. (if (interactive-p) (message "This is table line %d" cnt))
  8127. cnt)))
  8128. (defun org-table-goto-column (n &optional on-delim force)
  8129. "Move the cursor to the Nth column in the current table line.
  8130. With optional argument ON-DELIM, stop with point before the left delimiter
  8131. of the field.
  8132. If there are less than N fields, just go to after the last delimiter.
  8133. However, when FORCE is non-nil, create new columns if necessary."
  8134. (interactive "p")
  8135. (let ((pos (point-at-eol)))
  8136. (beginning-of-line 1)
  8137. (when (> n 0)
  8138. (while (and (> (setq n (1- n)) -1)
  8139. (or (search-forward "|" pos t)
  8140. (and force
  8141. (progn (end-of-line 1)
  8142. (skip-chars-backward "^|")
  8143. (insert " | "))))))
  8144. ; (backward-char 2) t)))))
  8145. (when (and force (not (looking-at ".*|")))
  8146. (save-excursion (end-of-line 1) (insert " | ")))
  8147. (if on-delim
  8148. (backward-char 1)
  8149. (if (looking-at " ") (forward-char 1))))))
  8150. (defun org-at-table-p (&optional table-type)
  8151. "Return t if the cursor is inside an org-type table.
  8152. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8153. (if org-enable-table-editor
  8154. (save-excursion
  8155. (beginning-of-line 1)
  8156. (looking-at (if table-type org-table-any-line-regexp
  8157. org-table-line-regexp)))
  8158. nil))
  8159. (defun org-at-table.el-p ()
  8160. "Return t if and only if we are at a table.el table."
  8161. (and (org-at-table-p 'any)
  8162. (save-excursion
  8163. (goto-char (org-table-begin 'any))
  8164. (looking-at org-table1-hline-regexp))))
  8165. (defun org-table-recognize-table.el ()
  8166. "If there is a table.el table nearby, recognize it and move into it."
  8167. (if org-table-tab-recognizes-table.el
  8168. (if (org-at-table.el-p)
  8169. (progn
  8170. (beginning-of-line 1)
  8171. (if (looking-at org-table-dataline-regexp)
  8172. nil
  8173. (if (looking-at org-table1-hline-regexp)
  8174. (progn
  8175. (beginning-of-line 2)
  8176. (if (looking-at org-table-any-border-regexp)
  8177. (beginning-of-line -1)))))
  8178. (if (re-search-forward "|" (org-table-end t) t)
  8179. (progn
  8180. (require 'table)
  8181. (if (table--at-cell-p (point))
  8182. t
  8183. (message "recognizing table.el table...")
  8184. (table-recognize-table)
  8185. (message "recognizing table.el table...done")))
  8186. (error "This should not happen..."))
  8187. t)
  8188. nil)
  8189. nil))
  8190. (defun org-at-table-hline-p ()
  8191. "Return t if the cursor is inside a hline in a table."
  8192. (if org-enable-table-editor
  8193. (save-excursion
  8194. (beginning-of-line 1)
  8195. (looking-at org-table-hline-regexp))
  8196. nil))
  8197. (defun org-table-insert-column ()
  8198. "Insert a new column into the table."
  8199. (interactive)
  8200. (if (not (org-at-table-p))
  8201. (error "Not at a table"))
  8202. (org-table-find-dataline)
  8203. (let* ((col (max 1 (org-table-current-column)))
  8204. (beg (org-table-begin))
  8205. (end (org-table-end))
  8206. ;; Current cursor position
  8207. (linepos (org-current-line))
  8208. (colpos col))
  8209. (goto-char beg)
  8210. (while (< (point) end)
  8211. (if (org-at-table-hline-p)
  8212. nil
  8213. (org-table-goto-column col t)
  8214. (insert "| "))
  8215. (beginning-of-line 2))
  8216. (move-marker end nil)
  8217. (goto-line linepos)
  8218. (org-table-goto-column colpos)
  8219. (org-table-align)
  8220. (org-table-fix-formulas "$" nil (1- col) 1)))
  8221. (defun org-table-find-dataline ()
  8222. "Find a dataline in the current table, which is needed for column commands."
  8223. (if (and (org-at-table-p)
  8224. (not (org-at-table-hline-p)))
  8225. t
  8226. (let ((col (current-column))
  8227. (end (org-table-end)))
  8228. (move-to-column col)
  8229. (while (and (< (point) end)
  8230. (or (not (= (current-column) col))
  8231. (org-at-table-hline-p)))
  8232. (beginning-of-line 2)
  8233. (move-to-column col))
  8234. (if (and (org-at-table-p)
  8235. (not (org-at-table-hline-p)))
  8236. t
  8237. (error
  8238. "Please position cursor in a data line for column operations")))))
  8239. (defun org-table-delete-column ()
  8240. "Delete a column from the table."
  8241. (interactive)
  8242. (if (not (org-at-table-p))
  8243. (error "Not at a table"))
  8244. (org-table-find-dataline)
  8245. (org-table-check-inside-data-field)
  8246. (let* ((col (org-table-current-column))
  8247. (beg (org-table-begin))
  8248. (end (org-table-end))
  8249. ;; Current cursor position
  8250. (linepos (org-current-line))
  8251. (colpos col))
  8252. (goto-char beg)
  8253. (while (< (point) end)
  8254. (if (org-at-table-hline-p)
  8255. nil
  8256. (org-table-goto-column col t)
  8257. (and (looking-at "|[^|\n]+|")
  8258. (replace-match "|")))
  8259. (beginning-of-line 2))
  8260. (move-marker end nil)
  8261. (goto-line linepos)
  8262. (org-table-goto-column colpos)
  8263. (org-table-align)
  8264. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8265. col -1 col)))
  8266. (defun org-table-move-column-right ()
  8267. "Move column to the right."
  8268. (interactive)
  8269. (org-table-move-column nil))
  8270. (defun org-table-move-column-left ()
  8271. "Move column to the left."
  8272. (interactive)
  8273. (org-table-move-column 'left))
  8274. (defun org-table-move-column (&optional left)
  8275. "Move the current column to the right. With arg LEFT, move to the left."
  8276. (interactive "P")
  8277. (if (not (org-at-table-p))
  8278. (error "Not at a table"))
  8279. (org-table-find-dataline)
  8280. (org-table-check-inside-data-field)
  8281. (let* ((col (org-table-current-column))
  8282. (col1 (if left (1- col) col))
  8283. (beg (org-table-begin))
  8284. (end (org-table-end))
  8285. ;; Current cursor position
  8286. (linepos (org-current-line))
  8287. (colpos (if left (1- col) (1+ col))))
  8288. (if (and left (= col 1))
  8289. (error "Cannot move column further left"))
  8290. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8291. (error "Cannot move column further right"))
  8292. (goto-char beg)
  8293. (while (< (point) end)
  8294. (if (org-at-table-hline-p)
  8295. nil
  8296. (org-table-goto-column col1 t)
  8297. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8298. (replace-match "|\\2|\\1|")))
  8299. (beginning-of-line 2))
  8300. (move-marker end nil)
  8301. (goto-line linepos)
  8302. (org-table-goto-column colpos)
  8303. (org-table-align)
  8304. (org-table-fix-formulas
  8305. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8306. (cons (number-to-string colpos) (number-to-string col))))))
  8307. (defun org-table-move-row-down ()
  8308. "Move table row down."
  8309. (interactive)
  8310. (org-table-move-row nil))
  8311. (defun org-table-move-row-up ()
  8312. "Move table row up."
  8313. (interactive)
  8314. (org-table-move-row 'up))
  8315. (defun org-table-move-row (&optional up)
  8316. "Move the current table line down. With arg UP, move it up."
  8317. (interactive "P")
  8318. (let* ((col (current-column))
  8319. (pos (point))
  8320. (hline1p (save-excursion (beginning-of-line 1)
  8321. (looking-at org-table-hline-regexp)))
  8322. (dline1 (org-table-current-dline))
  8323. (dline2 (+ dline1 (if up -1 1)))
  8324. (tonew (if up 0 2))
  8325. txt hline2p)
  8326. (beginning-of-line tonew)
  8327. (unless (org-at-table-p)
  8328. (goto-char pos)
  8329. (error "Cannot move row further"))
  8330. (setq hline2p (looking-at org-table-hline-regexp))
  8331. (goto-char pos)
  8332. (beginning-of-line 1)
  8333. (setq pos (point))
  8334. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8335. (delete-region (point) (1+ (point-at-eol)))
  8336. (beginning-of-line tonew)
  8337. (insert txt)
  8338. (beginning-of-line 0)
  8339. (move-to-column col)
  8340. (unless (or hline1p hline2p)
  8341. (org-table-fix-formulas
  8342. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8343. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8344. (defun org-table-insert-row (&optional arg)
  8345. "Insert a new row above the current line into the table.
  8346. With prefix ARG, insert below the current line."
  8347. (interactive "P")
  8348. (if (not (org-at-table-p))
  8349. (error "Not at a table"))
  8350. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8351. (new (org-table-clean-line line)))
  8352. ;; Fix the first field if necessary
  8353. (if (string-match "^[ \t]*| *[#$] *|" line)
  8354. (setq new (replace-match (match-string 0 line) t t new)))
  8355. (beginning-of-line (if arg 2 1))
  8356. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8357. (beginning-of-line 0)
  8358. (re-search-forward "| ?" (point-at-eol) t)
  8359. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8360. (org-table-align))
  8361. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8362. (defun org-table-insert-hline (&optional above)
  8363. "Insert a horizontal-line below the current line into the table.
  8364. With prefix ABOVE, insert above the current line."
  8365. (interactive "P")
  8366. (if (not (org-at-table-p))
  8367. (error "Not at a table"))
  8368. (let ((line (org-table-clean-line
  8369. (buffer-substring (point-at-bol) (point-at-eol))))
  8370. (col (current-column)))
  8371. (while (string-match "|\\( +\\)|" line)
  8372. (setq line (replace-match
  8373. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8374. ?-) "|") t t line)))
  8375. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8376. (beginning-of-line (if above 1 2))
  8377. (insert line "\n")
  8378. (beginning-of-line (if above 1 -1))
  8379. (move-to-column col)
  8380. (and org-table-overlay-coordinates (org-table-align))))
  8381. (defun org-table-hline-and-move (&optional same-column)
  8382. "Insert a hline and move to the row below that line."
  8383. (interactive "P")
  8384. (let ((col (org-table-current-column)))
  8385. (org-table-maybe-eval-formula)
  8386. (org-table-maybe-recalculate-line)
  8387. (org-table-insert-hline)
  8388. (end-of-line 2)
  8389. (if (looking-at "\n[ \t]*|-")
  8390. (progn (insert "\n|") (org-table-align))
  8391. (org-table-next-field))
  8392. (if same-column (org-table-goto-column col))))
  8393. (defun org-table-clean-line (s)
  8394. "Convert a table line S into a string with only \"|\" and space.
  8395. In particular, this does handle wide and invisible characters."
  8396. (if (string-match "^[ \t]*|-" s)
  8397. ;; It's a hline, just map the characters
  8398. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8399. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8400. (setq s (replace-match
  8401. (concat "|" (make-string (org-string-width (match-string 1 s))
  8402. ?\ ) "|")
  8403. t t s)))
  8404. s))
  8405. (defun org-table-kill-row ()
  8406. "Delete the current row or horizontal line from the table."
  8407. (interactive)
  8408. (if (not (org-at-table-p))
  8409. (error "Not at a table"))
  8410. (let ((col (current-column))
  8411. (dline (org-table-current-dline)))
  8412. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8413. (if (not (org-at-table-p)) (beginning-of-line 0))
  8414. (move-to-column col)
  8415. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8416. dline -1 dline)))
  8417. (defun org-table-sort-lines (with-case &optional sorting-type)
  8418. "Sort table lines according to the column at point.
  8419. The position of point indicates the column to be used for
  8420. sorting, and the range of lines is the range between the nearest
  8421. horizontal separator lines, or the entire table of no such lines
  8422. exist. If point is before the first column, you will be prompted
  8423. for the sorting column. If there is an active region, the mark
  8424. specifies the first line and the sorting column, while point
  8425. should be in the last line to be included into the sorting.
  8426. The command then prompts for the sorting type which can be
  8427. alphabetically, numerically, or by time (as given in a time stamp
  8428. in the field). Sorting in reverse order is also possible.
  8429. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8430. If SORTING-TYPE is specified when this function is called from a Lisp
  8431. program, no prompting will take place. SORTING-TYPE must be a character,
  8432. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8433. should be done in reverse order."
  8434. (interactive "P")
  8435. (let* ((thisline (org-current-line))
  8436. (thiscol (org-table-current-column))
  8437. beg end bcol ecol tend tbeg column lns pos)
  8438. (when (equal thiscol 0)
  8439. (if (interactive-p)
  8440. (setq thiscol
  8441. (string-to-number
  8442. (read-string "Use column N for sorting: ")))
  8443. (setq thiscol 1))
  8444. (org-table-goto-column thiscol))
  8445. (org-table-check-inside-data-field)
  8446. (if (org-region-active-p)
  8447. (progn
  8448. (setq beg (region-beginning) end (region-end))
  8449. (goto-char beg)
  8450. (setq column (org-table-current-column)
  8451. beg (point-at-bol))
  8452. (goto-char end)
  8453. (setq end (point-at-bol 2)))
  8454. (setq column (org-table-current-column)
  8455. pos (point)
  8456. tbeg (org-table-begin)
  8457. tend (org-table-end))
  8458. (if (re-search-backward org-table-hline-regexp tbeg t)
  8459. (setq beg (point-at-bol 2))
  8460. (goto-char tbeg)
  8461. (setq beg (point-at-bol 1)))
  8462. (goto-char pos)
  8463. (if (re-search-forward org-table-hline-regexp tend t)
  8464. (setq end (point-at-bol 1))
  8465. (goto-char tend)
  8466. (setq end (point-at-bol))))
  8467. (setq beg (move-marker (make-marker) beg)
  8468. end (move-marker (make-marker) end))
  8469. (untabify beg end)
  8470. (goto-char beg)
  8471. (org-table-goto-column column)
  8472. (skip-chars-backward "^|")
  8473. (setq bcol (current-column))
  8474. (org-table-goto-column (1+ column))
  8475. (skip-chars-backward "^|")
  8476. (setq ecol (1- (current-column)))
  8477. (org-table-goto-column column)
  8478. (setq lns (mapcar (lambda(x) (cons
  8479. (org-sort-remove-invisible
  8480. (nth (1- column)
  8481. (org-split-string x "[ \t]*|[ \t]*")))
  8482. x))
  8483. (org-split-string (buffer-substring beg end) "\n")))
  8484. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8485. (delete-region beg end)
  8486. (move-marker beg nil)
  8487. (move-marker end nil)
  8488. (insert (mapconcat 'cdr lns "\n") "\n")
  8489. (goto-line thisline)
  8490. (org-table-goto-column thiscol)
  8491. (message "%d lines sorted, based on column %d" (length lns) column)))
  8492. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8493. (defun org-sort-remove-invisible (s)
  8494. (remove-text-properties 0 (length s) org-rm-props s)
  8495. (while (string-match org-bracket-link-regexp s)
  8496. (setq s (replace-match (if (match-end 2)
  8497. (match-string 3 s)
  8498. (match-string 1 s)) t t s)))
  8499. s)
  8500. (defun org-table-cut-region (beg end)
  8501. "Copy region in table to the clipboard and blank all relevant fields."
  8502. (interactive "r")
  8503. (org-table-copy-region beg end 'cut))
  8504. (defun org-table-copy-region (beg end &optional cut)
  8505. "Copy rectangular region in table to clipboard.
  8506. A special clipboard is used which can only be accessed
  8507. with `org-table-paste-rectangle'."
  8508. (interactive "rP")
  8509. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8510. region cols
  8511. (rpl (if cut " " nil)))
  8512. (goto-char beg)
  8513. (org-table-check-inside-data-field)
  8514. (setq l01 (org-current-line)
  8515. c01 (org-table-current-column))
  8516. (goto-char end)
  8517. (org-table-check-inside-data-field)
  8518. (setq l02 (org-current-line)
  8519. c02 (org-table-current-column))
  8520. (setq l1 (min l01 l02) l2 (max l01 l02)
  8521. c1 (min c01 c02) c2 (max c01 c02))
  8522. (catch 'exit
  8523. (while t
  8524. (catch 'nextline
  8525. (if (> l1 l2) (throw 'exit t))
  8526. (goto-line l1)
  8527. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8528. (setq cols nil ic1 c1 ic2 c2)
  8529. (while (< ic1 (1+ ic2))
  8530. (push (org-table-get-field ic1 rpl) cols)
  8531. (setq ic1 (1+ ic1)))
  8532. (push (nreverse cols) region)
  8533. (setq l1 (1+ l1)))))
  8534. (setq org-table-clip (nreverse region))
  8535. (if cut (org-table-align))
  8536. org-table-clip))
  8537. (defun org-table-paste-rectangle ()
  8538. "Paste a rectangular region into a table.
  8539. The upper right corner ends up in the current field. All involved fields
  8540. will be overwritten. If the rectangle does not fit into the present table,
  8541. the table is enlarged as needed. The process ignores horizontal separator
  8542. lines."
  8543. (interactive)
  8544. (unless (and org-table-clip (listp org-table-clip))
  8545. (error "First cut/copy a region to paste!"))
  8546. (org-table-check-inside-data-field)
  8547. (let* ((clip org-table-clip)
  8548. (line (org-current-line))
  8549. (col (org-table-current-column))
  8550. (org-enable-table-editor t)
  8551. (org-table-automatic-realign nil)
  8552. c cols field)
  8553. (while (setq cols (pop clip))
  8554. (while (org-at-table-hline-p) (beginning-of-line 2))
  8555. (if (not (org-at-table-p))
  8556. (progn (end-of-line 0) (org-table-next-field)))
  8557. (setq c col)
  8558. (while (setq field (pop cols))
  8559. (org-table-goto-column c nil 'force)
  8560. (org-table-get-field nil field)
  8561. (setq c (1+ c)))
  8562. (beginning-of-line 2))
  8563. (goto-line line)
  8564. (org-table-goto-column col)
  8565. (org-table-align)))
  8566. (defun org-table-convert ()
  8567. "Convert from `org-mode' table to table.el and back.
  8568. Obviously, this only works within limits. When an Org-mode table is
  8569. converted to table.el, all horizontal separator lines get lost, because
  8570. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8571. A table.el table can be converted to an Org-mode table only if it does not
  8572. do row or column spanning. Multiline cells will become multiple cells.
  8573. Beware, Org-mode does not test if the table can be successfully converted - it
  8574. blindly applies a recipe that works for simple tables."
  8575. (interactive)
  8576. (require 'table)
  8577. (if (org-at-table.el-p)
  8578. ;; convert to Org-mode table
  8579. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8580. (end (move-marker (make-marker) (org-table-end t))))
  8581. (table-unrecognize-region beg end)
  8582. (goto-char beg)
  8583. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8584. (replace-match ""))
  8585. (goto-char beg))
  8586. (if (org-at-table-p)
  8587. ;; convert to table.el table
  8588. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8589. (end (move-marker (make-marker) (org-table-end))))
  8590. ;; first, get rid of all horizontal lines
  8591. (goto-char beg)
  8592. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8593. (replace-match ""))
  8594. ;; insert a hline before first
  8595. (goto-char beg)
  8596. (org-table-insert-hline 'above)
  8597. (beginning-of-line -1)
  8598. ;; insert a hline after each line
  8599. (while (progn (beginning-of-line 3) (< (point) end))
  8600. (org-table-insert-hline))
  8601. (goto-char beg)
  8602. (setq end (move-marker end (org-table-end)))
  8603. ;; replace "+" at beginning and ending of hlines
  8604. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8605. (replace-match "\\1+-"))
  8606. (goto-char beg)
  8607. (while (re-search-forward "-|[ \t]*$" end t)
  8608. (replace-match "-+"))
  8609. (goto-char beg)))))
  8610. (defun org-table-wrap-region (arg)
  8611. "Wrap several fields in a column like a paragraph.
  8612. This is useful if you'd like to spread the contents of a field over several
  8613. lines, in order to keep the table compact.
  8614. If there is an active region, and both point and mark are in the same column,
  8615. the text in the column is wrapped to minimum width for the given number of
  8616. lines. Generally, this makes the table more compact. A prefix ARG may be
  8617. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8618. formats the selected text to two lines. If the region was longer than two
  8619. lines, the remaining lines remain empty. A negative prefix argument reduces
  8620. the current number of lines by that amount. The wrapped text is pasted back
  8621. into the table. If you formatted it to more lines than it was before, fields
  8622. further down in the table get overwritten - so you might need to make space in
  8623. the table first.
  8624. If there is no region, the current field is split at the cursor position and
  8625. the text fragment to the right of the cursor is prepended to the field one
  8626. line down.
  8627. If there is no region, but you specify a prefix ARG, the current field gets
  8628. blank, and the content is appended to the field above."
  8629. (interactive "P")
  8630. (org-table-check-inside-data-field)
  8631. (if (org-region-active-p)
  8632. ;; There is a region: fill as a paragraph
  8633. (let* ((beg (region-beginning))
  8634. (cline (save-excursion (goto-char beg) (org-current-line)))
  8635. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8636. nlines)
  8637. (org-table-cut-region (region-beginning) (region-end))
  8638. (if (> (length (car org-table-clip)) 1)
  8639. (error "Region must be limited to single column"))
  8640. (setq nlines (if arg
  8641. (if (< arg 1)
  8642. (+ (length org-table-clip) arg)
  8643. arg)
  8644. (length org-table-clip)))
  8645. (setq org-table-clip
  8646. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8647. nil nlines)))
  8648. (goto-line cline)
  8649. (org-table-goto-column ccol)
  8650. (org-table-paste-rectangle))
  8651. ;; No region, split the current field at point
  8652. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8653. (skip-chars-forward "^\r\n|"))
  8654. (if arg
  8655. ;; combine with field above
  8656. (let ((s (org-table-blank-field))
  8657. (col (org-table-current-column)))
  8658. (beginning-of-line 0)
  8659. (while (org-at-table-hline-p) (beginning-of-line 0))
  8660. (org-table-goto-column col)
  8661. (skip-chars-forward "^|")
  8662. (skip-chars-backward " ")
  8663. (insert " " (org-trim s))
  8664. (org-table-align))
  8665. ;; split field
  8666. (if (looking-at "\\([^|]+\\)+|")
  8667. (let ((s (match-string 1)))
  8668. (replace-match " |")
  8669. (goto-char (match-beginning 0))
  8670. (org-table-next-row)
  8671. (insert (org-trim s) " ")
  8672. (org-table-align))
  8673. (org-table-next-row)))))
  8674. (defvar org-field-marker nil)
  8675. (defun org-table-edit-field (arg)
  8676. "Edit table field in a different window.
  8677. This is mainly useful for fields that contain hidden parts.
  8678. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8679. it can be edited in place."
  8680. (interactive "P")
  8681. (if arg
  8682. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8683. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8684. (remove-text-properties b e '(org-cwidth t invisible t
  8685. display t intangible t))
  8686. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8687. (font-lock-fontify-block)))
  8688. (let ((pos (move-marker (make-marker) (point)))
  8689. (field (org-table-get-field))
  8690. (cw (current-window-configuration))
  8691. p)
  8692. (org-switch-to-buffer-other-window "*Org tmp*")
  8693. (erase-buffer)
  8694. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8695. (let ((org-inhibit-startup t)) (org-mode))
  8696. (goto-char (setq p (point-max)))
  8697. (insert (org-trim field))
  8698. (remove-text-properties p (point-max)
  8699. '(invisible t org-cwidth t display t
  8700. intangible t))
  8701. (goto-char p)
  8702. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8703. (org-set-local 'org-window-configuration cw)
  8704. (org-set-local 'org-field-marker pos)
  8705. (message "Edit and finish with C-c C-c"))))
  8706. (defun org-table-finish-edit-field ()
  8707. "Finish editing a table data field.
  8708. Remove all newline characters, insert the result into the table, realign
  8709. the table and kill the editing buffer."
  8710. (let ((pos org-field-marker)
  8711. (cw org-window-configuration)
  8712. (cb (current-buffer))
  8713. text)
  8714. (goto-char (point-min))
  8715. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8716. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8717. (replace-match " "))
  8718. (setq text (org-trim (buffer-string)))
  8719. (set-window-configuration cw)
  8720. (kill-buffer cb)
  8721. (select-window (get-buffer-window (marker-buffer pos)))
  8722. (goto-char pos)
  8723. (move-marker pos nil)
  8724. (org-table-check-inside-data-field)
  8725. (org-table-get-field nil text)
  8726. (org-table-align)
  8727. (message "New field value inserted")))
  8728. (defun org-trim (s)
  8729. "Remove whitespace at beginning and end of string."
  8730. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8731. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8732. s)
  8733. (defun org-wrap (string &optional width lines)
  8734. "Wrap string to either a number of lines, or a width in characters.
  8735. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8736. that costs. If there is a word longer than WIDTH, the text is actually
  8737. wrapped to the length of that word.
  8738. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8739. many lines, whatever width that takes.
  8740. The return value is a list of lines, without newlines at the end."
  8741. (let* ((words (org-split-string string "[ \t\n]+"))
  8742. (maxword (apply 'max (mapcar 'org-string-width words)))
  8743. w ll)
  8744. (cond (width
  8745. (org-do-wrap words (max maxword width)))
  8746. (lines
  8747. (setq w maxword)
  8748. (setq ll (org-do-wrap words maxword))
  8749. (if (<= (length ll) lines)
  8750. ll
  8751. (setq ll words)
  8752. (while (> (length ll) lines)
  8753. (setq w (1+ w))
  8754. (setq ll (org-do-wrap words w)))
  8755. ll))
  8756. (t (error "Cannot wrap this")))))
  8757. (defun org-do-wrap (words width)
  8758. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8759. (let (lines line)
  8760. (while words
  8761. (setq line (pop words))
  8762. (while (and words (< (+ (length line) (length (car words))) width))
  8763. (setq line (concat line " " (pop words))))
  8764. (setq lines (push line lines)))
  8765. (nreverse lines)))
  8766. (defun org-split-string (string &optional separators)
  8767. "Splits STRING into substrings at SEPARATORS.
  8768. No empty strings are returned if there are matches at the beginning
  8769. and end of string."
  8770. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8771. (start 0)
  8772. notfirst
  8773. (list nil))
  8774. (while (and (string-match rexp string
  8775. (if (and notfirst
  8776. (= start (match-beginning 0))
  8777. (< start (length string)))
  8778. (1+ start) start))
  8779. (< (match-beginning 0) (length string)))
  8780. (setq notfirst t)
  8781. (or (eq (match-beginning 0) 0)
  8782. (and (eq (match-beginning 0) (match-end 0))
  8783. (eq (match-beginning 0) start))
  8784. (setq list
  8785. (cons (substring string start (match-beginning 0))
  8786. list)))
  8787. (setq start (match-end 0)))
  8788. (or (eq start (length string))
  8789. (setq list
  8790. (cons (substring string start)
  8791. list)))
  8792. (nreverse list)))
  8793. (defun org-table-map-tables (function)
  8794. "Apply FUNCTION to the start of all tables in the buffer."
  8795. (save-excursion
  8796. (save-restriction
  8797. (widen)
  8798. (goto-char (point-min))
  8799. (while (re-search-forward org-table-any-line-regexp nil t)
  8800. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8801. (beginning-of-line 1)
  8802. (if (looking-at org-table-line-regexp)
  8803. (save-excursion (funcall function)))
  8804. (re-search-forward org-table-any-border-regexp nil 1))))
  8805. (message "Mapping tables: done"))
  8806. (defvar org-timecnt) ; dynamically scoped parameter
  8807. (defun org-table-sum (&optional beg end nlast)
  8808. "Sum numbers in region of current table column.
  8809. The result will be displayed in the echo area, and will be available
  8810. as kill to be inserted with \\[yank].
  8811. If there is an active region, it is interpreted as a rectangle and all
  8812. numbers in that rectangle will be summed. If there is no active
  8813. region and point is located in a table column, sum all numbers in that
  8814. column.
  8815. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8816. numbers are assumed to be times as well (in decimal hours) and the
  8817. numbers are added as such.
  8818. If NLAST is a number, only the NLAST fields will actually be summed."
  8819. (interactive)
  8820. (save-excursion
  8821. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8822. (cond
  8823. ((and beg end)) ; beg and end given explicitly
  8824. ((org-region-active-p)
  8825. (setq beg (region-beginning) end (region-end)))
  8826. (t
  8827. (setq col (org-table-current-column))
  8828. (goto-char (org-table-begin))
  8829. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8830. (error "No table data"))
  8831. (org-table-goto-column col)
  8832. (setq beg (point))
  8833. (goto-char (org-table-end))
  8834. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8835. (error "No table data"))
  8836. (org-table-goto-column col)
  8837. (setq end (point))))
  8838. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8839. (items1 (cond ((not nlast) items)
  8840. ((>= nlast (length items)) items)
  8841. (t (setq items (reverse items))
  8842. (setcdr (nthcdr (1- nlast) items) nil)
  8843. (nreverse items))))
  8844. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8845. items1)))
  8846. (res (apply '+ numbers))
  8847. (sres (if (= org-timecnt 0)
  8848. (format "%g" res)
  8849. (setq diff (* 3600 res)
  8850. h (floor (/ diff 3600)) diff (mod diff 3600)
  8851. m (floor (/ diff 60)) diff (mod diff 60)
  8852. s diff)
  8853. (format "%d:%02d:%02d" h m s))))
  8854. (kill-new sres)
  8855. (if (interactive-p)
  8856. (message "%s"
  8857. (substitute-command-keys
  8858. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8859. (length numbers) sres))))
  8860. sres))))
  8861. (defun org-table-get-number-for-summing (s)
  8862. (let (n)
  8863. (if (string-match "^ *|? *" s)
  8864. (setq s (replace-match "" nil nil s)))
  8865. (if (string-match " *|? *$" s)
  8866. (setq s (replace-match "" nil nil s)))
  8867. (setq n (string-to-number s))
  8868. (cond
  8869. ((and (string-match "0" s)
  8870. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8871. ((string-match "\\`[ \t]+\\'" s) nil)
  8872. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8873. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8874. (m (string-to-number (or (match-string 2 s) "0")))
  8875. (s (string-to-number (or (match-string 4 s) "0"))))
  8876. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8877. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8878. ((equal n 0) nil)
  8879. (t n))))
  8880. (defun org-table-current-field-formula (&optional key noerror)
  8881. "Return the formula active for the current field.
  8882. Assumes that specials are in place.
  8883. If KEY is given, return the key to this formula.
  8884. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8885. (let* ((name (car (rassoc (list (org-current-line)
  8886. (org-table-current-column))
  8887. org-table-named-field-locations)))
  8888. (col (org-table-current-column))
  8889. (scol (int-to-string col))
  8890. (ref (format "@%d$%d" (org-table-current-dline) col))
  8891. (stored-list (org-table-get-stored-formulas noerror))
  8892. (ass (or (assoc name stored-list)
  8893. (assoc ref stored-list)
  8894. (assoc scol stored-list))))
  8895. (if key
  8896. (car ass)
  8897. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8898. (cdr ass))))))
  8899. (defun org-table-get-formula (&optional equation named)
  8900. "Read a formula from the minibuffer, offer stored formula as default.
  8901. When NAMED is non-nil, look for a named equation."
  8902. (let* ((stored-list (org-table-get-stored-formulas))
  8903. (name (car (rassoc (list (org-current-line)
  8904. (org-table-current-column))
  8905. org-table-named-field-locations)))
  8906. (ref (format "@%d$%d" (org-table-current-dline)
  8907. (org-table-current-column)))
  8908. (refass (assoc ref stored-list))
  8909. (scol (if named
  8910. (if name name ref)
  8911. (int-to-string (org-table-current-column))))
  8912. (dummy (and (or name refass) (not named)
  8913. (not (y-or-n-p "Replace field formula with column formula? " ))
  8914. (error "Abort")))
  8915. (name (or name ref))
  8916. (org-table-may-need-update nil)
  8917. (stored (cdr (assoc scol stored-list)))
  8918. (eq (cond
  8919. ((and stored equation (string-match "^ *=? *$" equation))
  8920. stored)
  8921. ((stringp equation)
  8922. equation)
  8923. (t (org-table-formula-from-user
  8924. (read-string
  8925. (org-table-formula-to-user
  8926. (format "%s formula %s%s="
  8927. (if named "Field" "Column")
  8928. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8929. scol))
  8930. (if stored (org-table-formula-to-user stored) "")
  8931. 'org-table-formula-history
  8932. )))))
  8933. mustsave)
  8934. (when (not (string-match "\\S-" eq))
  8935. ;; remove formula
  8936. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8937. (org-table-store-formulas stored-list)
  8938. (error "Formula removed"))
  8939. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8940. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8941. (if (and name (not named))
  8942. ;; We set the column equation, delete the named one.
  8943. (setq stored-list (delq (assoc name stored-list) stored-list)
  8944. mustsave t))
  8945. (if stored
  8946. (setcdr (assoc scol stored-list) eq)
  8947. (setq stored-list (cons (cons scol eq) stored-list)))
  8948. (if (or mustsave (not (equal stored eq)))
  8949. (org-table-store-formulas stored-list))
  8950. eq))
  8951. (defun org-table-store-formulas (alist)
  8952. "Store the list of formulas below the current table."
  8953. (setq alist (sort alist 'org-table-formula-less-p))
  8954. (save-excursion
  8955. (goto-char (org-table-end))
  8956. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8957. (progn
  8958. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8959. (goto-char (match-beginning 2))
  8960. (delete-region (match-beginning 2) (match-end 0)))
  8961. (insert "#+TBLFM:"))
  8962. (insert " "
  8963. (mapconcat (lambda (x)
  8964. (concat
  8965. (if (equal (string-to-char (car x)) ?@) "" "$")
  8966. (car x) "=" (cdr x)))
  8967. alist "::")
  8968. "\n")))
  8969. (defsubst org-table-formula-make-cmp-string (a)
  8970. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8971. (concat
  8972. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8973. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8974. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8975. (defun org-table-formula-less-p (a b)
  8976. "Compare two formulas for sorting."
  8977. (let ((as (org-table-formula-make-cmp-string (car a)))
  8978. (bs (org-table-formula-make-cmp-string (car b))))
  8979. (and as bs (string< as bs))))
  8980. (defun org-table-get-stored-formulas (&optional noerror)
  8981. "Return an alist with the stored formulas directly after current table."
  8982. (interactive)
  8983. (let (scol eq eq-alist strings string seen)
  8984. (save-excursion
  8985. (goto-char (org-table-end))
  8986. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8987. (setq strings (org-split-string (match-string 2) " *:: *"))
  8988. (while (setq string (pop strings))
  8989. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8990. (setq scol (if (match-end 2)
  8991. (match-string 2 string)
  8992. (match-string 1 string))
  8993. eq (match-string 3 string)
  8994. eq-alist (cons (cons scol eq) eq-alist))
  8995. (if (member scol seen)
  8996. (if noerror
  8997. (progn
  8998. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8999. (ding)
  9000. (sit-for 2))
  9001. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9002. (push scol seen))))))
  9003. (nreverse eq-alist)))
  9004. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9005. "Modify the equations after the table structure has been edited.
  9006. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9007. For all numbers larger than LIMIT, shift them by DELTA."
  9008. (save-excursion
  9009. (goto-char (org-table-end))
  9010. (when (looking-at "#\\+TBLFM:")
  9011. (let ((re (concat key "\\([0-9]+\\)"))
  9012. (re2
  9013. (when remove
  9014. (if (equal key "$")
  9015. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9016. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9017. s n a)
  9018. (when remove
  9019. (while (re-search-forward re2 (point-at-eol) t)
  9020. (replace-match "")))
  9021. (while (re-search-forward re (point-at-eol) t)
  9022. (setq s (match-string 1) n (string-to-number s))
  9023. (cond
  9024. ((setq a (assoc s replace))
  9025. (replace-match (concat key (cdr a)) t t))
  9026. ((and limit (> n limit))
  9027. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9028. (defun org-table-get-specials ()
  9029. "Get the column names and local parameters for this table."
  9030. (save-excursion
  9031. (let ((beg (org-table-begin)) (end (org-table-end))
  9032. names name fields fields1 field cnt
  9033. c v l line col types dlines hlines)
  9034. (setq org-table-column-names nil
  9035. org-table-local-parameters nil
  9036. org-table-named-field-locations nil
  9037. org-table-current-begin-line nil
  9038. org-table-current-begin-pos nil
  9039. org-table-current-line-types nil)
  9040. (goto-char beg)
  9041. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9042. (setq names (org-split-string (match-string 1) " *| *")
  9043. cnt 1)
  9044. (while (setq name (pop names))
  9045. (setq cnt (1+ cnt))
  9046. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9047. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9048. (setq org-table-column-names (nreverse org-table-column-names))
  9049. (setq org-table-column-name-regexp
  9050. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9051. (goto-char beg)
  9052. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9053. (setq fields (org-split-string (match-string 1) " *| *"))
  9054. (while (setq field (pop fields))
  9055. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9056. (push (cons (match-string 1 field) (match-string 2 field))
  9057. org-table-local-parameters))))
  9058. (goto-char beg)
  9059. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9060. (setq c (match-string 1)
  9061. fields (org-split-string (match-string 2) " *| *"))
  9062. (save-excursion
  9063. (beginning-of-line (if (equal c "_") 2 0))
  9064. (setq line (org-current-line) col 1)
  9065. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9066. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9067. (while (and fields1 (setq field (pop fields)))
  9068. (setq v (pop fields1) col (1+ col))
  9069. (when (and (stringp field) (stringp v)
  9070. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9071. (push (cons field v) org-table-local-parameters)
  9072. (push (list field line col) org-table-named-field-locations))))
  9073. ;; Analyse the line types
  9074. (goto-char beg)
  9075. (setq org-table-current-begin-line (org-current-line)
  9076. org-table-current-begin-pos (point)
  9077. l org-table-current-begin-line)
  9078. (while (looking-at "[ \t]*|\\(-\\)?")
  9079. (push (if (match-end 1) 'hline 'dline) types)
  9080. (if (match-end 1) (push l hlines) (push l dlines))
  9081. (beginning-of-line 2)
  9082. (setq l (1+ l)))
  9083. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9084. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9085. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9086. (defun org-table-maybe-eval-formula ()
  9087. "Check if the current field starts with \"=\" or \":=\".
  9088. If yes, store the formula and apply it."
  9089. ;; We already know we are in a table. Get field will only return a formula
  9090. ;; when appropriate. It might return a separator line, but no problem.
  9091. (when org-table-formula-evaluate-inline
  9092. (let* ((field (org-trim (or (org-table-get-field) "")))
  9093. named eq)
  9094. (when (string-match "^:?=\\(.*\\)" field)
  9095. (setq named (equal (string-to-char field) ?:)
  9096. eq (match-string 1 field))
  9097. (if (or (fboundp 'calc-eval)
  9098. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9099. (org-table-eval-formula (if named '(4) nil)
  9100. (org-table-formula-from-user eq))
  9101. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9102. (defvar org-recalc-commands nil
  9103. "List of commands triggering the recalculation of a line.
  9104. Will be filled automatically during use.")
  9105. (defvar org-recalc-marks
  9106. '((" " . "Unmarked: no special line, no automatic recalculation")
  9107. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9108. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9109. ("!" . "Column name definition line. Reference in formula as $name.")
  9110. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9111. ("_" . "Names for values in row below this one.")
  9112. ("^" . "Names for values in row above this one.")))
  9113. (defun org-table-rotate-recalc-marks (&optional newchar)
  9114. "Rotate the recalculation mark in the first column.
  9115. If in any row, the first field is not consistent with a mark,
  9116. insert a new column for the markers.
  9117. When there is an active region, change all the lines in the region,
  9118. after prompting for the marking character.
  9119. After each change, a message will be displayed indicating the meaning
  9120. of the new mark."
  9121. (interactive)
  9122. (unless (org-at-table-p) (error "Not at a table"))
  9123. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9124. (beg (org-table-begin))
  9125. (end (org-table-end))
  9126. (l (org-current-line))
  9127. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9128. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9129. (have-col
  9130. (save-excursion
  9131. (goto-char beg)
  9132. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9133. (col (org-table-current-column))
  9134. (forcenew (car (assoc newchar org-recalc-marks)))
  9135. epos new)
  9136. (when l1
  9137. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9138. (setq newchar (char-to-string (read-char-exclusive))
  9139. forcenew (car (assoc newchar org-recalc-marks))))
  9140. (if (and newchar (not forcenew))
  9141. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9142. newchar))
  9143. (if l1 (goto-line l1))
  9144. (save-excursion
  9145. (beginning-of-line 1)
  9146. (unless (looking-at org-table-dataline-regexp)
  9147. (error "Not at a table data line")))
  9148. (unless have-col
  9149. (org-table-goto-column 1)
  9150. (org-table-insert-column)
  9151. (org-table-goto-column (1+ col)))
  9152. (setq epos (point-at-eol))
  9153. (save-excursion
  9154. (beginning-of-line 1)
  9155. (org-table-get-field
  9156. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9157. (concat " "
  9158. (setq new (or forcenew
  9159. (cadr (member (match-string 1) marks))))
  9160. " ")
  9161. " # ")))
  9162. (if (and l1 l2)
  9163. (progn
  9164. (goto-line l1)
  9165. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9166. (and (looking-at org-table-dataline-regexp)
  9167. (org-table-get-field 1 (concat " " new " "))))
  9168. (goto-line l1)))
  9169. (if (not (= epos (point-at-eol))) (org-table-align))
  9170. (goto-line l)
  9171. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9172. (defun org-table-maybe-recalculate-line ()
  9173. "Recompute the current line if marked for it, and if we haven't just done it."
  9174. (interactive)
  9175. (and org-table-allow-automatic-line-recalculation
  9176. (not (and (memq last-command org-recalc-commands)
  9177. (equal org-last-recalc-line (org-current-line))))
  9178. (save-excursion (beginning-of-line 1)
  9179. (looking-at org-table-auto-recalculate-regexp))
  9180. (org-table-recalculate) t))
  9181. (defvar org-table-formula-debug nil
  9182. "Non-nil means, debug table formulas.
  9183. When nil, simply write \"#ERROR\" in corrupted fields.")
  9184. (make-variable-buffer-local 'org-table-formula-debug)
  9185. (defvar modes)
  9186. (defsubst org-set-calc-mode (var &optional value)
  9187. (if (stringp var)
  9188. (setq var (assoc var '(("D" calc-angle-mode deg)
  9189. ("R" calc-angle-mode rad)
  9190. ("F" calc-prefer-frac t)
  9191. ("S" calc-symbolic-mode t)))
  9192. value (nth 2 var) var (nth 1 var)))
  9193. (if (memq var modes)
  9194. (setcar (cdr (memq var modes)) value)
  9195. (cons var (cons value modes)))
  9196. modes)
  9197. (defun org-table-eval-formula (&optional arg equation
  9198. suppress-align suppress-const
  9199. suppress-store suppress-analysis)
  9200. "Replace the table field value at the cursor by the result of a calculation.
  9201. This function makes use of Dave Gillespie's Calc package, in my view the
  9202. most exciting program ever written for GNU Emacs. So you need to have Calc
  9203. installed in order to use this function.
  9204. In a table, this command replaces the value in the current field with the
  9205. result of a formula. It also installs the formula as the \"current\" column
  9206. formula, by storing it in a special line below the table. When called
  9207. with a `C-u' prefix, the current field must ba a named field, and the
  9208. formula is installed as valid in only this specific field.
  9209. When called with two `C-u' prefixes, insert the active equation
  9210. for the field back into the current field, so that it can be
  9211. edited there. This is useful in order to use \\[org-table-show-reference]
  9212. to check the referenced fields.
  9213. When called, the command first prompts for a formula, which is read in
  9214. the minibuffer. Previously entered formulas are available through the
  9215. history list, and the last used formula is offered as a default.
  9216. These stored formulas are adapted correctly when moving, inserting, or
  9217. deleting columns with the corresponding commands.
  9218. The formula can be any algebraic expression understood by the Calc package.
  9219. For details, see the Org-mode manual.
  9220. This function can also be called from Lisp programs and offers
  9221. additional arguments: EQUATION can be the formula to apply. If this
  9222. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9223. used to speed-up recursive calls by by-passing unnecessary aligns.
  9224. SUPPRESS-CONST suppresses the interpretation of constants in the
  9225. formula, assuming that this has been done already outside the function.
  9226. SUPPRESS-STORE means the formula should not be stored, either because
  9227. it is already stored, or because it is a modified equation that should
  9228. not overwrite the stored one."
  9229. (interactive "P")
  9230. (org-table-check-inside-data-field)
  9231. (or suppress-analysis (org-table-get-specials))
  9232. (if (equal arg '(16))
  9233. (let ((eq (org-table-current-field-formula)))
  9234. (or eq (error "No equation active for current field"))
  9235. (org-table-get-field nil eq)
  9236. (org-table-align)
  9237. (setq org-table-may-need-update t))
  9238. (let* (fields
  9239. (ndown (if (integerp arg) arg 1))
  9240. (org-table-automatic-realign nil)
  9241. (case-fold-search nil)
  9242. (down (> ndown 1))
  9243. (formula (if (and equation suppress-store)
  9244. equation
  9245. (org-table-get-formula equation (equal arg '(4)))))
  9246. (n0 (org-table-current-column))
  9247. (modes (copy-sequence org-calc-default-modes))
  9248. (numbers nil) ; was a variable, now fixed default
  9249. (keep-empty nil)
  9250. n form form0 bw fmt x ev orig c lispp literal)
  9251. ;; Parse the format string. Since we have a lot of modes, this is
  9252. ;; a lot of work. However, I think calc still uses most of the time.
  9253. (if (string-match ";" formula)
  9254. (let ((tmp (org-split-string formula ";")))
  9255. (setq formula (car tmp)
  9256. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9257. (nth 1 tmp)))
  9258. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9259. (setq c (string-to-char (match-string 1 fmt))
  9260. n (string-to-number (match-string 2 fmt)))
  9261. (if (= c ?p)
  9262. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9263. (setq modes (org-set-calc-mode
  9264. 'calc-float-format
  9265. (list (cdr (assoc c '((?n . float) (?f . fix)
  9266. (?s . sci) (?e . eng))))
  9267. n))))
  9268. (setq fmt (replace-match "" t t fmt)))
  9269. (if (string-match "[NT]" fmt)
  9270. (setq numbers (equal (match-string 0 fmt) "N")
  9271. fmt (replace-match "" t t fmt)))
  9272. (if (string-match "L" fmt)
  9273. (setq literal t
  9274. fmt (replace-match "" t t fmt)))
  9275. (if (string-match "E" fmt)
  9276. (setq keep-empty t
  9277. fmt (replace-match "" t t fmt)))
  9278. (while (string-match "[DRFS]" fmt)
  9279. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9280. (setq fmt (replace-match "" t t fmt)))
  9281. (unless (string-match "\\S-" fmt)
  9282. (setq fmt nil))))
  9283. (if (and (not suppress-const) org-table-formula-use-constants)
  9284. (setq formula (org-table-formula-substitute-names formula)))
  9285. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9286. (while (> ndown 0)
  9287. (setq fields (org-split-string
  9288. (org-no-properties
  9289. (buffer-substring (point-at-bol) (point-at-eol)))
  9290. " *| *"))
  9291. (if (eq numbers t)
  9292. (setq fields (mapcar
  9293. (lambda (x) (number-to-string (string-to-number x)))
  9294. fields)))
  9295. (setq ndown (1- ndown))
  9296. (setq form (copy-sequence formula)
  9297. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9298. (if (and lispp literal) (setq lispp 'literal))
  9299. ;; Check for old vertical references
  9300. (setq form (org-rewrite-old-row-references form))
  9301. ;; Insert complex ranges
  9302. (while (string-match org-table-range-regexp form)
  9303. (setq form
  9304. (replace-match
  9305. (save-match-data
  9306. (org-table-make-reference
  9307. (org-table-get-range (match-string 0 form) nil n0)
  9308. keep-empty numbers lispp))
  9309. t t form)))
  9310. ;; Insert simple ranges
  9311. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9312. (setq form
  9313. (replace-match
  9314. (save-match-data
  9315. (org-table-make-reference
  9316. (org-sublist
  9317. fields (string-to-number (match-string 1 form))
  9318. (string-to-number (match-string 2 form)))
  9319. keep-empty numbers lispp))
  9320. t t form)))
  9321. (setq form0 form)
  9322. ;; Insert the references to fields in same row
  9323. (while (string-match "\\$\\([0-9]+\\)" form)
  9324. (setq n (string-to-number (match-string 1 form))
  9325. x (nth (1- (if (= n 0) n0 n)) fields))
  9326. (unless x (error "Invalid field specifier \"%s\""
  9327. (match-string 0 form)))
  9328. (setq form (replace-match
  9329. (save-match-data
  9330. (org-table-make-reference x nil numbers lispp))
  9331. t t form)))
  9332. (if lispp
  9333. (setq ev (condition-case nil
  9334. (eval (eval (read form)))
  9335. (error "#ERROR"))
  9336. ev (if (numberp ev) (number-to-string ev) ev))
  9337. (or (fboundp 'calc-eval)
  9338. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9339. (setq ev (calc-eval (cons form modes)
  9340. (if numbers 'num))))
  9341. (when org-table-formula-debug
  9342. (with-output-to-temp-buffer "*Substitution History*"
  9343. (princ (format "Substitution history of formula
  9344. Orig: %s
  9345. $xyz-> %s
  9346. @r$c-> %s
  9347. $1-> %s\n" orig formula form0 form))
  9348. (if (listp ev)
  9349. (princ (format " %s^\nError: %s"
  9350. (make-string (car ev) ?\-) (nth 1 ev)))
  9351. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9352. ev (or fmt "NONE")
  9353. (if fmt (format fmt (string-to-number ev)) ev)))))
  9354. (setq bw (get-buffer-window "*Substitution History*"))
  9355. (shrink-window-if-larger-than-buffer bw)
  9356. (unless (and (interactive-p) (not ndown))
  9357. (unless (let (inhibit-redisplay)
  9358. (y-or-n-p "Debugging Formula. Continue to next? "))
  9359. (org-table-align)
  9360. (error "Abort"))
  9361. (delete-window bw)
  9362. (message "")))
  9363. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9364. (org-table-justify-field-maybe
  9365. (if fmt (format fmt (string-to-number ev)) ev))
  9366. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9367. (call-interactively 'org-return)
  9368. (setq ndown 0)))
  9369. (and down (org-table-maybe-recalculate-line))
  9370. (or suppress-align (and org-table-may-need-update
  9371. (org-table-align))))))
  9372. (defun org-table-put-field-property (prop value)
  9373. (save-excursion
  9374. (put-text-property (progn (skip-chars-backward "^|") (point))
  9375. (progn (skip-chars-forward "^|") (point))
  9376. prop value)))
  9377. (defun org-table-get-range (desc &optional tbeg col highlight)
  9378. "Get a calc vector from a column, accorting to descriptor DESC.
  9379. Optional arguments TBEG and COL can give the beginning of the table and
  9380. the current column, to avoid unnecessary parsing.
  9381. HIGHLIGHT means, just highlight the range."
  9382. (if (not (equal (string-to-char desc) ?@))
  9383. (setq desc (concat "@" desc)))
  9384. (save-excursion
  9385. (or tbeg (setq tbeg (org-table-begin)))
  9386. (or col (setq col (org-table-current-column)))
  9387. (let ((thisline (org-current-line))
  9388. beg end c1 c2 r1 r2 rangep tmp)
  9389. (unless (string-match org-table-range-regexp desc)
  9390. (error "Invalid table range specifier `%s'" desc))
  9391. (setq rangep (match-end 3)
  9392. r1 (and (match-end 1) (match-string 1 desc))
  9393. r2 (and (match-end 4) (match-string 4 desc))
  9394. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9395. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9396. (and c1 (setq c1 (+ (string-to-number c1)
  9397. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9398. (and c2 (setq c2 (+ (string-to-number c2)
  9399. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9400. (if (equal r1 "") (setq r1 nil))
  9401. (if (equal r2 "") (setq r2 nil))
  9402. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9403. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9404. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9405. (if (not r1) (setq r1 thisline))
  9406. (if (not r2) (setq r2 thisline))
  9407. (if (not c1) (setq c1 col))
  9408. (if (not c2) (setq c2 col))
  9409. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9410. ;; just one field
  9411. (progn
  9412. (goto-line r1)
  9413. (while (not (looking-at org-table-dataline-regexp))
  9414. (beginning-of-line 2))
  9415. (prog1 (org-trim (org-table-get-field c1))
  9416. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9417. ;; A range, return a vector
  9418. ;; First sort the numbers to get a regular ractangle
  9419. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9420. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9421. (goto-line r1)
  9422. (while (not (looking-at org-table-dataline-regexp))
  9423. (beginning-of-line 2))
  9424. (org-table-goto-column c1)
  9425. (setq beg (point))
  9426. (goto-line r2)
  9427. (while (not (looking-at org-table-dataline-regexp))
  9428. (beginning-of-line 0))
  9429. (org-table-goto-column c2)
  9430. (setq end (point))
  9431. (if highlight
  9432. (org-table-highlight-rectangle
  9433. beg (progn (skip-chars-forward "^|\n") (point))))
  9434. ;; return string representation of calc vector
  9435. (mapcar 'org-trim
  9436. (apply 'append (org-table-copy-region beg end)))))))
  9437. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9438. "Analyze descriptor DESC and retrieve the corresponding line number.
  9439. The cursor is currently in line CLINE, the table begins in line BLINE,
  9440. and TABLE is a vector with line types."
  9441. (if (string-match "^[0-9]+$" desc)
  9442. (aref org-table-dlines (string-to-number desc))
  9443. (setq cline (or cline (org-current-line))
  9444. bline (or bline org-table-current-begin-line)
  9445. table (or table org-table-current-line-types))
  9446. (if (or
  9447. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9448. ;; 1 2 3 4 5 6
  9449. (and (not (match-end 3)) (not (match-end 6)))
  9450. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9451. (error "invalid row descriptor `%s'" desc))
  9452. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9453. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9454. (odir (and (match-end 5) (match-string 5 desc)))
  9455. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9456. (i (- cline bline))
  9457. (rel (and (match-end 6)
  9458. (or (and (match-end 1) (not (match-end 3)))
  9459. (match-end 5)))))
  9460. (if (and hn (not hdir))
  9461. (progn
  9462. (setq i 0 hdir "+")
  9463. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9464. (if (and (not hn) on (not odir))
  9465. (error "should never happen");;(aref org-table-dlines on)
  9466. (if (and hn (> hn 0))
  9467. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9468. (if on
  9469. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9470. (+ bline i)))))
  9471. (defun org-find-row-type (table i type backwards relative n)
  9472. (let ((l (length table)))
  9473. (while (> n 0)
  9474. (while (and (setq i (+ i (if backwards -1 1)))
  9475. (>= i 0) (< i l)
  9476. (not (eq (aref table i) type))
  9477. (if (and relative (eq (aref table i) 'hline))
  9478. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9479. t)))
  9480. (setq n (1- n)))
  9481. (if (or (< i 0) (>= i l))
  9482. (error "Row descriptior leads outside table")
  9483. i)))
  9484. (defun org-rewrite-old-row-references (s)
  9485. (if (string-match "&[-+0-9I]" s)
  9486. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9487. s))
  9488. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9489. "Convert list ELEMENTS to something appropriate to insert into formula.
  9490. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9491. NUMBERS indicates that everything should be converted to numbers.
  9492. LISPP means to return something appropriate for a Lisp list."
  9493. (if (stringp elements) ; just a single val
  9494. (if lispp
  9495. (if (eq lispp 'literal)
  9496. elements
  9497. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9498. (if (equal elements "") (setq elements "0"))
  9499. (if numbers (number-to-string (string-to-number elements)) elements))
  9500. (unless keep-empty
  9501. (setq elements
  9502. (delq nil
  9503. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9504. elements))))
  9505. (setq elements (or elements '("0")))
  9506. (if lispp
  9507. (mapconcat
  9508. (lambda (x)
  9509. (if (eq lispp 'literal)
  9510. x
  9511. (prin1-to-string (if numbers (string-to-number x) x))))
  9512. elements " ")
  9513. (concat "[" (mapconcat
  9514. (lambda (x)
  9515. (if numbers (number-to-string (string-to-number x)) x))
  9516. elements
  9517. ",") "]"))))
  9518. (defun org-table-recalculate (&optional all noalign)
  9519. "Recalculate the current table line by applying all stored formulas.
  9520. With prefix arg ALL, do this for all lines in the table."
  9521. (interactive "P")
  9522. (or (memq this-command org-recalc-commands)
  9523. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9524. (unless (org-at-table-p) (error "Not at a table"))
  9525. (if (equal all '(16))
  9526. (org-table-iterate)
  9527. (org-table-get-specials)
  9528. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9529. (lambda (a b) (string< (car a) (car b)))))
  9530. (inhibit-redisplay (not debug-on-error))
  9531. (line-re org-table-dataline-regexp)
  9532. (thisline (org-current-line))
  9533. (thiscol (org-table-current-column))
  9534. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9535. ;; Insert constants in all formulas
  9536. (setq eqlist
  9537. (mapcar (lambda (x)
  9538. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9539. x)
  9540. eqlist))
  9541. ;; Split the equation list
  9542. (while (setq eq (pop eqlist))
  9543. (if (<= (string-to-char (car eq)) ?9)
  9544. (push eq eqlnum)
  9545. (push eq eqlname)))
  9546. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9547. (if all
  9548. (progn
  9549. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9550. (goto-char (setq beg (org-table-begin)))
  9551. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9552. ;; This is a table with marked lines, compute selected lines
  9553. (setq line-re org-table-recalculate-regexp)
  9554. ;; Move forward to the first non-header line
  9555. (if (and (re-search-forward org-table-dataline-regexp end t)
  9556. (re-search-forward org-table-hline-regexp end t)
  9557. (re-search-forward org-table-dataline-regexp end t))
  9558. (setq beg (match-beginning 0))
  9559. nil))) ;; just leave beg where it is
  9560. (setq beg (point-at-bol)
  9561. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9562. (goto-char beg)
  9563. (and all (message "Re-applying formulas to full table..."))
  9564. ;; First find the named fields, and mark them untouchanble
  9565. (remove-text-properties beg end '(org-untouchable t))
  9566. (while (setq eq (pop eqlname))
  9567. (setq name (car eq)
  9568. a (assoc name org-table-named-field-locations))
  9569. (and (not a)
  9570. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9571. (setq a (list name
  9572. (aref org-table-dlines
  9573. (string-to-number (match-string 1 name)))
  9574. (string-to-number (match-string 2 name)))))
  9575. (when (and a (or all (equal (nth 1 a) thisline)))
  9576. (message "Re-applying formula to field: %s" name)
  9577. (goto-line (nth 1 a))
  9578. (org-table-goto-column (nth 2 a))
  9579. (push (append a (list (cdr eq))) eqlname1)
  9580. (org-table-put-field-property :org-untouchable t)))
  9581. ;; Now evauluate the column formulas, but skip fields covered by
  9582. ;; field formulas
  9583. (goto-char beg)
  9584. (while (re-search-forward line-re end t)
  9585. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9586. ;; Unprotected line, recalculate
  9587. (and all (message "Re-applying formulas to full table...(line %d)"
  9588. (setq cnt (1+ cnt))))
  9589. (setq org-last-recalc-line (org-current-line))
  9590. (setq eql eqlnum)
  9591. (while (setq entry (pop eql))
  9592. (goto-line org-last-recalc-line)
  9593. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9594. (unless (get-text-property (point) :org-untouchable)
  9595. (org-table-eval-formula nil (cdr entry)
  9596. 'noalign 'nocst 'nostore 'noanalysis)))))
  9597. ;; Now evaluate the field formulas
  9598. (while (setq eq (pop eqlname1))
  9599. (message "Re-applying formula to field: %s" (car eq))
  9600. (goto-line (nth 1 eq))
  9601. (org-table-goto-column (nth 2 eq))
  9602. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9603. 'nostore 'noanalysis))
  9604. (goto-line thisline)
  9605. (org-table-goto-column thiscol)
  9606. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9607. (or noalign (and org-table-may-need-update (org-table-align))
  9608. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9609. ;; back to initial position
  9610. (message "Re-applying formulas...done")
  9611. (goto-line thisline)
  9612. (org-table-goto-column thiscol)
  9613. (or noalign (and org-table-may-need-update (org-table-align))
  9614. (and all (message "Re-applying formulas...done"))))))
  9615. (defun org-table-iterate (&optional arg)
  9616. "Recalculate the table until it does not change anymore."
  9617. (interactive "P")
  9618. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9619. (i 0)
  9620. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9621. thistbl)
  9622. (catch 'exit
  9623. (while (< i imax)
  9624. (setq i (1+ i))
  9625. (org-table-recalculate 'all)
  9626. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9627. (if (not (string= lasttbl thistbl))
  9628. (setq lasttbl thistbl)
  9629. (if (> i 1)
  9630. (message "Convergence after %d iterations" i)
  9631. (message "Table was already stable"))
  9632. (throw 'exit t)))
  9633. (error "No convergence after %d iterations" i))))
  9634. (defun org-table-formula-substitute-names (f)
  9635. "Replace $const with values in string F."
  9636. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9637. ;; First, check for column names
  9638. (while (setq start (string-match org-table-column-name-regexp f start))
  9639. (setq start (1+ start))
  9640. (setq a (assoc (match-string 1 f) org-table-column-names))
  9641. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9642. ;; Parameters and constants
  9643. (setq start 0)
  9644. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9645. (setq start (1+ start))
  9646. (if (setq a (save-match-data
  9647. (org-table-get-constant (match-string 1 f))))
  9648. (setq f (replace-match
  9649. (concat (if pp "(") a (if pp ")")) t t f))))
  9650. (if org-table-formula-debug
  9651. (put-text-property 0 (length f) :orig-formula f1 f))
  9652. f))
  9653. (defun org-table-get-constant (const)
  9654. "Find the value for a parameter or constant in a formula.
  9655. Parameters get priority."
  9656. (or (cdr (assoc const org-table-local-parameters))
  9657. (cdr (assoc const org-table-formula-constants-local))
  9658. (cdr (assoc const org-table-formula-constants))
  9659. (and (fboundp 'constants-get) (constants-get const))
  9660. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9661. (org-entry-get nil (substring const 5) 'inherit))
  9662. "#UNDEFINED_NAME"))
  9663. (defvar org-table-fedit-map
  9664. (let ((map (make-sparse-keymap)))
  9665. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9666. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9667. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9668. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9669. (org-defkey map "\C-c?" 'org-table-show-reference)
  9670. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9671. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9672. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9673. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9674. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9675. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9676. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9677. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9678. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9679. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9680. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9681. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9682. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9683. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9684. map))
  9685. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9686. '("Edit-Formulas"
  9687. ["Finish and Install" org-table-fedit-finish t]
  9688. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9689. ["Abort" org-table-fedit-abort t]
  9690. "--"
  9691. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9692. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9693. "--"
  9694. "Shift Reference at Point"
  9695. ["Up" org-table-fedit-ref-up t]
  9696. ["Down" org-table-fedit-ref-down t]
  9697. ["Left" org-table-fedit-ref-left t]
  9698. ["Right" org-table-fedit-ref-right t]
  9699. "-"
  9700. "Change Test Row for Column Formulas"
  9701. ["Up" org-table-fedit-line-up t]
  9702. ["Down" org-table-fedit-line-down t]
  9703. "--"
  9704. ["Scroll Table Window" org-table-fedit-scroll t]
  9705. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9706. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9707. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9708. org-table-overlay-coordinates)]
  9709. "--"
  9710. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9711. :style toggle :selected org-table-buffer-is-an]))
  9712. (defvar org-pos)
  9713. (defun org-table-edit-formulas ()
  9714. "Edit the formulas of the current table in a separate buffer."
  9715. (interactive)
  9716. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9717. (beginning-of-line 0))
  9718. (unless (org-at-table-p) (error "Not at a table"))
  9719. (org-table-get-specials)
  9720. (let ((key (org-table-current-field-formula 'key 'noerror))
  9721. (eql (sort (org-table-get-stored-formulas 'noerror)
  9722. 'org-table-formula-less-p))
  9723. (pos (move-marker (make-marker) (point)))
  9724. (startline 1)
  9725. (wc (current-window-configuration))
  9726. (titles '((column . "# Column Formulas\n")
  9727. (field . "# Field Formulas\n")
  9728. (named . "# Named Field Formulas\n")))
  9729. entry s type title)
  9730. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9731. (erase-buffer)
  9732. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9733. (let ((font-lock-global-modes '(not fundamental-mode)))
  9734. (fundamental-mode))
  9735. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9736. (org-set-local 'org-pos pos)
  9737. (org-set-local 'org-window-configuration wc)
  9738. (use-local-map org-table-fedit-map)
  9739. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9740. (easy-menu-add org-table-fedit-menu)
  9741. (setq startline (org-current-line))
  9742. (while (setq entry (pop eql))
  9743. (setq type (cond
  9744. ((equal (string-to-char (car entry)) ?@) 'field)
  9745. ((string-match "^[0-9]" (car entry)) 'column)
  9746. (t 'named)))
  9747. (when (setq title (assq type titles))
  9748. (or (bobp) (insert "\n"))
  9749. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9750. (setq titles (delq title titles)))
  9751. (if (equal key (car entry)) (setq startline (org-current-line)))
  9752. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9753. (car entry) " = " (cdr entry) "\n"))
  9754. (remove-text-properties 0 (length s) '(face nil) s)
  9755. (insert s))
  9756. (if (eq org-table-use-standard-references t)
  9757. (org-table-fedit-toggle-ref-type))
  9758. (goto-line startline)
  9759. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9760. (defun org-table-fedit-post-command ()
  9761. (when (not (memq this-command '(lisp-complete-symbol)))
  9762. (let ((win (selected-window)))
  9763. (save-excursion
  9764. (condition-case nil
  9765. (org-table-show-reference)
  9766. (error nil))
  9767. (select-window win)))))
  9768. (defun org-table-formula-to-user (s)
  9769. "Convert a formula from internal to user representation."
  9770. (if (eq org-table-use-standard-references t)
  9771. (org-table-convert-refs-to-an s)
  9772. s))
  9773. (defun org-table-formula-from-user (s)
  9774. "Convert a formula from user to internal representation."
  9775. (if org-table-use-standard-references
  9776. (org-table-convert-refs-to-rc s)
  9777. s))
  9778. (defun org-table-convert-refs-to-rc (s)
  9779. "Convert spreadsheet references from AB7 to @7$28.
  9780. Works for single references, but also for entire formulas and even the
  9781. full TBLFM line."
  9782. (let ((start 0))
  9783. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9784. (cond
  9785. ((match-end 3)
  9786. ;; format match, just advance
  9787. (setq start (match-end 0)))
  9788. ((and (> (match-beginning 0) 0)
  9789. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9790. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9791. ;; 3.e5 or something like this.
  9792. (setq start (match-end 0)))
  9793. (t
  9794. (setq start (match-beginning 0)
  9795. s (replace-match
  9796. (if (equal (match-string 2 s) "&")
  9797. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9798. (format "@%d$%d"
  9799. (string-to-number (match-string 2 s))
  9800. (org-letters-to-number (match-string 1 s))))
  9801. t t s)))))
  9802. s))
  9803. (defun org-table-convert-refs-to-an (s)
  9804. "Convert spreadsheet references from to @7$28 to AB7.
  9805. Works for single references, but also for entire formulas and even the
  9806. full TBLFM line."
  9807. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9808. (setq s (replace-match
  9809. (format "%s%d"
  9810. (org-number-to-letters
  9811. (string-to-number (match-string 2 s)))
  9812. (string-to-number (match-string 1 s)))
  9813. t t s)))
  9814. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9815. (setq s (replace-match (concat "\\1"
  9816. (org-number-to-letters
  9817. (string-to-number (match-string 2 s))) "&")
  9818. t nil s)))
  9819. s)
  9820. (defun org-letters-to-number (s)
  9821. "Convert a base 26 number represented by letters into an integer.
  9822. For example: AB -> 28."
  9823. (let ((n 0))
  9824. (setq s (upcase s))
  9825. (while (> (length s) 0)
  9826. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9827. s (substring s 1)))
  9828. n))
  9829. (defun org-number-to-letters (n)
  9830. "Convert an integer into a base 26 number represented by letters.
  9831. For example: 28 -> AB."
  9832. (let ((s ""))
  9833. (while (> n 0)
  9834. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9835. n (/ (1- n) 26)))
  9836. s))
  9837. (defun org-table-fedit-convert-buffer (function)
  9838. "Convert all references in this buffer, using FUNTION."
  9839. (let ((line (org-current-line)))
  9840. (goto-char (point-min))
  9841. (while (not (eobp))
  9842. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9843. (delete-region (point) (point-at-eol))
  9844. (or (eobp) (forward-char 1)))
  9845. (goto-line line)))
  9846. (defun org-table-fedit-toggle-ref-type ()
  9847. "Convert all references in the buffer from B3 to @3$2 and back."
  9848. (interactive)
  9849. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9850. (org-table-fedit-convert-buffer
  9851. (if org-table-buffer-is-an
  9852. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9853. (message "Reference type switched to %s"
  9854. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9855. (defun org-table-fedit-ref-up ()
  9856. "Shift the reference at point one row/hline up."
  9857. (interactive)
  9858. (org-table-fedit-shift-reference 'up))
  9859. (defun org-table-fedit-ref-down ()
  9860. "Shift the reference at point one row/hline down."
  9861. (interactive)
  9862. (org-table-fedit-shift-reference 'down))
  9863. (defun org-table-fedit-ref-left ()
  9864. "Shift the reference at point one field to the left."
  9865. (interactive)
  9866. (org-table-fedit-shift-reference 'left))
  9867. (defun org-table-fedit-ref-right ()
  9868. "Shift the reference at point one field to the right."
  9869. (interactive)
  9870. (org-table-fedit-shift-reference 'right))
  9871. (defun org-table-fedit-shift-reference (dir)
  9872. (cond
  9873. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9874. (if (memq dir '(left right))
  9875. (org-rematch-and-replace 1 (eq dir 'left))
  9876. (error "Cannot shift reference in this direction")))
  9877. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9878. ;; A B3-like reference
  9879. (if (memq dir '(up down))
  9880. (org-rematch-and-replace 2 (eq dir 'up))
  9881. (org-rematch-and-replace 1 (eq dir 'left))))
  9882. ((org-at-regexp-p
  9883. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9884. ;; An internal reference
  9885. (if (memq dir '(up down))
  9886. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9887. (org-rematch-and-replace 5 (eq dir 'left))))))
  9888. (defun org-rematch-and-replace (n &optional decr hline)
  9889. "Re-match the group N, and replace it with the shifted refrence."
  9890. (or (match-end n) (error "Cannot shift reference in this direction"))
  9891. (goto-char (match-beginning n))
  9892. (and (looking-at (regexp-quote (match-string n)))
  9893. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9894. t t)))
  9895. (defun org-shift-refpart (ref &optional decr hline)
  9896. "Shift a refrence part REF.
  9897. If DECR is set, decrease the references row/column, else increase.
  9898. If HLINE is set, this may be a hline reference, it certainly is not
  9899. a translation reference."
  9900. (save-match-data
  9901. (let* ((sign (string-match "^[-+]" ref)) n)
  9902. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9903. (cond
  9904. ((and hline (string-match "^I+" ref))
  9905. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9906. (setq n (+ n (if decr -1 1)))
  9907. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9908. (if sign
  9909. (setq sign (if (< n 0) "-" "+") n (abs n))
  9910. (setq n (max 1 n)))
  9911. (concat sign (make-string n ?I)))
  9912. ((string-match "^[0-9]+" ref)
  9913. (setq n (string-to-number (concat sign ref)))
  9914. (setq n (+ n (if decr -1 1)))
  9915. (if sign
  9916. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9917. (number-to-string (max 1 n))))
  9918. ((string-match "^[a-zA-Z]+" ref)
  9919. (org-number-to-letters
  9920. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9921. (t (error "Cannot shift reference"))))))
  9922. (defun org-table-fedit-toggle-coordinates ()
  9923. "Toggle the display of coordinates in the refrenced table."
  9924. (interactive)
  9925. (let ((pos (marker-position org-pos)))
  9926. (with-current-buffer (marker-buffer org-pos)
  9927. (save-excursion
  9928. (goto-char pos)
  9929. (org-table-toggle-coordinate-overlays)))))
  9930. (defun org-table-fedit-finish (&optional arg)
  9931. "Parse the buffer for formula definitions and install them.
  9932. With prefix ARG, apply the new formulas to the table."
  9933. (interactive "P")
  9934. (org-table-remove-rectangle-highlight)
  9935. (if org-table-use-standard-references
  9936. (progn
  9937. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9938. (setq org-table-buffer-is-an nil)))
  9939. (let ((pos org-pos) eql var form)
  9940. (goto-char (point-min))
  9941. (while (re-search-forward
  9942. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9943. nil t)
  9944. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9945. form (match-string 3))
  9946. (setq form (org-trim form))
  9947. (when (not (equal form ""))
  9948. (while (string-match "[ \t]*\n[ \t]*" form)
  9949. (setq form (replace-match " " t t form)))
  9950. (when (assoc var eql)
  9951. (error "Double formulas for %s" var))
  9952. (push (cons var form) eql)))
  9953. (setq org-pos nil)
  9954. (set-window-configuration org-window-configuration)
  9955. (select-window (get-buffer-window (marker-buffer pos)))
  9956. (goto-char pos)
  9957. (unless (org-at-table-p)
  9958. (error "Lost table position - cannot install formulae"))
  9959. (org-table-store-formulas eql)
  9960. (move-marker pos nil)
  9961. (kill-buffer "*Edit Formulas*")
  9962. (if arg
  9963. (org-table-recalculate 'all)
  9964. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9965. (defun org-table-fedit-abort ()
  9966. "Abort editing formulas, without installing the changes."
  9967. (interactive)
  9968. (org-table-remove-rectangle-highlight)
  9969. (let ((pos org-pos))
  9970. (set-window-configuration org-window-configuration)
  9971. (select-window (get-buffer-window (marker-buffer pos)))
  9972. (goto-char pos)
  9973. (move-marker pos nil)
  9974. (message "Formula editing aborted without installing changes")))
  9975. (defun org-table-fedit-lisp-indent ()
  9976. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9977. (interactive)
  9978. (let ((pos (point)) beg end ind)
  9979. (beginning-of-line 1)
  9980. (cond
  9981. ((looking-at "[ \t]")
  9982. (goto-char pos)
  9983. (call-interactively 'lisp-indent-line))
  9984. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9985. ((not (fboundp 'pp-buffer))
  9986. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9987. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9988. (goto-char (- (match-end 0) 2))
  9989. (setq beg (point))
  9990. (setq ind (make-string (current-column) ?\ ))
  9991. (condition-case nil (forward-sexp 1)
  9992. (error
  9993. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9994. (setq end (point))
  9995. (save-restriction
  9996. (narrow-to-region beg end)
  9997. (if (eq last-command this-command)
  9998. (progn
  9999. (goto-char (point-min))
  10000. (setq this-command nil)
  10001. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10002. (replace-match " ")))
  10003. (pp-buffer)
  10004. (untabify (point-min) (point-max))
  10005. (goto-char (1+ (point-min)))
  10006. (while (re-search-forward "^." nil t)
  10007. (beginning-of-line 1)
  10008. (insert ind))
  10009. (goto-char (point-max))
  10010. (backward-delete-char 1)))
  10011. (goto-char beg))
  10012. (t nil))))
  10013. (defvar org-show-positions nil)
  10014. (defun org-table-show-reference (&optional local)
  10015. "Show the location/value of the $ expression at point."
  10016. (interactive)
  10017. (org-table-remove-rectangle-highlight)
  10018. (catch 'exit
  10019. (let ((pos (if local (point) org-pos))
  10020. (face2 'highlight)
  10021. (org-inhibit-highlight-removal t)
  10022. (win (selected-window))
  10023. (org-show-positions nil)
  10024. var name e what match dest)
  10025. (if local (org-table-get-specials))
  10026. (setq what (cond
  10027. ((or (org-at-regexp-p org-table-range-regexp2)
  10028. (org-at-regexp-p org-table-translate-regexp)
  10029. (org-at-regexp-p org-table-range-regexp))
  10030. (setq match
  10031. (save-match-data
  10032. (org-table-convert-refs-to-rc (match-string 0))))
  10033. 'range)
  10034. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10035. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10036. ((not local) nil)
  10037. (t (error "No reference at point")))
  10038. match (and what (or match (match-string 0))))
  10039. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10040. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10041. 'secondary-selection))
  10042. (org-add-hook 'before-change-functions
  10043. 'org-table-remove-rectangle-highlight)
  10044. (if (eq what 'name) (setq var (substring match 1)))
  10045. (when (eq what 'range)
  10046. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10047. (setq match (org-table-formula-substitute-names match)))
  10048. (unless local
  10049. (save-excursion
  10050. (end-of-line 1)
  10051. (re-search-backward "^\\S-" nil t)
  10052. (beginning-of-line 1)
  10053. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10054. (setq dest
  10055. (save-match-data
  10056. (org-table-convert-refs-to-rc (match-string 1))))
  10057. (org-table-add-rectangle-overlay
  10058. (match-beginning 1) (match-end 1) face2))))
  10059. (if (and (markerp pos) (marker-buffer pos))
  10060. (if (get-buffer-window (marker-buffer pos))
  10061. (select-window (get-buffer-window (marker-buffer pos)))
  10062. (org-switch-to-buffer-other-window (get-buffer-window
  10063. (marker-buffer pos)))))
  10064. (goto-char pos)
  10065. (org-table-force-dataline)
  10066. (when dest
  10067. (setq name (substring dest 1))
  10068. (cond
  10069. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10070. (setq e (assoc name org-table-named-field-locations))
  10071. (goto-line (nth 1 e))
  10072. (org-table-goto-column (nth 2 e)))
  10073. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10074. (let ((l (string-to-number (match-string 1 dest)))
  10075. (c (string-to-number (match-string 2 dest))))
  10076. (goto-line (aref org-table-dlines l))
  10077. (org-table-goto-column c)))
  10078. (t (org-table-goto-column (string-to-number name))))
  10079. (move-marker pos (point))
  10080. (org-table-highlight-rectangle nil nil face2))
  10081. (cond
  10082. ((equal dest match))
  10083. ((not match))
  10084. ((eq what 'range)
  10085. (condition-case nil
  10086. (save-excursion
  10087. (org-table-get-range match nil nil 'highlight))
  10088. (error nil)))
  10089. ((setq e (assoc var org-table-named-field-locations))
  10090. (goto-line (nth 1 e))
  10091. (org-table-goto-column (nth 2 e))
  10092. (org-table-highlight-rectangle (point) (point))
  10093. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10094. ((setq e (assoc var org-table-column-names))
  10095. (org-table-goto-column (string-to-number (cdr e)))
  10096. (org-table-highlight-rectangle (point) (point))
  10097. (goto-char (org-table-begin))
  10098. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10099. (org-table-end) t)
  10100. (progn
  10101. (goto-char (match-beginning 1))
  10102. (org-table-highlight-rectangle)
  10103. (message "Named column (column %s)" (cdr e)))
  10104. (error "Column name not found")))
  10105. ((eq what 'column)
  10106. ;; column number
  10107. (org-table-goto-column (string-to-number (substring match 1)))
  10108. (org-table-highlight-rectangle (point) (point))
  10109. (message "Column %s" (substring match 1)))
  10110. ((setq e (assoc var org-table-local-parameters))
  10111. (goto-char (org-table-begin))
  10112. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10113. (progn
  10114. (goto-char (match-beginning 1))
  10115. (org-table-highlight-rectangle)
  10116. (message "Local parameter."))
  10117. (error "Parameter not found")))
  10118. (t
  10119. (cond
  10120. ((not var) (error "No reference at point"))
  10121. ((setq e (assoc var org-table-formula-constants-local))
  10122. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10123. var (cdr e)))
  10124. ((setq e (assoc var org-table-formula-constants))
  10125. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10126. var (cdr e)))
  10127. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10128. (message "Constant: $%s=%s, from `constants.el'%s."
  10129. var e (format " (%s units)" constants-unit-system)))
  10130. (t (error "Undefined name $%s" var)))))
  10131. (goto-char pos)
  10132. (when (and org-show-positions
  10133. (not (memq this-command '(org-table-fedit-scroll
  10134. org-table-fedit-scroll-down))))
  10135. (push pos org-show-positions)
  10136. (push org-table-current-begin-pos org-show-positions)
  10137. (let ((min (apply 'min org-show-positions))
  10138. (max (apply 'max org-show-positions)))
  10139. (goto-char min) (recenter 0)
  10140. (goto-char max)
  10141. (or (pos-visible-in-window-p max) (recenter -1))))
  10142. (select-window win))))
  10143. (defun org-table-force-dataline ()
  10144. "Make sure the cursor is in a dataline in a table."
  10145. (unless (save-excursion
  10146. (beginning-of-line 1)
  10147. (looking-at org-table-dataline-regexp))
  10148. (let* ((re org-table-dataline-regexp)
  10149. (p1 (save-excursion (re-search-forward re nil 'move)))
  10150. (p2 (save-excursion (re-search-backward re nil 'move))))
  10151. (cond ((and p1 p2)
  10152. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10153. p1 p2)))
  10154. ((or p1 p2) (goto-char (or p1 p2)))
  10155. (t (error "No table dataline around here"))))))
  10156. (defun org-table-fedit-line-up ()
  10157. "Move cursor one line up in the window showing the table."
  10158. (interactive)
  10159. (org-table-fedit-move 'previous-line))
  10160. (defun org-table-fedit-line-down ()
  10161. "Move cursor one line down in the window showing the table."
  10162. (interactive)
  10163. (org-table-fedit-move 'next-line))
  10164. (defun org-table-fedit-move (command)
  10165. "Move the cursor in the window shoinw the table.
  10166. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10167. (let ((org-table-allow-automatic-line-recalculation nil)
  10168. (pos org-pos) (win (selected-window)) p)
  10169. (select-window (get-buffer-window (marker-buffer org-pos)))
  10170. (setq p (point))
  10171. (call-interactively command)
  10172. (while (and (org-at-table-p)
  10173. (org-at-table-hline-p))
  10174. (call-interactively command))
  10175. (or (org-at-table-p) (goto-char p))
  10176. (move-marker pos (point))
  10177. (select-window win)))
  10178. (defun org-table-fedit-scroll (N)
  10179. (interactive "p")
  10180. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10181. (scroll-other-window N)))
  10182. (defun org-table-fedit-scroll-down (N)
  10183. (interactive "p")
  10184. (org-table-fedit-scroll (- N)))
  10185. (defvar org-table-rectangle-overlays nil)
  10186. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10187. "Add a new overlay."
  10188. (let ((ov (org-make-overlay beg end)))
  10189. (org-overlay-put ov 'face (or face 'secondary-selection))
  10190. (push ov org-table-rectangle-overlays)))
  10191. (defun org-table-highlight-rectangle (&optional beg end face)
  10192. "Highlight rectangular region in a table."
  10193. (setq beg (or beg (point)) end (or end (point)))
  10194. (let ((b (min beg end))
  10195. (e (max beg end))
  10196. l1 c1 l2 c2 tmp)
  10197. (and (boundp 'org-show-positions)
  10198. (setq org-show-positions (cons b (cons e org-show-positions))))
  10199. (goto-char (min beg end))
  10200. (setq l1 (org-current-line)
  10201. c1 (org-table-current-column))
  10202. (goto-char (max beg end))
  10203. (setq l2 (org-current-line)
  10204. c2 (org-table-current-column))
  10205. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10206. (goto-line l1)
  10207. (beginning-of-line 1)
  10208. (loop for line from l1 to l2 do
  10209. (when (looking-at org-table-dataline-regexp)
  10210. (org-table-goto-column c1)
  10211. (skip-chars-backward "^|\n") (setq beg (point))
  10212. (org-table-goto-column c2)
  10213. (skip-chars-forward "^|\n") (setq end (point))
  10214. (org-table-add-rectangle-overlay beg end face))
  10215. (beginning-of-line 2))
  10216. (goto-char b))
  10217. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10218. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10219. "Remove the rectangle overlays."
  10220. (unless org-inhibit-highlight-removal
  10221. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10222. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10223. (setq org-table-rectangle-overlays nil)))
  10224. (defvar org-table-coordinate-overlays nil
  10225. "Collects the cooordinate grid overlays, so that they can be removed.")
  10226. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10227. (defun org-table-overlay-coordinates ()
  10228. "Add overlays to the table at point, to show row/column coordinates."
  10229. (interactive)
  10230. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10231. (setq org-table-coordinate-overlays nil)
  10232. (save-excursion
  10233. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10234. (goto-char (org-table-begin))
  10235. (while (org-at-table-p)
  10236. (setq eol (point-at-eol))
  10237. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10238. (push ov org-table-coordinate-overlays)
  10239. (setq hline (looking-at org-table-hline-regexp))
  10240. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10241. (format "%4d" (setq id (1+ id)))))
  10242. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10243. (when hline
  10244. (setq ic 0)
  10245. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10246. (setq beg (1+ (match-beginning 0))
  10247. ic (1+ ic)
  10248. s1 (concat "$" (int-to-string ic))
  10249. s2 (org-number-to-letters ic)
  10250. str (if (eq org-table-use-standard-references t) s2 s1))
  10251. (setq ov (org-make-overlay beg (+ beg (length str))))
  10252. (push ov org-table-coordinate-overlays)
  10253. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10254. (beginning-of-line 2)))))
  10255. (defun org-table-toggle-coordinate-overlays ()
  10256. "Toggle the display of Row/Column numbers in tables."
  10257. (interactive)
  10258. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10259. (message "Row/Column number display turned %s"
  10260. (if org-table-overlay-coordinates "on" "off"))
  10261. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10262. (org-table-align))
  10263. (unless org-table-overlay-coordinates
  10264. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10265. (setq org-table-coordinate-overlays nil)))
  10266. (defun org-table-toggle-formula-debugger ()
  10267. "Toggle the formula debugger in tables."
  10268. (interactive)
  10269. (setq org-table-formula-debug (not org-table-formula-debug))
  10270. (message "Formula debugging has been turned %s"
  10271. (if org-table-formula-debug "on" "off")))
  10272. ;;; The orgtbl minor mode
  10273. ;; Define a minor mode which can be used in other modes in order to
  10274. ;; integrate the org-mode table editor.
  10275. ;; This is really a hack, because the org-mode table editor uses several
  10276. ;; keys which normally belong to the major mode, for example the TAB and
  10277. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10278. ;; necessary to operate the table editor, but wraps the commands into a
  10279. ;; function which tests if the cursor is currently inside a table. If that
  10280. ;; is the case, the table editor command is executed. However, when any of
  10281. ;; those keys is used outside a table, the function uses `key-binding' to
  10282. ;; look up if the key has an associated command in another currently active
  10283. ;; keymap (minor modes, major mode, global), and executes that command.
  10284. ;; There might be problems if any of the keys used by the table editor is
  10285. ;; otherwise used as a prefix key.
  10286. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10287. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10288. ;; addresses this by checking explicitly for both bindings.
  10289. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10290. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10291. ;; Some modes bind other commands to simple characters, for example
  10292. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10293. ;; active, this binding is ignored inside tables and replaced with a
  10294. ;; modified self-insert.
  10295. (defvar orgtbl-mode nil
  10296. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10297. table editor in arbitrary modes.")
  10298. (make-variable-buffer-local 'orgtbl-mode)
  10299. (defvar orgtbl-mode-map (make-keymap)
  10300. "Keymap for `orgtbl-mode'.")
  10301. ;;;###autoload
  10302. (defun turn-on-orgtbl ()
  10303. "Unconditionally turn on `orgtbl-mode'."
  10304. (orgtbl-mode 1))
  10305. (defvar org-old-auto-fill-inhibit-regexp nil
  10306. "Local variable used by `orgtbl-mode'")
  10307. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10308. "Matches a line belonging to an orgtbl.")
  10309. (defconst orgtbl-extra-font-lock-keywords
  10310. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10311. 0 (quote 'org-table) 'prepend))
  10312. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10313. ;;;###autoload
  10314. (defun orgtbl-mode (&optional arg)
  10315. "The `org-mode' table editor as a minor mode for use in other modes."
  10316. (interactive)
  10317. (if (org-mode-p)
  10318. ;; Exit without error, in case some hook functions calls this
  10319. ;; by accident in org-mode.
  10320. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10321. (setq orgtbl-mode
  10322. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10323. (if orgtbl-mode
  10324. (progn
  10325. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10326. ;; Make sure we are first in minor-mode-map-alist
  10327. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10328. (and c (setq minor-mode-map-alist
  10329. (cons c (delq c minor-mode-map-alist)))))
  10330. (org-set-local (quote org-table-may-need-update) t)
  10331. (org-add-hook 'before-change-functions 'org-before-change-function
  10332. nil 'local)
  10333. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10334. auto-fill-inhibit-regexp)
  10335. (org-set-local 'auto-fill-inhibit-regexp
  10336. (if auto-fill-inhibit-regexp
  10337. (concat orgtbl-line-start-regexp "\\|"
  10338. auto-fill-inhibit-regexp)
  10339. orgtbl-line-start-regexp))
  10340. (org-add-to-invisibility-spec '(org-cwidth))
  10341. (when (fboundp 'font-lock-add-keywords)
  10342. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10343. (org-restart-font-lock))
  10344. (easy-menu-add orgtbl-mode-menu)
  10345. (run-hooks 'orgtbl-mode-hook))
  10346. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10347. (org-cleanup-narrow-column-properties)
  10348. (org-remove-from-invisibility-spec '(org-cwidth))
  10349. (remove-hook 'before-change-functions 'org-before-change-function t)
  10350. (when (fboundp 'font-lock-remove-keywords)
  10351. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10352. (org-restart-font-lock))
  10353. (easy-menu-remove orgtbl-mode-menu)
  10354. (force-mode-line-update 'all))))
  10355. (defun org-cleanup-narrow-column-properties ()
  10356. "Remove all properties related to narrow-column invisibility."
  10357. (let ((s 1))
  10358. (while (setq s (text-property-any s (point-max)
  10359. 'display org-narrow-column-arrow))
  10360. (remove-text-properties s (1+ s) '(display t)))
  10361. (setq s 1)
  10362. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10363. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10364. (setq s 1)
  10365. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10366. (remove-text-properties s (1+ s) '(invisible t)))))
  10367. ;; Install it as a minor mode.
  10368. (put 'orgtbl-mode :included t)
  10369. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10370. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10371. (defun orgtbl-make-binding (fun n &rest keys)
  10372. "Create a function for binding in the table minor mode.
  10373. FUN is the command to call inside a table. N is used to create a unique
  10374. command name. KEYS are keys that should be checked in for a command
  10375. to execute outside of tables."
  10376. (eval
  10377. (list 'defun
  10378. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10379. '(arg)
  10380. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10381. "Outside of tables, run the binding of `"
  10382. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10383. "'.")
  10384. '(interactive "p")
  10385. (list 'if
  10386. '(org-at-table-p)
  10387. (list 'call-interactively (list 'quote fun))
  10388. (list 'let '(orgtbl-mode)
  10389. (list 'call-interactively
  10390. (append '(or)
  10391. (mapcar (lambda (k)
  10392. (list 'key-binding k))
  10393. keys)
  10394. '('orgtbl-error))))))))
  10395. (defun orgtbl-error ()
  10396. "Error when there is no default binding for a table key."
  10397. (interactive)
  10398. (error "This key has no function outside tables"))
  10399. (defun orgtbl-setup ()
  10400. "Setup orgtbl keymaps."
  10401. (let ((nfunc 0)
  10402. (bindings
  10403. (list
  10404. '([(meta shift left)] org-table-delete-column)
  10405. '([(meta left)] org-table-move-column-left)
  10406. '([(meta right)] org-table-move-column-right)
  10407. '([(meta shift right)] org-table-insert-column)
  10408. '([(meta shift up)] org-table-kill-row)
  10409. '([(meta shift down)] org-table-insert-row)
  10410. '([(meta up)] org-table-move-row-up)
  10411. '([(meta down)] org-table-move-row-down)
  10412. '("\C-c\C-w" org-table-cut-region)
  10413. '("\C-c\M-w" org-table-copy-region)
  10414. '("\C-c\C-y" org-table-paste-rectangle)
  10415. '("\C-c-" org-table-insert-hline)
  10416. '("\C-c}" org-table-toggle-coordinate-overlays)
  10417. '("\C-c{" org-table-toggle-formula-debugger)
  10418. '("\C-m" org-table-next-row)
  10419. '([(shift return)] org-table-copy-down)
  10420. '("\C-c\C-q" org-table-wrap-region)
  10421. '("\C-c?" org-table-field-info)
  10422. '("\C-c " org-table-blank-field)
  10423. '("\C-c+" org-table-sum)
  10424. '("\C-c=" org-table-eval-formula)
  10425. '("\C-c'" org-table-edit-formulas)
  10426. '("\C-c`" org-table-edit-field)
  10427. '("\C-c*" org-table-recalculate)
  10428. '("\C-c|" org-table-create-or-convert-from-region)
  10429. '("\C-c^" org-table-sort-lines)
  10430. '([(control ?#)] org-table-rotate-recalc-marks)))
  10431. elt key fun cmd)
  10432. (while (setq elt (pop bindings))
  10433. (setq nfunc (1+ nfunc))
  10434. (setq key (org-key (car elt))
  10435. fun (nth 1 elt)
  10436. cmd (orgtbl-make-binding fun nfunc key))
  10437. (org-defkey orgtbl-mode-map key cmd))
  10438. ;; Special treatment needed for TAB and RET
  10439. (org-defkey orgtbl-mode-map [(return)]
  10440. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10441. (org-defkey orgtbl-mode-map "\C-m"
  10442. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10443. (org-defkey orgtbl-mode-map [(tab)]
  10444. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10445. (org-defkey orgtbl-mode-map "\C-i"
  10446. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10447. (org-defkey orgtbl-mode-map [(shift tab)]
  10448. (orgtbl-make-binding 'org-table-previous-field 104
  10449. [(shift tab)] [(tab)] "\C-i"))
  10450. (org-defkey orgtbl-mode-map "\M-\C-m"
  10451. (orgtbl-make-binding 'org-table-wrap-region 105
  10452. "\M-\C-m" [(meta return)]))
  10453. (org-defkey orgtbl-mode-map [(meta return)]
  10454. (orgtbl-make-binding 'org-table-wrap-region 106
  10455. [(meta return)] "\M-\C-m"))
  10456. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10457. (when orgtbl-optimized
  10458. ;; If the user wants maximum table support, we need to hijack
  10459. ;; some standard editing functions
  10460. (org-remap orgtbl-mode-map
  10461. 'self-insert-command 'orgtbl-self-insert-command
  10462. 'delete-char 'org-delete-char
  10463. 'delete-backward-char 'org-delete-backward-char)
  10464. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10465. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10466. '("OrgTbl"
  10467. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10468. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10469. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10470. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10471. "--"
  10472. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10473. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10474. ["Copy Field from Above"
  10475. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10476. "--"
  10477. ("Column"
  10478. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10479. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10480. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10481. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10482. ("Row"
  10483. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10484. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10485. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10486. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10487. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10488. "--"
  10489. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10490. ("Rectangle"
  10491. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10492. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10493. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10494. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10495. "--"
  10496. ("Radio tables"
  10497. ["Insert table template" orgtbl-insert-radio-table
  10498. (assq major-mode orgtbl-radio-table-templates)]
  10499. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10500. "--"
  10501. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10502. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10503. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10504. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10505. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10506. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10507. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10508. ["Sum Column/Rectangle" org-table-sum
  10509. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10510. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10511. ["Debug Formulas"
  10512. org-table-toggle-formula-debugger :active (org-at-table-p)
  10513. :keys "C-c {"
  10514. :style toggle :selected org-table-formula-debug]
  10515. ["Show Col/Row Numbers"
  10516. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10517. :keys "C-c }"
  10518. :style toggle :selected org-table-overlay-coordinates]
  10519. ))
  10520. t))
  10521. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10522. "If the cursor is inside a table, realign the table.
  10523. It it is a table to be sent away to a receiver, do it.
  10524. With prefix arg, also recompute table."
  10525. (interactive "P")
  10526. (let ((pos (point)) action)
  10527. (save-excursion
  10528. (beginning-of-line 1)
  10529. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10530. ((looking-at "[ \t]*|") pos)
  10531. ((looking-at "#\\+TBLFM:") 'recalc))))
  10532. (cond
  10533. ((integerp action)
  10534. (goto-char action)
  10535. (org-table-maybe-eval-formula)
  10536. (if arg
  10537. (call-interactively 'org-table-recalculate)
  10538. (org-table-maybe-recalculate-line))
  10539. (call-interactively 'org-table-align)
  10540. (orgtbl-send-table 'maybe))
  10541. ((eq action 'recalc)
  10542. (save-excursion
  10543. (beginning-of-line 1)
  10544. (skip-chars-backward " \r\n\t")
  10545. (if (org-at-table-p)
  10546. (org-call-with-arg 'org-table-recalculate t))))
  10547. (t (let (orgtbl-mode)
  10548. (call-interactively (key-binding "\C-c\C-c")))))))
  10549. (defun orgtbl-tab (arg)
  10550. "Justification and field motion for `orgtbl-mode'."
  10551. (interactive "P")
  10552. (if arg (org-table-edit-field t)
  10553. (org-table-justify-field-maybe)
  10554. (org-table-next-field)))
  10555. (defun orgtbl-ret ()
  10556. "Justification and field motion for `orgtbl-mode'."
  10557. (interactive)
  10558. (org-table-justify-field-maybe)
  10559. (org-table-next-row))
  10560. (defun orgtbl-self-insert-command (N)
  10561. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10562. If the cursor is in a table looking at whitespace, the whitespace is
  10563. overwritten, and the table is not marked as requiring realignment."
  10564. (interactive "p")
  10565. (if (and (org-at-table-p)
  10566. (or
  10567. (and org-table-auto-blank-field
  10568. (member last-command
  10569. '(orgtbl-hijacker-command-100
  10570. orgtbl-hijacker-command-101
  10571. orgtbl-hijacker-command-102
  10572. orgtbl-hijacker-command-103
  10573. orgtbl-hijacker-command-104
  10574. orgtbl-hijacker-command-105))
  10575. (org-table-blank-field))
  10576. t)
  10577. (eq N 1)
  10578. (looking-at "[^|\n]* +|"))
  10579. (let (org-table-may-need-update)
  10580. (goto-char (1- (match-end 0)))
  10581. (delete-backward-char 1)
  10582. (goto-char (match-beginning 0))
  10583. (self-insert-command N))
  10584. (setq org-table-may-need-update t)
  10585. (let (orgtbl-mode)
  10586. (call-interactively (key-binding (vector last-input-event))))))
  10587. (defun org-force-self-insert (N)
  10588. "Needed to enforce self-insert under remapping."
  10589. (interactive "p")
  10590. (self-insert-command N))
  10591. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10592. "Regula expression matching exponentials as produced by calc.")
  10593. (defvar org-table-clean-did-remove-column nil)
  10594. (defun orgtbl-export (table target)
  10595. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10596. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10597. org-table-last-alignment org-table-last-column-widths
  10598. maxcol column)
  10599. (if (not (fboundp func))
  10600. (error "Cannot export orgtbl table to %s" target))
  10601. (setq lines (org-table-clean-before-export lines))
  10602. (setq table
  10603. (mapcar
  10604. (lambda (x)
  10605. (if (string-match org-table-hline-regexp x)
  10606. 'hline
  10607. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10608. lines))
  10609. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10610. table)))
  10611. (loop for i from (1- maxcol) downto 0 do
  10612. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10613. (setq column (delq nil column))
  10614. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10615. (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))
  10616. (funcall func table nil)))
  10617. (defun orgtbl-send-table (&optional maybe)
  10618. "Send a tranformed version of this table to the receiver position.
  10619. With argument MAYBE, fail quietly if no transformation is defined for
  10620. this table."
  10621. (interactive)
  10622. (catch 'exit
  10623. (unless (org-at-table-p) (error "Not at a table"))
  10624. ;; when non-interactive, we assume align has just happened.
  10625. (when (interactive-p) (org-table-align))
  10626. (save-excursion
  10627. (goto-char (org-table-begin))
  10628. (beginning-of-line 0)
  10629. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10630. (if maybe
  10631. (throw 'exit nil)
  10632. (error "Don't know how to transform this table."))))
  10633. (let* ((name (match-string 1))
  10634. beg
  10635. (transform (intern (match-string 2)))
  10636. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10637. (skip (plist-get params :skip))
  10638. (skipcols (plist-get params :skipcols))
  10639. (txt (buffer-substring-no-properties
  10640. (org-table-begin) (org-table-end)))
  10641. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10642. (lines (org-table-clean-before-export lines))
  10643. (i0 (if org-table-clean-did-remove-column 2 1))
  10644. (table (mapcar
  10645. (lambda (x)
  10646. (if (string-match org-table-hline-regexp x)
  10647. 'hline
  10648. (org-remove-by-index
  10649. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10650. skipcols i0)))
  10651. lines))
  10652. (fun (if (= i0 2) 'cdr 'identity))
  10653. (org-table-last-alignment
  10654. (org-remove-by-index (funcall fun org-table-last-alignment)
  10655. skipcols i0))
  10656. (org-table-last-column-widths
  10657. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10658. skipcols i0)))
  10659. (unless (fboundp transform)
  10660. (error "No such transformation function %s" transform))
  10661. (setq txt (funcall transform table params))
  10662. ;; Find the insertion place
  10663. (save-excursion
  10664. (goto-char (point-min))
  10665. (unless (re-search-forward
  10666. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10667. (error "Don't know where to insert translated table"))
  10668. (goto-char (match-beginning 0))
  10669. (beginning-of-line 2)
  10670. (setq beg (point))
  10671. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10672. (error "Cannot find end of insertion region"))
  10673. (beginning-of-line 1)
  10674. (delete-region beg (point))
  10675. (goto-char beg)
  10676. (insert txt "\n"))
  10677. (message "Table converted and installed at receiver location"))))
  10678. (defun org-remove-by-index (list indices &optional i0)
  10679. "Remove the elements in LIST with indices in INDICES.
  10680. First element has index 0, or I0 if given."
  10681. (if (not indices)
  10682. list
  10683. (if (integerp indices) (setq indices (list indices)))
  10684. (setq i0 (1- (or i0 0)))
  10685. (delq :rm (mapcar (lambda (x)
  10686. (setq i0 (1+ i0))
  10687. (if (memq i0 indices) :rm x))
  10688. list))))
  10689. (defun orgtbl-toggle-comment ()
  10690. "Comment or uncomment the orgtbl at point."
  10691. (interactive)
  10692. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10693. (re2 (concat "^" orgtbl-line-start-regexp))
  10694. (commented (save-excursion (beginning-of-line 1)
  10695. (cond ((looking-at re1) t)
  10696. ((looking-at re2) nil)
  10697. (t (error "Not at an org table")))))
  10698. (re (if commented re1 re2))
  10699. beg end)
  10700. (save-excursion
  10701. (beginning-of-line 1)
  10702. (while (looking-at re) (beginning-of-line 0))
  10703. (beginning-of-line 2)
  10704. (setq beg (point))
  10705. (while (looking-at re) (beginning-of-line 2))
  10706. (setq end (point)))
  10707. (comment-region beg end (if commented '(4) nil))))
  10708. (defun orgtbl-insert-radio-table ()
  10709. "Insert a radio table template appropriate for this major mode."
  10710. (interactive)
  10711. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10712. (txt (nth 1 e))
  10713. name pos)
  10714. (unless e (error "No radio table setup defined for %s" major-mode))
  10715. (setq name (read-string "Table name: "))
  10716. (while (string-match "%n" txt)
  10717. (setq txt (replace-match name t t txt)))
  10718. (or (bolp) (insert "\n"))
  10719. (setq pos (point))
  10720. (insert txt)
  10721. (goto-char pos)))
  10722. (defun org-get-param (params header i sym &optional hsym)
  10723. "Get parameter value for symbol SYM.
  10724. If this is a header line, actually get the value for the symbol with an
  10725. additional \"h\" inserted after the colon.
  10726. If the value is a protperty list, get the element for the current column.
  10727. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10728. (let ((val (plist-get params sym)))
  10729. (and hsym header (setq val (or (plist-get params hsym) val)))
  10730. (if (consp val) (plist-get val i) val)))
  10731. (defun orgtbl-to-generic (table params)
  10732. "Convert the orgtbl-mode TABLE to some other format.
  10733. This generic routine can be used for many standard cases.
  10734. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10735. separator line, or a list of fields for that line.
  10736. PARAMS is a property list of parameters that can influence the conversion.
  10737. For the generic converter, some parameters are obligatory: You need to
  10738. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10739. :splice, you must have :tstart and :tend.
  10740. Valid parameters are
  10741. :tstart String to start the table. Ignored when :splice is t.
  10742. :tend String to end the table. Ignored when :splice is t.
  10743. :splice When set to t, return only table body lines, don't wrap
  10744. them into :tstart and :tend. Default is nil.
  10745. :hline String to be inserted on horizontal separation lines.
  10746. May be nil to ignore hlines.
  10747. :lstart String to start a new table line.
  10748. :lend String to end a table line
  10749. :sep Separator between two fields
  10750. :lfmt Format for entire line, with enough %s to capture all fields.
  10751. If this is present, :lstart, :lend, and :sep are ignored.
  10752. :fmt A format to be used to wrap the field, should contain
  10753. %s for the original field value. For example, to wrap
  10754. everything in dollars, you could use :fmt \"$%s$\".
  10755. This may also be a property list with column numbers and
  10756. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10757. :hlstart :hlend :hlsep :hlfmt :hfmt
  10758. Same as above, specific for the header lines in the table.
  10759. All lines before the first hline are treated as header.
  10760. If any of these is not present, the data line value is used.
  10761. :efmt Use this format to print numbers with exponentials.
  10762. The format should have %s twice for inserting mantissa
  10763. and exponent, for example \"%s\\\\times10^{%s}\". This
  10764. may also be a property list with column numbers and
  10765. formats. :fmt will still be applied after :efmt.
  10766. In addition to this, the parameters :skip and :skipcols are always handled
  10767. directly by `orgtbl-send-table'. See manual."
  10768. (interactive)
  10769. (let* ((p params)
  10770. (splicep (plist-get p :splice))
  10771. (hline (plist-get p :hline))
  10772. rtn line i fm efm lfmt h)
  10773. ;; Do we have a header?
  10774. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10775. (setq h t))
  10776. ;; Put header
  10777. (unless splicep
  10778. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10779. ;; Now loop over all lines
  10780. (while (setq line (pop table))
  10781. (if (eq line 'hline)
  10782. ;; A horizontal separator line
  10783. (progn (if hline (push hline rtn))
  10784. (setq h nil)) ; no longer in header
  10785. ;; A normal line. Convert the fields, push line onto the result list
  10786. (setq i 0)
  10787. (setq line
  10788. (mapcar
  10789. (lambda (f)
  10790. (setq i (1+ i)
  10791. fm (org-get-param p h i :fmt :hfmt)
  10792. efm (org-get-param p h i :efmt))
  10793. (if (and efm (string-match orgtbl-exp-regexp f))
  10794. (setq f (format
  10795. efm (match-string 1 f) (match-string 2 f))))
  10796. (if fm (setq f (format fm f)))
  10797. f)
  10798. line))
  10799. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10800. (push (apply 'format lfmt line) rtn)
  10801. (push (concat
  10802. (org-get-param p h i :lstart :hlstart)
  10803. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10804. (org-get-param p h i :lend :hlend))
  10805. rtn))))
  10806. (unless splicep
  10807. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10808. (mapconcat 'identity (nreverse rtn) "\n")))
  10809. (defun orgtbl-to-latex (table params)
  10810. "Convert the orgtbl-mode TABLE to LaTeX.
  10811. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10812. separator line, or a list of fields for that line.
  10813. PARAMS is a property list of parameters that can influence the conversion.
  10814. Supports all parameters from `orgtbl-to-generic'. Most important for
  10815. LaTeX are:
  10816. :splice When set to t, return only table body lines, don't wrap
  10817. them into a tabular environment. Default is nil.
  10818. :fmt A format to be used to wrap the field, should contain %s for the
  10819. original field value. For example, to wrap everything in dollars,
  10820. use :fmt \"$%s$\". This may also be a property list with column
  10821. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10822. :efmt Format for transforming numbers with exponentials. The format
  10823. should have %s twice for inserting mantissa and exponent, for
  10824. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10825. This may also be a property list with column numbers and formats.
  10826. The general parameters :skip and :skipcols have already been applied when
  10827. this function is called."
  10828. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10829. org-table-last-alignment ""))
  10830. (params2
  10831. (list
  10832. :tstart (concat "\\begin{tabular}{" alignment "}")
  10833. :tend "\\end{tabular}"
  10834. :lstart "" :lend " \\\\" :sep " & "
  10835. :efmt "%s\\,(%s)" :hline "\\hline")))
  10836. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10837. (defun orgtbl-to-html (table params)
  10838. "Convert the orgtbl-mode TABLE to LaTeX.
  10839. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10840. separator line, or a list of fields for that line.
  10841. PARAMS is a property list of parameters that can influence the conversion.
  10842. Currently this function recognizes the following parameters:
  10843. :splice When set to t, return only table body lines, don't wrap
  10844. them into a <table> environment. Default is nil.
  10845. The general parameters :skip and :skipcols have already been applied when
  10846. this function is called. The function does *not* use `orgtbl-to-generic',
  10847. so you cannot specify parameters for it."
  10848. (let* ((splicep (plist-get params :splice))
  10849. html)
  10850. ;; Just call the formatter we already have
  10851. ;; We need to make text lines for it, so put the fields back together.
  10852. (setq html (org-format-org-table-html
  10853. (mapcar
  10854. (lambda (x)
  10855. (if (eq x 'hline)
  10856. "|----+----|"
  10857. (concat "| " (mapconcat 'identity x " | ") " |")))
  10858. table)
  10859. splicep))
  10860. (if (string-match "\n+\\'" html)
  10861. (setq html (replace-match "" t t html)))
  10862. html))
  10863. (defun orgtbl-to-texinfo (table params)
  10864. "Convert the orgtbl-mode TABLE to TeXInfo.
  10865. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10866. separator line, or a list of fields for that line.
  10867. PARAMS is a property list of parameters that can influence the conversion.
  10868. Supports all parameters from `orgtbl-to-generic'. Most important for
  10869. TeXInfo are:
  10870. :splice nil/t When set to t, return only table body lines, don't wrap
  10871. them into a multitable environment. Default is nil.
  10872. :fmt fmt A format to be used to wrap the field, should contain
  10873. %s for the original field value. For example, to wrap
  10874. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10875. This may also be a property list with column numbers and
  10876. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10877. :cf \"f1 f2..\" The column fractions for the table. By default these
  10878. are computed automatically from the width of the columns
  10879. under org-mode.
  10880. The general parameters :skip and :skipcols have already been applied when
  10881. this function is called."
  10882. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10883. (colfrac (or (plist-get params :cf)
  10884. (mapconcat
  10885. (lambda (x) (format "%.3f" (/ (float x) total)))
  10886. org-table-last-column-widths " ")))
  10887. (params2
  10888. (list
  10889. :tstart (concat "@multitable @columnfractions " colfrac)
  10890. :tend "@end multitable"
  10891. :lstart "@item " :lend "" :sep " @tab "
  10892. :hlstart "@headitem ")))
  10893. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10894. ;;;; Link Stuff
  10895. ;;; Link abbreviations
  10896. (defun org-link-expand-abbrev (link)
  10897. "Apply replacements as defined in `org-link-abbrev-alist."
  10898. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10899. (let* ((key (match-string 1 link))
  10900. (as (or (assoc key org-link-abbrev-alist-local)
  10901. (assoc key org-link-abbrev-alist)))
  10902. (tag (and (match-end 2) (match-string 3 link)))
  10903. rpl)
  10904. (if (not as)
  10905. link
  10906. (setq rpl (cdr as))
  10907. (cond
  10908. ((symbolp rpl) (funcall rpl tag))
  10909. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10910. (t (concat rpl tag)))))
  10911. link))
  10912. ;;; Storing and inserting links
  10913. (defvar org-insert-link-history nil
  10914. "Minibuffer history for links inserted with `org-insert-link'.")
  10915. (defvar org-stored-links nil
  10916. "Contains the links stored with `org-store-link'.")
  10917. (defvar org-store-link-plist nil
  10918. "Plist with info about the most recently link created with `org-store-link'.")
  10919. (defvar org-link-protocols nil
  10920. "Link protocols added to Org-mode using `org-add-link-type'.")
  10921. (defvar org-store-link-functions nil
  10922. "List of functions that are called to create and store a link.
  10923. Each function will be called in turn until one returns a non-nil
  10924. value. Each function should check if it is responsible for creating
  10925. this link (for example by looking at the major mode).
  10926. If not, it must exit and return nil.
  10927. If yes, it should return a non-nil value after a calling
  10928. `org-store-link-props' with a list of properties and values.
  10929. Special properties are:
  10930. :type The link prefix. like \"http\". This must be given.
  10931. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10932. This is obligatory as well.
  10933. :description Optional default description for the second pair
  10934. of brackets in an Org-mode link. The user can still change
  10935. this when inserting this link into an Org-mode buffer.
  10936. In addition to these, any additional properties can be specified
  10937. and then used in remember templates.")
  10938. (defun org-add-link-type (type &optional follow publish)
  10939. "Add TYPE to the list of `org-link-types'.
  10940. Re-compute all regular expressions depending on `org-link-types'
  10941. FOLLOW and PUBLISH are two functions. Both take the link path as
  10942. an argument.
  10943. FOLLOW should do whatever is necessary to follow the link, for example
  10944. to find a file or display a mail message.
  10945. PUBLISH takes the path and retuns the string that should be used when
  10946. this document is published. FIMXE: This is actually not yet implemented."
  10947. (add-to-list 'org-link-types type t)
  10948. (org-make-link-regexps)
  10949. (add-to-list 'org-link-protocols
  10950. (list type follow publish)))
  10951. (defun org-add-agenda-custom-command (entry)
  10952. "Replace or add a command in `org-agenda-custom-commands'.
  10953. This is mostly for hacking and trying a new command - once the command
  10954. works you probably want to add it to `org-agenda-custom-commands' for good."
  10955. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10956. (if ass
  10957. (setcdr ass (cdr entry))
  10958. (push entry org-agenda-custom-commands))))
  10959. ;;;###autoload
  10960. (defun org-store-link (arg)
  10961. "\\<org-mode-map>Store an org-link to the current location.
  10962. This link is added to `org-stored-links' and can later be inserted
  10963. into an org-buffer with \\[org-insert-link].
  10964. For some link types, a prefix arg is interpreted:
  10965. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10966. For file links, arg negates `org-context-in-file-links'."
  10967. (interactive "P")
  10968. (setq org-store-link-plist nil) ; reset
  10969. (let (link cpltxt desc description search txt)
  10970. (cond
  10971. ((run-hook-with-args-until-success 'org-store-link-functions)
  10972. (setq link (plist-get org-store-link-plist :link)
  10973. desc (or (plist-get org-store-link-plist :description) link)))
  10974. ((eq major-mode 'bbdb-mode)
  10975. (let ((name (bbdb-record-name (bbdb-current-record)))
  10976. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10977. (setq cpltxt (concat "bbdb:" (or name company))
  10978. link (org-make-link cpltxt))
  10979. (org-store-link-props :type "bbdb" :name name :company company)))
  10980. ((eq major-mode 'Info-mode)
  10981. (setq link (org-make-link "info:"
  10982. (file-name-nondirectory Info-current-file)
  10983. ":" Info-current-node))
  10984. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10985. ":" Info-current-node))
  10986. (org-store-link-props :type "info" :file Info-current-file
  10987. :node Info-current-node))
  10988. ((eq major-mode 'calendar-mode)
  10989. (let ((cd (calendar-cursor-to-date)))
  10990. (setq link
  10991. (format-time-string
  10992. (car org-time-stamp-formats)
  10993. (apply 'encode-time
  10994. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10995. nil nil nil))))
  10996. (org-store-link-props :type "calendar" :date cd)))
  10997. ((or (eq major-mode 'vm-summary-mode)
  10998. (eq major-mode 'vm-presentation-mode))
  10999. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11000. (vm-follow-summary-cursor)
  11001. (save-excursion
  11002. (vm-select-folder-buffer)
  11003. (let* ((message (car vm-message-pointer))
  11004. (folder buffer-file-name)
  11005. (subject (vm-su-subject message))
  11006. (to (vm-get-header-contents message "To"))
  11007. (from (vm-get-header-contents message "From"))
  11008. (message-id (vm-su-message-id message)))
  11009. (org-store-link-props :type "vm" :from from :to to :subject subject
  11010. :message-id message-id)
  11011. (setq message-id (org-remove-angle-brackets message-id))
  11012. (setq folder (abbreviate-file-name folder))
  11013. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11014. folder)
  11015. (setq folder (replace-match "" t t folder)))
  11016. (setq cpltxt (org-email-link-description))
  11017. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11018. ((eq major-mode 'wl-summary-mode)
  11019. (let* ((msgnum (wl-summary-message-number))
  11020. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11021. msgnum 'message-id))
  11022. (wl-message-entity
  11023. (if (fboundp 'elmo-message-entity)
  11024. (elmo-message-entity
  11025. wl-summary-buffer-elmo-folder msgnum)
  11026. (elmo-msgdb-overview-get-entity
  11027. msgnum (wl-summary-buffer-msgdb))))
  11028. (from (wl-summary-line-from))
  11029. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11030. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11031. (wl-summary-line-subject))))
  11032. (org-store-link-props :type "wl" :from from :to to
  11033. :subject subject :message-id message-id)
  11034. (setq message-id (org-remove-angle-brackets message-id))
  11035. (setq cpltxt (org-email-link-description))
  11036. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11037. "#" message-id))))
  11038. ((or (equal major-mode 'mh-folder-mode)
  11039. (equal major-mode 'mh-show-mode))
  11040. (let ((from (org-mhe-get-header "From:"))
  11041. (to (org-mhe-get-header "To:"))
  11042. (message-id (org-mhe-get-header "Message-Id:"))
  11043. (subject (org-mhe-get-header "Subject:")))
  11044. (org-store-link-props :type "mh" :from from :to to
  11045. :subject subject :message-id message-id)
  11046. (setq cpltxt (org-email-link-description))
  11047. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11048. (org-remove-angle-brackets message-id)))))
  11049. ((or (eq major-mode 'rmail-mode)
  11050. (eq major-mode 'rmail-summary-mode))
  11051. (save-window-excursion
  11052. (save-restriction
  11053. (when (eq major-mode 'rmail-summary-mode)
  11054. (rmail-show-message rmail-current-message))
  11055. (rmail-narrow-to-non-pruned-header)
  11056. (let ((folder buffer-file-name)
  11057. (message-id (mail-fetch-field "message-id"))
  11058. (from (mail-fetch-field "from"))
  11059. (to (mail-fetch-field "to"))
  11060. (subject (mail-fetch-field "subject")))
  11061. (org-store-link-props
  11062. :type "rmail" :from from :to to
  11063. :subject subject :message-id message-id)
  11064. (setq message-id (org-remove-angle-brackets message-id))
  11065. (setq cpltxt (org-email-link-description))
  11066. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11067. (rmail-show-message rmail-current-message))))
  11068. ((eq major-mode 'gnus-group-mode)
  11069. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11070. (gnus-group-group-name)) ; version
  11071. ((fboundp 'gnus-group-name)
  11072. (gnus-group-name))
  11073. (t "???"))))
  11074. (unless group (error "Not on a group"))
  11075. (org-store-link-props :type "gnus" :group group)
  11076. (setq cpltxt (concat
  11077. (if (org-xor arg org-usenet-links-prefer-google)
  11078. "http://groups.google.com/groups?group="
  11079. "gnus:")
  11080. group)
  11081. link (org-make-link cpltxt))))
  11082. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11083. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11084. (let* ((group gnus-newsgroup-name)
  11085. (article (gnus-summary-article-number))
  11086. (header (gnus-summary-article-header article))
  11087. (from (mail-header-from header))
  11088. (message-id (mail-header-id header))
  11089. (date (mail-header-date header))
  11090. (subject (gnus-summary-subject-string)))
  11091. (org-store-link-props :type "gnus" :from from :subject subject
  11092. :message-id message-id :group group)
  11093. (setq cpltxt (org-email-link-description))
  11094. (if (org-xor arg org-usenet-links-prefer-google)
  11095. (setq link
  11096. (concat
  11097. cpltxt "\n "
  11098. (format "http://groups.google.com/groups?as_umsgid=%s"
  11099. (org-fixup-message-id-for-http message-id))))
  11100. (setq link (org-make-link "gnus:" group
  11101. "#" (number-to-string article))))))
  11102. ((eq major-mode 'w3-mode)
  11103. (setq cpltxt (url-view-url t)
  11104. link (org-make-link cpltxt))
  11105. (org-store-link-props :type "w3" :url (url-view-url t)))
  11106. ((eq major-mode 'w3m-mode)
  11107. (setq cpltxt (or w3m-current-title w3m-current-url)
  11108. link (org-make-link w3m-current-url))
  11109. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11110. ((setq search (run-hook-with-args-until-success
  11111. 'org-create-file-search-functions))
  11112. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11113. "::" search))
  11114. (setq cpltxt (or description link)))
  11115. ((eq major-mode 'image-mode)
  11116. (setq cpltxt (concat "file:"
  11117. (abbreviate-file-name buffer-file-name))
  11118. link (org-make-link cpltxt))
  11119. (org-store-link-props :type "image" :file buffer-file-name))
  11120. ((eq major-mode 'dired-mode)
  11121. ;; link to the file in the current line
  11122. (setq cpltxt (concat "file:"
  11123. (abbreviate-file-name
  11124. (expand-file-name
  11125. (dired-get-filename nil t))))
  11126. link (org-make-link cpltxt)))
  11127. ((and buffer-file-name (org-mode-p))
  11128. ;; Just link to current headline
  11129. (setq cpltxt (concat "file:"
  11130. (abbreviate-file-name buffer-file-name)))
  11131. ;; Add a context search string
  11132. (when (org-xor org-context-in-file-links arg)
  11133. ;; Check if we are on a target
  11134. (if (org-in-regexp "<<\\(.*?\\)>>")
  11135. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11136. (setq txt (cond
  11137. ((org-on-heading-p) nil)
  11138. ((org-region-active-p)
  11139. (buffer-substring (region-beginning) (region-end)))
  11140. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11141. (when (or (null txt) (string-match "\\S-" txt))
  11142. (setq cpltxt
  11143. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11144. desc "NONE"))))
  11145. (if (string-match "::\\'" cpltxt)
  11146. (setq cpltxt (substring cpltxt 0 -2)))
  11147. (setq link (org-make-link cpltxt)))
  11148. ((buffer-file-name (buffer-base-buffer))
  11149. ;; Just link to this file here.
  11150. (setq cpltxt (concat "file:"
  11151. (abbreviate-file-name
  11152. (buffer-file-name (buffer-base-buffer)))))
  11153. ;; Add a context string
  11154. (when (org-xor org-context-in-file-links arg)
  11155. (setq txt (if (org-region-active-p)
  11156. (buffer-substring (region-beginning) (region-end))
  11157. (buffer-substring (point-at-bol) (point-at-eol))))
  11158. ;; Only use search option if there is some text.
  11159. (when (string-match "\\S-" txt)
  11160. (setq cpltxt
  11161. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11162. desc "NONE")))
  11163. (setq link (org-make-link cpltxt)))
  11164. ((interactive-p)
  11165. (error "Cannot link to a buffer which is not visiting a file"))
  11166. (t (setq link nil)))
  11167. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11168. (setq link (or link cpltxt)
  11169. desc (or desc cpltxt))
  11170. (if (equal desc "NONE") (setq desc nil))
  11171. (if (and (interactive-p) link)
  11172. (progn
  11173. (setq org-stored-links
  11174. (cons (list link desc) org-stored-links))
  11175. (message "Stored: %s" (or desc link)))
  11176. (and link (org-make-link-string link desc)))))
  11177. (defun org-store-link-props (&rest plist)
  11178. "Store link properties, extract names and addresses."
  11179. (let (x adr)
  11180. (when (setq x (plist-get plist :from))
  11181. (setq adr (mail-extract-address-components x))
  11182. (plist-put plist :fromname (car adr))
  11183. (plist-put plist :fromaddress (nth 1 adr)))
  11184. (when (setq x (plist-get plist :to))
  11185. (setq adr (mail-extract-address-components x))
  11186. (plist-put plist :toname (car adr))
  11187. (plist-put plist :toaddress (nth 1 adr))))
  11188. (let ((from (plist-get plist :from))
  11189. (to (plist-get plist :to)))
  11190. (when (and from to org-from-is-user-regexp)
  11191. (plist-put plist :fromto
  11192. (if (string-match org-from-is-user-regexp from)
  11193. (concat "to %t")
  11194. (concat "from %f")))))
  11195. (setq org-store-link-plist plist))
  11196. (defun org-email-link-description (&optional fmt)
  11197. "Return the description part of an email link.
  11198. This takes information from `org-store-link-plist' and formats it
  11199. according to FMT (default from `org-email-link-description-format')."
  11200. (setq fmt (or fmt org-email-link-description-format))
  11201. (let* ((p org-store-link-plist)
  11202. (to (plist-get p :toaddress))
  11203. (from (plist-get p :fromaddress))
  11204. (table
  11205. (list
  11206. (cons "%c" (plist-get p :fromto))
  11207. (cons "%F" (plist-get p :from))
  11208. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11209. (cons "%T" (plist-get p :to))
  11210. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11211. (cons "%s" (plist-get p :subject))
  11212. (cons "%m" (plist-get p :message-id)))))
  11213. (when (string-match "%c" fmt)
  11214. ;; Check if the user wrote this message
  11215. (if (and org-from-is-user-regexp from to
  11216. (save-match-data (string-match org-from-is-user-regexp from)))
  11217. (setq fmt (replace-match "to %t" t t fmt))
  11218. (setq fmt (replace-match "from %f" t t fmt))))
  11219. (org-replace-escapes fmt table)))
  11220. (defun org-make-org-heading-search-string (&optional string heading)
  11221. "Make search string for STRING or current headline."
  11222. (interactive)
  11223. (let ((s (or string (org-get-heading))))
  11224. (unless (and string (not heading))
  11225. ;; We are using a headline, clean up garbage in there.
  11226. (if (string-match org-todo-regexp s)
  11227. (setq s (replace-match "" t t s)))
  11228. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11229. (setq s (replace-match "" t t s)))
  11230. (setq s (org-trim s))
  11231. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11232. org-comment-string "\\)") s)
  11233. (setq s (replace-match "" t t s)))
  11234. (while (string-match org-ts-regexp s)
  11235. (setq s (replace-match "" t t s))))
  11236. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11237. (setq s (replace-match " " t t s)))
  11238. (or string (setq s (concat "*" s))) ; Add * for headlines
  11239. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11240. (defun org-make-link (&rest strings)
  11241. "Concatenate STRINGS."
  11242. (apply 'concat strings))
  11243. (defun org-make-link-string (link &optional description)
  11244. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11245. (unless (string-match "\\S-" link)
  11246. (error "Empty link"))
  11247. (when (stringp description)
  11248. ;; Remove brackets from the description, they are fatal.
  11249. (while (string-match "\\[" description)
  11250. (setq description (replace-match "{" t t description)))
  11251. (while (string-match "\\]" description)
  11252. (setq description (replace-match "}" t t description))))
  11253. (when (equal (org-link-escape link) description)
  11254. ;; No description needed, it is identical
  11255. (setq description nil))
  11256. (when (and (not description)
  11257. (not (equal link (org-link-escape link))))
  11258. (setq description link))
  11259. (concat "[[" (org-link-escape link) "]"
  11260. (if description (concat "[" description "]") "")
  11261. "]"))
  11262. (defconst org-link-escape-chars
  11263. '((?\ . "%20")
  11264. (?\[ . "%5B")
  11265. (?\] . "%5D")
  11266. (?\340 . "%E0") ; `a
  11267. (?\342 . "%E2") ; ^a
  11268. (?\347 . "%E7") ; ,c
  11269. (?\350 . "%E8") ; `e
  11270. (?\351 . "%E9") ; 'e
  11271. (?\352 . "%EA") ; ^e
  11272. (?\356 . "%EE") ; ^i
  11273. (?\364 . "%F4") ; ^o
  11274. (?\371 . "%F9") ; `u
  11275. (?\373 . "%FB") ; ^u
  11276. (?\; . "%3B")
  11277. (?? . "%3F")
  11278. (?= . "%3D")
  11279. (?+ . "%2B")
  11280. )
  11281. "Association list of escapes for some characters problematic in links.
  11282. This is the list that is used for internal purposes.")
  11283. (defconst org-link-escape-chars-browser
  11284. '((?\ . "%20")) ; 32 for the SPC char
  11285. "Association list of escapes for some characters problematic in links.
  11286. This is the list that is used before handing over to the browser.")
  11287. (defun org-link-escape (text &optional table)
  11288. "Escape charaters in TEXT that are problematic for links."
  11289. (setq table (or table org-link-escape-chars))
  11290. (when text
  11291. (let ((re (mapconcat (lambda (x) (regexp-quote
  11292. (char-to-string (car x))))
  11293. table "\\|")))
  11294. (while (string-match re text)
  11295. (setq text
  11296. (replace-match
  11297. (cdr (assoc (string-to-char (match-string 0 text))
  11298. table))
  11299. t t text)))
  11300. text)))
  11301. (defun org-link-unescape (text &optional table)
  11302. "Reverse the action of `org-link-escape'."
  11303. (setq table (or table org-link-escape-chars))
  11304. (when text
  11305. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11306. table "\\|")))
  11307. (while (string-match re text)
  11308. (setq text
  11309. (replace-match
  11310. (char-to-string (car (rassoc (match-string 0 text) table)))
  11311. t t text)))
  11312. text)))
  11313. (defun org-xor (a b)
  11314. "Exclusive or."
  11315. (if a (not b) b))
  11316. (defun org-get-header (header)
  11317. "Find a header field in the current buffer."
  11318. (save-excursion
  11319. (goto-char (point-min))
  11320. (let ((case-fold-search t) s)
  11321. (cond
  11322. ((eq header 'from)
  11323. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11324. (setq s (match-string 1)))
  11325. (while (string-match "\"" s)
  11326. (setq s (replace-match "" t t s)))
  11327. (if (string-match "[<(].*" s)
  11328. (setq s (replace-match "" t t s))))
  11329. ((eq header 'message-id)
  11330. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11331. (setq s (match-string 1))))
  11332. ((eq header 'subject)
  11333. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11334. (setq s (match-string 1)))))
  11335. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11336. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11337. s)))
  11338. (defun org-fixup-message-id-for-http (s)
  11339. "Replace special characters in a message id, so it can be used in an http query."
  11340. (while (string-match "<" s)
  11341. (setq s (replace-match "%3C" t t s)))
  11342. (while (string-match ">" s)
  11343. (setq s (replace-match "%3E" t t s)))
  11344. (while (string-match "@" s)
  11345. (setq s (replace-match "%40" t t s)))
  11346. s)
  11347. ;;;###autoload
  11348. (defun org-insert-link-global ()
  11349. "Insert a link like Org-mode does.
  11350. This command can be called in any mode to insert a link in Org-mode syntax."
  11351. (interactive)
  11352. (org-run-like-in-org-mode 'org-insert-link))
  11353. (defun org-insert-link (&optional complete-file)
  11354. "Insert a link. At the prompt, enter the link.
  11355. Completion can be used to select a link previously stored with
  11356. `org-store-link'. When the empty string is entered (i.e. if you just
  11357. press RET at the prompt), the link defaults to the most recently
  11358. stored link. As SPC triggers completion in the minibuffer, you need to
  11359. use M-SPC or C-q SPC to force the insertion of a space character.
  11360. You will also be prompted for a description, and if one is given, it will
  11361. be displayed in the buffer instead of the link.
  11362. If there is already a link at point, this command will allow you to edit link
  11363. and description parts.
  11364. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11365. selected using completion. The path to the file will be relative to
  11366. the current directory if the file is in the current directory or a
  11367. subdirectory. Otherwise, the link will be the absolute path as
  11368. completed in the minibuffer (i.e. normally ~/path/to/file).
  11369. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11370. is in the current directory or below.
  11371. With three \\[universal-argument] prefixes, negate the meaning of
  11372. `org-keep-stored-link-after-insertion'."
  11373. (interactive "P")
  11374. (let* ((wcf (current-window-configuration))
  11375. (region (if (org-region-active-p)
  11376. (buffer-substring (region-beginning) (region-end))))
  11377. (remove (and region (list (region-beginning) (region-end))))
  11378. (desc region)
  11379. tmphist ; byte-compile incorrectly complains about this
  11380. link entry file)
  11381. (cond
  11382. ((org-in-regexp org-bracket-link-regexp 1)
  11383. ;; We do have a link at point, and we are going to edit it.
  11384. (setq remove (list (match-beginning 0) (match-end 0)))
  11385. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11386. (setq link (read-string "Link: "
  11387. (org-link-unescape
  11388. (org-match-string-no-properties 1)))))
  11389. ((or (org-in-regexp org-angle-link-re)
  11390. (org-in-regexp org-plain-link-re))
  11391. ;; Convert to bracket link
  11392. (setq remove (list (match-beginning 0) (match-end 0))
  11393. link (read-string "Link: "
  11394. (org-remove-angle-brackets (match-string 0)))))
  11395. ((equal complete-file '(4))
  11396. ;; Completing read for file names.
  11397. (setq file (read-file-name "File: "))
  11398. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11399. (pwd1 (file-name-as-directory (abbreviate-file-name
  11400. (expand-file-name ".")))))
  11401. (cond
  11402. ((equal complete-file '(16))
  11403. (setq link (org-make-link
  11404. "file:"
  11405. (abbreviate-file-name (expand-file-name file)))))
  11406. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11407. (setq link (org-make-link "file:" (match-string 1 file))))
  11408. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11409. (expand-file-name file))
  11410. (setq link (org-make-link
  11411. "file:" (match-string 1 (expand-file-name file)))))
  11412. (t (setq link (org-make-link "file:" file))))))
  11413. (t
  11414. ;; Read link, with completion for stored links.
  11415. (with-output-to-temp-buffer "*Org Links*"
  11416. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11417. (when org-stored-links
  11418. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11419. (princ (mapconcat
  11420. (lambda (x)
  11421. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11422. (reverse org-stored-links) "\n"))))
  11423. (let ((cw (selected-window)))
  11424. (select-window (get-buffer-window "*Org Links*"))
  11425. (shrink-window-if-larger-than-buffer)
  11426. (setq truncate-lines t)
  11427. (select-window cw))
  11428. ;; Fake a link history, containing the stored links.
  11429. (setq tmphist (append (mapcar 'car org-stored-links)
  11430. org-insert-link-history))
  11431. (unwind-protect
  11432. (setq link (org-completing-read
  11433. "Link: "
  11434. (append
  11435. (mapcar (lambda (x) (list (concat (car x) ":")))
  11436. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11437. (mapcar (lambda (x) (list (concat x ":")))
  11438. org-link-types))
  11439. nil nil nil
  11440. 'tmphist
  11441. (or (car (car org-stored-links)))))
  11442. (set-window-configuration wcf)
  11443. (kill-buffer "*Org Links*"))
  11444. (setq entry (assoc link org-stored-links))
  11445. (or entry (push link org-insert-link-history))
  11446. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11447. (not org-keep-stored-link-after-insertion))
  11448. (setq org-stored-links (delq (assoc link org-stored-links)
  11449. org-stored-links)))
  11450. (setq desc (or desc (nth 1 entry)))))
  11451. (if (string-match org-plain-link-re link)
  11452. ;; URL-like link, normalize the use of angular brackets.
  11453. (setq link (org-make-link (org-remove-angle-brackets link))))
  11454. ;; Check if we are linking to the current file with a search option
  11455. ;; If yes, simplify the link by using only the search option.
  11456. (when (and buffer-file-name
  11457. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11458. (let* ((path (match-string 1 link))
  11459. (case-fold-search nil)
  11460. (search (match-string 2 link)))
  11461. (save-match-data
  11462. (if (equal (file-truename buffer-file-name) (file-truename path))
  11463. ;; We are linking to this same file, with a search option
  11464. (setq link search)))))
  11465. ;; Check if we can/should use a relative path. If yes, simplify the link
  11466. (when (string-match "\\<file:\\(.*\\)" link)
  11467. (let* ((path (match-string 1 link))
  11468. (origpath path)
  11469. (case-fold-search nil))
  11470. (cond
  11471. ((eq org-link-file-path-type 'absolute)
  11472. (setq path (abbreviate-file-name (expand-file-name path))))
  11473. ((eq org-link-file-path-type 'noabbrev)
  11474. (setq path (expand-file-name path)))
  11475. ((eq org-link-file-path-type 'relative)
  11476. (setq path (file-relative-name path)))
  11477. (t
  11478. (save-match-data
  11479. (if (string-match (concat "^" (regexp-quote
  11480. (file-name-as-directory
  11481. (expand-file-name "."))))
  11482. (expand-file-name path))
  11483. ;; We are linking a file with relative path name.
  11484. (setq path (substring (expand-file-name path)
  11485. (match-end 0)))))))
  11486. (setq link (concat "file:" path))
  11487. (if (equal desc origpath)
  11488. (setq desc path))))
  11489. (setq desc (read-string "Description: " desc))
  11490. (unless (string-match "\\S-" desc) (setq desc nil))
  11491. (if remove (apply 'delete-region remove))
  11492. (insert (org-make-link-string link desc))))
  11493. (defun org-completing-read (&rest args)
  11494. (let ((minibuffer-local-completion-map
  11495. (copy-keymap minibuffer-local-completion-map)))
  11496. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11497. (apply 'completing-read args)))
  11498. ;;; Opening/following a link
  11499. (defvar org-link-search-failed nil)
  11500. (defun org-next-link ()
  11501. "Move forward to the next link.
  11502. If the link is in hidden text, expose it."
  11503. (interactive)
  11504. (when (and org-link-search-failed (eq this-command last-command))
  11505. (goto-char (point-min))
  11506. (message "Link search wrapped back to beginning of buffer"))
  11507. (setq org-link-search-failed nil)
  11508. (let* ((pos (point))
  11509. (ct (org-context))
  11510. (a (assoc :link ct)))
  11511. (if a (goto-char (nth 2 a)))
  11512. (if (re-search-forward org-any-link-re nil t)
  11513. (progn
  11514. (goto-char (match-beginning 0))
  11515. (if (org-invisible-p) (org-show-context)))
  11516. (goto-char pos)
  11517. (setq org-link-search-failed t)
  11518. (error "No further link found"))))
  11519. (defun org-previous-link ()
  11520. "Move backward to the previous link.
  11521. If the link is in hidden text, expose it."
  11522. (interactive)
  11523. (when (and org-link-search-failed (eq this-command last-command))
  11524. (goto-char (point-max))
  11525. (message "Link search wrapped back to end of buffer"))
  11526. (setq org-link-search-failed nil)
  11527. (let* ((pos (point))
  11528. (ct (org-context))
  11529. (a (assoc :link ct)))
  11530. (if a (goto-char (nth 1 a)))
  11531. (if (re-search-backward org-any-link-re nil t)
  11532. (progn
  11533. (goto-char (match-beginning 0))
  11534. (if (org-invisible-p) (org-show-context)))
  11535. (goto-char pos)
  11536. (setq org-link-search-failed t)
  11537. (error "No further link found"))))
  11538. (defun org-find-file-at-mouse (ev)
  11539. "Open file link or URL at mouse."
  11540. (interactive "e")
  11541. (mouse-set-point ev)
  11542. (org-open-at-point 'in-emacs))
  11543. (defun org-open-at-mouse (ev)
  11544. "Open file link or URL at mouse."
  11545. (interactive "e")
  11546. (mouse-set-point ev)
  11547. (org-open-at-point))
  11548. (defvar org-window-config-before-follow-link nil
  11549. "The window configuration before following a link.
  11550. This is saved in case the need arises to restore it.")
  11551. (defvar org-open-link-marker (make-marker)
  11552. "Marker pointing to the location where `org-open-at-point; was called.")
  11553. ;;;###autoload
  11554. (defun org-open-at-point-global ()
  11555. "Follow a link like Org-mode does.
  11556. This command can be called in any mode to follow a link that has
  11557. Org-mode syntax."
  11558. (interactive)
  11559. (org-run-like-in-org-mode 'org-open-at-point))
  11560. (defun org-open-at-point (&optional in-emacs)
  11561. "Open link at or after point.
  11562. If there is no link at point, this function will search forward up to
  11563. the end of the current subtree.
  11564. Normally, files will be opened by an appropriate application. If the
  11565. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11566. (interactive "P")
  11567. (catch 'abort
  11568. (move-marker org-open-link-marker (point))
  11569. (setq org-window-config-before-follow-link (current-window-configuration))
  11570. (org-remove-occur-highlights nil nil t)
  11571. (if (org-at-timestamp-p t)
  11572. (org-follow-timestamp-link)
  11573. (let (type path link line search (pos (point)))
  11574. (catch 'match
  11575. (save-excursion
  11576. (skip-chars-forward "^]\n\r")
  11577. (when (org-in-regexp org-bracket-link-regexp)
  11578. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11579. (while (string-match " *\n *" link)
  11580. (setq link (replace-match " " t t link)))
  11581. (setq link (org-link-expand-abbrev link))
  11582. (if (string-match org-link-re-with-space2 link)
  11583. (setq type (match-string 1 link) path (match-string 2 link))
  11584. (setq type "thisfile" path link))
  11585. (throw 'match t)))
  11586. (when (get-text-property (point) 'org-linked-text)
  11587. (setq type "thisfile"
  11588. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11589. (1+ (point)) (point))
  11590. path (buffer-substring
  11591. (previous-single-property-change pos 'org-linked-text)
  11592. (next-single-property-change pos 'org-linked-text)))
  11593. (throw 'match t))
  11594. (save-excursion
  11595. (when (or (org-in-regexp org-angle-link-re)
  11596. (org-in-regexp org-plain-link-re))
  11597. (setq type (match-string 1) path (match-string 2))
  11598. (throw 'match t)))
  11599. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11600. (setq type "tree-match"
  11601. path (match-string 1))
  11602. (throw 'match t))
  11603. (save-excursion
  11604. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11605. (setq type "tags"
  11606. path (match-string 1))
  11607. (while (string-match ":" path)
  11608. (setq path (replace-match "+" t t path)))
  11609. (throw 'match t))))
  11610. (unless path
  11611. (error "No link found"))
  11612. ;; Remove any trailing spaces in path
  11613. (if (string-match " +\\'" path)
  11614. (setq path (replace-match "" t t path)))
  11615. (cond
  11616. ((assoc type org-link-protocols)
  11617. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11618. ((equal type "mailto")
  11619. (let ((cmd (car org-link-mailto-program))
  11620. (args (cdr org-link-mailto-program)) args1
  11621. (address path) (subject "") a)
  11622. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11623. (setq address (match-string 1 path)
  11624. subject (org-link-escape (match-string 2 path))))
  11625. (while args
  11626. (cond
  11627. ((not (stringp (car args))) (push (pop args) args1))
  11628. (t (setq a (pop args))
  11629. (if (string-match "%a" a)
  11630. (setq a (replace-match address t t a)))
  11631. (if (string-match "%s" a)
  11632. (setq a (replace-match subject t t a)))
  11633. (push a args1))))
  11634. (apply cmd (nreverse args1))))
  11635. ((member type '("http" "https" "ftp" "news"))
  11636. (browse-url (concat type ":" (org-link-escape
  11637. path org-link-escape-chars-browser))))
  11638. ((member type '("message"))
  11639. (browse-url (concat type ":" path)))
  11640. ((string= type "tags")
  11641. (org-tags-view in-emacs path))
  11642. ((string= type "thisfile")
  11643. (if in-emacs
  11644. (switch-to-buffer-other-window
  11645. (org-get-buffer-for-internal-link (current-buffer)))
  11646. (org-mark-ring-push))
  11647. (let ((cmd `(org-link-search
  11648. ,path
  11649. ,(cond ((equal in-emacs '(4)) 'occur)
  11650. ((equal in-emacs '(16)) 'org-occur)
  11651. (t nil))
  11652. ,pos)))
  11653. (condition-case nil (eval cmd)
  11654. (error (progn (widen) (eval cmd))))))
  11655. ((string= type "tree-match")
  11656. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11657. ((string= type "file")
  11658. (if (string-match "::\\([0-9]+\\)\\'" path)
  11659. (setq line (string-to-number (match-string 1 path))
  11660. path (substring path 0 (match-beginning 0)))
  11661. (if (string-match "::\\(.+\\)\\'" path)
  11662. (setq search (match-string 1 path)
  11663. path (substring path 0 (match-beginning 0)))))
  11664. (if (string-match "[*?{]" (file-name-nondirectory path))
  11665. (dired path)
  11666. (org-open-file path in-emacs line search)))
  11667. ((string= type "news")
  11668. (org-follow-gnus-link path))
  11669. ((string= type "bbdb")
  11670. (org-follow-bbdb-link path))
  11671. ((string= type "info")
  11672. (org-follow-info-link path))
  11673. ((string= type "gnus")
  11674. (let (group article)
  11675. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11676. (error "Error in Gnus link"))
  11677. (setq group (match-string 1 path)
  11678. article (match-string 3 path))
  11679. (org-follow-gnus-link group article)))
  11680. ((string= type "vm")
  11681. (let (folder article)
  11682. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11683. (error "Error in VM link"))
  11684. (setq folder (match-string 1 path)
  11685. article (match-string 3 path))
  11686. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11687. (org-follow-vm-link folder article in-emacs)))
  11688. ((string= type "wl")
  11689. (let (folder article)
  11690. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11691. (error "Error in Wanderlust link"))
  11692. (setq folder (match-string 1 path)
  11693. article (match-string 3 path))
  11694. (org-follow-wl-link folder article)))
  11695. ((string= type "mhe")
  11696. (let (folder article)
  11697. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11698. (error "Error in MHE link"))
  11699. (setq folder (match-string 1 path)
  11700. article (match-string 3 path))
  11701. (org-follow-mhe-link folder article)))
  11702. ((string= type "rmail")
  11703. (let (folder article)
  11704. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11705. (error "Error in RMAIL link"))
  11706. (setq folder (match-string 1 path)
  11707. article (match-string 3 path))
  11708. (org-follow-rmail-link folder article)))
  11709. ((string= type "shell")
  11710. (let ((cmd path))
  11711. (if (or (not org-confirm-shell-link-function)
  11712. (funcall org-confirm-shell-link-function
  11713. (format "Execute \"%s\" in shell? "
  11714. (org-add-props cmd nil
  11715. 'face 'org-warning))))
  11716. (progn
  11717. (message "Executing %s" cmd)
  11718. (shell-command cmd))
  11719. (error "Abort"))))
  11720. ((string= type "elisp")
  11721. (let ((cmd path))
  11722. (if (or (not org-confirm-elisp-link-function)
  11723. (funcall org-confirm-elisp-link-function
  11724. (format "Execute \"%s\" as elisp? "
  11725. (org-add-props cmd nil
  11726. 'face 'org-warning))))
  11727. (message "%s => %s" cmd (eval (read cmd)))
  11728. (error "Abort"))))
  11729. (t
  11730. (browse-url-at-point)))))
  11731. (move-marker org-open-link-marker nil)))
  11732. ;;; File search
  11733. (defvar org-create-file-search-functions nil
  11734. "List of functions to construct the right search string for a file link.
  11735. These functions are called in turn with point at the location to
  11736. which the link should point.
  11737. A function in the hook should first test if it would like to
  11738. handle this file type, for example by checking the major-mode or
  11739. the file extension. If it decides not to handle this file, it
  11740. should just return nil to give other functions a chance. If it
  11741. does handle the file, it must return the search string to be used
  11742. when following the link. The search string will be part of the
  11743. file link, given after a double colon, and `org-open-at-point'
  11744. will automatically search for it. If special measures must be
  11745. taken to make the search successful, another function should be
  11746. added to the companion hook `org-execute-file-search-functions',
  11747. which see.
  11748. A function in this hook may also use `setq' to set the variable
  11749. `description' to provide a suggestion for the descriptive text to
  11750. be used for this link when it gets inserted into an Org-mode
  11751. buffer with \\[org-insert-link].")
  11752. (defvar org-execute-file-search-functions nil
  11753. "List of functions to execute a file search triggered by a link.
  11754. Functions added to this hook must accept a single argument, the
  11755. search string that was part of the file link, the part after the
  11756. double colon. The function must first check if it would like to
  11757. handle this search, for example by checking the major-mode or the
  11758. file extension. If it decides not to handle this search, it
  11759. should just return nil to give other functions a chance. If it
  11760. does handle the search, it must return a non-nil value to keep
  11761. other functions from trying.
  11762. Each function can access the current prefix argument through the
  11763. variable `current-prefix-argument'. Note that a single prefix is
  11764. used to force opening a link in Emacs, so it may be good to only
  11765. use a numeric or double prefix to guide the search function.
  11766. In case this is needed, a function in this hook can also restore
  11767. the window configuration before `org-open-at-point' was called using:
  11768. (set-window-configuration org-window-config-before-follow-link)")
  11769. (defun org-link-search (s &optional type avoid-pos)
  11770. "Search for a link search option.
  11771. If S is surrounded by forward slashes, it is interpreted as a
  11772. regular expression. In org-mode files, this will create an `org-occur'
  11773. sparse tree. In ordinary files, `occur' will be used to list matches.
  11774. If the current buffer is in `dired-mode', grep will be used to search
  11775. in all files. If AVOID-POS is given, ignore matches near that position."
  11776. (let ((case-fold-search t)
  11777. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11778. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11779. (append '(("") (" ") ("\t") ("\n"))
  11780. org-emphasis-alist)
  11781. "\\|") "\\)"))
  11782. (pos (point))
  11783. (pre "") (post "")
  11784. words re0 re1 re2 re3 re4 re5 re2a reall)
  11785. (cond
  11786. ;; First check if there are any special
  11787. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11788. ;; Now try the builtin stuff
  11789. ((save-excursion
  11790. (goto-char (point-min))
  11791. (and
  11792. (re-search-forward
  11793. (concat "<<" (regexp-quote s0) ">>") nil t)
  11794. (setq pos (match-beginning 0))))
  11795. ;; There is an exact target for this
  11796. (goto-char pos))
  11797. ((string-match "^/\\(.*\\)/$" s)
  11798. ;; A regular expression
  11799. (cond
  11800. ((org-mode-p)
  11801. (org-occur (match-string 1 s)))
  11802. ;;((eq major-mode 'dired-mode)
  11803. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11804. (t (org-do-occur (match-string 1 s)))))
  11805. (t
  11806. ;; A normal search strings
  11807. (when (equal (string-to-char s) ?*)
  11808. ;; Anchor on headlines, post may include tags.
  11809. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11810. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11811. s (substring s 1)))
  11812. (remove-text-properties
  11813. 0 (length s)
  11814. '(face nil mouse-face nil keymap nil fontified nil) s)
  11815. ;; Make a series of regular expressions to find a match
  11816. (setq words (org-split-string s "[ \n\r\t]+")
  11817. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11818. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11819. "\\)" markers)
  11820. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11821. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11822. re1 (concat pre re2 post)
  11823. re3 (concat pre re4 post)
  11824. re5 (concat pre ".*" re4)
  11825. re2 (concat pre re2)
  11826. re2a (concat pre re2a)
  11827. re4 (concat pre re4)
  11828. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11829. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11830. re5 "\\)"
  11831. ))
  11832. (cond
  11833. ((eq type 'org-occur) (org-occur reall))
  11834. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11835. (t (goto-char (point-min))
  11836. (if (or (org-search-not-self 1 re0 nil t)
  11837. (org-search-not-self 1 re1 nil t)
  11838. (org-search-not-self 1 re2 nil t)
  11839. (org-search-not-self 1 re2a nil t)
  11840. (org-search-not-self 1 re3 nil t)
  11841. (org-search-not-self 1 re4 nil t)
  11842. (org-search-not-self 1 re5 nil t)
  11843. )
  11844. (goto-char (match-beginning 1))
  11845. (goto-char pos)
  11846. (error "No match")))))
  11847. (t
  11848. ;; Normal string-search
  11849. (goto-char (point-min))
  11850. (if (search-forward s nil t)
  11851. (goto-char (match-beginning 0))
  11852. (error "No match"))))
  11853. (and (org-mode-p) (org-show-context 'link-search))))
  11854. (defun org-search-not-self (group &rest args)
  11855. "Execute `re-search-forward', but only accept matches that do not
  11856. enclose the position of `org-open-link-marker'."
  11857. (let ((m org-open-link-marker))
  11858. (catch 'exit
  11859. (while (apply 're-search-forward args)
  11860. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11861. (goto-char (match-end group))
  11862. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11863. (> (match-beginning 0) (marker-position m))
  11864. (< (match-end 0) (marker-position m)))
  11865. (save-match-data
  11866. (or (not (org-in-regexp
  11867. org-bracket-link-analytic-regexp 1))
  11868. (not (match-end 4)) ; no description
  11869. (and (<= (match-beginning 4) (point))
  11870. (>= (match-end 4) (point))))))
  11871. (throw 'exit (point))))))))
  11872. (defun org-get-buffer-for-internal-link (buffer)
  11873. "Return a buffer to be used for displaying the link target of internal links."
  11874. (cond
  11875. ((not org-display-internal-link-with-indirect-buffer)
  11876. buffer)
  11877. ((string-match "(Clone)$" (buffer-name buffer))
  11878. (message "Buffer is already a clone, not making another one")
  11879. ;; we also do not modify visibility in this case
  11880. buffer)
  11881. (t ; make a new indirect buffer for displaying the link
  11882. (let* ((bn (buffer-name buffer))
  11883. (ibn (concat bn "(Clone)"))
  11884. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11885. (with-current-buffer ib (org-overview))
  11886. ib))))
  11887. (defun org-do-occur (regexp &optional cleanup)
  11888. "Call the Emacs command `occur'.
  11889. If CLEANUP is non-nil, remove the printout of the regular expression
  11890. in the *Occur* buffer. This is useful if the regex is long and not useful
  11891. to read."
  11892. (occur regexp)
  11893. (when cleanup
  11894. (let ((cwin (selected-window)) win beg end)
  11895. (when (setq win (get-buffer-window "*Occur*"))
  11896. (select-window win))
  11897. (goto-char (point-min))
  11898. (when (re-search-forward "match[a-z]+" nil t)
  11899. (setq beg (match-end 0))
  11900. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11901. (setq end (1- (match-beginning 0)))))
  11902. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11903. (goto-char (point-min))
  11904. (select-window cwin))))
  11905. ;;; The mark ring for links jumps
  11906. (defvar org-mark-ring nil
  11907. "Mark ring for positions before jumps in Org-mode.")
  11908. (defvar org-mark-ring-last-goto nil
  11909. "Last position in the mark ring used to go back.")
  11910. ;; Fill and close the ring
  11911. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11912. (loop for i from 1 to org-mark-ring-length do
  11913. (push (make-marker) org-mark-ring))
  11914. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11915. org-mark-ring)
  11916. (defun org-mark-ring-push (&optional pos buffer)
  11917. "Put the current position or POS into the mark ring and rotate it."
  11918. (interactive)
  11919. (setq pos (or pos (point)))
  11920. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11921. (move-marker (car org-mark-ring)
  11922. (or pos (point))
  11923. (or buffer (current-buffer)))
  11924. (message "%s"
  11925. (substitute-command-keys
  11926. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11927. (defun org-mark-ring-goto (&optional n)
  11928. "Jump to the previous position in the mark ring.
  11929. With prefix arg N, jump back that many stored positions. When
  11930. called several times in succession, walk through the entire ring.
  11931. Org-mode commands jumping to a different position in the current file,
  11932. or to another Org-mode file, automatically push the old position
  11933. onto the ring."
  11934. (interactive "p")
  11935. (let (p m)
  11936. (if (eq last-command this-command)
  11937. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11938. (setq p org-mark-ring))
  11939. (setq org-mark-ring-last-goto p)
  11940. (setq m (car p))
  11941. (switch-to-buffer (marker-buffer m))
  11942. (goto-char m)
  11943. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11944. (defun org-remove-angle-brackets (s)
  11945. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11946. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11947. s)
  11948. (defun org-add-angle-brackets (s)
  11949. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11950. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11951. s)
  11952. ;;; Following specific links
  11953. (defun org-follow-timestamp-link ()
  11954. (cond
  11955. ((org-at-date-range-p t)
  11956. (let ((org-agenda-start-on-weekday)
  11957. (t1 (match-string 1))
  11958. (t2 (match-string 2)))
  11959. (setq t1 (time-to-days (org-time-string-to-time t1))
  11960. t2 (time-to-days (org-time-string-to-time t2)))
  11961. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11962. ((org-at-timestamp-p t)
  11963. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11964. (substring (match-string 1) 0 10)))
  11965. 1))
  11966. (t (error "This should not happen"))))
  11967. (defun org-follow-bbdb-link (name)
  11968. "Follow a BBDB link to NAME."
  11969. (require 'bbdb)
  11970. (let ((inhibit-redisplay (not debug-on-error))
  11971. (bbdb-electric-p nil))
  11972. (catch 'exit
  11973. ;; Exact match on name
  11974. (bbdb-name (concat "\\`" name "\\'") nil)
  11975. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11976. ;; Exact match on name
  11977. (bbdb-company (concat "\\`" name "\\'") nil)
  11978. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11979. ;; Partial match on name
  11980. (bbdb-name name nil)
  11981. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11982. ;; Partial match on company
  11983. (bbdb-company name nil)
  11984. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11985. ;; General match including network address and notes
  11986. (bbdb name nil)
  11987. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11988. (delete-window (get-buffer-window "*BBDB*"))
  11989. (error "No matching BBDB record")))))
  11990. (defun org-follow-info-link (name)
  11991. "Follow an info file & node link to NAME."
  11992. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11993. (string-match "\\(.*\\)" name))
  11994. (progn
  11995. (require 'info)
  11996. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11997. (Info-find-node (match-string 1 name) (match-string 2 name))
  11998. (Info-find-node (match-string 1 name) "Top")))
  11999. (message "Could not open: %s" name)))
  12000. (defun org-follow-gnus-link (&optional group article)
  12001. "Follow a Gnus link to GROUP and ARTICLE."
  12002. (require 'gnus)
  12003. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12004. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12005. (cond ((and group article)
  12006. (gnus-group-read-group 1 nil group)
  12007. (gnus-summary-goto-article (string-to-number article) nil t))
  12008. (group (gnus-group-jump-to-group group))))
  12009. (defun org-follow-vm-link (&optional folder article readonly)
  12010. "Follow a VM link to FOLDER and ARTICLE."
  12011. (require 'vm)
  12012. (setq article (org-add-angle-brackets article))
  12013. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12014. ;; ange-ftp or efs or tramp access
  12015. (let ((user (or (match-string 1 folder) (user-login-name)))
  12016. (host (match-string 2 folder))
  12017. (file (match-string 3 folder)))
  12018. (cond
  12019. ((featurep 'tramp)
  12020. ;; use tramp to access the file
  12021. (if (featurep 'xemacs)
  12022. (setq folder (format "[%s@%s]%s" user host file))
  12023. (setq folder (format "/%s@%s:%s" user host file))))
  12024. (t
  12025. ;; use ange-ftp or efs
  12026. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12027. (setq folder (format "/%s@%s:%s" user host file))))))
  12028. (when folder
  12029. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12030. (sit-for 0.1)
  12031. (when article
  12032. (vm-select-folder-buffer)
  12033. (widen)
  12034. (let ((case-fold-search t))
  12035. (goto-char (point-min))
  12036. (if (not (re-search-forward
  12037. (concat "^" "message-id: *" (regexp-quote article))))
  12038. (error "Could not find the specified message in this folder"))
  12039. (vm-isearch-update)
  12040. (vm-isearch-narrow)
  12041. (vm-beginning-of-message)
  12042. (vm-summarize)))))
  12043. (defun org-follow-wl-link (folder article)
  12044. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12045. (if (and (string= folder "%")
  12046. article
  12047. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12048. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12049. ;; Thus, we recompose folder and article ids.
  12050. (setq folder (format "%s#%s" folder (match-string 1 article))
  12051. article (match-string 3 article)))
  12052. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12053. (error "No such folder: %s" folder))
  12054. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12055. (and article
  12056. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12057. (wl-summary-redisplay)))
  12058. (defun org-follow-rmail-link (folder article)
  12059. "Follow an RMAIL link to FOLDER and ARTICLE."
  12060. (setq article (org-add-angle-brackets article))
  12061. (let (message-number)
  12062. (save-excursion
  12063. (save-window-excursion
  12064. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12065. (setq message-number
  12066. (save-restriction
  12067. (widen)
  12068. (goto-char (point-max))
  12069. (if (re-search-backward
  12070. (concat "^Message-ID:\\s-+" (regexp-quote
  12071. (or article "")))
  12072. nil t)
  12073. (rmail-what-message))))))
  12074. (if message-number
  12075. (progn
  12076. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12077. (rmail-show-message message-number)
  12078. message-number)
  12079. (error "Message not found"))))
  12080. ;;; mh-e integration based on planner-mode
  12081. (defun org-mhe-get-message-real-folder ()
  12082. "Return the name of the current message real folder, so if you use
  12083. sequences, it will now work."
  12084. (save-excursion
  12085. (let* ((folder
  12086. (if (equal major-mode 'mh-folder-mode)
  12087. mh-current-folder
  12088. ;; Refer to the show buffer
  12089. mh-show-folder-buffer))
  12090. (end-index
  12091. (if (boundp 'mh-index-folder)
  12092. (min (length mh-index-folder) (length folder))))
  12093. )
  12094. ;; a simple test on mh-index-data does not work, because
  12095. ;; mh-index-data is always nil in a show buffer.
  12096. (if (and (boundp 'mh-index-folder)
  12097. (string= mh-index-folder (substring folder 0 end-index)))
  12098. (if (equal major-mode 'mh-show-mode)
  12099. (save-window-excursion
  12100. (let (pop-up-frames)
  12101. (when (buffer-live-p (get-buffer folder))
  12102. (progn
  12103. (pop-to-buffer folder)
  12104. (org-mhe-get-message-folder-from-index)
  12105. )
  12106. )))
  12107. (org-mhe-get-message-folder-from-index)
  12108. )
  12109. folder
  12110. )
  12111. )))
  12112. (defun org-mhe-get-message-folder-from-index ()
  12113. "Returns the name of the message folder in a index folder buffer."
  12114. (save-excursion
  12115. (mh-index-previous-folder)
  12116. (re-search-forward "^\\(+.*\\)$" nil t)
  12117. (message "%s" (match-string 1))))
  12118. (defun org-mhe-get-message-folder ()
  12119. "Return the name of the current message folder. Be careful if you
  12120. use sequences."
  12121. (save-excursion
  12122. (if (equal major-mode 'mh-folder-mode)
  12123. mh-current-folder
  12124. ;; Refer to the show buffer
  12125. mh-show-folder-buffer)))
  12126. (defun org-mhe-get-message-num ()
  12127. "Return the number of the current message. Be careful if you
  12128. use sequences."
  12129. (save-excursion
  12130. (if (equal major-mode 'mh-folder-mode)
  12131. (mh-get-msg-num nil)
  12132. ;; Refer to the show buffer
  12133. (mh-show-buffer-message-number))))
  12134. (defun org-mhe-get-header (header)
  12135. "Return a header of the message in folder mode. This will create a
  12136. show buffer for the corresponding message. If you have a more clever
  12137. idea..."
  12138. (let* ((folder (org-mhe-get-message-folder))
  12139. (num (org-mhe-get-message-num))
  12140. (buffer (get-buffer-create (concat "show-" folder)))
  12141. (header-field))
  12142. (with-current-buffer buffer
  12143. (mh-display-msg num folder)
  12144. (if (equal major-mode 'mh-folder-mode)
  12145. (mh-header-display)
  12146. (mh-show-header-display))
  12147. (set-buffer buffer)
  12148. (setq header-field (mh-get-header-field header))
  12149. (if (equal major-mode 'mh-folder-mode)
  12150. (mh-show)
  12151. (mh-show-show))
  12152. header-field)))
  12153. (defun org-follow-mhe-link (folder article)
  12154. "Follow an MHE link to FOLDER and ARTICLE.
  12155. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12156. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12157. ARTICLE is searched in all folders. Indexed searches (swish++,
  12158. namazu, and others supported by MH-E) will always search in all
  12159. folders."
  12160. (require 'mh-e)
  12161. (require 'mh-search)
  12162. (require 'mh-utils)
  12163. (mh-find-path)
  12164. (if (not article)
  12165. (mh-visit-folder (mh-normalize-folder-name folder))
  12166. (setq article (org-add-angle-brackets article))
  12167. (mh-search-choose)
  12168. (if (equal mh-searcher 'pick)
  12169. (progn
  12170. (mh-search folder (list "--message-id" article))
  12171. (when (and org-mhe-search-all-folders
  12172. (not (org-mhe-get-message-real-folder)))
  12173. (kill-this-buffer)
  12174. (mh-search "+" (list "--message-id" article))))
  12175. (mh-search "+" article))
  12176. (if (org-mhe-get-message-real-folder)
  12177. (mh-show-msg 1)
  12178. (kill-this-buffer)
  12179. (error "Message not found"))))
  12180. ;;; BibTeX links
  12181. ;; Use the custom search meachnism to construct and use search strings for
  12182. ;; file links to BibTeX database entries.
  12183. (defun org-create-file-search-in-bibtex ()
  12184. "Create the search string and description for a BibTeX database entry."
  12185. (when (eq major-mode 'bibtex-mode)
  12186. ;; yes, we want to construct this search string.
  12187. ;; Make a good description for this entry, using names, year and the title
  12188. ;; Put it into the `description' variable which is dynamically scoped.
  12189. (let ((bibtex-autokey-names 1)
  12190. (bibtex-autokey-names-stretch 1)
  12191. (bibtex-autokey-name-case-convert-function 'identity)
  12192. (bibtex-autokey-name-separator " & ")
  12193. (bibtex-autokey-additional-names " et al.")
  12194. (bibtex-autokey-year-length 4)
  12195. (bibtex-autokey-name-year-separator " ")
  12196. (bibtex-autokey-titlewords 3)
  12197. (bibtex-autokey-titleword-separator " ")
  12198. (bibtex-autokey-titleword-case-convert-function 'identity)
  12199. (bibtex-autokey-titleword-length 'infty)
  12200. (bibtex-autokey-year-title-separator ": "))
  12201. (setq description (bibtex-generate-autokey)))
  12202. ;; Now parse the entry, get the key and return it.
  12203. (save-excursion
  12204. (bibtex-beginning-of-entry)
  12205. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12206. (defun org-execute-file-search-in-bibtex (s)
  12207. "Find the link search string S as a key for a database entry."
  12208. (when (eq major-mode 'bibtex-mode)
  12209. ;; Yes, we want to do the search in this file.
  12210. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12211. (goto-char (point-min))
  12212. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12213. (regexp-quote s) "[ \t\n]*,") nil t)
  12214. (goto-char (match-beginning 0)))
  12215. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12216. ;; Use double prefix to indicate that any web link should be browsed
  12217. (let ((b (current-buffer)) (p (point)))
  12218. ;; Restore the window configuration because we just use the web link
  12219. (set-window-configuration org-window-config-before-follow-link)
  12220. (save-excursion (set-buffer b) (goto-char p)
  12221. (bibtex-url)))
  12222. (recenter 0)) ; Move entry start to beginning of window
  12223. ;; return t to indicate that the search is done.
  12224. t))
  12225. ;; Finally add the functions to the right hooks.
  12226. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12227. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12228. ;; end of Bibtex link setup
  12229. ;;; Following file links
  12230. (defun org-open-file (path &optional in-emacs line search)
  12231. "Open the file at PATH.
  12232. First, this expands any special file name abbreviations. Then the
  12233. configuration variable `org-file-apps' is checked if it contains an
  12234. entry for this file type, and if yes, the corresponding command is launched.
  12235. If no application is found, Emacs simply visits the file.
  12236. With optional argument IN-EMACS, Emacs will visit the file.
  12237. Optional LINE specifies a line to go to, optional SEARCH a string to
  12238. search for. If LINE or SEARCH is given, the file will always be
  12239. opened in Emacs.
  12240. If the file does not exist, an error is thrown."
  12241. (setq in-emacs (or in-emacs line search))
  12242. (let* ((file (if (equal path "")
  12243. buffer-file-name
  12244. (substitute-in-file-name (expand-file-name path))))
  12245. (apps (append org-file-apps (org-default-apps)))
  12246. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12247. (dirp (if remp nil (file-directory-p file)))
  12248. (dfile (downcase file))
  12249. (old-buffer (current-buffer))
  12250. (old-pos (point))
  12251. (old-mode major-mode)
  12252. ext cmd)
  12253. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12254. (setq ext (match-string 1 dfile))
  12255. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12256. (setq ext (match-string 1 dfile))))
  12257. (if in-emacs
  12258. (setq cmd 'emacs)
  12259. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12260. (and dirp (cdr (assoc 'directory apps)))
  12261. (cdr (assoc ext apps))
  12262. (cdr (assoc t apps)))))
  12263. (when (eq cmd 'mailcap)
  12264. (require 'mailcap)
  12265. (mailcap-parse-mailcaps)
  12266. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12267. (command (mailcap-mime-info mime-type)))
  12268. (if (stringp command)
  12269. (setq cmd command)
  12270. (setq cmd 'emacs))))
  12271. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12272. (not (file-exists-p file))
  12273. (not org-open-non-existing-files))
  12274. (error "No such file: %s" file))
  12275. (cond
  12276. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12277. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12278. (while (string-match "['\"]%s['\"]" cmd)
  12279. (setq cmd (replace-match "%s" t t cmd)))
  12280. (while (string-match "%s" cmd)
  12281. (setq cmd (replace-match
  12282. (save-match-data (shell-quote-argument file))
  12283. t t cmd)))
  12284. (save-window-excursion
  12285. (start-process-shell-command cmd nil cmd)))
  12286. ((or (stringp cmd)
  12287. (eq cmd 'emacs))
  12288. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12289. (widen)
  12290. (if line (goto-line line)
  12291. (if search (org-link-search search))))
  12292. ((consp cmd)
  12293. (eval cmd))
  12294. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12295. (and (org-mode-p) (eq old-mode 'org-mode)
  12296. (or (not (equal old-buffer (current-buffer)))
  12297. (not (equal old-pos (point))))
  12298. (org-mark-ring-push old-pos old-buffer))))
  12299. (defun org-default-apps ()
  12300. "Return the default applications for this operating system."
  12301. (cond
  12302. ((eq system-type 'darwin)
  12303. org-file-apps-defaults-macosx)
  12304. ((eq system-type 'windows-nt)
  12305. org-file-apps-defaults-windowsnt)
  12306. (t org-file-apps-defaults-gnu)))
  12307. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12308. (defun org-file-remote-p (file)
  12309. "Test whether FILE specifies a location on a remote system.
  12310. Return non-nil if the location is indeed remote.
  12311. For example, the filename \"/user@host:/foo\" specifies a location
  12312. on the system \"/user@host:\"."
  12313. (cond ((fboundp 'file-remote-p)
  12314. (file-remote-p file))
  12315. ((fboundp 'tramp-handle-file-remote-p)
  12316. (tramp-handle-file-remote-p file))
  12317. ((and (boundp 'ange-ftp-name-format)
  12318. (string-match (car ange-ftp-name-format) file))
  12319. t)
  12320. (t nil)))
  12321. ;;;; Hooks for remember.el, and refiling
  12322. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12323. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12324. ;;;###autoload
  12325. (defun org-remember-insinuate ()
  12326. "Setup remember.el for use wiht Org-mode."
  12327. (require 'remember)
  12328. (setq remember-annotation-functions '(org-remember-annotation))
  12329. (setq remember-handler-functions '(org-remember-handler))
  12330. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12331. ;;;###autoload
  12332. (defun org-remember-annotation ()
  12333. "Return a link to the current location as an annotation for remember.el.
  12334. If you are using Org-mode files as target for data storage with
  12335. remember.el, then the annotations should include a link compatible with the
  12336. conventions in Org-mode. This function returns such a link."
  12337. (org-store-link nil))
  12338. (defconst org-remember-help
  12339. "Select a destination location for the note.
  12340. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12341. RET on headline -> Store as sublevel entry to current headline
  12342. RET at beg-of-buf -> Append to file as level 2 headline
  12343. <left>/<right> -> before/after current headline, same headings level")
  12344. (defvar org-remember-previous-location nil)
  12345. (defvar org-force-remember-template-char) ;; dynamically scoped
  12346. (defun org-select-remember-template (&optional use-char)
  12347. (when org-remember-templates
  12348. (let* ((templates (mapcar (lambda (x)
  12349. (if (stringp (car x))
  12350. (append (list (nth 1 x) (car x)) (cddr x))
  12351. (append (list (car x) "") (cdr x))))
  12352. org-remember-templates))
  12353. (char (or use-char
  12354. (cond
  12355. ((= (length templates) 1)
  12356. (caar templates))
  12357. ((and (boundp 'org-force-remember-template-char)
  12358. org-force-remember-template-char)
  12359. (if (stringp org-force-remember-template-char)
  12360. (string-to-char org-force-remember-template-char)
  12361. org-force-remember-template-char))
  12362. (t
  12363. (message "Select template: %s"
  12364. (mapconcat
  12365. (lambda (x)
  12366. (cond
  12367. ((not (string-match "\\S-" (nth 1 x)))
  12368. (format "[%c]" (car x)))
  12369. ((equal (downcase (car x))
  12370. (downcase (aref (nth 1 x) 0)))
  12371. (format "[%c]%s" (car x)
  12372. (substring (nth 1 x) 1)))
  12373. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12374. templates " "))
  12375. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12376. (when (equal char0 ?\C-g)
  12377. (jump-to-register remember-register)
  12378. (kill-buffer remember-buffer))
  12379. char0))))))
  12380. (cddr (assoc char templates)))))
  12381. (defvar x-last-selected-text)
  12382. (defvar x-last-selected-text-primary)
  12383. ;;;###autoload
  12384. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12385. "Initialize *remember* buffer with template, invoke `org-mode'.
  12386. This function should be placed into `remember-mode-hook' and in fact requires
  12387. to be run from that hook to function properly."
  12388. (if org-remember-templates
  12389. (let* ((entry (org-select-remember-template use-char))
  12390. (tpl (car entry))
  12391. (plist-p (if org-store-link-plist t nil))
  12392. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12393. (string-match "\\S-" (nth 1 entry)))
  12394. (nth 1 entry)
  12395. org-default-notes-file))
  12396. (headline (nth 2 entry))
  12397. (v-c (or (and (eq window-system 'x)
  12398. (fboundp 'x-cut-buffer-or-selection-value)
  12399. (x-cut-buffer-or-selection-value))
  12400. (org-bound-and-true-p x-last-selected-text)
  12401. (org-bound-and-true-p x-last-selected-text-primary)
  12402. (and (> (length kill-ring) 0) (current-kill 0))))
  12403. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12404. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12405. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12406. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12407. ;; `initial' and `annotation' are bound in `remember'
  12408. (v-i (if (boundp 'initial) initial))
  12409. (v-a (if (and (boundp 'annotation) annotation)
  12410. (if (equal annotation "[[]]") "" annotation)
  12411. ""))
  12412. (v-A (if (and v-a
  12413. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12414. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12415. v-a))
  12416. (v-n user-full-name)
  12417. (org-startup-folded nil)
  12418. org-time-was-given org-end-time-was-given x
  12419. prompt completions char time pos default histvar)
  12420. (setq org-store-link-plist
  12421. (append (list :annotation v-a :initial v-i)
  12422. org-store-link-plist))
  12423. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12424. (erase-buffer)
  12425. (insert (substitute-command-keys
  12426. (format
  12427. "## Filing location: Select interactively, default, or last used:
  12428. ## %s to select file and header location interactively.
  12429. ## %s \"%s\" -> \"* %s\"
  12430. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12431. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12432. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12433. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12434. (abbreviate-file-name (or file org-default-notes-file))
  12435. (or headline "")
  12436. (or (car org-remember-previous-location) "???")
  12437. (or (cdr org-remember-previous-location) "???"))))
  12438. (insert tpl) (goto-char (point-min))
  12439. ;; Simple %-escapes
  12440. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12441. (when (and initial (equal (match-string 0) "%i"))
  12442. (save-match-data
  12443. (let* ((lead (buffer-substring
  12444. (point-at-bol) (match-beginning 0))))
  12445. (setq v-i (mapconcat 'identity
  12446. (org-split-string initial "\n")
  12447. (concat "\n" lead))))))
  12448. (replace-match
  12449. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12450. t t))
  12451. ;; %[] Insert contents of a file.
  12452. (goto-char (point-min))
  12453. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12454. (let ((start (match-beginning 0))
  12455. (end (match-end 0))
  12456. (filename (expand-file-name (match-string 1))))
  12457. (goto-char start)
  12458. (delete-region start end)
  12459. (condition-case error
  12460. (insert-file-contents filename)
  12461. (error (insert (format "%%![Couldn't insert %s: %s]"
  12462. filename error))))))
  12463. ;; %() embedded elisp
  12464. (goto-char (point-min))
  12465. (while (re-search-forward "%\\((.+)\\)" nil t)
  12466. (goto-char (match-beginning 0))
  12467. (let ((template-start (point)))
  12468. (forward-char 1)
  12469. (let ((result
  12470. (condition-case error
  12471. (eval (read (current-buffer)))
  12472. (error (format "%%![Error: %s]" error)))))
  12473. (delete-region template-start (point))
  12474. (insert result))))
  12475. ;; From the property list
  12476. (when plist-p
  12477. (goto-char (point-min))
  12478. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12479. (and (setq x (or (plist-get org-store-link-plist
  12480. (intern (match-string 1))) ""))
  12481. (replace-match x t t))))
  12482. ;; Turn on org-mode in the remember buffer, set local variables
  12483. (org-mode)
  12484. (org-set-local 'org-finish-function 'org-remember-finalize)
  12485. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12486. (org-set-local 'org-default-notes-file file))
  12487. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12488. (org-set-local 'org-remember-default-headline headline))
  12489. ;; Interactive template entries
  12490. (goto-char (point-min))
  12491. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12492. (setq char (if (match-end 3) (match-string 3))
  12493. prompt (if (match-end 2) (match-string 2)))
  12494. (goto-char (match-beginning 0))
  12495. (replace-match "")
  12496. (setq completions nil default nil)
  12497. (when prompt
  12498. (setq completions (org-split-string prompt "|")
  12499. prompt (pop completions)
  12500. default (car completions)
  12501. histvar (intern (concat
  12502. "org-remember-template-prompt-history::"
  12503. (or prompt "")))
  12504. completions (mapcar 'list completions)))
  12505. (cond
  12506. ((member char '("G" "g"))
  12507. (let* ((org-last-tags-completion-table
  12508. (org-global-tags-completion-table
  12509. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12510. (org-add-colon-after-tag-completion t)
  12511. (ins (completing-read
  12512. (if prompt (concat prompt ": ") "Tags: ")
  12513. 'org-tags-completion-function nil nil nil
  12514. 'org-tags-history)))
  12515. (setq ins (mapconcat 'identity
  12516. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12517. ":"))
  12518. (when (string-match "\\S-" ins)
  12519. (or (equal (char-before) ?:) (insert ":"))
  12520. (insert ins)
  12521. (or (equal (char-after) ?:) (insert ":")))))
  12522. (char
  12523. (setq org-time-was-given (equal (upcase char) char))
  12524. (setq time (org-read-date (equal (upcase char) "U") t nil
  12525. prompt))
  12526. (org-insert-time-stamp time org-time-was-given
  12527. (member char '("u" "U"))
  12528. nil nil (list org-end-time-was-given)))
  12529. (t
  12530. (insert (org-completing-read
  12531. (concat (if prompt prompt "Enter string")
  12532. (if default (concat " [" default "]"))
  12533. ": ")
  12534. completions nil nil nil histvar default)))))
  12535. (goto-char (point-min))
  12536. (if (re-search-forward "%\\?" nil t)
  12537. (replace-match "")
  12538. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12539. (org-mode)
  12540. (org-set-local 'org-finish-function 'org-remember-finalize))
  12541. (when (save-excursion
  12542. (goto-char (point-min))
  12543. (re-search-forward "%!" nil t))
  12544. (replace-match "")
  12545. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12546. (defun org-remember-finish-immediately ()
  12547. "File remember note immediately.
  12548. This should be run in `post-command-hook' and will remove itself
  12549. from that hook."
  12550. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12551. (when org-finish-function
  12552. (funcall org-finish-function)))
  12553. (defvar org-clock-marker) ; Defined below
  12554. (defun org-remember-finalize ()
  12555. "Finalize the remember process."
  12556. (unless (fboundp 'remember-finalize)
  12557. (defalias 'remember-finalize 'remember-buffer))
  12558. (when (and org-clock-marker
  12559. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12560. ;; FIXME: test this, this is w/o notetaking!
  12561. (let (org-log-note-clock-out) (org-clock-out)))
  12562. (when buffer-file-name
  12563. (save-buffer)
  12564. (setq buffer-file-name nil))
  12565. (remember-finalize))
  12566. ;;;###autoload
  12567. (defun org-remember (&optional goto org-force-remember-template-char)
  12568. "Call `remember'. If this is already a remember buffer, re-apply template.
  12569. If there is an active region, make sure remember uses it as initial content
  12570. of the remember buffer.
  12571. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12572. anything, just go to the file/headline where the selected template usually
  12573. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12574. note stored by remember.
  12575. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12576. associated with a template in `org-remember-templates'."
  12577. (interactive "P")
  12578. (cond
  12579. ((equal goto '(4)) (org-go-to-remember-target))
  12580. ((equal goto '(16)) (org-remember-goto-last-stored))
  12581. (t
  12582. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12583. (progn
  12584. (when (< (length org-remember-templates) 2)
  12585. (error "No other template available"))
  12586. (erase-buffer)
  12587. (let ((annotation (plist-get org-store-link-plist :annotation))
  12588. (initial (plist-get org-store-link-plist :initial)))
  12589. (org-remember-apply-template))
  12590. (message "Press C-c C-c to remember data"))
  12591. (if (org-region-active-p)
  12592. (remember (buffer-substring (point) (mark)))
  12593. (call-interactively 'remember))))))
  12594. (defun org-remember-goto-last-stored ()
  12595. "Go to the location where the last remember note was stored."
  12596. (interactive)
  12597. (bookmark-jump "org-remember-last-stored")
  12598. (message "This is the last note stored by remember"))
  12599. (defun org-go-to-remember-target (&optional template-key)
  12600. "Go to the target location of a remember template.
  12601. The user is queried for the template."
  12602. (interactive)
  12603. (let* ((entry (org-select-remember-template template-key))
  12604. (file (nth 1 entry))
  12605. (heading (nth 2 entry))
  12606. visiting)
  12607. (unless (and file (stringp file) (string-match "\\S-" file))
  12608. (setq file org-default-notes-file))
  12609. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12610. (setq heading org-remember-default-headline))
  12611. (setq visiting (org-find-base-buffer-visiting file))
  12612. (if (not visiting) (find-file-noselect file))
  12613. (switch-to-buffer (or visiting (get-file-buffer file)))
  12614. (widen)
  12615. (goto-char (point-min))
  12616. (if (re-search-forward
  12617. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12618. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12619. nil t)
  12620. (goto-char (match-beginning 0))
  12621. (error "Target headline not found: %s" heading))))
  12622. (defvar org-note-abort nil) ; dynamically scoped
  12623. ;;;###autoload
  12624. (defun org-remember-handler ()
  12625. "Store stuff from remember.el into an org file.
  12626. First prompts for an org file. If the user just presses return, the value
  12627. of `org-default-notes-file' is used.
  12628. Then the command offers the headings tree of the selected file in order to
  12629. file the text at a specific location.
  12630. You can either immediately press RET to get the note appended to the
  12631. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12632. find a better place. Then press RET or <left> or <right> in insert the note.
  12633. Key Cursor position Note gets inserted
  12634. -----------------------------------------------------------------------------
  12635. RET buffer-start as level 1 heading at end of file
  12636. RET on headline as sublevel of the heading at cursor
  12637. RET no heading at cursor position, level taken from context.
  12638. Or use prefix arg to specify level manually.
  12639. <left> on headline as same level, before current heading
  12640. <right> on headline as same level, after current heading
  12641. So the fastest way to store the note is to press RET RET to append it to
  12642. the default file. This way your current train of thought is not
  12643. interrupted, in accordance with the principles of remember.el.
  12644. You can also get the fast execution without prompting by using
  12645. C-u C-c C-c to exit the remember buffer. See also the variable
  12646. `org-remember-store-without-prompt'.
  12647. Before being stored away, the function ensures that the text has a
  12648. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12649. is constructed from the current date and some additional data.
  12650. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12651. also indented so that it starts in the same column as the headline
  12652. \(i.e. after the stars).
  12653. See also the variable `org-reverse-note-order'."
  12654. (goto-char (point-min))
  12655. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12656. (replace-match ""))
  12657. (goto-char (point-max))
  12658. (beginning-of-line 1)
  12659. (while (looking-at "[ \t]*$\\|##.*")
  12660. (delete-region (1- (point)) (point-max))
  12661. (beginning-of-line 1))
  12662. (catch 'quit
  12663. (if org-note-abort (throw 'quit nil))
  12664. (let* ((txt (buffer-substring (point-min) (point-max)))
  12665. (fastp (org-xor (equal current-prefix-arg '(4))
  12666. org-remember-store-without-prompt))
  12667. (file (cond
  12668. (fastp org-default-notes-file)
  12669. ((and (eq org-remember-interactive-interface 'refile)
  12670. org-refile-targets)
  12671. org-default-notes-file)
  12672. ((not (and (equal current-prefix-arg '(16))
  12673. org-remember-previous-location))
  12674. (org-get-org-file))))
  12675. (heading org-remember-default-headline)
  12676. (visiting (and file (org-find-base-buffer-visiting file)))
  12677. (org-startup-folded nil)
  12678. (org-startup-align-all-tables nil)
  12679. (org-goto-start-pos 1)
  12680. spos exitcmd level indent reversed)
  12681. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12682. (setq file (car org-remember-previous-location)
  12683. heading (cdr org-remember-previous-location)
  12684. fastp t))
  12685. (setq current-prefix-arg nil)
  12686. (if (string-match "[ \t\n]+\\'" txt)
  12687. (setq txt (replace-match "" t t txt)))
  12688. ;; Modify text so that it becomes a nice subtree which can be inserted
  12689. ;; into an org tree.
  12690. (let* ((lines (split-string txt "\n"))
  12691. first)
  12692. (setq first (car lines) lines (cdr lines))
  12693. (if (string-match "^\\*+ " first)
  12694. ;; Is already a headline
  12695. (setq indent nil)
  12696. ;; We need to add a headline: Use time and first buffer line
  12697. (setq lines (cons first lines)
  12698. first (concat "* " (current-time-string)
  12699. " (" (remember-buffer-desc) ")")
  12700. indent " "))
  12701. (if (and org-adapt-indentation indent)
  12702. (setq lines (mapcar
  12703. (lambda (x)
  12704. (if (string-match "\\S-" x)
  12705. (concat indent x) x))
  12706. lines)))
  12707. (setq txt (concat first "\n"
  12708. (mapconcat 'identity lines "\n"))))
  12709. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12710. (setq txt (replace-match "\n\n" t t txt))
  12711. (if (string-match "[ \t\n]*\\'" txt)
  12712. (setq txt (replace-match "\n" t t txt))))
  12713. ;; Put the modified text back into the remember buffer, for refile.
  12714. (erase-buffer)
  12715. (insert txt)
  12716. (goto-char (point-min))
  12717. (when (and (eq org-remember-interactive-interface 'refile)
  12718. (not fastp))
  12719. (org-refile nil (or visiting (find-file-noselect file)))
  12720. (throw 'quit t))
  12721. ;; Find the file
  12722. (if (not visiting) (find-file-noselect file))
  12723. (with-current-buffer (or visiting (get-file-buffer file))
  12724. (unless (org-mode-p)
  12725. (error "Target files for remember notes must be in Org-mode"))
  12726. (save-excursion
  12727. (save-restriction
  12728. (widen)
  12729. (and (goto-char (point-min))
  12730. (not (re-search-forward "^\\* " nil t))
  12731. (insert "\n* " (or heading "Notes") "\n"))
  12732. (setq reversed (org-notes-order-reversed-p))
  12733. ;; Find the default location
  12734. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12735. (goto-char (point-min))
  12736. (if (re-search-forward
  12737. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12738. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12739. nil t)
  12740. (setq org-goto-start-pos (match-beginning 0))
  12741. (when fastp
  12742. (goto-char (point-max))
  12743. (unless (bolp) (newline))
  12744. (insert "* " heading "\n")
  12745. (setq org-goto-start-pos (point-at-bol 0)))))
  12746. ;; Ask the User for a location, using the appropriate interface
  12747. (cond
  12748. (fastp (setq spos org-goto-start-pos
  12749. exitcmd 'return))
  12750. ((eq org-remember-interactive-interface 'outline)
  12751. (setq spos (org-get-location (current-buffer)
  12752. org-remember-help)
  12753. exitcmd (cdr spos)
  12754. spos (car spos)))
  12755. ((eq org-remember-interactive-interface 'outline-path-completion)
  12756. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12757. (org-refile-use-outline-path t))
  12758. (setq spos (org-refile-get-location "Heading: ")
  12759. exitcmd 'return
  12760. spos (nth 3 spos))))
  12761. (t (error "this should not hapen")))
  12762. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12763. ; not handle this note
  12764. (goto-char spos)
  12765. (cond ((org-on-heading-p t)
  12766. (org-back-to-heading t)
  12767. (setq level (funcall outline-level))
  12768. (cond
  12769. ((eq exitcmd 'return)
  12770. ;; sublevel of current
  12771. (setq org-remember-previous-location
  12772. (cons (abbreviate-file-name file)
  12773. (org-get-heading 'notags)))
  12774. (if reversed
  12775. (outline-next-heading)
  12776. (org-end-of-subtree t)
  12777. (if (not (bolp))
  12778. (if (looking-at "[ \t]*\n")
  12779. (beginning-of-line 2)
  12780. (end-of-line 1)
  12781. (insert "\n"))))
  12782. (bookmark-set "org-remember-last-stored")
  12783. (org-paste-subtree (org-get-legal-level level 1) txt))
  12784. ((eq exitcmd 'left)
  12785. ;; before current
  12786. (bookmark-set "org-remember-last-stored")
  12787. (org-paste-subtree level txt))
  12788. ((eq exitcmd 'right)
  12789. ;; after current
  12790. (org-end-of-subtree t)
  12791. (bookmark-set "org-remember-last-stored")
  12792. (org-paste-subtree level txt))
  12793. (t (error "This should not happen"))))
  12794. ((and (bobp) (not reversed))
  12795. ;; Put it at the end, one level below level 1
  12796. (save-restriction
  12797. (widen)
  12798. (goto-char (point-max))
  12799. (if (not (bolp)) (newline))
  12800. (bookmark-set "org-remember-last-stored")
  12801. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12802. ((and (bobp) reversed)
  12803. ;; Put it at the start, as level 1
  12804. (save-restriction
  12805. (widen)
  12806. (goto-char (point-min))
  12807. (re-search-forward "^\\*+ " nil t)
  12808. (beginning-of-line 1)
  12809. (bookmark-set "org-remember-last-stored")
  12810. (org-paste-subtree 1 txt)))
  12811. (t
  12812. ;; Put it right there, with automatic level determined by
  12813. ;; org-paste-subtree or from prefix arg
  12814. (bookmark-set "org-remember-last-stored")
  12815. (org-paste-subtree
  12816. (if (numberp current-prefix-arg) current-prefix-arg)
  12817. txt)))
  12818. (when remember-save-after-remembering
  12819. (save-buffer)
  12820. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12821. t) ;; return t to indicate that we took care of this note.
  12822. (defun org-get-org-file ()
  12823. "Read a filename, with default directory `org-directory'."
  12824. (let ((default (or org-default-notes-file remember-data-file)))
  12825. (read-file-name (format "File name [%s]: " default)
  12826. (file-name-as-directory org-directory)
  12827. default)))
  12828. (defun org-notes-order-reversed-p ()
  12829. "Check if the current file should receive notes in reversed order."
  12830. (cond
  12831. ((not org-reverse-note-order) nil)
  12832. ((eq t org-reverse-note-order) t)
  12833. ((not (listp org-reverse-note-order)) nil)
  12834. (t (catch 'exit
  12835. (let ((all org-reverse-note-order)
  12836. entry)
  12837. (while (setq entry (pop all))
  12838. (if (string-match (car entry) buffer-file-name)
  12839. (throw 'exit (cdr entry))))
  12840. nil)))))
  12841. ;;; Refiling
  12842. (defvar org-refile-target-table nil
  12843. "The list of refile targets, created by `org-refile'.")
  12844. (defvar org-agenda-new-buffers nil
  12845. "Buffers created to visit agenda files.")
  12846. (defun org-get-refile-targets (&optional default-buffer)
  12847. "Produce a table with refile targets."
  12848. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12849. targets txt re files f desc descre)
  12850. (with-current-buffer (or default-buffer (current-buffer))
  12851. (while (setq entry (pop entries))
  12852. (setq files (car entry) desc (cdr entry))
  12853. (cond
  12854. ((null files) (setq files (list (current-buffer))))
  12855. ((eq files 'org-agenda-files)
  12856. (setq files (org-agenda-files 'unrestricted)))
  12857. ((and (symbolp files) (fboundp files))
  12858. (setq files (funcall files)))
  12859. ((and (symbolp files) (boundp files))
  12860. (setq files (symbol-value files))))
  12861. (if (stringp files) (setq files (list files)))
  12862. (cond
  12863. ((eq (car desc) :tag)
  12864. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12865. ((eq (car desc) :todo)
  12866. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12867. ((eq (car desc) :regexp)
  12868. (setq descre (cdr desc)))
  12869. ((eq (car desc) :level)
  12870. (setq descre (concat "^\\*\\{" (number-to-string
  12871. (if org-odd-levels-only
  12872. (1- (* 2 (cdr desc)))
  12873. (cdr desc)))
  12874. "\\}[ \t]")))
  12875. ((eq (car desc) :maxlevel)
  12876. (setq descre (concat "^\\*\\{1," (number-to-string
  12877. (if org-odd-levels-only
  12878. (1- (* 2 (cdr desc)))
  12879. (cdr desc)))
  12880. "\\}[ \t]")))
  12881. (t (error "Bad refiling target description %s" desc)))
  12882. (while (setq f (pop files))
  12883. (save-excursion
  12884. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12885. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12886. (save-excursion
  12887. (save-restriction
  12888. (widen)
  12889. (goto-char (point-min))
  12890. (while (re-search-forward descre nil t)
  12891. (goto-char (point-at-bol))
  12892. (when (looking-at org-complex-heading-regexp)
  12893. (setq txt (match-string 4)
  12894. re (concat "^" (regexp-quote
  12895. (buffer-substring (match-beginning 1)
  12896. (match-end 4)))))
  12897. (if (match-end 5) (setq re (concat re "[ \t]+"
  12898. (regexp-quote
  12899. (match-string 5)))))
  12900. (setq re (concat re "[ \t]*$"))
  12901. (when org-refile-use-outline-path
  12902. (setq txt (mapconcat 'identity
  12903. (append
  12904. (if (eq org-refile-use-outline-path 'file)
  12905. (list (file-name-nondirectory
  12906. (buffer-file-name (buffer-base-buffer))))
  12907. (if (eq org-refile-use-outline-path 'full-file-path)
  12908. (list (buffer-file-name (buffer-base-buffer)))))
  12909. (org-get-outline-path)
  12910. (list txt))
  12911. "/")))
  12912. (push (list txt f re (point)) targets))
  12913. (goto-char (point-at-eol))))))))
  12914. (nreverse targets))))
  12915. (defun org-get-outline-path ()
  12916. "Return the outline path to the current entry, as a list."
  12917. (let (rtn)
  12918. (save-excursion
  12919. (while (org-up-heading-safe)
  12920. (when (looking-at org-complex-heading-regexp)
  12921. (push (org-match-string-no-properties 4) rtn)))
  12922. rtn)))
  12923. (defvar org-refile-history nil
  12924. "History for refiling operations.")
  12925. (defun org-refile (&optional goto default-buffer)
  12926. "Move the entry at point to another heading.
  12927. The list of target headings is compiled using the information in
  12928. `org-refile-targets', which see. This list is created upon first use, and
  12929. you can update it by calling this command with a double prefix (`C-u C-u').
  12930. FIXME: Can we find a better way of updating?
  12931. At the target location, the entry is filed as a subitem of the target heading.
  12932. Depending on `org-reverse-note-order', the new subitem will either be the
  12933. first of the last subitem.
  12934. With prefix arg GOTO, the command will only visit the target location,
  12935. not actually move anything.
  12936. With a double prefix `C-c C-c', go to the location where the last refiling
  12937. operation has put the subtree.
  12938. With a double prefix argument, the command can be used to jump to any
  12939. heading in the current buffer."
  12940. (interactive "P")
  12941. (let* ((cbuf (current-buffer))
  12942. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12943. pos it nbuf file re level reversed)
  12944. (if (equal goto '(16))
  12945. (org-refile-goto-last-stored)
  12946. (when (setq it (org-refile-get-location
  12947. (if goto "Goto: " "Refile to: ") default-buffer))
  12948. (setq file (nth 1 it)
  12949. re (nth 2 it)
  12950. pos (nth 3 it))
  12951. (setq nbuf (or (find-buffer-visiting file)
  12952. (find-file-noselect file)))
  12953. (if goto
  12954. (progn
  12955. (switch-to-buffer nbuf)
  12956. (goto-char pos)
  12957. (org-show-context 'org-goto))
  12958. (org-copy-special)
  12959. (save-excursion
  12960. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12961. (find-file-noselect file))))
  12962. (setq reversed (org-notes-order-reversed-p))
  12963. (save-excursion
  12964. (save-restriction
  12965. (widen)
  12966. (goto-char pos)
  12967. (looking-at outline-regexp)
  12968. (setq level (org-get-legal-level (funcall outline-level) 1))
  12969. (goto-char
  12970. (if reversed
  12971. (outline-next-heading)
  12972. (or (save-excursion (outline-get-next-sibling))
  12973. (org-end-of-subtree t t)
  12974. (point-max))))
  12975. (bookmark-set "org-refile-last-stored")
  12976. (org-paste-subtree level))))
  12977. (org-cut-special)
  12978. (message "Entry refiled to \"%s\"" (car it)))))))
  12979. (defun org-refile-goto-last-stored ()
  12980. "Go to the location where the last refile was stored."
  12981. (interactive)
  12982. (bookmark-jump "org-refile-last-stored")
  12983. (message "This is the location of the last refile"))
  12984. (defun org-refile-get-location (&optional prompt default-buffer)
  12985. "Prompt the user for a refile location, using PROMPT."
  12986. (let ((org-refile-targets org-refile-targets)
  12987. (org-refile-use-outline-path org-refile-use-outline-path))
  12988. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12989. (unless org-refile-target-table
  12990. (error "No refile targets"))
  12991. (let* ((cbuf (current-buffer))
  12992. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12993. (fname (and filename (file-truename filename)))
  12994. (tbl (mapcar
  12995. (lambda (x)
  12996. (if (not (equal fname (file-truename (nth 1 x))))
  12997. (cons (concat (car x) " (" (file-name-nondirectory
  12998. (nth 1 x)) ")")
  12999. (cdr x))
  13000. x))
  13001. org-refile-target-table))
  13002. (completion-ignore-case t))
  13003. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13004. tbl)))
  13005. ;;;; Dynamic blocks
  13006. (defun org-find-dblock (name)
  13007. "Find the first dynamic block with name NAME in the buffer.
  13008. If not found, stay at current position and return nil."
  13009. (let (pos)
  13010. (save-excursion
  13011. (goto-char (point-min))
  13012. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13013. nil t)
  13014. (match-beginning 0))))
  13015. (if pos (goto-char pos))
  13016. pos))
  13017. (defconst org-dblock-start-re
  13018. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13019. "Matches the startline of a dynamic block, with parameters.")
  13020. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13021. "Matches the end of a dyhamic block.")
  13022. (defun org-create-dblock (plist)
  13023. "Create a dynamic block section, with parameters taken from PLIST.
  13024. PLIST must containe a :name entry which is used as name of the block."
  13025. (unless (bolp) (newline))
  13026. (let ((name (plist-get plist :name)))
  13027. (insert "#+BEGIN: " name)
  13028. (while plist
  13029. (if (eq (car plist) :name)
  13030. (setq plist (cddr plist))
  13031. (insert " " (prin1-to-string (pop plist)))))
  13032. (insert "\n\n#+END:\n")
  13033. (beginning-of-line -2)))
  13034. (defun org-prepare-dblock ()
  13035. "Prepare dynamic block for refresh.
  13036. This empties the block, puts the cursor at the insert position and returns
  13037. the property list including an extra property :name with the block name."
  13038. (unless (looking-at org-dblock-start-re)
  13039. (error "Not at a dynamic block"))
  13040. (let* ((begdel (1+ (match-end 0)))
  13041. (name (org-no-properties (match-string 1)))
  13042. (params (append (list :name name)
  13043. (read (concat "(" (match-string 3) ")")))))
  13044. (unless (re-search-forward org-dblock-end-re nil t)
  13045. (error "Dynamic block not terminated"))
  13046. (delete-region begdel (match-beginning 0))
  13047. (goto-char begdel)
  13048. (open-line 1)
  13049. params))
  13050. (defun org-map-dblocks (&optional command)
  13051. "Apply COMMAND to all dynamic blocks in the current buffer.
  13052. If COMMAND is not given, use `org-update-dblock'."
  13053. (let ((cmd (or command 'org-update-dblock))
  13054. pos)
  13055. (save-excursion
  13056. (goto-char (point-min))
  13057. (while (re-search-forward org-dblock-start-re nil t)
  13058. (goto-char (setq pos (match-beginning 0)))
  13059. (condition-case nil
  13060. (funcall cmd)
  13061. (error (message "Error during update of dynamic block")))
  13062. (goto-char pos)
  13063. (unless (re-search-forward org-dblock-end-re nil t)
  13064. (error "Dynamic block not terminated"))))))
  13065. (defun org-dblock-update (&optional arg)
  13066. "User command for updating dynamic blocks.
  13067. Update the dynamic block at point. With prefix ARG, update all dynamic
  13068. blocks in the buffer."
  13069. (interactive "P")
  13070. (if arg
  13071. (org-update-all-dblocks)
  13072. (or (looking-at org-dblock-start-re)
  13073. (org-beginning-of-dblock))
  13074. (org-update-dblock)))
  13075. (defun org-update-dblock ()
  13076. "Update the dynamic block at point
  13077. This means to empty the block, parse for parameters and then call
  13078. the correct writing function."
  13079. (save-window-excursion
  13080. (let* ((pos (point))
  13081. (line (org-current-line))
  13082. (params (org-prepare-dblock))
  13083. (name (plist-get params :name))
  13084. (cmd (intern (concat "org-dblock-write:" name))))
  13085. (message "Updating dynamic block `%s' at line %d..." name line)
  13086. (funcall cmd params)
  13087. (message "Updating dynamic block `%s' at line %d...done" name line)
  13088. (goto-char pos))))
  13089. (defun org-beginning-of-dblock ()
  13090. "Find the beginning of the dynamic block at point.
  13091. Error if there is no scuh block at point."
  13092. (let ((pos (point))
  13093. beg)
  13094. (end-of-line 1)
  13095. (if (and (re-search-backward org-dblock-start-re nil t)
  13096. (setq beg (match-beginning 0))
  13097. (re-search-forward org-dblock-end-re nil t)
  13098. (> (match-end 0) pos))
  13099. (goto-char beg)
  13100. (goto-char pos)
  13101. (error "Not in a dynamic block"))))
  13102. (defun org-update-all-dblocks ()
  13103. "Update all dynamic blocks in the buffer.
  13104. This function can be used in a hook."
  13105. (when (org-mode-p)
  13106. (org-map-dblocks 'org-update-dblock)))
  13107. ;;;; Completion
  13108. (defconst org-additional-option-like-keywords
  13109. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13110. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13111. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13112. (defun org-complete (&optional arg)
  13113. "Perform completion on word at point.
  13114. At the beginning of a headline, this completes TODO keywords as given in
  13115. `org-todo-keywords'.
  13116. If the current word is preceded by a backslash, completes the TeX symbols
  13117. that are supported for HTML support.
  13118. If the current word is preceded by \"#+\", completes special words for
  13119. setting file options.
  13120. In the line after \"#+STARTUP:, complete valid keywords.\"
  13121. At all other locations, this simply calls the value of
  13122. `org-completion-fallback-command'."
  13123. (interactive "P")
  13124. (org-without-partial-completion
  13125. (catch 'exit
  13126. (let* ((end (point))
  13127. (beg1 (save-excursion
  13128. (skip-chars-backward (org-re "[:alnum:]_@"))
  13129. (point)))
  13130. (beg (save-excursion
  13131. (skip-chars-backward "a-zA-Z0-9_:$")
  13132. (point)))
  13133. (confirm (lambda (x) (stringp (car x))))
  13134. (searchhead (equal (char-before beg) ?*))
  13135. (tag (and (equal (char-before beg1) ?:)
  13136. (equal (char-after (point-at-bol)) ?*)))
  13137. (prop (and (equal (char-before beg1) ?:)
  13138. (not (equal (char-after (point-at-bol)) ?*))))
  13139. (texp (equal (char-before beg) ?\\))
  13140. (link (equal (char-before beg) ?\[))
  13141. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13142. beg)
  13143. "#+"))
  13144. (startup (string-match "^#\\+STARTUP:.*"
  13145. (buffer-substring (point-at-bol) (point))))
  13146. (completion-ignore-case opt)
  13147. (type nil)
  13148. (tbl nil)
  13149. (table (cond
  13150. (opt
  13151. (setq type :opt)
  13152. (append
  13153. (mapcar
  13154. (lambda (x)
  13155. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13156. (cons (match-string 2 x) (match-string 1 x)))
  13157. (org-split-string (org-get-current-options) "\n"))
  13158. (mapcar 'list org-additional-option-like-keywords)))
  13159. (startup
  13160. (setq type :startup)
  13161. org-startup-options)
  13162. (link (append org-link-abbrev-alist-local
  13163. org-link-abbrev-alist))
  13164. (texp
  13165. (setq type :tex)
  13166. org-html-entities)
  13167. ((string-match "\\`\\*+[ \t]+\\'"
  13168. (buffer-substring (point-at-bol) beg))
  13169. (setq type :todo)
  13170. (mapcar 'list org-todo-keywords-1))
  13171. (searchhead
  13172. (setq type :searchhead)
  13173. (save-excursion
  13174. (goto-char (point-min))
  13175. (while (re-search-forward org-todo-line-regexp nil t)
  13176. (push (list
  13177. (org-make-org-heading-search-string
  13178. (match-string 3) t))
  13179. tbl)))
  13180. tbl)
  13181. (tag (setq type :tag beg beg1)
  13182. (or org-tag-alist (org-get-buffer-tags)))
  13183. (prop (setq type :prop beg beg1)
  13184. (mapcar 'list (org-buffer-property-keys nil t t)))
  13185. (t (progn
  13186. (call-interactively org-completion-fallback-command)
  13187. (throw 'exit nil)))))
  13188. (pattern (buffer-substring-no-properties beg end))
  13189. (completion (try-completion pattern table confirm)))
  13190. (cond ((eq completion t)
  13191. (if (not (assoc (upcase pattern) table))
  13192. (message "Already complete")
  13193. (if (equal type :opt)
  13194. (insert (substring (cdr (assoc (upcase pattern) table))
  13195. (length pattern)))
  13196. (if (memq type '(:tag :prop)) (insert ":")))))
  13197. ((null completion)
  13198. (message "Can't find completion for \"%s\"" pattern)
  13199. (ding))
  13200. ((not (string= pattern completion))
  13201. (delete-region beg end)
  13202. (if (string-match " +$" completion)
  13203. (setq completion (replace-match "" t t completion)))
  13204. (insert completion)
  13205. (if (get-buffer-window "*Completions*")
  13206. (delete-window (get-buffer-window "*Completions*")))
  13207. (if (assoc completion table)
  13208. (if (eq type :todo) (insert " ")
  13209. (if (memq type '(:tag :prop)) (insert ":"))))
  13210. (if (and (equal type :opt) (assoc completion table))
  13211. (message "%s" (substitute-command-keys
  13212. "Press \\[org-complete] again to insert example settings"))))
  13213. (t
  13214. (message "Making completion list...")
  13215. (let ((list (sort (all-completions pattern table confirm)
  13216. 'string<)))
  13217. (with-output-to-temp-buffer "*Completions*"
  13218. (condition-case nil
  13219. ;; Protection needed for XEmacs and emacs 21
  13220. (display-completion-list list pattern)
  13221. (error (display-completion-list list)))))
  13222. (message "Making completion list...%s" "done")))))))
  13223. ;;;; TODO, DEADLINE, Comments
  13224. (defun org-toggle-comment ()
  13225. "Change the COMMENT state of an entry."
  13226. (interactive)
  13227. (save-excursion
  13228. (org-back-to-heading)
  13229. (let (case-fold-search)
  13230. (if (looking-at (concat outline-regexp
  13231. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13232. (replace-match "" t t nil 1)
  13233. (if (looking-at outline-regexp)
  13234. (progn
  13235. (goto-char (match-end 0))
  13236. (insert org-comment-string " ")))))))
  13237. (defvar org-last-todo-state-is-todo nil
  13238. "This is non-nil when the last TODO state change led to a TODO state.
  13239. If the last change removed the TODO tag or switched to DONE, then
  13240. this is nil.")
  13241. (defvar org-setting-tags nil) ; dynamically skiped
  13242. ;; FIXME: better place
  13243. (defun org-property-or-variable-value (var &optional inherit)
  13244. "Check if there is a property fixing the value of VAR.
  13245. If yes, return this value. If not, return the current value of the variable."
  13246. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13247. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13248. (read prop)
  13249. (symbol-value var))))
  13250. (defun org-parse-local-options (string var)
  13251. "Parse STRING for startup setting relevant for variable VAR."
  13252. (let ((rtn (symbol-value var))
  13253. e opts)
  13254. (save-match-data
  13255. (if (or (not string) (not (string-match "\\S-" string)))
  13256. rtn
  13257. (setq opts (delq nil (mapcar (lambda (x)
  13258. (setq e (assoc x org-startup-options))
  13259. (if (eq (nth 1 e) var) e nil))
  13260. (org-split-string string "[ \t]+"))))
  13261. (if (not opts)
  13262. rtn
  13263. (setq rtn nil)
  13264. (while (setq e (pop opts))
  13265. (if (not (nth 3 e))
  13266. (setq rtn (nth 2 e))
  13267. (if (not (listp rtn)) (setq rtn nil))
  13268. (push (nth 2 e) rtn)))
  13269. rtn)))))
  13270. (defvar org-blocker-hook nil
  13271. "Hook for functions that are allowed to block a state change.
  13272. Each function gets as its single argument a property list, see
  13273. `org-trigger-hook' for more information about this list.
  13274. If any of the functions in this hook returns nil, the state change
  13275. is blocked.")
  13276. (defvar org-trigger-hook nil
  13277. "Hook for functions that are triggered by a state change.
  13278. Each function gets as its single argument a property list with at least
  13279. the following elements:
  13280. (:type type-of-change :position pos-at-entry-start
  13281. :from old-state :to new-state)
  13282. Depending on the type, more properties may be present.
  13283. This mechanism is currently implemented for:
  13284. TODO state changes
  13285. ------------------
  13286. :type todo-state-change
  13287. :from previous state (keyword as a string), or nil
  13288. :to new state (keyword as a string), or nil")
  13289. (defun org-todo (&optional arg)
  13290. "Change the TODO state of an item.
  13291. The state of an item is given by a keyword at the start of the heading,
  13292. like
  13293. *** TODO Write paper
  13294. *** DONE Call mom
  13295. The different keywords are specified in the variable `org-todo-keywords'.
  13296. By default the available states are \"TODO\" and \"DONE\".
  13297. So for this example: when the item starts with TODO, it is changed to DONE.
  13298. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13299. DONE are present, add TODO at the beginning of the heading.
  13300. With C-u prefix arg, use completion to determine the new state.
  13301. With numeric prefix arg, switch to that state.
  13302. For calling through lisp, arg is also interpreted in the following way:
  13303. 'none -> empty state
  13304. \"\"(empty string) -> switch to empty state
  13305. 'done -> switch to DONE
  13306. 'nextset -> switch to the next set of keywords
  13307. 'previousset -> switch to the previous set of keywords
  13308. \"WAITING\" -> switch to the specified keyword, but only if it
  13309. really is a member of `org-todo-keywords'."
  13310. (interactive "P")
  13311. (save-excursion
  13312. (catch 'exit
  13313. (org-back-to-heading)
  13314. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13315. (or (looking-at (concat " +" org-todo-regexp " *"))
  13316. (looking-at " *"))
  13317. (let* ((match-data (match-data))
  13318. (startpos (point-at-bol))
  13319. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13320. (org-log-done org-log-done)
  13321. (org-log-repeat org-log-repeat)
  13322. (org-todo-log-states org-todo-log-states)
  13323. (this (match-string 1))
  13324. (hl-pos (match-beginning 0))
  13325. (head (org-get-todo-sequence-head this))
  13326. (ass (assoc head org-todo-kwd-alist))
  13327. (interpret (nth 1 ass))
  13328. (done-word (nth 3 ass))
  13329. (final-done-word (nth 4 ass))
  13330. (last-state (or this ""))
  13331. (completion-ignore-case t)
  13332. (member (member this org-todo-keywords-1))
  13333. (tail (cdr member))
  13334. (state (cond
  13335. ((and org-todo-key-trigger
  13336. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13337. (and (not arg) org-use-fast-todo-selection
  13338. (not (eq org-use-fast-todo-selection 'prefix)))))
  13339. ;; Use fast selection
  13340. (org-fast-todo-selection))
  13341. ((and (equal arg '(4))
  13342. (or (not org-use-fast-todo-selection)
  13343. (not org-todo-key-trigger)))
  13344. ;; Read a state with completion
  13345. (completing-read "State: " (mapcar (lambda(x) (list x))
  13346. org-todo-keywords-1)
  13347. nil t))
  13348. ((eq arg 'right)
  13349. (if this
  13350. (if tail (car tail) nil)
  13351. (car org-todo-keywords-1)))
  13352. ((eq arg 'left)
  13353. (if (equal member org-todo-keywords-1)
  13354. nil
  13355. (if this
  13356. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13357. org-todo-keywords-1)
  13358. (org-last org-todo-keywords-1))))
  13359. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13360. (setq arg nil))) ; hack to fall back to cycling
  13361. (arg
  13362. ;; user or caller requests a specific state
  13363. (cond
  13364. ((equal arg "") nil)
  13365. ((eq arg 'none) nil)
  13366. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13367. ((eq arg 'nextset)
  13368. (or (car (cdr (member head org-todo-heads)))
  13369. (car org-todo-heads)))
  13370. ((eq arg 'previousset)
  13371. (let ((org-todo-heads (reverse org-todo-heads)))
  13372. (or (car (cdr (member head org-todo-heads)))
  13373. (car org-todo-heads))))
  13374. ((car (member arg org-todo-keywords-1)))
  13375. ((nth (1- (prefix-numeric-value arg))
  13376. org-todo-keywords-1))))
  13377. ((null member) (or head (car org-todo-keywords-1)))
  13378. ((equal this final-done-word) nil) ;; -> make empty
  13379. ((null tail) nil) ;; -> first entry
  13380. ((eq interpret 'sequence)
  13381. (car tail))
  13382. ((memq interpret '(type priority))
  13383. (if (eq this-command last-command)
  13384. (car tail)
  13385. (if (> (length tail) 0)
  13386. (or done-word (car org-done-keywords))
  13387. nil)))
  13388. (t nil)))
  13389. (next (if state (concat " " state " ") " "))
  13390. (change-plist (list :type 'todo-state-change :from this :to state
  13391. :position startpos))
  13392. dolog now-done-p)
  13393. (when org-blocker-hook
  13394. (unless (save-excursion
  13395. (save-match-data
  13396. (run-hook-with-args-until-failure
  13397. 'org-blocker-hook change-plist)))
  13398. (if (interactive-p)
  13399. (error "TODO state change from %s to %s blocked" this state)
  13400. ;; fail silently
  13401. (message "TODO state change from %s to %s blocked" this state)
  13402. (throw 'exit nil))))
  13403. (store-match-data match-data)
  13404. (replace-match next t t)
  13405. (unless (pos-visible-in-window-p hl-pos)
  13406. (message "TODO state changed to %s" (org-trim next)))
  13407. (unless head
  13408. (setq head (org-get-todo-sequence-head state)
  13409. ass (assoc head org-todo-kwd-alist)
  13410. interpret (nth 1 ass)
  13411. done-word (nth 3 ass)
  13412. final-done-word (nth 4 ass)))
  13413. (when (memq arg '(nextset previousset))
  13414. (message "Keyword-Set %d/%d: %s"
  13415. (- (length org-todo-sets) -1
  13416. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13417. (length org-todo-sets)
  13418. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13419. (setq org-last-todo-state-is-todo
  13420. (not (member state org-done-keywords)))
  13421. (setq now-done-p (and (member state org-done-keywords)
  13422. (not (member this org-done-keywords))))
  13423. (and logging (org-local-logging logging))
  13424. (when (and (or org-todo-log-states org-log-done)
  13425. (not (memq arg '(nextset previousset))))
  13426. ;; we need to look at recording a time and note
  13427. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13428. (nth 2 (assoc this org-todo-log-states))))
  13429. (when (and state
  13430. (member state org-not-done-keywords)
  13431. (not (member this org-not-done-keywords)))
  13432. ;; This is now a todo state and was not one before
  13433. ;; If there was a CLOSED time stamp, get rid of it.
  13434. (org-add-planning-info nil nil 'closed))
  13435. (when (and now-done-p org-log-done)
  13436. ;; It is now done, and it was not done before
  13437. (org-add-planning-info 'closed (org-current-time))
  13438. (if (and (not dolog) (eq 'note org-log-done))
  13439. (org-add-log-maybe 'done state 'findpos 'note)))
  13440. (when (and state dolog)
  13441. ;; This is a non-nil state, and we need to log it
  13442. (org-add-log-maybe 'state state 'findpos dolog)))
  13443. ;; Fixup tag positioning
  13444. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13445. (run-hooks 'org-after-todo-state-change-hook)
  13446. (if (and arg (not (member state org-done-keywords)))
  13447. (setq head (org-get-todo-sequence-head state)))
  13448. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13449. ;; Do we need to trigger a repeat?
  13450. (when now-done-p (org-auto-repeat-maybe state))
  13451. ;; Fixup cursor location if close to the keyword
  13452. (if (and (outline-on-heading-p)
  13453. (not (bolp))
  13454. (save-excursion (beginning-of-line 1)
  13455. (looking-at org-todo-line-regexp))
  13456. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13457. (progn
  13458. (goto-char (or (match-end 2) (match-end 1)))
  13459. (just-one-space)))
  13460. (when org-trigger-hook
  13461. (save-excursion
  13462. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13463. (defun org-local-logging (value)
  13464. "Get logging settings from a property VALUE."
  13465. (let* (words w a)
  13466. ;; directly set the variables, they are already local.
  13467. (setq org-log-done nil
  13468. org-log-repeat nil
  13469. org-todo-log-states nil)
  13470. (setq words (org-split-string value))
  13471. (while (setq w (pop words))
  13472. (cond
  13473. ((setq a (assoc w org-startup-options))
  13474. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13475. (set (nth 1 a) (nth 2 a))))
  13476. ((setq a (org-extract-log-state-settings w))
  13477. (and (member (car a) org-todo-keywords-1)
  13478. (push a org-todo-log-states)))))))
  13479. (defun org-get-todo-sequence-head (kwd)
  13480. "Return the head of the TODO sequence to which KWD belongs.
  13481. If KWD is not set, check if there is a text property remembering the
  13482. right sequence."
  13483. (let (p)
  13484. (cond
  13485. ((not kwd)
  13486. (or (get-text-property (point-at-bol) 'org-todo-head)
  13487. (progn
  13488. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13489. nil (point-at-eol)))
  13490. (get-text-property p 'org-todo-head))))
  13491. ((not (member kwd org-todo-keywords-1))
  13492. (car org-todo-keywords-1))
  13493. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13494. (defun org-fast-todo-selection ()
  13495. "Fast TODO keyword selection with single keys.
  13496. Returns the new TODO keyword, or nil if no state change should occur."
  13497. (let* ((fulltable org-todo-key-alist)
  13498. (done-keywords org-done-keywords) ;; needed for the faces.
  13499. (maxlen (apply 'max (mapcar
  13500. (lambda (x)
  13501. (if (stringp (car x)) (string-width (car x)) 0))
  13502. fulltable)))
  13503. (expert nil)
  13504. (fwidth (+ maxlen 3 1 3))
  13505. (ncol (/ (- (window-width) 4) fwidth))
  13506. tg cnt e c tbl
  13507. groups ingroup)
  13508. (save-window-excursion
  13509. (if expert
  13510. (set-buffer (get-buffer-create " *Org todo*"))
  13511. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13512. (erase-buffer)
  13513. (org-set-local 'org-done-keywords done-keywords)
  13514. (setq tbl fulltable cnt 0)
  13515. (while (setq e (pop tbl))
  13516. (cond
  13517. ((equal e '(:startgroup))
  13518. (push '() groups) (setq ingroup t)
  13519. (when (not (= cnt 0))
  13520. (setq cnt 0)
  13521. (insert "\n"))
  13522. (insert "{ "))
  13523. ((equal e '(:endgroup))
  13524. (setq ingroup nil cnt 0)
  13525. (insert "}\n"))
  13526. (t
  13527. (setq tg (car e) c (cdr e))
  13528. (if ingroup (push tg (car groups)))
  13529. (setq tg (org-add-props tg nil 'face
  13530. (org-get-todo-face tg)))
  13531. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13532. (insert "[" c "] " tg (make-string
  13533. (- fwidth 4 (length tg)) ?\ ))
  13534. (when (= (setq cnt (1+ cnt)) ncol)
  13535. (insert "\n")
  13536. (if ingroup (insert " "))
  13537. (setq cnt 0)))))
  13538. (insert "\n")
  13539. (goto-char (point-min))
  13540. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13541. (fit-window-to-buffer))
  13542. (message "[a-z..]:Set [SPC]:clear")
  13543. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13544. (cond
  13545. ((or (= c ?\C-g)
  13546. (and (= c ?q) (not (rassoc c fulltable))))
  13547. (setq quit-flag t))
  13548. ((= c ?\ ) nil)
  13549. ((setq e (rassoc c fulltable) tg (car e))
  13550. tg)
  13551. (t (setq quit-flag t))))))
  13552. (defun org-get-repeat ()
  13553. "Check if tere is a deadline/schedule with repeater in this entry."
  13554. (save-match-data
  13555. (save-excursion
  13556. (org-back-to-heading t)
  13557. (if (re-search-forward
  13558. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13559. (match-string 1)))))
  13560. (defvar org-last-changed-timestamp)
  13561. (defvar org-log-post-message)
  13562. (defvar org-log-note-purpose)
  13563. (defun org-auto-repeat-maybe (done-word)
  13564. "Check if the current headline contains a repeated deadline/schedule.
  13565. If yes, set TODO state back to what it was and change the base date
  13566. of repeating deadline/scheduled time stamps to new date.
  13567. This function is run automatically after each state change to a DONE state."
  13568. ;; last-state is dynamically scoped into this function
  13569. (let* ((repeat (org-get-repeat))
  13570. (aa (assoc last-state org-todo-kwd-alist))
  13571. (interpret (nth 1 aa))
  13572. (head (nth 2 aa))
  13573. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13574. (msg "Entry repeats: ")
  13575. (org-log-done nil)
  13576. (org-todo-log-states nil)
  13577. re type n what ts)
  13578. (when repeat
  13579. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13580. (org-todo (if (eq interpret 'type) last-state head))
  13581. (when (and org-log-repeat
  13582. (or (not (memq 'org-add-log-note
  13583. (default-value 'post-command-hook)))
  13584. (eq org-log-note-purpose 'done)))
  13585. ;; Make sure a note is taken;
  13586. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13587. 'findpos org-log-repeat))
  13588. (org-back-to-heading t)
  13589. (org-add-planning-info nil nil 'closed)
  13590. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13591. org-deadline-time-regexp "\\)\\|\\("
  13592. org-ts-regexp "\\)"))
  13593. (while (re-search-forward
  13594. re (save-excursion (outline-next-heading) (point)) t)
  13595. (setq type (if (match-end 1) org-scheduled-string
  13596. (if (match-end 3) org-deadline-string "Plain:"))
  13597. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13598. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13599. (setq n (string-to-number (match-string 1 ts))
  13600. what (match-string 2 ts))
  13601. (if (equal what "w") (setq n (* n 7) what "d"))
  13602. (org-timestamp-change n (cdr (assoc what whata)))
  13603. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13604. (setq org-log-post-message msg)
  13605. (message "%s" msg))))
  13606. (defun org-show-todo-tree (arg)
  13607. "Make a compact tree which shows all headlines marked with TODO.
  13608. The tree will show the lines where the regexp matches, and all higher
  13609. headlines above the match.
  13610. With a \\[universal-argument] prefix, also show the DONE entries.
  13611. With a numeric prefix N, construct a sparse tree for the Nth element
  13612. of `org-todo-keywords-1'."
  13613. (interactive "P")
  13614. (let ((case-fold-search nil)
  13615. (kwd-re
  13616. (cond ((null arg) org-not-done-regexp)
  13617. ((equal arg '(4))
  13618. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13619. (mapcar 'list org-todo-keywords-1))))
  13620. (concat "\\("
  13621. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13622. "\\)\\>")))
  13623. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13624. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13625. org-todo-keywords-1)))
  13626. (t (error "Invalid prefix argument: %s" arg)))))
  13627. (message "%d TODO entries found"
  13628. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13629. (defun org-deadline (&optional remove)
  13630. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13631. With argument REMOVE, remove any deadline from the item."
  13632. (interactive "P")
  13633. (if remove
  13634. (progn
  13635. (org-remove-timestamp-with-keyword org-deadline-string)
  13636. (message "Item no longer has a deadline."))
  13637. (org-add-planning-info 'deadline nil 'closed)))
  13638. (defun org-schedule (&optional remove)
  13639. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13640. With argument REMOVE, remove any scheduling date from the item."
  13641. (interactive "P")
  13642. (if remove
  13643. (progn
  13644. (org-remove-timestamp-with-keyword org-scheduled-string)
  13645. (message "Item is no longer scheduled."))
  13646. (org-add-planning-info 'scheduled nil 'closed)))
  13647. (defun org-remove-timestamp-with-keyword (keyword)
  13648. "Remove all time stamps with KEYWORD in the current entry."
  13649. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13650. beg)
  13651. (save-excursion
  13652. (org-back-to-heading t)
  13653. (setq beg (point))
  13654. (org-end-of-subtree t t)
  13655. (while (re-search-backward re beg t)
  13656. (replace-match "")
  13657. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13658. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13659. (defun org-add-planning-info (what &optional time &rest remove)
  13660. "Insert new timestamp with keyword in the line directly after the headline.
  13661. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13662. If non is given, the user is prompted for a date.
  13663. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13664. be removed."
  13665. (interactive)
  13666. (let (org-time-was-given org-end-time-was-given)
  13667. (when what (setq time (or time (org-read-date nil 'to-time))))
  13668. (when (and org-insert-labeled-timestamps-at-point
  13669. (member what '(scheduled deadline)))
  13670. (insert
  13671. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13672. (org-insert-time-stamp time org-time-was-given
  13673. nil nil nil (list org-end-time-was-given))
  13674. (setq what nil))
  13675. (save-excursion
  13676. (save-restriction
  13677. (let (col list elt ts buffer-invisibility-spec)
  13678. (org-back-to-heading t)
  13679. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13680. (goto-char (match-end 1))
  13681. (setq col (current-column))
  13682. (goto-char (match-end 0))
  13683. (if (eobp) (insert "\n") (forward-char 1))
  13684. (if (and (not (looking-at outline-regexp))
  13685. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13686. "[^\r\n]*"))
  13687. (not (equal (match-string 1) org-clock-string)))
  13688. (narrow-to-region (match-beginning 0) (match-end 0))
  13689. (insert-before-markers "\n")
  13690. (backward-char 1)
  13691. (narrow-to-region (point) (point))
  13692. (indent-to-column col))
  13693. ;; Check if we have to remove something.
  13694. (setq list (cons what remove))
  13695. (while list
  13696. (setq elt (pop list))
  13697. (goto-char (point-min))
  13698. (when (or (and (eq elt 'scheduled)
  13699. (re-search-forward org-scheduled-time-regexp nil t))
  13700. (and (eq elt 'deadline)
  13701. (re-search-forward org-deadline-time-regexp nil t))
  13702. (and (eq elt 'closed)
  13703. (re-search-forward org-closed-time-regexp nil t)))
  13704. (replace-match "")
  13705. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13706. (if (looking-at " +") (replace-match ""))))
  13707. (goto-char (point-max))
  13708. (when what
  13709. (insert
  13710. (if (not (equal (char-before) ?\ )) " " "")
  13711. (cond ((eq what 'scheduled) org-scheduled-string)
  13712. ((eq what 'deadline) org-deadline-string)
  13713. ((eq what 'closed) org-closed-string))
  13714. " ")
  13715. (setq ts (org-insert-time-stamp
  13716. time
  13717. (or org-time-was-given
  13718. (and (eq what 'closed) org-log-done-with-time))
  13719. (eq what 'closed)
  13720. nil nil (list org-end-time-was-given)))
  13721. (end-of-line 1))
  13722. (goto-char (point-min))
  13723. (widen)
  13724. (if (looking-at "[ \t]+\r?\n")
  13725. (replace-match ""))
  13726. ts)))))
  13727. (defvar org-log-note-marker (make-marker))
  13728. (defvar org-log-note-purpose nil)
  13729. (defvar org-log-note-state nil)
  13730. (defvar org-log-note-how nil)
  13731. (defvar org-log-note-window-configuration nil)
  13732. (defvar org-log-note-return-to (make-marker))
  13733. (defvar org-log-post-message nil
  13734. "Message to be displayed after a log note has been stored.
  13735. The auto-repeater uses this.")
  13736. (defun org-add-log-maybe (&optional purpose state findpos how)
  13737. "Set up the post command hook to take a note.
  13738. If this is about to TODO state change, the new state is expected in STATE.
  13739. When FINDPOS is non-nil, find the correct position for the note in
  13740. the current entry. If not, assume that it can be inserted at point."
  13741. (save-excursion
  13742. (when findpos
  13743. (org-back-to-heading t)
  13744. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13745. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13746. "[^\r\n]*\\)?"))
  13747. (goto-char (match-end 0))
  13748. (unless org-log-states-order-reversed
  13749. (and (= (char-after) ?\n) (forward-char 1))
  13750. (org-skip-over-state-notes)
  13751. (skip-chars-backward " \t\n\r")))
  13752. (move-marker org-log-note-marker (point))
  13753. (setq org-log-note-purpose purpose
  13754. org-log-note-state state
  13755. org-log-note-how how)
  13756. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13757. (defun org-skip-over-state-notes ()
  13758. "Skip past the list of State notes in an entry."
  13759. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13760. (while (looking-at "[ \t]*- State")
  13761. (condition-case nil
  13762. (org-next-item)
  13763. (error (org-end-of-item)))))
  13764. (defun org-add-log-note (&optional purpose)
  13765. "Pop up a window for taking a note, and add this note later at point."
  13766. (remove-hook 'post-command-hook 'org-add-log-note)
  13767. (setq org-log-note-window-configuration (current-window-configuration))
  13768. (delete-other-windows)
  13769. (move-marker org-log-note-return-to (point))
  13770. (switch-to-buffer (marker-buffer org-log-note-marker))
  13771. (goto-char org-log-note-marker)
  13772. (org-switch-to-buffer-other-window "*Org Note*")
  13773. (erase-buffer)
  13774. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13775. (org-store-log-note)
  13776. (let ((org-inhibit-startup t)) (org-mode))
  13777. (insert (format "# Insert note for %s.
  13778. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13779. (cond
  13780. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13781. ((eq org-log-note-purpose 'done) "closed todo item")
  13782. ((eq org-log-note-purpose 'state)
  13783. (format "state change to \"%s\"" org-log-note-state))
  13784. (t (error "This should not happen")))))
  13785. (org-set-local 'org-finish-function 'org-store-log-note)))
  13786. (defun org-store-log-note ()
  13787. "Finish taking a log note, and insert it to where it belongs."
  13788. (let ((txt (buffer-string))
  13789. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13790. lines ind)
  13791. (kill-buffer (current-buffer))
  13792. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13793. (setq txt (replace-match "" t t txt)))
  13794. (if (string-match "\\s-+\\'" txt)
  13795. (setq txt (replace-match "" t t txt)))
  13796. (setq lines (org-split-string txt "\n"))
  13797. (when (and note (string-match "\\S-" note))
  13798. (setq note
  13799. (org-replace-escapes
  13800. note
  13801. (list (cons "%u" (user-login-name))
  13802. (cons "%U" user-full-name)
  13803. (cons "%t" (format-time-string
  13804. (org-time-stamp-format 'long 'inactive)
  13805. (current-time)))
  13806. (cons "%s" (if org-log-note-state
  13807. (concat "\"" org-log-note-state "\"")
  13808. "")))))
  13809. (if lines (setq note (concat note " \\\\")))
  13810. (push note lines))
  13811. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13812. (when lines
  13813. (save-excursion
  13814. (set-buffer (marker-buffer org-log-note-marker))
  13815. (save-excursion
  13816. (goto-char org-log-note-marker)
  13817. (move-marker org-log-note-marker nil)
  13818. (end-of-line 1)
  13819. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13820. (indent-relative nil)
  13821. (insert "- " (pop lines))
  13822. (org-indent-line-function)
  13823. (beginning-of-line 1)
  13824. (looking-at "[ \t]*")
  13825. (setq ind (concat (match-string 0) " "))
  13826. (end-of-line 1)
  13827. (while lines (insert "\n" ind (pop lines)))))))
  13828. (set-window-configuration org-log-note-window-configuration)
  13829. (with-current-buffer (marker-buffer org-log-note-return-to)
  13830. (goto-char org-log-note-return-to))
  13831. (move-marker org-log-note-return-to nil)
  13832. (and org-log-post-message (message "%s" org-log-post-message)))
  13833. ;; FIXME: what else would be useful?
  13834. ;; - priority
  13835. ;; - date
  13836. (defun org-sparse-tree (&optional arg)
  13837. "Create a sparse tree, prompt for the details.
  13838. This command can create sparse trees. You first need to select the type
  13839. of match used to create the tree:
  13840. t Show entries with a specific TODO keyword.
  13841. T Show entries selected by a tags match.
  13842. p Enter a property name and its value (both with completion on existing
  13843. names/values) and show entries with that property.
  13844. r Show entries matching a regular expression
  13845. d Show deadlines due within `org-deadline-warning-days'."
  13846. (interactive "P")
  13847. (let (ans kwd value)
  13848. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13849. (setq ans (read-char-exclusive))
  13850. (cond
  13851. ((equal ans ?d)
  13852. (call-interactively 'org-check-deadlines))
  13853. ((equal ans ?b)
  13854. (call-interactively 'org-check-before-date))
  13855. ((equal ans ?t)
  13856. (org-show-todo-tree '(4)))
  13857. ((equal ans ?T)
  13858. (call-interactively 'org-tags-sparse-tree))
  13859. ((member ans '(?p ?P))
  13860. (setq kwd (completing-read "Property: "
  13861. (mapcar 'list (org-buffer-property-keys))))
  13862. (setq value (completing-read "Value: "
  13863. (mapcar 'list (org-property-values kwd))))
  13864. (unless (string-match "\\`{.*}\\'" value)
  13865. (setq value (concat "\"" value "\"")))
  13866. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13867. ((member ans '(?r ?R ?/))
  13868. (call-interactively 'org-occur))
  13869. (t (error "No such sparse tree command \"%c\"" ans)))))
  13870. (defvar org-occur-highlights nil)
  13871. (make-variable-buffer-local 'org-occur-highlights)
  13872. (defun org-occur (regexp &optional keep-previous callback)
  13873. "Make a compact tree which shows all matches of REGEXP.
  13874. The tree will show the lines where the regexp matches, and all higher
  13875. headlines above the match. It will also show the heading after the match,
  13876. to make sure editing the matching entry is easy.
  13877. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13878. call to `org-occur' will be kept, to allow stacking of calls to this
  13879. command.
  13880. If CALLBACK is non-nil, it is a function which is called to confirm
  13881. that the match should indeed be shown."
  13882. (interactive "sRegexp: \nP")
  13883. (or keep-previous (org-remove-occur-highlights nil nil t))
  13884. (let ((cnt 0))
  13885. (save-excursion
  13886. (goto-char (point-min))
  13887. (if (or (not keep-previous) ; do not want to keep
  13888. (not org-occur-highlights)) ; no previous matches
  13889. ;; hide everything
  13890. (org-overview))
  13891. (while (re-search-forward regexp nil t)
  13892. (when (or (not callback)
  13893. (save-match-data (funcall callback)))
  13894. (setq cnt (1+ cnt))
  13895. (when org-highlight-sparse-tree-matches
  13896. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13897. (org-show-context 'occur-tree))))
  13898. (when org-remove-highlights-with-change
  13899. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13900. nil 'local))
  13901. (unless org-sparse-tree-open-archived-trees
  13902. (org-hide-archived-subtrees (point-min) (point-max)))
  13903. (run-hooks 'org-occur-hook)
  13904. (if (interactive-p)
  13905. (message "%d match(es) for regexp %s" cnt regexp))
  13906. cnt))
  13907. (defun org-show-context (&optional key)
  13908. "Make sure point and context and visible.
  13909. How much context is shown depends upon the variables
  13910. `org-show-hierarchy-above', `org-show-following-heading'. and
  13911. `org-show-siblings'."
  13912. (let ((heading-p (org-on-heading-p t))
  13913. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13914. (following-p (org-get-alist-option org-show-following-heading key))
  13915. (entry-p (org-get-alist-option org-show-entry-below key))
  13916. (siblings-p (org-get-alist-option org-show-siblings key)))
  13917. (catch 'exit
  13918. ;; Show heading or entry text
  13919. (if (and heading-p (not entry-p))
  13920. (org-flag-heading nil) ; only show the heading
  13921. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13922. (org-show-hidden-entry))) ; show entire entry
  13923. (when following-p
  13924. ;; Show next sibling, or heading below text
  13925. (save-excursion
  13926. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13927. (org-flag-heading nil))))
  13928. (when siblings-p (org-show-siblings))
  13929. (when hierarchy-p
  13930. ;; show all higher headings, possibly with siblings
  13931. (save-excursion
  13932. (while (and (condition-case nil
  13933. (progn (org-up-heading-all 1) t)
  13934. (error nil))
  13935. (not (bobp)))
  13936. (org-flag-heading nil)
  13937. (when siblings-p (org-show-siblings))))))))
  13938. (defun org-reveal (&optional siblings)
  13939. "Show current entry, hierarchy above it, and the following headline.
  13940. This can be used to show a consistent set of context around locations
  13941. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13942. not t for the search context.
  13943. With optional argument SIBLINGS, on each level of the hierarchy all
  13944. siblings are shown. This repairs the tree structure to what it would
  13945. look like when opened with hierarchical calls to `org-cycle'."
  13946. (interactive "P")
  13947. (let ((org-show-hierarchy-above t)
  13948. (org-show-following-heading t)
  13949. (org-show-siblings (if siblings t org-show-siblings)))
  13950. (org-show-context nil)))
  13951. (defun org-highlight-new-match (beg end)
  13952. "Highlight from BEG to END and mark the highlight is an occur headline."
  13953. (let ((ov (org-make-overlay beg end)))
  13954. (org-overlay-put ov 'face 'secondary-selection)
  13955. (push ov org-occur-highlights)))
  13956. (defun org-remove-occur-highlights (&optional beg end noremove)
  13957. "Remove the occur highlights from the buffer.
  13958. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13959. from the `before-change-functions' in the current buffer."
  13960. (interactive)
  13961. (unless org-inhibit-highlight-removal
  13962. (mapc 'org-delete-overlay org-occur-highlights)
  13963. (setq org-occur-highlights nil)
  13964. (unless noremove
  13965. (remove-hook 'before-change-functions
  13966. 'org-remove-occur-highlights 'local))))
  13967. ;;;; Priorities
  13968. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13969. "Regular expression matching the priority indicator.")
  13970. (defvar org-remove-priority-next-time nil)
  13971. (defun org-priority-up ()
  13972. "Increase the priority of the current item."
  13973. (interactive)
  13974. (org-priority 'up))
  13975. (defun org-priority-down ()
  13976. "Decrease the priority of the current item."
  13977. (interactive)
  13978. (org-priority 'down))
  13979. (defun org-priority (&optional action)
  13980. "Change the priority of an item by ARG.
  13981. ACTION can be `set', `up', `down', or a character."
  13982. (interactive)
  13983. (setq action (or action 'set))
  13984. (let (current new news have remove)
  13985. (save-excursion
  13986. (org-back-to-heading)
  13987. (if (looking-at org-priority-regexp)
  13988. (setq current (string-to-char (match-string 2))
  13989. have t)
  13990. (setq current org-default-priority))
  13991. (cond
  13992. ((or (eq action 'set) (integerp action))
  13993. (if (integerp action)
  13994. (setq new action)
  13995. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13996. (setq new (read-char-exclusive)))
  13997. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13998. (= (upcase org-lowest-priority) org-lowest-priority))
  13999. (setq new (upcase new)))
  14000. (cond ((equal new ?\ ) (setq remove t))
  14001. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14002. (error "Priority must be between `%c' and `%c'"
  14003. org-highest-priority org-lowest-priority))))
  14004. ((eq action 'up)
  14005. (if (and (not have) (eq last-command this-command))
  14006. (setq new org-lowest-priority)
  14007. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14008. org-default-priority (1- current)))))
  14009. ((eq action 'down)
  14010. (if (and (not have) (eq last-command this-command))
  14011. (setq new org-highest-priority)
  14012. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14013. org-default-priority (1+ current)))))
  14014. (t (error "Invalid action")))
  14015. (if (or (< (upcase new) org-highest-priority)
  14016. (> (upcase new) org-lowest-priority))
  14017. (setq remove t))
  14018. (setq news (format "%c" new))
  14019. (if have
  14020. (if remove
  14021. (replace-match "" t t nil 1)
  14022. (replace-match news t t nil 2))
  14023. (if remove
  14024. (error "No priority cookie found in line")
  14025. (looking-at org-todo-line-regexp)
  14026. (if (match-end 2)
  14027. (progn
  14028. (goto-char (match-end 2))
  14029. (insert " [#" news "]"))
  14030. (goto-char (match-beginning 3))
  14031. (insert "[#" news "] ")))))
  14032. (org-preserve-lc (org-set-tags nil 'align))
  14033. (if remove
  14034. (message "Priority removed")
  14035. (message "Priority of current item set to %s" news))))
  14036. (defun org-get-priority (s)
  14037. "Find priority cookie and return priority."
  14038. (save-match-data
  14039. (if (not (string-match org-priority-regexp s))
  14040. (* 1000 (- org-lowest-priority org-default-priority))
  14041. (* 1000 (- org-lowest-priority
  14042. (string-to-char (match-string 2 s)))))))
  14043. ;;;; Tags
  14044. (defun org-scan-tags (action matcher &optional todo-only)
  14045. "Scan headline tags with inheritance and produce output ACTION.
  14046. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14047. evaluated, testing if a given set of tags qualifies a headline for
  14048. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14049. are included in the output."
  14050. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14051. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14052. (org-re
  14053. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14054. (props (list 'face nil
  14055. 'done-face 'org-done
  14056. 'undone-face nil
  14057. 'mouse-face 'highlight
  14058. 'org-not-done-regexp org-not-done-regexp
  14059. 'org-todo-regexp org-todo-regexp
  14060. 'keymap org-agenda-keymap
  14061. 'help-echo
  14062. (format "mouse-2 or RET jump to org file %s"
  14063. (abbreviate-file-name
  14064. (or (buffer-file-name (buffer-base-buffer))
  14065. (buffer-name (buffer-base-buffer)))))))
  14066. (case-fold-search nil)
  14067. lspos
  14068. tags tags-list tags-alist (llast 0) rtn level category i txt
  14069. todo marker entry priority)
  14070. (save-excursion
  14071. (goto-char (point-min))
  14072. (when (eq action 'sparse-tree)
  14073. (org-overview)
  14074. (org-remove-occur-highlights))
  14075. (while (re-search-forward re nil t)
  14076. (catch :skip
  14077. (setq todo (if (match-end 1) (match-string 2))
  14078. tags (if (match-end 4) (match-string 4)))
  14079. (goto-char (setq lspos (1+ (match-beginning 0))))
  14080. (setq level (org-reduced-level (funcall outline-level))
  14081. category (org-get-category))
  14082. (setq i llast llast level)
  14083. ;; remove tag lists from same and sublevels
  14084. (while (>= i level)
  14085. (when (setq entry (assoc i tags-alist))
  14086. (setq tags-alist (delete entry tags-alist)))
  14087. (setq i (1- i)))
  14088. ;; add the nex tags
  14089. (when tags
  14090. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14091. tags-alist
  14092. (cons (cons level tags) tags-alist)))
  14093. ;; compile tags for current headline
  14094. (setq tags-list
  14095. (if org-use-tag-inheritance
  14096. (apply 'append (mapcar 'cdr tags-alist))
  14097. tags))
  14098. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14099. (eval matcher)
  14100. (or (not org-agenda-skip-archived-trees)
  14101. (not (member org-archive-tag tags-list))))
  14102. (and (eq action 'agenda) (org-agenda-skip))
  14103. ;; list this headline
  14104. (if (eq action 'sparse-tree)
  14105. (progn
  14106. (and org-highlight-sparse-tree-matches
  14107. (org-get-heading) (match-end 0)
  14108. (org-highlight-new-match
  14109. (match-beginning 0) (match-beginning 1)))
  14110. (org-show-context 'tags-tree))
  14111. (setq txt (org-format-agenda-item
  14112. ""
  14113. (concat
  14114. (if org-tags-match-list-sublevels
  14115. (make-string (1- level) ?.) "")
  14116. (org-get-heading))
  14117. category tags-list)
  14118. priority (org-get-priority txt))
  14119. (goto-char lspos)
  14120. (setq marker (org-agenda-new-marker))
  14121. (org-add-props txt props
  14122. 'org-marker marker 'org-hd-marker marker 'org-category category
  14123. 'priority priority 'type "tagsmatch")
  14124. (push txt rtn))
  14125. ;; if we are to skip sublevels, jump to end of subtree
  14126. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14127. (when (and (eq action 'sparse-tree)
  14128. (not org-sparse-tree-open-archived-trees))
  14129. (org-hide-archived-subtrees (point-min) (point-max)))
  14130. (nreverse rtn)))
  14131. (defvar todo-only) ;; dynamically scoped
  14132. (defun org-tags-sparse-tree (&optional todo-only match)
  14133. "Create a sparse tree according to tags string MATCH.
  14134. MATCH can contain positive and negative selection of tags, like
  14135. \"+WORK+URGENT-WITHBOSS\".
  14136. If optional argument TODO_ONLY is non-nil, only select lines that are
  14137. also TODO lines."
  14138. (interactive "P")
  14139. (org-prepare-agenda-buffers (list (current-buffer)))
  14140. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14141. (defvar org-cached-props nil)
  14142. (defun org-cached-entry-get (pom property)
  14143. (if (or (eq t org-use-property-inheritance)
  14144. (member property org-use-property-inheritance))
  14145. ;; Caching is not possible, check it directly
  14146. (org-entry-get pom property 'inherit)
  14147. ;; Get all properties, so that we can do complicated checks easily
  14148. (cdr (assoc property (or org-cached-props
  14149. (setq org-cached-props
  14150. (org-entry-properties pom)))))))
  14151. (defun org-global-tags-completion-table (&optional files)
  14152. "Return the list of all tags in all agenda buffer/files."
  14153. (save-excursion
  14154. (org-uniquify
  14155. (delq nil
  14156. (apply 'append
  14157. (mapcar
  14158. (lambda (file)
  14159. (set-buffer (find-file-noselect file))
  14160. (append (org-get-buffer-tags)
  14161. (mapcar (lambda (x) (if (stringp (car-safe x))
  14162. (list (car-safe x)) nil))
  14163. org-tag-alist)))
  14164. (if (and files (car files))
  14165. files
  14166. (org-agenda-files))))))))
  14167. (defun org-make-tags-matcher (match)
  14168. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14169. ;; todo-only is scoped dynamically into this function, and the function
  14170. ;; may change it it the matcher asksk for it.
  14171. (unless match
  14172. ;; Get a new match request, with completion
  14173. (let ((org-last-tags-completion-table
  14174. (org-global-tags-completion-table)))
  14175. (setq match (completing-read
  14176. "Match: " 'org-tags-completion-function nil nil nil
  14177. 'org-tags-history))))
  14178. ;; Parse the string and create a lisp form
  14179. (let ((match0 match)
  14180. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14181. minus tag mm
  14182. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14183. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14184. (if (string-match "/+" match)
  14185. ;; match contains also a todo-matching request
  14186. (progn
  14187. (setq tagsmatch (substring match 0 (match-beginning 0))
  14188. todomatch (substring match (match-end 0)))
  14189. (if (string-match "^!" todomatch)
  14190. (setq todo-only t todomatch (substring todomatch 1)))
  14191. (if (string-match "^\\s-*$" todomatch)
  14192. (setq todomatch nil)))
  14193. ;; only matching tags
  14194. (setq tagsmatch match todomatch nil))
  14195. ;; Make the tags matcher
  14196. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14197. (setq tagsmatcher t)
  14198. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14199. (while (setq term (pop orterms))
  14200. (while (and (equal (substring term -1) "\\") orterms)
  14201. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14202. (while (string-match re term)
  14203. (setq minus (and (match-end 1)
  14204. (equal (match-string 1 term) "-"))
  14205. tag (match-string 2 term)
  14206. re-p (equal (string-to-char tag) ?{)
  14207. level-p (match-end 3)
  14208. prop-p (match-end 4)
  14209. mm (cond
  14210. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14211. (level-p `(= level ,(string-to-number
  14212. (match-string 3 term))))
  14213. (prop-p
  14214. (setq pn (match-string 4 term)
  14215. pv (match-string 5 term)
  14216. cat-p (equal pn "CATEGORY")
  14217. re-p (equal (string-to-char pv) ?{)
  14218. pv (substring pv 1 -1))
  14219. (if (equal pn "CATEGORY")
  14220. (setq gv '(get-text-property (point) 'org-category))
  14221. (setq gv `(org-cached-entry-get nil ,pn)))
  14222. (if re-p
  14223. `(string-match ,pv (or ,gv ""))
  14224. `(equal ,pv (or ,gv ""))))
  14225. (t `(member ,(downcase tag) tags-list)))
  14226. mm (if minus (list 'not mm) mm)
  14227. term (substring term (match-end 0)))
  14228. (push mm tagsmatcher))
  14229. (push (if (> (length tagsmatcher) 1)
  14230. (cons 'and tagsmatcher)
  14231. (car tagsmatcher))
  14232. orlist)
  14233. (setq tagsmatcher nil))
  14234. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14235. (setq tagsmatcher
  14236. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14237. ;; Make the todo matcher
  14238. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14239. (setq todomatcher t)
  14240. (setq orterms (org-split-string todomatch "|") orlist nil)
  14241. (while (setq term (pop orterms))
  14242. (while (string-match re term)
  14243. (setq minus (and (match-end 1)
  14244. (equal (match-string 1 term) "-"))
  14245. kwd (match-string 2 term)
  14246. re-p (equal (string-to-char kwd) ?{)
  14247. term (substring term (match-end 0))
  14248. mm (if re-p
  14249. `(string-match ,(substring kwd 1 -1) todo)
  14250. (list 'equal 'todo kwd))
  14251. mm (if minus (list 'not mm) mm))
  14252. (push mm todomatcher))
  14253. (push (if (> (length todomatcher) 1)
  14254. (cons 'and todomatcher)
  14255. (car todomatcher))
  14256. orlist)
  14257. (setq todomatcher nil))
  14258. (setq todomatcher (if (> (length orlist) 1)
  14259. (cons 'or orlist) (car orlist))))
  14260. ;; Return the string and lisp forms of the matcher
  14261. (setq matcher (if todomatcher
  14262. (list 'and tagsmatcher todomatcher)
  14263. tagsmatcher))
  14264. (cons match0 matcher)))
  14265. (defun org-match-any-p (re list)
  14266. "Does re match any element of list?"
  14267. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14268. (delq nil list))
  14269. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14270. (defvar org-tags-overlay (org-make-overlay 1 1))
  14271. (org-detach-overlay org-tags-overlay)
  14272. (defun org-align-tags-here (to-col)
  14273. ;; Assumes that this is a headline
  14274. (let ((pos (point)) (col (current-column)) tags)
  14275. (beginning-of-line 1)
  14276. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14277. (< pos (match-beginning 2)))
  14278. (progn
  14279. (setq tags (match-string 2))
  14280. (goto-char (match-beginning 1))
  14281. (insert " ")
  14282. (delete-region (point) (1+ (match-end 0)))
  14283. (backward-char 1)
  14284. (move-to-column
  14285. (max (1+ (current-column))
  14286. (1+ col)
  14287. (if (> to-col 0)
  14288. to-col
  14289. (- (abs to-col) (length tags))))
  14290. t)
  14291. (insert tags)
  14292. (move-to-column (min (current-column) col) t))
  14293. (goto-char pos))))
  14294. (defun org-set-tags (&optional arg just-align)
  14295. "Set the tags for the current headline.
  14296. With prefix ARG, realign all tags in headings in the current buffer."
  14297. (interactive "P")
  14298. (let* ((re (concat "^" outline-regexp))
  14299. (current (org-get-tags-string))
  14300. (col (current-column))
  14301. (org-setting-tags t)
  14302. table current-tags inherited-tags ; computed below when needed
  14303. tags p0 c0 c1 rpl)
  14304. (if arg
  14305. (save-excursion
  14306. (goto-char (point-min))
  14307. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14308. (while (re-search-forward re nil t)
  14309. (org-set-tags nil t)
  14310. (end-of-line 1)))
  14311. (message "All tags realigned to column %d" org-tags-column))
  14312. (if just-align
  14313. (setq tags current)
  14314. ;; Get a new set of tags from the user
  14315. (save-excursion
  14316. (setq table (or org-tag-alist (org-get-buffer-tags))
  14317. org-last-tags-completion-table table
  14318. current-tags (org-split-string current ":")
  14319. inherited-tags (nreverse
  14320. (nthcdr (length current-tags)
  14321. (nreverse (org-get-tags-at))))
  14322. tags
  14323. (if (or (eq t org-use-fast-tag-selection)
  14324. (and org-use-fast-tag-selection
  14325. (delq nil (mapcar 'cdr table))))
  14326. (org-fast-tag-selection
  14327. current-tags inherited-tags table
  14328. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14329. (let ((org-add-colon-after-tag-completion t))
  14330. (org-trim
  14331. (org-without-partial-completion
  14332. (completing-read "Tags: " 'org-tags-completion-function
  14333. nil nil current 'org-tags-history)))))))
  14334. (while (string-match "[-+&]+" tags)
  14335. ;; No boolean logic, just a list
  14336. (setq tags (replace-match ":" t t tags))))
  14337. (if (string-match "\\`[\t ]*\\'" tags)
  14338. (setq tags "")
  14339. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14340. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14341. ;; Insert new tags at the correct column
  14342. (beginning-of-line 1)
  14343. (cond
  14344. ((and (equal current "") (equal tags "")))
  14345. ((re-search-forward
  14346. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14347. (point-at-eol) t)
  14348. (if (equal tags "")
  14349. (setq rpl "")
  14350. (goto-char (match-beginning 0))
  14351. (setq c0 (current-column) p0 (point)
  14352. c1 (max (1+ c0) (if (> org-tags-column 0)
  14353. org-tags-column
  14354. (- (- org-tags-column) (length tags))))
  14355. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14356. (replace-match rpl t t)
  14357. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14358. tags)
  14359. (t (error "Tags alignment failed")))
  14360. (move-to-column col)
  14361. (unless just-align
  14362. (run-hooks 'org-after-tags-change-hook)))))
  14363. (defun org-change-tag-in-region (beg end tag off)
  14364. "Add or remove TAG for each entry in the region.
  14365. This works in the agenda, and also in an org-mode buffer."
  14366. (interactive
  14367. (list (region-beginning) (region-end)
  14368. (let ((org-last-tags-completion-table
  14369. (if (org-mode-p)
  14370. (org-get-buffer-tags)
  14371. (org-global-tags-completion-table))))
  14372. (completing-read
  14373. "Tag: " 'org-tags-completion-function nil nil nil
  14374. 'org-tags-history))
  14375. (progn
  14376. (message "[s]et or [r]emove? ")
  14377. (equal (read-char-exclusive) ?r))))
  14378. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14379. (let ((agendap (equal major-mode 'org-agenda-mode))
  14380. l1 l2 m buf pos newhead (cnt 0))
  14381. (goto-char end)
  14382. (setq l2 (1- (org-current-line)))
  14383. (goto-char beg)
  14384. (setq l1 (org-current-line))
  14385. (loop for l from l1 to l2 do
  14386. (goto-line l)
  14387. (setq m (get-text-property (point) 'org-hd-marker))
  14388. (when (or (and (org-mode-p) (org-on-heading-p))
  14389. (and agendap m))
  14390. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14391. pos (if agendap m (point)))
  14392. (with-current-buffer buf
  14393. (save-excursion
  14394. (save-restriction
  14395. (goto-char pos)
  14396. (setq cnt (1+ cnt))
  14397. (org-toggle-tag tag (if off 'off 'on))
  14398. (setq newhead (org-get-heading)))))
  14399. (and agendap (org-agenda-change-all-lines newhead m))))
  14400. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14401. (defun org-tags-completion-function (string predicate &optional flag)
  14402. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14403. (confirm (lambda (x) (stringp (car x)))))
  14404. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14405. (setq s1 (match-string 1 string)
  14406. s2 (match-string 2 string))
  14407. (setq s1 "" s2 string))
  14408. (cond
  14409. ((eq flag nil)
  14410. ;; try completion
  14411. (setq rtn (try-completion s2 ctable confirm))
  14412. (if (stringp rtn)
  14413. (setq rtn
  14414. (concat s1 s2 (substring rtn (length s2))
  14415. (if (and org-add-colon-after-tag-completion
  14416. (assoc rtn ctable))
  14417. ":" ""))))
  14418. rtn)
  14419. ((eq flag t)
  14420. ;; all-completions
  14421. (all-completions s2 ctable confirm)
  14422. )
  14423. ((eq flag 'lambda)
  14424. ;; exact match?
  14425. (assoc s2 ctable)))
  14426. ))
  14427. (defun org-fast-tag-insert (kwd tags face &optional end)
  14428. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14429. (insert (format "%-12s" (concat kwd ":"))
  14430. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14431. (or end "")))
  14432. (defun org-fast-tag-show-exit (flag)
  14433. (save-excursion
  14434. (goto-line 3)
  14435. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14436. (replace-match ""))
  14437. (when flag
  14438. (end-of-line 1)
  14439. (move-to-column (- (window-width) 19) t)
  14440. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14441. (defun org-set-current-tags-overlay (current prefix)
  14442. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14443. (if (featurep 'xemacs)
  14444. (org-overlay-display org-tags-overlay (concat prefix s)
  14445. 'secondary-selection)
  14446. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14447. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14448. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14449. "Fast tag selection with single keys.
  14450. CURRENT is the current list of tags in the headline, INHERITED is the
  14451. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14452. possibly with grouping information. TODO-TABLE is a similar table with
  14453. TODO keywords, should these have keys assigned to them.
  14454. If the keys are nil, a-z are automatically assigned.
  14455. Returns the new tags string, or nil to not change the current settings."
  14456. (let* ((fulltable (append table todo-table))
  14457. (maxlen (apply 'max (mapcar
  14458. (lambda (x)
  14459. (if (stringp (car x)) (string-width (car x)) 0))
  14460. fulltable)))
  14461. (buf (current-buffer))
  14462. (expert (eq org-fast-tag-selection-single-key 'expert))
  14463. (buffer-tags nil)
  14464. (fwidth (+ maxlen 3 1 3))
  14465. (ncol (/ (- (window-width) 4) fwidth))
  14466. (i-face 'org-done)
  14467. (c-face 'org-todo)
  14468. tg cnt e c char c1 c2 ntable tbl rtn
  14469. ov-start ov-end ov-prefix
  14470. (exit-after-next org-fast-tag-selection-single-key)
  14471. (done-keywords org-done-keywords)
  14472. groups ingroup)
  14473. (save-excursion
  14474. (beginning-of-line 1)
  14475. (if (looking-at
  14476. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14477. (setq ov-start (match-beginning 1)
  14478. ov-end (match-end 1)
  14479. ov-prefix "")
  14480. (setq ov-start (1- (point-at-eol))
  14481. ov-end (1+ ov-start))
  14482. (skip-chars-forward "^\n\r")
  14483. (setq ov-prefix
  14484. (concat
  14485. (buffer-substring (1- (point)) (point))
  14486. (if (> (current-column) org-tags-column)
  14487. " "
  14488. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14489. (org-move-overlay org-tags-overlay ov-start ov-end)
  14490. (save-window-excursion
  14491. (if expert
  14492. (set-buffer (get-buffer-create " *Org tags*"))
  14493. (delete-other-windows)
  14494. (split-window-vertically)
  14495. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14496. (erase-buffer)
  14497. (org-set-local 'org-done-keywords done-keywords)
  14498. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14499. (org-fast-tag-insert "Current" current c-face "\n\n")
  14500. (org-fast-tag-show-exit exit-after-next)
  14501. (org-set-current-tags-overlay current ov-prefix)
  14502. (setq tbl fulltable char ?a cnt 0)
  14503. (while (setq e (pop tbl))
  14504. (cond
  14505. ((equal e '(:startgroup))
  14506. (push '() groups) (setq ingroup t)
  14507. (when (not (= cnt 0))
  14508. (setq cnt 0)
  14509. (insert "\n"))
  14510. (insert "{ "))
  14511. ((equal e '(:endgroup))
  14512. (setq ingroup nil cnt 0)
  14513. (insert "}\n"))
  14514. (t
  14515. (setq tg (car e) c2 nil)
  14516. (if (cdr e)
  14517. (setq c (cdr e))
  14518. ;; automatically assign a character.
  14519. (setq c1 (string-to-char
  14520. (downcase (substring
  14521. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14522. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14523. (while (or (rassoc char ntable) (rassoc char table))
  14524. (setq char (1+ char)))
  14525. (setq c2 c1))
  14526. (setq c (or c2 char)))
  14527. (if ingroup (push tg (car groups)))
  14528. (setq tg (org-add-props tg nil 'face
  14529. (cond
  14530. ((not (assoc tg table))
  14531. (org-get-todo-face tg))
  14532. ((member tg current) c-face)
  14533. ((member tg inherited) i-face)
  14534. (t nil))))
  14535. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14536. (insert "[" c "] " tg (make-string
  14537. (- fwidth 4 (length tg)) ?\ ))
  14538. (push (cons tg c) ntable)
  14539. (when (= (setq cnt (1+ cnt)) ncol)
  14540. (insert "\n")
  14541. (if ingroup (insert " "))
  14542. (setq cnt 0)))))
  14543. (setq ntable (nreverse ntable))
  14544. (insert "\n")
  14545. (goto-char (point-min))
  14546. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14547. (fit-window-to-buffer))
  14548. (setq rtn
  14549. (catch 'exit
  14550. (while t
  14551. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14552. (if groups " [!] no groups" " [!]groups")
  14553. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14554. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14555. (cond
  14556. ((= c ?\r) (throw 'exit t))
  14557. ((= c ?!)
  14558. (setq groups (not groups))
  14559. (goto-char (point-min))
  14560. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14561. ((= c ?\C-c)
  14562. (if (not expert)
  14563. (org-fast-tag-show-exit
  14564. (setq exit-after-next (not exit-after-next)))
  14565. (setq expert nil)
  14566. (delete-other-windows)
  14567. (split-window-vertically)
  14568. (org-switch-to-buffer-other-window " *Org tags*")
  14569. (and (fboundp 'fit-window-to-buffer)
  14570. (fit-window-to-buffer))))
  14571. ((or (= c ?\C-g)
  14572. (and (= c ?q) (not (rassoc c ntable))))
  14573. (org-detach-overlay org-tags-overlay)
  14574. (setq quit-flag t))
  14575. ((= c ?\ )
  14576. (setq current nil)
  14577. (if exit-after-next (setq exit-after-next 'now)))
  14578. ((= c ?\t)
  14579. (condition-case nil
  14580. (setq tg (completing-read
  14581. "Tag: "
  14582. (or buffer-tags
  14583. (with-current-buffer buf
  14584. (org-get-buffer-tags)))))
  14585. (quit (setq tg "")))
  14586. (when (string-match "\\S-" tg)
  14587. (add-to-list 'buffer-tags (list tg))
  14588. (if (member tg current)
  14589. (setq current (delete tg current))
  14590. (push tg current)))
  14591. (if exit-after-next (setq exit-after-next 'now)))
  14592. ((setq e (rassoc c todo-table) tg (car e))
  14593. (with-current-buffer buf
  14594. (save-excursion (org-todo tg)))
  14595. (if exit-after-next (setq exit-after-next 'now)))
  14596. ((setq e (rassoc c ntable) tg (car e))
  14597. (if (member tg current)
  14598. (setq current (delete tg current))
  14599. (loop for g in groups do
  14600. (if (member tg g)
  14601. (mapc (lambda (x)
  14602. (setq current (delete x current)))
  14603. g)))
  14604. (push tg current))
  14605. (if exit-after-next (setq exit-after-next 'now))))
  14606. ;; Create a sorted list
  14607. (setq current
  14608. (sort current
  14609. (lambda (a b)
  14610. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14611. (if (eq exit-after-next 'now) (throw 'exit t))
  14612. (goto-char (point-min))
  14613. (beginning-of-line 2)
  14614. (delete-region (point) (point-at-eol))
  14615. (org-fast-tag-insert "Current" current c-face)
  14616. (org-set-current-tags-overlay current ov-prefix)
  14617. (while (re-search-forward
  14618. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14619. (setq tg (match-string 1))
  14620. (add-text-properties
  14621. (match-beginning 1) (match-end 1)
  14622. (list 'face
  14623. (cond
  14624. ((member tg current) c-face)
  14625. ((member tg inherited) i-face)
  14626. (t (get-text-property (match-beginning 1) 'face))))))
  14627. (goto-char (point-min)))))
  14628. (org-detach-overlay org-tags-overlay)
  14629. (if rtn
  14630. (mapconcat 'identity current ":")
  14631. nil))))
  14632. (defun org-get-tags-string ()
  14633. "Get the TAGS string in the current headline."
  14634. (unless (org-on-heading-p t)
  14635. (error "Not on a heading"))
  14636. (save-excursion
  14637. (beginning-of-line 1)
  14638. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14639. (org-match-string-no-properties 1)
  14640. "")))
  14641. (defun org-get-tags ()
  14642. "Get the list of tags specified in the current headline."
  14643. (org-split-string (org-get-tags-string) ":"))
  14644. (defun org-get-buffer-tags ()
  14645. "Get a table of all tags used in the buffer, for completion."
  14646. (let (tags)
  14647. (save-excursion
  14648. (goto-char (point-min))
  14649. (while (re-search-forward
  14650. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14651. (when (equal (char-after (point-at-bol 0)) ?*)
  14652. (mapc (lambda (x) (add-to-list 'tags x))
  14653. (org-split-string (org-match-string-no-properties 1) ":")))))
  14654. (mapcar 'list tags)))
  14655. ;;;; Properties
  14656. ;;; Setting and retrieving properties
  14657. (defconst org-special-properties
  14658. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14659. "TIMESTAMP" "TIMESTAMP_IA")
  14660. "The special properties valid in Org-mode.
  14661. These are properties that are not defined in the property drawer,
  14662. but in some other way.")
  14663. (defconst org-default-properties
  14664. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14665. "LOCATION" "LOGGING" "COLUMNS")
  14666. "Some properties that are used by Org-mode for various purposes.
  14667. Being in this list makes sure that they are offered for completion.")
  14668. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14669. "Regular expression matching the first line of a property drawer.")
  14670. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14671. "Regular expression matching the first line of a property drawer.")
  14672. (defun org-property-action ()
  14673. "Do an action on properties."
  14674. (interactive)
  14675. (let (c)
  14676. (org-at-property-p)
  14677. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14678. (setq c (read-char-exclusive))
  14679. (cond
  14680. ((equal c ?s)
  14681. (call-interactively 'org-set-property))
  14682. ((equal c ?d)
  14683. (call-interactively 'org-delete-property))
  14684. ((equal c ?D)
  14685. (call-interactively 'org-delete-property-globally))
  14686. ((equal c ?c)
  14687. (call-interactively 'org-compute-property-at-point))
  14688. (t (error "No such property action %c" c)))))
  14689. (defun org-at-property-p ()
  14690. "Is the cursor in a property line?"
  14691. ;; FIXME: Does not check if we are actually in the drawer.
  14692. ;; FIXME: also returns true on any drawers.....
  14693. ;; This is used by C-c C-c for property action.
  14694. (save-excursion
  14695. (beginning-of-line 1)
  14696. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14697. (defmacro org-with-point-at (pom &rest body)
  14698. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14699. (declare (indent 1) (debug t))
  14700. `(save-excursion
  14701. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14702. (save-excursion
  14703. (goto-char (or pom (point)))
  14704. ,@body)))
  14705. (defun org-get-property-block (&optional beg end force)
  14706. "Return the (beg . end) range of the body of the property drawer.
  14707. BEG and END can be beginning and end of subtree, if not given
  14708. they will be found.
  14709. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14710. (catch 'exit
  14711. (save-excursion
  14712. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14713. (end (or end (progn (outline-next-heading) (point)))))
  14714. (goto-char beg)
  14715. (if (re-search-forward org-property-start-re end t)
  14716. (setq beg (1+ (match-end 0)))
  14717. (if force
  14718. (save-excursion
  14719. (org-insert-property-drawer)
  14720. (setq end (progn (outline-next-heading) (point))))
  14721. (throw 'exit nil))
  14722. (goto-char beg)
  14723. (if (re-search-forward org-property-start-re end t)
  14724. (setq beg (1+ (match-end 0)))))
  14725. (if (re-search-forward org-property-end-re end t)
  14726. (setq end (match-beginning 0))
  14727. (or force (throw 'exit nil))
  14728. (goto-char beg)
  14729. (setq end beg)
  14730. (org-indent-line-function)
  14731. (insert ":END:\n"))
  14732. (cons beg end)))))
  14733. (defun org-entry-properties (&optional pom which)
  14734. "Get all properties of the entry at point-or-marker POM.
  14735. This includes the TODO keyword, the tags, time strings for deadline,
  14736. scheduled, and clocking, and any additional properties defined in the
  14737. entry. The return value is an alist, keys may occur multiple times
  14738. if the property key was used several times.
  14739. POM may also be nil, in which case the current entry is used.
  14740. If WHICH is nil or `all', get all properties. If WHICH is
  14741. `special' or `standard', only get that subclass."
  14742. (setq which (or which 'all))
  14743. (org-with-point-at pom
  14744. (let ((clockstr (substring org-clock-string 0 -1))
  14745. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14746. beg end range props sum-props key value string clocksum)
  14747. (save-excursion
  14748. (when (condition-case nil (org-back-to-heading t) (error nil))
  14749. (setq beg (point))
  14750. (setq sum-props (get-text-property (point) 'org-summaries))
  14751. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14752. (outline-next-heading)
  14753. (setq end (point))
  14754. (when (memq which '(all special))
  14755. ;; Get the special properties, like TODO and tags
  14756. (goto-char beg)
  14757. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14758. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14759. (when (looking-at org-priority-regexp)
  14760. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14761. (when (and (setq value (org-get-tags-string))
  14762. (string-match "\\S-" value))
  14763. (push (cons "TAGS" value) props))
  14764. (when (setq value (org-get-tags-at))
  14765. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14766. props))
  14767. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14768. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14769. string (if (equal key clockstr)
  14770. (org-no-properties
  14771. (org-trim
  14772. (buffer-substring
  14773. (match-beginning 3) (goto-char (point-at-eol)))))
  14774. (substring (org-match-string-no-properties 3) 1 -1)))
  14775. (unless key
  14776. (if (= (char-after (match-beginning 3)) ?\[)
  14777. (setq key "TIMESTAMP_IA")
  14778. (setq key "TIMESTAMP")))
  14779. (when (or (equal key clockstr) (not (assoc key props)))
  14780. (push (cons key string) props)))
  14781. )
  14782. (when (memq which '(all standard))
  14783. ;; Get the standard properties, like :PORP: ...
  14784. (setq range (org-get-property-block beg end))
  14785. (when range
  14786. (goto-char (car range))
  14787. (while (re-search-forward
  14788. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14789. (cdr range) t)
  14790. (setq key (org-match-string-no-properties 1)
  14791. value (org-trim (or (org-match-string-no-properties 2) "")))
  14792. (unless (member key excluded)
  14793. (push (cons key (or value "")) props)))))
  14794. (if clocksum
  14795. (push (cons "CLOCKSUM"
  14796. (org-column-number-to-string (/ (float clocksum) 60.)
  14797. 'add_times))
  14798. props))
  14799. (append sum-props (nreverse props)))))))
  14800. (defun org-entry-get (pom property &optional inherit)
  14801. "Get value of PROPERTY for entry at point-or-marker POM.
  14802. If INHERIT is non-nil and the entry does not have the property,
  14803. then also check higher levels of the hierarchy.
  14804. If the property is present but empty, the return value is the empty string.
  14805. If the property is not present at all, nil is returned."
  14806. (org-with-point-at pom
  14807. (if inherit
  14808. (org-entry-get-with-inheritance property)
  14809. (if (member property org-special-properties)
  14810. ;; We need a special property. Use brute force, get all properties.
  14811. (cdr (assoc property (org-entry-properties nil 'special)))
  14812. (let ((range (org-get-property-block)))
  14813. (if (and range
  14814. (goto-char (car range))
  14815. (re-search-forward
  14816. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14817. (cdr range) t))
  14818. ;; Found the property, return it.
  14819. (if (match-end 1)
  14820. (org-match-string-no-properties 1)
  14821. "")))))))
  14822. (defun org-entry-delete (pom property)
  14823. "Delete the property PROPERTY from entry at point-or-marker POM."
  14824. (org-with-point-at pom
  14825. (if (member property org-special-properties)
  14826. nil ; cannot delete these properties.
  14827. (let ((range (org-get-property-block)))
  14828. (if (and range
  14829. (goto-char (car range))
  14830. (re-search-forward
  14831. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14832. (cdr range) t))
  14833. (progn
  14834. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14835. t)
  14836. nil)))))
  14837. ;; Multi-values properties are properties that contain multiple values
  14838. ;; These values are assumed to be single words, separated by whitespace.
  14839. (defun org-entry-add-to-multivalued-property (pom property value)
  14840. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14841. (let* ((old (org-entry-get pom property))
  14842. (values (and old (org-split-string old "[ \t]"))))
  14843. (unless (member value values)
  14844. (setq values (cons value values))
  14845. (org-entry-put pom property
  14846. (mapconcat 'identity values " ")))))
  14847. (defun org-entry-remove-from-multivalued-property (pom property value)
  14848. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14849. (let* ((old (org-entry-get pom property))
  14850. (values (and old (org-split-string old "[ \t]"))))
  14851. (when (member value values)
  14852. (setq values (delete value values))
  14853. (org-entry-put pom property
  14854. (mapconcat 'identity values " ")))))
  14855. (defun org-entry-member-in-multivalued-property (pom property value)
  14856. "Is VALUE one of 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. (member value values)))
  14860. (defvar org-entry-property-inherited-from (make-marker))
  14861. (defun org-entry-get-with-inheritance (property)
  14862. "Get entry property, and search higher levels if not present."
  14863. (let (tmp)
  14864. (save-excursion
  14865. (save-restriction
  14866. (widen)
  14867. (catch 'ex
  14868. (while t
  14869. (when (setq tmp (org-entry-get nil property))
  14870. (org-back-to-heading t)
  14871. (move-marker org-entry-property-inherited-from (point))
  14872. (throw 'ex tmp))
  14873. (or (org-up-heading-safe) (throw 'ex nil)))))
  14874. (or tmp (cdr (assoc property org-local-properties))
  14875. (cdr (assoc property org-global-properties))))))
  14876. (defun org-entry-put (pom property value)
  14877. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14878. (org-with-point-at pom
  14879. (org-back-to-heading t)
  14880. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14881. range)
  14882. (cond
  14883. ((equal property "TODO")
  14884. (when (and (stringp value) (string-match "\\S-" value)
  14885. (not (member value org-todo-keywords-1)))
  14886. (error "\"%s\" is not a valid TODO state" value))
  14887. (if (or (not value)
  14888. (not (string-match "\\S-" value)))
  14889. (setq value 'none))
  14890. (org-todo value)
  14891. (org-set-tags nil 'align))
  14892. ((equal property "PRIORITY")
  14893. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14894. (string-to-char value) ?\ ))
  14895. (org-set-tags nil 'align))
  14896. ((equal property "SCHEDULED")
  14897. (if (re-search-forward org-scheduled-time-regexp end t)
  14898. (cond
  14899. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14900. ((eq value 'later) (org-timestamp-change 1 'day))
  14901. (t (call-interactively 'org-schedule)))
  14902. (call-interactively 'org-schedule)))
  14903. ((equal property "DEADLINE")
  14904. (if (re-search-forward org-deadline-time-regexp end t)
  14905. (cond
  14906. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14907. ((eq value 'later) (org-timestamp-change 1 'day))
  14908. (t (call-interactively 'org-deadline)))
  14909. (call-interactively 'org-deadline)))
  14910. ((member property org-special-properties)
  14911. (error "The %s property can not yet be set with `org-entry-put'"
  14912. property))
  14913. (t ; a non-special property
  14914. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14915. (setq range (org-get-property-block beg end 'force))
  14916. (goto-char (car range))
  14917. (if (re-search-forward
  14918. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14919. (progn
  14920. (delete-region (match-beginning 1) (match-end 1))
  14921. (goto-char (match-beginning 1)))
  14922. (goto-char (cdr range))
  14923. (insert "\n")
  14924. (backward-char 1)
  14925. (org-indent-line-function)
  14926. (insert ":" property ":"))
  14927. (and value (insert " " value))
  14928. (org-indent-line-function)))))))
  14929. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14930. "Get all property keys in the current buffer.
  14931. With INCLUDE-SPECIALS, also list the special properties that relect things
  14932. like tags and TODO state.
  14933. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14934. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14935. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14936. formats in the current buffer."
  14937. (let (rtn range cfmt cols s p)
  14938. (save-excursion
  14939. (save-restriction
  14940. (widen)
  14941. (goto-char (point-min))
  14942. (while (re-search-forward org-property-start-re nil t)
  14943. (setq range (org-get-property-block))
  14944. (goto-char (car range))
  14945. (while (re-search-forward
  14946. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14947. (cdr range) t)
  14948. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14949. (outline-next-heading))))
  14950. (when include-specials
  14951. (setq rtn (append org-special-properties rtn)))
  14952. (when include-defaults
  14953. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14954. (when include-columns
  14955. (save-excursion
  14956. (save-restriction
  14957. (widen)
  14958. (goto-char (point-min))
  14959. (while (re-search-forward
  14960. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14961. nil t)
  14962. (setq cfmt (match-string 2) s 0)
  14963. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14964. cfmt s)
  14965. (setq s (match-end 0)
  14966. p (match-string 1 cfmt))
  14967. (unless (or (equal p "ITEM")
  14968. (member p org-special-properties))
  14969. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14970. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14971. (defun org-property-values (key)
  14972. "Return a list of all values of property KEY."
  14973. (save-excursion
  14974. (save-restriction
  14975. (widen)
  14976. (goto-char (point-min))
  14977. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14978. values)
  14979. (while (re-search-forward re nil t)
  14980. (add-to-list 'values (org-trim (match-string 1))))
  14981. (delete "" values)))))
  14982. (defun org-insert-property-drawer ()
  14983. "Insert a property drawer into the current entry."
  14984. (interactive)
  14985. (org-back-to-heading t)
  14986. (looking-at outline-regexp)
  14987. (let ((indent (- (match-end 0)(match-beginning 0)))
  14988. (beg (point))
  14989. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14990. end hiddenp)
  14991. (outline-next-heading)
  14992. (setq end (point))
  14993. (goto-char beg)
  14994. (while (re-search-forward re end t))
  14995. (setq hiddenp (org-invisible-p))
  14996. (end-of-line 1)
  14997. (and (equal (char-after) ?\n) (forward-char 1))
  14998. (org-skip-over-state-notes)
  14999. (skip-chars-backward " \t\n\r")
  15000. (if (eq (char-before) ?*) (forward-char 1))
  15001. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15002. (beginning-of-line 0)
  15003. (indent-to-column indent)
  15004. (beginning-of-line 2)
  15005. (indent-to-column indent)
  15006. (beginning-of-line 0)
  15007. (if hiddenp
  15008. (save-excursion
  15009. (org-back-to-heading t)
  15010. (hide-entry))
  15011. (org-flag-drawer t))))
  15012. (defun org-set-property (property value)
  15013. "In the current entry, set PROPERTY to VALUE.
  15014. When called interactively, this will prompt for a property name, offering
  15015. completion on existing and default properties. And then it will prompt
  15016. for a value, offering competion either on allowed values (via an inherited
  15017. xxx_ALL property) or on existing values in other instances of this property
  15018. in the current file."
  15019. (interactive
  15020. (let* ((prop (completing-read
  15021. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15022. (cur (org-entry-get nil prop))
  15023. (allowed (org-property-get-allowed-values nil prop 'table))
  15024. (existing (mapcar 'list (org-property-values prop)))
  15025. (val (if allowed
  15026. (completing-read "Value: " allowed nil 'req-match)
  15027. (completing-read
  15028. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15029. (concat "[" cur "]") "")
  15030. ": ")
  15031. existing nil nil "" nil cur))))
  15032. (list prop (if (equal val "") cur val))))
  15033. (unless (equal (org-entry-get nil property) value)
  15034. (org-entry-put nil property value)))
  15035. (defun org-delete-property (property)
  15036. "In the current entry, delete PROPERTY."
  15037. (interactive
  15038. (let* ((prop (completing-read
  15039. "Property: " (org-entry-properties nil 'standard))))
  15040. (list prop)))
  15041. (message "Property %s %s" property
  15042. (if (org-entry-delete nil property)
  15043. "deleted"
  15044. "was not present in the entry")))
  15045. (defun org-delete-property-globally (property)
  15046. "Remove PROPERTY globally, from all entries."
  15047. (interactive
  15048. (let* ((prop (completing-read
  15049. "Globally remove property: "
  15050. (mapcar 'list (org-buffer-property-keys)))))
  15051. (list prop)))
  15052. (save-excursion
  15053. (save-restriction
  15054. (widen)
  15055. (goto-char (point-min))
  15056. (let ((cnt 0))
  15057. (while (re-search-forward
  15058. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15059. nil t)
  15060. (setq cnt (1+ cnt))
  15061. (replace-match ""))
  15062. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15063. (defvar org-columns-current-fmt-compiled) ; defined below
  15064. (defun org-compute-property-at-point ()
  15065. "Compute the property at point.
  15066. This looks for an enclosing column format, extracts the operator and
  15067. then applies it to the proerty in the column format's scope."
  15068. (interactive)
  15069. (unless (org-at-property-p)
  15070. (error "Not at a property"))
  15071. (let ((prop (org-match-string-no-properties 2)))
  15072. (org-columns-get-format-and-top-level)
  15073. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15074. (error "No operator defined for property %s" prop))
  15075. (org-columns-compute prop)))
  15076. (defun org-property-get-allowed-values (pom property &optional table)
  15077. "Get allowed values for the property PROPERTY.
  15078. When TABLE is non-nil, return an alist that can directly be used for
  15079. completion."
  15080. (let (vals)
  15081. (cond
  15082. ((equal property "TODO")
  15083. (setq vals (org-with-point-at pom
  15084. (append org-todo-keywords-1 '("")))))
  15085. ((equal property "PRIORITY")
  15086. (let ((n org-lowest-priority))
  15087. (while (>= n org-highest-priority)
  15088. (push (char-to-string n) vals)
  15089. (setq n (1- n)))))
  15090. ((member property org-special-properties))
  15091. (t
  15092. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15093. (when (and vals (string-match "\\S-" vals))
  15094. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15095. (setq vals (mapcar (lambda (x)
  15096. (cond ((stringp x) x)
  15097. ((numberp x) (number-to-string x))
  15098. ((symbolp x) (symbol-name x))
  15099. (t "???")))
  15100. vals)))))
  15101. (if table (mapcar 'list vals) vals)))
  15102. (defun org-property-previous-allowed-value (&optional previous)
  15103. "Switch to the next allowed value for this property."
  15104. (interactive)
  15105. (org-property-next-allowed-value t))
  15106. (defun org-property-next-allowed-value (&optional previous)
  15107. "Switch to the next allowed value for this property."
  15108. (interactive)
  15109. (unless (org-at-property-p)
  15110. (error "Not at a property"))
  15111. (let* ((key (match-string 2))
  15112. (value (match-string 3))
  15113. (allowed (or (org-property-get-allowed-values (point) key)
  15114. (and (member value '("[ ]" "[-]" "[X]"))
  15115. '("[ ]" "[X]"))))
  15116. nval)
  15117. (unless allowed
  15118. (error "Allowed values for this property have not been defined"))
  15119. (if previous (setq allowed (reverse allowed)))
  15120. (if (member value allowed)
  15121. (setq nval (car (cdr (member value allowed)))))
  15122. (setq nval (or nval (car allowed)))
  15123. (if (equal nval value)
  15124. (error "Only one allowed value for this property"))
  15125. (org-at-property-p)
  15126. (replace-match (concat " :" key ": " nval) t t)
  15127. (org-indent-line-function)
  15128. (beginning-of-line 1)
  15129. (skip-chars-forward " \t")))
  15130. (defun org-find-entry-with-id (ident)
  15131. "Locate the entry that contains the ID property with exact value IDENT.
  15132. IDENT can be a string, a symbol or a number, this function will search for
  15133. the string representation of it.
  15134. Return the position where this entry starts, or nil if there is no such entry."
  15135. (let ((id (cond
  15136. ((stringp ident) ident)
  15137. ((symbol-name ident) (symbol-name ident))
  15138. ((numberp ident) (number-to-string ident))
  15139. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15140. (case-fold-search nil))
  15141. (save-excursion
  15142. (save-restriction
  15143. (widen)
  15144. (goto-char (point-min))
  15145. (when (re-search-forward
  15146. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15147. nil t)
  15148. (org-back-to-heading)
  15149. (point))))))
  15150. ;;; Column View
  15151. (defvar org-columns-overlays nil
  15152. "Holds the list of current column overlays.")
  15153. (defvar org-columns-current-fmt nil
  15154. "Local variable, holds the currently active column format.")
  15155. (defvar org-columns-current-fmt-compiled nil
  15156. "Local variable, holds the currently active column format.
  15157. This is the compiled version of the format.")
  15158. (defvar org-columns-current-widths nil
  15159. "Loval variable, holds the currently widths of fields.")
  15160. (defvar org-columns-current-maxwidths nil
  15161. "Loval variable, holds the currently active maximum column widths.")
  15162. (defvar org-columns-begin-marker (make-marker)
  15163. "Points to the position where last a column creation command was called.")
  15164. (defvar org-columns-top-level-marker (make-marker)
  15165. "Points to the position where current columns region starts.")
  15166. (defvar org-columns-map (make-sparse-keymap)
  15167. "The keymap valid in column display.")
  15168. (defun org-columns-content ()
  15169. "Switch to contents view while in columns view."
  15170. (interactive)
  15171. (org-overview)
  15172. (org-content))
  15173. (org-defkey org-columns-map "c" 'org-columns-content)
  15174. (org-defkey org-columns-map "o" 'org-overview)
  15175. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15176. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15177. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15178. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15179. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15180. (org-defkey org-columns-map "q" 'org-columns-quit)
  15181. (org-defkey org-columns-map "r" 'org-columns-redo)
  15182. (org-defkey org-columns-map "g" 'org-columns-redo)
  15183. (org-defkey org-columns-map [left] 'backward-char)
  15184. (org-defkey org-columns-map "\M-b" 'backward-char)
  15185. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15186. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15187. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15188. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15189. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15190. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15191. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15192. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15193. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15194. (org-defkey org-columns-map ">" 'org-columns-widen)
  15195. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15196. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15197. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15198. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15199. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15200. '("Column"
  15201. ["Edit property" org-columns-edit-value t]
  15202. ["Next allowed value" org-columns-next-allowed-value t]
  15203. ["Previous allowed value" org-columns-previous-allowed-value t]
  15204. ["Show full value" org-columns-show-value t]
  15205. ["Edit allowed values" org-columns-edit-allowed t]
  15206. "--"
  15207. ["Edit column attributes" org-columns-edit-attributes t]
  15208. ["Increase column width" org-columns-widen t]
  15209. ["Decrease column width" org-columns-narrow t]
  15210. "--"
  15211. ["Move column right" org-columns-move-right t]
  15212. ["Move column left" org-columns-move-left t]
  15213. ["Add column" org-columns-new t]
  15214. ["Delete column" org-columns-delete t]
  15215. "--"
  15216. ["CONTENTS" org-columns-content t]
  15217. ["OVERVIEW" org-overview t]
  15218. ["Refresh columns display" org-columns-redo t]
  15219. "--"
  15220. ["Open link" org-columns-open-link t]
  15221. "--"
  15222. ["Quit" org-columns-quit t]))
  15223. (defun org-columns-new-overlay (beg end &optional string face)
  15224. "Create a new column overlay and add it to the list."
  15225. (let ((ov (org-make-overlay beg end)))
  15226. (org-overlay-put ov 'face (or face 'secondary-selection))
  15227. (org-overlay-display ov string face)
  15228. (push ov org-columns-overlays)
  15229. ov))
  15230. (defun org-columns-display-here (&optional props)
  15231. "Overlay the current line with column display."
  15232. (interactive)
  15233. (let* ((fmt org-columns-current-fmt-compiled)
  15234. (beg (point-at-bol))
  15235. (level-face (save-excursion
  15236. (beginning-of-line 1)
  15237. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15238. (org-get-level-face 2))))
  15239. (color (list :foreground
  15240. (face-attribute (or level-face 'default) :foreground)))
  15241. props pom property ass width f string ov column val modval)
  15242. ;; Check if the entry is in another buffer.
  15243. (unless props
  15244. (if (eq major-mode 'org-agenda-mode)
  15245. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15246. (get-text-property (point) 'org-marker))
  15247. props (if pom (org-entry-properties pom) nil))
  15248. (setq props (org-entry-properties nil))))
  15249. ;; Walk the format
  15250. (while (setq column (pop fmt))
  15251. (setq property (car column)
  15252. ass (if (equal property "ITEM")
  15253. (cons "ITEM"
  15254. (save-match-data
  15255. (org-no-properties
  15256. (org-remove-tabs
  15257. (buffer-substring-no-properties
  15258. (point-at-bol) (point-at-eol))))))
  15259. (assoc property props))
  15260. width (or (cdr (assoc property org-columns-current-maxwidths))
  15261. (nth 2 column)
  15262. (length property))
  15263. f (format "%%-%d.%ds | " width width)
  15264. val (or (cdr ass) "")
  15265. modval (if (equal property "ITEM")
  15266. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15267. string (format f (or modval val)))
  15268. ;; Create the overlay
  15269. (org-unmodified
  15270. (setq ov (org-columns-new-overlay
  15271. beg (setq beg (1+ beg)) string
  15272. (list color 'org-column)))
  15273. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15274. (org-overlay-put ov 'keymap org-columns-map)
  15275. (org-overlay-put ov 'org-columns-key property)
  15276. (org-overlay-put ov 'org-columns-value (cdr ass))
  15277. (org-overlay-put ov 'org-columns-value-modified modval)
  15278. (org-overlay-put ov 'org-columns-pom pom)
  15279. (org-overlay-put ov 'org-columns-format f))
  15280. (if (or (not (char-after beg))
  15281. (equal (char-after beg) ?\n))
  15282. (let ((inhibit-read-only t))
  15283. (save-excursion
  15284. (goto-char beg)
  15285. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15286. ;; Make the rest of the line disappear.
  15287. (org-unmodified
  15288. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15289. (org-overlay-put ov 'invisible t)
  15290. (org-overlay-put ov 'keymap org-columns-map)
  15291. (org-overlay-put ov 'intangible t)
  15292. (push ov org-columns-overlays)
  15293. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15294. (org-overlay-put ov 'keymap org-columns-map)
  15295. (push ov org-columns-overlays)
  15296. (let ((inhibit-read-only t))
  15297. (put-text-property (max (point-min) (1- (point-at-bol)))
  15298. (min (point-max) (1+ (point-at-eol)))
  15299. 'read-only "Type `e' to edit property")))))
  15300. (defvar org-previous-header-line-format nil
  15301. "The header line format before column view was turned on.")
  15302. (defvar org-columns-inhibit-recalculation nil
  15303. "Inhibit recomputing of columns on column view startup.")
  15304. (defvar header-line-format)
  15305. (defun org-columns-display-here-title ()
  15306. "Overlay the newline before the current line with the table title."
  15307. (interactive)
  15308. (let ((fmt org-columns-current-fmt-compiled)
  15309. string (title "")
  15310. property width f column str widths)
  15311. (while (setq column (pop fmt))
  15312. (setq property (car column)
  15313. str (or (nth 1 column) property)
  15314. width (or (cdr (assoc property org-columns-current-maxwidths))
  15315. (nth 2 column)
  15316. (length str))
  15317. widths (push width widths)
  15318. f (format "%%-%d.%ds | " width width)
  15319. string (format f str)
  15320. title (concat title string)))
  15321. (setq title (concat
  15322. (org-add-props " " nil 'display '(space :align-to 0))
  15323. (org-add-props title nil 'face '(:weight bold :underline t))))
  15324. (org-set-local 'org-previous-header-line-format header-line-format)
  15325. (org-set-local 'org-columns-current-widths (nreverse widths))
  15326. (setq header-line-format title)))
  15327. (defun org-columns-remove-overlays ()
  15328. "Remove all currently active column overlays."
  15329. (interactive)
  15330. (when (marker-buffer org-columns-begin-marker)
  15331. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15332. (when (local-variable-p 'org-previous-header-line-format)
  15333. (setq header-line-format org-previous-header-line-format)
  15334. (kill-local-variable 'org-previous-header-line-format))
  15335. (move-marker org-columns-begin-marker nil)
  15336. (move-marker org-columns-top-level-marker nil)
  15337. (org-unmodified
  15338. (mapc 'org-delete-overlay org-columns-overlays)
  15339. (setq org-columns-overlays nil)
  15340. (let ((inhibit-read-only t))
  15341. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15342. (defun org-columns-cleanup-item (item fmt)
  15343. "Remove from ITEM what is a column in the format FMT."
  15344. (if (not org-complex-heading-regexp)
  15345. item
  15346. (when (string-match org-complex-heading-regexp item)
  15347. (concat
  15348. (org-add-props (concat (match-string 1 item) " ") nil
  15349. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15350. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15351. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15352. " " (match-string 4 item)
  15353. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15354. (defun org-columns-show-value ()
  15355. "Show the full value of the property."
  15356. (interactive)
  15357. (let ((value (get-char-property (point) 'org-columns-value)))
  15358. (message "Value is: %s" (or value ""))))
  15359. (defun org-columns-quit ()
  15360. "Remove the column overlays and in this way exit column editing."
  15361. (interactive)
  15362. (org-unmodified
  15363. (org-columns-remove-overlays)
  15364. (let ((inhibit-read-only t))
  15365. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15366. (when (eq major-mode 'org-agenda-mode)
  15367. (message
  15368. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15369. (defun org-columns-check-computed ()
  15370. "Check if this column value is computed.
  15371. If yes, throw an error indicating that changing it does not make sense."
  15372. (let ((val (get-char-property (point) 'org-columns-value)))
  15373. (when (and (stringp val)
  15374. (get-char-property 0 'org-computed val))
  15375. (error "This value is computed from the entry's children"))))
  15376. (defun org-columns-todo (&optional arg)
  15377. "Change the TODO state during column view."
  15378. (interactive "P")
  15379. (org-columns-edit-value "TODO"))
  15380. (defun org-columns-set-tags-or-toggle (&optional arg)
  15381. "Toggle checkbox at point, or set tags for current headline."
  15382. (interactive "P")
  15383. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15384. (get-char-property (point) 'org-columns-value))
  15385. (org-columns-next-allowed-value)
  15386. (org-columns-edit-value "TAGS")))
  15387. (defun org-columns-edit-value (&optional key)
  15388. "Edit the value of the property at point in column view.
  15389. Where possible, use the standard interface for changing this line."
  15390. (interactive)
  15391. (org-columns-check-computed)
  15392. (let* ((external-key key)
  15393. (col (current-column))
  15394. (key (or key (get-char-property (point) 'org-columns-key)))
  15395. (value (get-char-property (point) 'org-columns-value))
  15396. (bol (point-at-bol)) (eol (point-at-eol))
  15397. (pom (or (get-text-property bol 'org-hd-marker)
  15398. (point))) ; keep despite of compiler waring
  15399. (line-overlays
  15400. (delq nil (mapcar (lambda (x)
  15401. (and (eq (overlay-buffer x) (current-buffer))
  15402. (>= (overlay-start x) bol)
  15403. (<= (overlay-start x) eol)
  15404. x))
  15405. org-columns-overlays)))
  15406. nval eval allowed)
  15407. (cond
  15408. ((equal key "CLOCKSUM")
  15409. (error "This special column cannot be edited"))
  15410. ((equal key "ITEM")
  15411. (setq eval '(org-with-point-at pom
  15412. (org-edit-headline))))
  15413. ((equal key "TODO")
  15414. (setq eval '(org-with-point-at pom
  15415. (let ((current-prefix-arg
  15416. (if external-key current-prefix-arg '(4))))
  15417. (call-interactively 'org-todo)))))
  15418. ((equal key "PRIORITY")
  15419. (setq eval '(org-with-point-at pom
  15420. (call-interactively 'org-priority))))
  15421. ((equal key "TAGS")
  15422. (setq eval '(org-with-point-at pom
  15423. (let ((org-fast-tag-selection-single-key
  15424. (if (eq org-fast-tag-selection-single-key 'expert)
  15425. t org-fast-tag-selection-single-key)))
  15426. (call-interactively 'org-set-tags)))))
  15427. ((equal key "DEADLINE")
  15428. (setq eval '(org-with-point-at pom
  15429. (call-interactively 'org-deadline))))
  15430. ((equal key "SCHEDULED")
  15431. (setq eval '(org-with-point-at pom
  15432. (call-interactively 'org-schedule))))
  15433. (t
  15434. (setq allowed (org-property-get-allowed-values pom key 'table))
  15435. (if allowed
  15436. (setq nval (completing-read "Value: " allowed nil t))
  15437. (setq nval (read-string "Edit: " value)))
  15438. (setq nval (org-trim nval))
  15439. (when (not (equal nval value))
  15440. (setq eval '(org-entry-put pom key nval)))))
  15441. (when eval
  15442. (let ((inhibit-read-only t))
  15443. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15444. (unwind-protect
  15445. (progn
  15446. (setq org-columns-overlays
  15447. (org-delete-all line-overlays org-columns-overlays))
  15448. (mapc 'org-delete-overlay line-overlays)
  15449. (org-columns-eval eval))
  15450. (org-columns-display-here))))
  15451. (move-to-column col)
  15452. (if (and (org-mode-p)
  15453. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15454. (org-columns-update key))))
  15455. (defun org-edit-headline () ; FIXME: this is not columns specific
  15456. "Edit the current headline, the part without TODO keyword, TAGS."
  15457. (org-back-to-heading)
  15458. (when (looking-at org-todo-line-regexp)
  15459. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15460. (txt (match-string 3))
  15461. (post "")
  15462. txt2)
  15463. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15464. (setq post (match-string 0 txt)
  15465. txt (substring txt 0 (match-beginning 0))))
  15466. (setq txt2 (read-string "Edit: " txt))
  15467. (when (not (equal txt txt2))
  15468. (beginning-of-line 1)
  15469. (insert pre txt2 post)
  15470. (delete-region (point) (point-at-eol))
  15471. (org-set-tags nil t)))))
  15472. (defun org-columns-edit-allowed ()
  15473. "Edit the list of allowed values for the current property."
  15474. (interactive)
  15475. (let* ((key (get-char-property (point) 'org-columns-key))
  15476. (key1 (concat key "_ALL"))
  15477. (allowed (org-entry-get (point) key1 t))
  15478. nval)
  15479. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15480. (setq nval (read-string "Allowed: " allowed))
  15481. (org-entry-put
  15482. (cond ((marker-position org-entry-property-inherited-from)
  15483. org-entry-property-inherited-from)
  15484. ((marker-position org-columns-top-level-marker)
  15485. org-columns-top-level-marker))
  15486. key1 nval)))
  15487. (defmacro org-no-warnings (&rest body)
  15488. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15489. (defun org-columns-eval (form)
  15490. (let (hidep)
  15491. (save-excursion
  15492. (beginning-of-line 1)
  15493. ;; `next-line' is needed here, because it skips invisible line.
  15494. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15495. (setq hidep (org-on-heading-p 1)))
  15496. (eval form)
  15497. (and hidep (hide-entry))))
  15498. (defun org-columns-previous-allowed-value ()
  15499. "Switch to the previous allowed value for this column."
  15500. (interactive)
  15501. (org-columns-next-allowed-value t))
  15502. (defun org-columns-next-allowed-value (&optional previous)
  15503. "Switch to the next allowed value for this column."
  15504. (interactive)
  15505. (org-columns-check-computed)
  15506. (let* ((col (current-column))
  15507. (key (get-char-property (point) 'org-columns-key))
  15508. (value (get-char-property (point) 'org-columns-value))
  15509. (bol (point-at-bol)) (eol (point-at-eol))
  15510. (pom (or (get-text-property bol 'org-hd-marker)
  15511. (point))) ; keep despite of compiler waring
  15512. (line-overlays
  15513. (delq nil (mapcar (lambda (x)
  15514. (and (eq (overlay-buffer x) (current-buffer))
  15515. (>= (overlay-start x) bol)
  15516. (<= (overlay-start x) eol)
  15517. x))
  15518. org-columns-overlays)))
  15519. (allowed (or (org-property-get-allowed-values pom key)
  15520. (and (memq
  15521. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15522. '(checkbox checkbox-n-of-m checkbox-percent))
  15523. '("[ ]" "[X]"))))
  15524. nval)
  15525. (when (equal key "ITEM")
  15526. (error "Cannot edit item headline from here"))
  15527. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15528. (error "Allowed values for this property have not been defined"))
  15529. (if (member key '("SCHEDULED" "DEADLINE"))
  15530. (setq nval (if previous 'earlier 'later))
  15531. (if previous (setq allowed (reverse allowed)))
  15532. (if (member value allowed)
  15533. (setq nval (car (cdr (member value allowed)))))
  15534. (setq nval (or nval (car allowed)))
  15535. (if (equal nval value)
  15536. (error "Only one allowed value for this property")))
  15537. (let ((inhibit-read-only t))
  15538. (remove-text-properties (1- bol) eol '(read-only t))
  15539. (unwind-protect
  15540. (progn
  15541. (setq org-columns-overlays
  15542. (org-delete-all line-overlays org-columns-overlays))
  15543. (mapc 'org-delete-overlay line-overlays)
  15544. (org-columns-eval '(org-entry-put pom key nval)))
  15545. (org-columns-display-here)))
  15546. (move-to-column col)
  15547. (if (and (org-mode-p)
  15548. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15549. (org-columns-update key))))
  15550. (defun org-verify-version (task)
  15551. (cond
  15552. ((eq task 'columns)
  15553. (if (or (featurep 'xemacs)
  15554. (< emacs-major-version 22))
  15555. (error "Emacs 22 is required for the columns feature")))))
  15556. (defun org-columns-open-link (&optional arg)
  15557. (interactive "P")
  15558. (let ((value (get-char-property (point) 'org-columns-value)))
  15559. (org-open-link-from-string value arg)))
  15560. (defun org-open-link-from-string (s &optional arg)
  15561. "Open a link in the string S, as if it was in Org-mode."
  15562. (interactive)
  15563. (with-temp-buffer
  15564. (let ((org-inhibit-startup t))
  15565. (org-mode)
  15566. (insert s)
  15567. (goto-char (point-min))
  15568. (org-open-at-point arg))))
  15569. (defun org-columns-get-format-and-top-level ()
  15570. (let (fmt)
  15571. (when (condition-case nil (org-back-to-heading) (error nil))
  15572. (move-marker org-entry-property-inherited-from nil)
  15573. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15574. (setq fmt (or fmt org-columns-default-format))
  15575. (org-set-local 'org-columns-current-fmt fmt)
  15576. (org-columns-compile-format fmt)
  15577. (if (marker-position org-entry-property-inherited-from)
  15578. (move-marker org-columns-top-level-marker
  15579. org-entry-property-inherited-from)
  15580. (move-marker org-columns-top-level-marker (point)))
  15581. fmt))
  15582. (defun org-columns ()
  15583. "Turn on column view on an org-mode file."
  15584. (interactive)
  15585. (org-verify-version 'columns)
  15586. (org-columns-remove-overlays)
  15587. (move-marker org-columns-begin-marker (point))
  15588. (let (beg end fmt cache maxwidths)
  15589. (setq fmt (org-columns-get-format-and-top-level))
  15590. (save-excursion
  15591. (goto-char org-columns-top-level-marker)
  15592. (setq beg (point))
  15593. (unless org-columns-inhibit-recalculation
  15594. (org-columns-compute-all))
  15595. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15596. (point-max)))
  15597. ;; Get and cache the properties
  15598. (goto-char beg)
  15599. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15600. (save-excursion
  15601. (save-restriction
  15602. (narrow-to-region beg end)
  15603. (org-clock-sum))))
  15604. (while (re-search-forward (concat "^" outline-regexp) end t)
  15605. (push (cons (org-current-line) (org-entry-properties)) cache))
  15606. (when cache
  15607. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15608. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15609. (org-columns-display-here-title)
  15610. (mapc (lambda (x)
  15611. (goto-line (car x))
  15612. (org-columns-display-here (cdr x)))
  15613. cache)))))
  15614. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15615. "Insert a new column, to the leeft o the current column."
  15616. (interactive)
  15617. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15618. cell)
  15619. (setq prop (completing-read
  15620. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15621. nil nil prop))
  15622. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15623. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15624. (if (string-match "\\S-" width)
  15625. (setq width (string-to-number width))
  15626. (setq width nil))
  15627. (setq fmt (completing-read "Summary [none]: "
  15628. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15629. nil t))
  15630. (if (string-match "\\S-" fmt)
  15631. (setq fmt (intern fmt))
  15632. (setq fmt nil))
  15633. (if (eq fmt 'none) (setq fmt nil))
  15634. (if editp
  15635. (progn
  15636. (setcar editp prop)
  15637. (setcdr editp (list title width nil fmt)))
  15638. (setq cell (nthcdr (1- (current-column))
  15639. org-columns-current-fmt-compiled))
  15640. (setcdr cell (cons (list prop title width nil fmt)
  15641. (cdr cell))))
  15642. (org-columns-store-format)
  15643. (org-columns-redo)))
  15644. (defun org-columns-delete ()
  15645. "Delete the column at point from columns view."
  15646. (interactive)
  15647. (let* ((n (current-column))
  15648. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15649. (when (y-or-n-p
  15650. (format "Are you sure you want to remove column \"%s\"? " title))
  15651. (setq org-columns-current-fmt-compiled
  15652. (delq (nth n org-columns-current-fmt-compiled)
  15653. org-columns-current-fmt-compiled))
  15654. (org-columns-store-format)
  15655. (org-columns-redo)
  15656. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15657. (backward-char 1)))))
  15658. (defun org-columns-edit-attributes ()
  15659. "Edit the attributes of the current column."
  15660. (interactive)
  15661. (let* ((n (current-column))
  15662. (info (nth n org-columns-current-fmt-compiled)))
  15663. (apply 'org-columns-new info)))
  15664. (defun org-columns-widen (arg)
  15665. "Make the column wider by ARG characters."
  15666. (interactive "p")
  15667. (let* ((n (current-column))
  15668. (entry (nth n org-columns-current-fmt-compiled))
  15669. (width (or (nth 2 entry)
  15670. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15671. (setq width (max 1 (+ width arg)))
  15672. (setcar (nthcdr 2 entry) width)
  15673. (org-columns-store-format)
  15674. (org-columns-redo)))
  15675. (defun org-columns-narrow (arg)
  15676. "Make the column nrrower by ARG characters."
  15677. (interactive "p")
  15678. (org-columns-widen (- arg)))
  15679. (defun org-columns-move-right ()
  15680. "Swap this column with the one to the right."
  15681. (interactive)
  15682. (let* ((n (current-column))
  15683. (cell (nthcdr n org-columns-current-fmt-compiled))
  15684. e)
  15685. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15686. (error "Cannot shift this column further to the right"))
  15687. (setq e (car cell))
  15688. (setcar cell (car (cdr cell)))
  15689. (setcdr cell (cons e (cdr (cdr cell))))
  15690. (org-columns-store-format)
  15691. (org-columns-redo)
  15692. (forward-char 1)))
  15693. (defun org-columns-move-left ()
  15694. "Swap this column with the one to the left."
  15695. (interactive)
  15696. (let* ((n (current-column)))
  15697. (when (= n 0)
  15698. (error "Cannot shift this column further to the left"))
  15699. (backward-char 1)
  15700. (org-columns-move-right)
  15701. (backward-char 1)))
  15702. (defun org-columns-store-format ()
  15703. "Store the text version of the current columns format in appropriate place.
  15704. This is either in the COLUMNS property of the node starting the current column
  15705. display, or in the #+COLUMNS line of the current buffer."
  15706. (let (fmt (cnt 0))
  15707. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15708. (org-set-local 'org-columns-current-fmt fmt)
  15709. (if (marker-position org-columns-top-level-marker)
  15710. (save-excursion
  15711. (goto-char org-columns-top-level-marker)
  15712. (if (and (org-at-heading-p)
  15713. (org-entry-get nil "COLUMNS"))
  15714. (org-entry-put nil "COLUMNS" fmt)
  15715. (goto-char (point-min))
  15716. ;; Overwrite all #+COLUMNS lines....
  15717. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15718. (setq cnt (1+ cnt))
  15719. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15720. (unless (> cnt 0)
  15721. (goto-char (point-min))
  15722. (or (org-on-heading-p t) (outline-next-heading))
  15723. (let ((inhibit-read-only t))
  15724. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15725. (org-set-local 'org-columns-default-format fmt))))))
  15726. (defvar org-overriding-columns-format nil
  15727. "When set, overrides any other definition.")
  15728. (defvar org-agenda-view-columns-initially nil
  15729. "When set, switch to columns view immediately after creating the agenda.")
  15730. (defun org-agenda-columns ()
  15731. "Turn on column view in the agenda."
  15732. (interactive)
  15733. (org-verify-version 'columns)
  15734. (org-columns-remove-overlays)
  15735. (move-marker org-columns-begin-marker (point))
  15736. (let (fmt cache maxwidths m)
  15737. (cond
  15738. ((and (local-variable-p 'org-overriding-columns-format)
  15739. org-overriding-columns-format)
  15740. (setq fmt org-overriding-columns-format))
  15741. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15742. (setq fmt (org-entry-get m "COLUMNS" t)))
  15743. ((and (boundp 'org-columns-current-fmt)
  15744. (local-variable-p 'org-columns-current-fmt)
  15745. org-columns-current-fmt)
  15746. (setq fmt org-columns-current-fmt))
  15747. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15748. (setq m (get-text-property m 'org-hd-marker))
  15749. (setq fmt (org-entry-get m "COLUMNS" t))))
  15750. (setq fmt (or fmt org-columns-default-format))
  15751. (org-set-local 'org-columns-current-fmt fmt)
  15752. (org-columns-compile-format fmt)
  15753. (save-excursion
  15754. ;; Get and cache the properties
  15755. (goto-char (point-min))
  15756. (while (not (eobp))
  15757. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15758. (get-text-property (point) 'org-marker)))
  15759. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15760. (beginning-of-line 2))
  15761. (when cache
  15762. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15763. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15764. (org-columns-display-here-title)
  15765. (mapc (lambda (x)
  15766. (goto-line (car x))
  15767. (org-columns-display-here (cdr x)))
  15768. cache)))))
  15769. (defun org-columns-get-autowidth-alist (s cache)
  15770. "Derive the maximum column widths from the format and the cache."
  15771. (let ((start 0) rtn)
  15772. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15773. (push (cons (match-string 1 s) 1) rtn)
  15774. (setq start (match-end 0)))
  15775. (mapc (lambda (x)
  15776. (setcdr x (apply 'max
  15777. (mapcar
  15778. (lambda (y)
  15779. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15780. cache))))
  15781. rtn)
  15782. rtn))
  15783. (defun org-columns-compute-all ()
  15784. "Compute all columns that have operators defined."
  15785. (org-unmodified
  15786. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15787. (let ((columns org-columns-current-fmt-compiled) col)
  15788. (while (setq col (pop columns))
  15789. (when (nth 3 col)
  15790. (save-excursion
  15791. (org-columns-compute (car col)))))))
  15792. (defun org-columns-update (property)
  15793. "Recompute PROPERTY, and update the columns display for it."
  15794. (org-columns-compute property)
  15795. (let (fmt val pos)
  15796. (save-excursion
  15797. (mapc (lambda (ov)
  15798. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15799. (setq pos (org-overlay-start ov))
  15800. (goto-char pos)
  15801. (when (setq val (cdr (assoc property
  15802. (get-text-property
  15803. (point-at-bol) 'org-summaries))))
  15804. (setq fmt (org-overlay-get ov 'org-columns-format))
  15805. (org-overlay-put ov 'org-columns-value val)
  15806. (org-overlay-put ov 'display (format fmt val)))))
  15807. org-columns-overlays))))
  15808. (defun org-columns-compute (property)
  15809. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15810. (interactive)
  15811. (let* ((re (concat "^" outline-regexp))
  15812. (lmax 30) ; Does anyone use deeper levels???
  15813. (lsum (make-vector lmax 0))
  15814. (lflag (make-vector lmax nil))
  15815. (level 0)
  15816. (ass (assoc property org-columns-current-fmt-compiled))
  15817. (format (nth 4 ass))
  15818. (printf (nth 5 ass))
  15819. (beg org-columns-top-level-marker)
  15820. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15821. (save-excursion
  15822. ;; Find the region to compute
  15823. (goto-char beg)
  15824. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15825. (goto-char end)
  15826. ;; Walk the tree from the back and do the computations
  15827. (while (re-search-backward re beg t)
  15828. (setq sumpos (match-beginning 0)
  15829. last-level level
  15830. level (org-outline-level)
  15831. val (org-entry-get nil property)
  15832. valflag (and val (string-match "\\S-" val)))
  15833. (cond
  15834. ((< level last-level)
  15835. ;; put the sum of lower levels here as a property
  15836. (setq sum (aref lsum last-level) ; current sum
  15837. flag (aref lflag last-level) ; any valid entries from children?
  15838. str (org-column-number-to-string sum format printf)
  15839. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15840. useval (if flag str1 (if valflag val ""))
  15841. sum-alist (get-text-property sumpos 'org-summaries))
  15842. (if (assoc property sum-alist)
  15843. (setcdr (assoc property sum-alist) useval)
  15844. (push (cons property useval) sum-alist)
  15845. (org-unmodified
  15846. (add-text-properties sumpos (1+ sumpos)
  15847. (list 'org-summaries sum-alist))))
  15848. (when val
  15849. (org-entry-put nil property (if flag str val)))
  15850. ;; add current to current level accumulator
  15851. (when (or flag valflag)
  15852. (aset lsum level (+ (aref lsum level)
  15853. (if flag sum (org-column-string-to-number
  15854. (if flag str val) format))))
  15855. (aset lflag level t))
  15856. ;; clear accumulators for deeper levels
  15857. (loop for l from (1+ level) to (1- lmax) do
  15858. (aset lsum l 0)
  15859. (aset lflag l nil)))
  15860. ((>= level last-level)
  15861. ;; add what we have here to the accumulator for this level
  15862. (aset lsum level (+ (aref lsum level)
  15863. (org-column-string-to-number (or val "0") format)))
  15864. (and valflag (aset lflag level t)))
  15865. (t (error "This should not happen")))))))
  15866. (defun org-columns-redo ()
  15867. "Construct the column display again."
  15868. (interactive)
  15869. (message "Recomputing columns...")
  15870. (save-excursion
  15871. (if (marker-position org-columns-begin-marker)
  15872. (goto-char org-columns-begin-marker))
  15873. (org-columns-remove-overlays)
  15874. (if (org-mode-p)
  15875. (call-interactively 'org-columns)
  15876. (call-interactively 'org-agenda-columns)))
  15877. (message "Recomputing columns...done"))
  15878. (defun org-columns-not-in-agenda ()
  15879. (if (eq major-mode 'org-agenda-mode)
  15880. (error "This command is only allowed in Org-mode buffers")))
  15881. (defun org-string-to-number (s)
  15882. "Convert string to number, and interpret hh:mm:ss."
  15883. (if (not (string-match ":" s))
  15884. (string-to-number s)
  15885. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15886. (while l
  15887. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15888. sum)))
  15889. (defun org-column-number-to-string (n fmt &optional printf)
  15890. "Convert a computed column number to a string value, according to FMT."
  15891. (cond
  15892. ((eq fmt 'add_times)
  15893. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15894. (format "%d:%02d" h m)))
  15895. ((eq fmt 'checkbox)
  15896. (cond ((= n (floor n)) "[X]")
  15897. ((> n 1.) "[-]")
  15898. (t "[ ]")))
  15899. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15900. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15901. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15902. (printf (format printf n))
  15903. ((eq fmt 'currency)
  15904. (format "%.2f" n))
  15905. (t (number-to-string n))))
  15906. (defun org-nofm-to-completion (n m &optional percent)
  15907. (if (not percent)
  15908. (format "[%d/%d]" n m)
  15909. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15910. (defun org-column-string-to-number (s fmt)
  15911. "Convert a column value to a number that can be used for column computing."
  15912. (cond
  15913. ((string-match ":" s)
  15914. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15915. (while l
  15916. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15917. sum))
  15918. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15919. (if (equal s "[X]") 1. 0.000001))
  15920. (t (string-to-number s))))
  15921. (defun org-columns-uncompile-format (cfmt)
  15922. "Turn the compiled columns format back into a string representation."
  15923. (let ((rtn "") e s prop title op width fmt printf)
  15924. (while (setq e (pop cfmt))
  15925. (setq prop (car e)
  15926. title (nth 1 e)
  15927. width (nth 2 e)
  15928. op (nth 3 e)
  15929. fmt (nth 4 e)
  15930. printf (nth 5 e))
  15931. (cond
  15932. ((eq fmt 'add_times) (setq op ":"))
  15933. ((eq fmt 'checkbox) (setq op "X"))
  15934. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15935. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15936. ((eq fmt 'add_numbers) (setq op "+"))
  15937. ((eq fmt 'currency) (setq op "$")))
  15938. (if (and op printf) (setq op (concat op ";" printf)))
  15939. (if (equal title prop) (setq title nil))
  15940. (setq s (concat "%" (if width (number-to-string width))
  15941. prop
  15942. (if title (concat "(" title ")"))
  15943. (if op (concat "{" op "}"))))
  15944. (setq rtn (concat rtn " " s)))
  15945. (org-trim rtn)))
  15946. (defun org-columns-compile-format (fmt)
  15947. "Turn a column format string into an alist of specifications.
  15948. The alist has one entry for each column in the format. The elements of
  15949. that list are:
  15950. property the property
  15951. title the title field for the columns
  15952. width the column width in characters, can be nil for automatic
  15953. operator the operator if any
  15954. format the output format for computed results, derived from operator
  15955. printf a printf format for computed values"
  15956. (let ((start 0) width prop title op f printf)
  15957. (setq org-columns-current-fmt-compiled nil)
  15958. (while (string-match
  15959. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15960. fmt start)
  15961. (setq start (match-end 0)
  15962. width (match-string 1 fmt)
  15963. prop (match-string 2 fmt)
  15964. title (or (match-string 3 fmt) prop)
  15965. op (match-string 4 fmt)
  15966. f nil
  15967. printf nil)
  15968. (if width (setq width (string-to-number width)))
  15969. (when (and op (string-match ";" op))
  15970. (setq printf (substring op (match-end 0))
  15971. op (substring op 0 (match-beginning 0))))
  15972. (cond
  15973. ((equal op "+") (setq f 'add_numbers))
  15974. ((equal op "$") (setq f 'currency))
  15975. ((equal op ":") (setq f 'add_times))
  15976. ((equal op "X") (setq f 'checkbox))
  15977. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15978. ((equal op "X%") (setq f 'checkbox-percent))
  15979. )
  15980. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15981. (setq org-columns-current-fmt-compiled
  15982. (nreverse org-columns-current-fmt-compiled))))
  15983. ;;; Dynamic block for Column view
  15984. (defun org-columns-capture-view ()
  15985. "Get the column view of the current buffer and return it as a list.
  15986. The list will contains the title row and all other rows. Each row is
  15987. a list of fields."
  15988. (save-excursion
  15989. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15990. (n (length title)) row tbl)
  15991. (goto-char (point-min))
  15992. (while (re-search-forward "^\\*+ " nil t)
  15993. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15994. (setq row nil)
  15995. (loop for i from 0 to (1- n) do
  15996. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15997. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15998. "")
  15999. row))
  16000. (setq row (nreverse row))
  16001. (push row tbl)))
  16002. (append (list title 'hline) (nreverse tbl)))))
  16003. (defun org-dblock-write:columnview (params)
  16004. "Write the column view table.
  16005. PARAMS is a property list of parameters:
  16006. :width enforce same column widths with <N> specifiers.
  16007. :id the :ID: property of the entry where the columns view
  16008. should be built, as a string. When `local', call locally.
  16009. When `global' call column view with the cursor at the beginning
  16010. of the buffer (usually this means that the whole buffer switches
  16011. to column view).
  16012. :hlines When t, insert a hline before each item. When a number, insert
  16013. a hline before each level <= that number.
  16014. :vlines When t, make each column a colgroup to enforce vertical lines."
  16015. (let ((pos (move-marker (make-marker) (point)))
  16016. (hlines (plist-get params :hlines))
  16017. (vlines (plist-get params :vlines))
  16018. tbl id idpos nfields tmp)
  16019. (save-excursion
  16020. (save-restriction
  16021. (when (setq id (plist-get params :id))
  16022. (cond ((not id) nil)
  16023. ((eq id 'global) (goto-char (point-min)))
  16024. ((eq id 'local) nil)
  16025. ((setq idpos (org-find-entry-with-id id))
  16026. (goto-char idpos))
  16027. (t (error "Cannot find entry with :ID: %s" id))))
  16028. (org-columns)
  16029. (setq tbl (org-columns-capture-view))
  16030. (setq nfields (length (car tbl)))
  16031. (org-columns-quit)))
  16032. (goto-char pos)
  16033. (move-marker pos nil)
  16034. (when tbl
  16035. (when (plist-get params :hlines)
  16036. (setq tmp nil)
  16037. (while tbl
  16038. (if (eq (car tbl) 'hline)
  16039. (push (pop tbl) tmp)
  16040. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16041. (if (and (not (eq (car tmp) 'hline))
  16042. (or (eq hlines t)
  16043. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16044. (push 'hline tmp)))
  16045. (push (pop tbl) tmp)))
  16046. (setq tbl (nreverse tmp)))
  16047. (when vlines
  16048. (setq tbl (mapcar (lambda (x)
  16049. (if (eq 'hline x) x (cons "" x)))
  16050. tbl))
  16051. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16052. (setq pos (point))
  16053. (insert (org-listtable-to-string tbl))
  16054. (when (plist-get params :width)
  16055. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16056. org-columns-current-widths "|")))
  16057. (goto-char pos)
  16058. (org-table-align))))
  16059. (defun org-listtable-to-string (tbl)
  16060. "Convert a listtable TBL to a string that contains the Org-mode table.
  16061. The table still need to be alligned. The resulting string has no leading
  16062. and tailing newline characters."
  16063. (mapconcat
  16064. (lambda (x)
  16065. (cond
  16066. ((listp x)
  16067. (concat "|" (mapconcat 'identity x "|") "|"))
  16068. ((eq x 'hline) "|-|")
  16069. (t (error "Garbage in listtable: %s" x))))
  16070. tbl "\n"))
  16071. (defun org-insert-columns-dblock ()
  16072. "Create a dynamic block capturing a column view table."
  16073. (interactive)
  16074. (let ((defaults '(:name "columnview" :hlines 1))
  16075. (id (completing-read
  16076. "Capture columns (local, global, entry with :ID: property) [local]: "
  16077. (append '(("global") ("local"))
  16078. (mapcar 'list (org-property-values "ID"))))))
  16079. (if (equal id "") (setq id 'local))
  16080. (if (equal id "global") (setq id 'global))
  16081. (setq defaults (append defaults (list :id id)))
  16082. (org-create-dblock defaults)
  16083. (org-update-dblock)))
  16084. ;;;; Timestamps
  16085. (defvar org-last-changed-timestamp nil)
  16086. (defvar org-time-was-given) ; dynamically scoped parameter
  16087. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16088. (defvar org-ts-what) ; dynamically scoped parameter
  16089. (defun org-time-stamp (arg)
  16090. "Prompt for a date/time and insert a time stamp.
  16091. If the user specifies a time like HH:MM, or if this command is called
  16092. with a prefix argument, the time stamp will contain date and time.
  16093. Otherwise, only the date will be included. All parts of a date not
  16094. specified by the user will be filled in from the current date/time.
  16095. So if you press just return without typing anything, the time stamp
  16096. will represent the current date/time. If there is already a timestamp
  16097. at the cursor, it will be modified."
  16098. (interactive "P")
  16099. (let* ((ts nil)
  16100. (default-time
  16101. ;; Default time is either today, or, when entering a range,
  16102. ;; the range start.
  16103. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16104. (save-excursion
  16105. (re-search-backward
  16106. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16107. (- (point) 20) t)))
  16108. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16109. (current-time)))
  16110. (default-input (and ts (org-get-compact-tod ts)))
  16111. org-time-was-given org-end-time-was-given time)
  16112. (cond
  16113. ((and (org-at-timestamp-p)
  16114. (eq last-command 'org-time-stamp)
  16115. (eq this-command 'org-time-stamp))
  16116. (insert "--")
  16117. (setq time (let ((this-command this-command))
  16118. (org-read-date arg 'totime nil nil default-time default-input)))
  16119. (org-insert-time-stamp time (or org-time-was-given arg)))
  16120. ((org-at-timestamp-p)
  16121. (setq time (let ((this-command this-command))
  16122. (org-read-date arg 'totime nil nil default-time default-input)))
  16123. (when (org-at-timestamp-p) ; just to get the match data
  16124. (replace-match "")
  16125. (setq org-last-changed-timestamp
  16126. (org-insert-time-stamp
  16127. time (or org-time-was-given arg)
  16128. nil nil nil (list org-end-time-was-given))))
  16129. (message "Timestamp updated"))
  16130. (t
  16131. (setq time (let ((this-command this-command))
  16132. (org-read-date arg 'totime nil nil default-time default-input)))
  16133. (org-insert-time-stamp time (or org-time-was-given arg)
  16134. nil nil nil (list org-end-time-was-given))))))
  16135. ;; FIXME: can we use this for something else????
  16136. ;; like computing time differences?????
  16137. (defun org-get-compact-tod (s)
  16138. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16139. (let* ((t1 (match-string 1 s))
  16140. (h1 (string-to-number (match-string 2 s)))
  16141. (m1 (string-to-number (match-string 3 s)))
  16142. (t2 (and (match-end 4) (match-string 5 s)))
  16143. (h2 (and t2 (string-to-number (match-string 6 s))))
  16144. (m2 (and t2 (string-to-number (match-string 7 s))))
  16145. dh dm)
  16146. (if (not t2)
  16147. t1
  16148. (setq dh (- h2 h1) dm (- m2 m1))
  16149. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16150. (concat t1 "+" (number-to-string dh)
  16151. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16152. (defun org-time-stamp-inactive (&optional arg)
  16153. "Insert an inactive time stamp.
  16154. An inactive time stamp is enclosed in square brackets instead of angle
  16155. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16156. does not link to the calendar and cannot be changed with the S-cursor keys.
  16157. So these are more for recording a certain time/date."
  16158. (interactive "P")
  16159. (let (org-time-was-given org-end-time-was-given time)
  16160. (setq time (org-read-date arg 'totime))
  16161. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16162. nil nil (list org-end-time-was-given))))
  16163. (defvar org-date-ovl (org-make-overlay 1 1))
  16164. (org-overlay-put org-date-ovl 'face 'org-warning)
  16165. (org-detach-overlay org-date-ovl)
  16166. (defvar org-ans1) ; dynamically scoped parameter
  16167. (defvar org-ans2) ; dynamically scoped parameter
  16168. (defvar org-plain-time-of-day-regexp) ; defined below
  16169. (defvar org-read-date-overlay nil)
  16170. (defvar org-dcst nil) ; dynamically scoped
  16171. (defun org-read-date (&optional with-time to-time from-string prompt
  16172. default-time default-input)
  16173. "Read a date, possibly a time, and make things smooth for the user.
  16174. The prompt will suggest to enter an ISO date, but you can also enter anything
  16175. which will at least partially be understood by `parse-time-string'.
  16176. Unrecognized parts of the date will default to the current day, month, year,
  16177. hour and minute. If this command is called to replace a timestamp at point,
  16178. of to enter the second timestamp of a range, the default time is taken from the
  16179. existing stamp. For example,
  16180. 3-2-5 --> 2003-02-05
  16181. feb 15 --> currentyear-02-15
  16182. sep 12 9 --> 2009-09-12
  16183. 12:45 --> today 12:45
  16184. 22 sept 0:34 --> currentyear-09-22 0:34
  16185. 12 --> currentyear-currentmonth-12
  16186. Fri --> nearest Friday (today or later)
  16187. etc.
  16188. Furthermore you can specify a relative date by giving, as the *first* thing
  16189. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16190. change in days weeks, months, years.
  16191. With a single plus or minus, the date is relative to today. With a double
  16192. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16193. +4d --> four days from today
  16194. +4 --> same as above
  16195. +2w --> two weeks from today
  16196. ++5 --> five days from default date
  16197. The function understands only English month and weekday abbreviations,
  16198. but this can be configured with the variables `parse-time-months' and
  16199. `parse-time-weekdays'.
  16200. While prompting, a calendar is popped up - you can also select the
  16201. date with the mouse (button 1). The calendar shows a period of three
  16202. months. To scroll it to other months, use the keys `>' and `<'.
  16203. If you don't like the calendar, turn it off with
  16204. \(setq org-read-date-popup-calendar nil)
  16205. With optional argument TO-TIME, the date will immediately be converted
  16206. to an internal time.
  16207. With an optional argument WITH-TIME, the prompt will suggest to also
  16208. insert a time. Note that when WITH-TIME is not set, you can still
  16209. enter a time, and this function will inform the calling routine about
  16210. this change. The calling routine may then choose to change the format
  16211. used to insert the time stamp into the buffer to include the time.
  16212. With optional argument FROM-STRING, read from this string instead from
  16213. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16214. the time/date that is used for everything that is not specified by the
  16215. user."
  16216. (require 'parse-time)
  16217. (let* ((org-time-stamp-rounding-minutes
  16218. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16219. (org-dcst org-display-custom-times)
  16220. (ct (org-current-time))
  16221. (def (or default-time ct))
  16222. (defdecode (decode-time def))
  16223. (dummy (progn
  16224. (when (< (nth 2 defdecode) org-extend-today-until)
  16225. (setcar (nthcdr 2 defdecode) -1)
  16226. (setcar (nthcdr 1 defdecode) 59)
  16227. (setq def (apply 'encode-time defdecode)
  16228. defdecode (decode-time def)))))
  16229. (calendar-move-hook nil)
  16230. (view-diary-entries-initially nil)
  16231. (view-calendar-holidays-initially nil)
  16232. (timestr (format-time-string
  16233. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16234. (prompt (concat (if prompt (concat prompt " ") "")
  16235. (format "Date+time [%s]: " timestr)))
  16236. ans (org-ans0 "") org-ans1 org-ans2 final)
  16237. (cond
  16238. (from-string (setq ans from-string))
  16239. (org-read-date-popup-calendar
  16240. (save-excursion
  16241. (save-window-excursion
  16242. (calendar)
  16243. (calendar-forward-day (- (time-to-days def)
  16244. (calendar-absolute-from-gregorian
  16245. (calendar-current-date))))
  16246. (org-eval-in-calendar nil t)
  16247. (let* ((old-map (current-local-map))
  16248. (map (copy-keymap calendar-mode-map))
  16249. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16250. (org-defkey map (kbd "RET") 'org-calendar-select)
  16251. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16252. 'org-calendar-select-mouse)
  16253. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16254. 'org-calendar-select-mouse)
  16255. (org-defkey minibuffer-local-map [(meta shift left)]
  16256. (lambda () (interactive)
  16257. (org-eval-in-calendar '(calendar-backward-month 1))))
  16258. (org-defkey minibuffer-local-map [(meta shift right)]
  16259. (lambda () (interactive)
  16260. (org-eval-in-calendar '(calendar-forward-month 1))))
  16261. (org-defkey minibuffer-local-map [(meta shift up)]
  16262. (lambda () (interactive)
  16263. (org-eval-in-calendar '(calendar-backward-year 1))))
  16264. (org-defkey minibuffer-local-map [(meta shift down)]
  16265. (lambda () (interactive)
  16266. (org-eval-in-calendar '(calendar-forward-year 1))))
  16267. (org-defkey minibuffer-local-map [(shift up)]
  16268. (lambda () (interactive)
  16269. (org-eval-in-calendar '(calendar-backward-week 1))))
  16270. (org-defkey minibuffer-local-map [(shift down)]
  16271. (lambda () (interactive)
  16272. (org-eval-in-calendar '(calendar-forward-week 1))))
  16273. (org-defkey minibuffer-local-map [(shift left)]
  16274. (lambda () (interactive)
  16275. (org-eval-in-calendar '(calendar-backward-day 1))))
  16276. (org-defkey minibuffer-local-map [(shift right)]
  16277. (lambda () (interactive)
  16278. (org-eval-in-calendar '(calendar-forward-day 1))))
  16279. (org-defkey minibuffer-local-map ">"
  16280. (lambda () (interactive)
  16281. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16282. (org-defkey minibuffer-local-map "<"
  16283. (lambda () (interactive)
  16284. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16285. (unwind-protect
  16286. (progn
  16287. (use-local-map map)
  16288. (add-hook 'post-command-hook 'org-read-date-display)
  16289. (setq org-ans0 (read-string prompt default-input nil nil))
  16290. ;; org-ans0: from prompt
  16291. ;; org-ans1: from mouse click
  16292. ;; org-ans2: from calendar motion
  16293. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16294. (remove-hook 'post-command-hook 'org-read-date-display)
  16295. (use-local-map old-map)
  16296. (when org-read-date-overlay
  16297. (org-delete-overlay org-read-date-overlay)
  16298. (setq org-read-date-overlay nil)))))))
  16299. (t ; Naked prompt only
  16300. (unwind-protect
  16301. (setq ans (read-string prompt default-input nil timestr))
  16302. (when org-read-date-overlay
  16303. (org-delete-overlay org-read-date-overlay)
  16304. (setq org-read-date-overlay nil)))))
  16305. (setq final (org-read-date-analyze ans def defdecode))
  16306. (if to-time
  16307. (apply 'encode-time final)
  16308. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16309. (format "%04d-%02d-%02d %02d:%02d"
  16310. (nth 5 final) (nth 4 final) (nth 3 final)
  16311. (nth 2 final) (nth 1 final))
  16312. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16313. (defvar def)
  16314. (defvar defdecode)
  16315. (defvar with-time)
  16316. (defun org-read-date-display ()
  16317. "Display the currrent date prompt interpretation in the minibuffer."
  16318. (when org-read-date-display-live
  16319. (when org-read-date-overlay
  16320. (org-delete-overlay org-read-date-overlay))
  16321. (let ((p (point)))
  16322. (end-of-line 1)
  16323. (while (not (equal (buffer-substring
  16324. (max (point-min) (- (point) 4)) (point))
  16325. " "))
  16326. (insert " "))
  16327. (goto-char p))
  16328. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16329. " " (or org-ans1 org-ans2)))
  16330. (org-end-time-was-given nil)
  16331. (f (org-read-date-analyze ans def defdecode))
  16332. (fmts (if org-dcst
  16333. org-time-stamp-custom-formats
  16334. org-time-stamp-formats))
  16335. (fmt (if (or with-time
  16336. (and (boundp 'org-time-was-given) org-time-was-given))
  16337. (cdr fmts)
  16338. (car fmts)))
  16339. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16340. (when (and org-end-time-was-given
  16341. (string-match org-plain-time-of-day-regexp txt))
  16342. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16343. org-end-time-was-given
  16344. (substring txt (match-end 0)))))
  16345. (setq org-read-date-overlay
  16346. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16347. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16348. (defun org-read-date-analyze (ans def defdecode)
  16349. "Analyze the combined answer of the date prompt."
  16350. ;; FIXME: cleanup and comment
  16351. (let (delta deltan deltaw deltadef year month day
  16352. hour minute second wday pm h2 m2 tl wday1)
  16353. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16354. (setq ans (replace-match "" t t ans)
  16355. deltan (car delta)
  16356. deltaw (nth 1 delta)
  16357. deltadef (nth 2 delta)))
  16358. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16359. (when (string-match
  16360. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16361. (setq year (if (match-end 2)
  16362. (string-to-number (match-string 2 ans))
  16363. (string-to-number (format-time-string "%Y")))
  16364. month (string-to-number (match-string 3 ans))
  16365. day (string-to-number (match-string 4 ans)))
  16366. (if (< year 100) (setq year (+ 2000 year)))
  16367. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16368. t nil ans)))
  16369. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16370. ;; If there is a time with am/pm, and *no* time without it, we convert
  16371. ;; so that matching will be successful.
  16372. (loop for i from 1 to 2 do ; twice, for end time as well
  16373. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16374. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16375. (setq hour (string-to-number (match-string 1 ans))
  16376. minute (if (match-end 3)
  16377. (string-to-number (match-string 3 ans))
  16378. 0)
  16379. pm (equal ?p
  16380. (string-to-char (downcase (match-string 4 ans)))))
  16381. (if (and (= hour 12) (not pm))
  16382. (setq hour 0)
  16383. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16384. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16385. t t ans))))
  16386. ;; Check if a time range is given as a duration
  16387. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16388. (setq hour (string-to-number (match-string 1 ans))
  16389. h2 (+ hour (string-to-number (match-string 3 ans)))
  16390. minute (string-to-number (match-string 2 ans))
  16391. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16392. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16393. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16394. ;; Check if there is a time range
  16395. (when (boundp 'org-end-time-was-given)
  16396. (setq org-time-was-given nil)
  16397. (when (and (string-match org-plain-time-of-day-regexp ans)
  16398. (match-end 8))
  16399. (setq org-end-time-was-given (match-string 8 ans))
  16400. (setq ans (concat (substring ans 0 (match-beginning 7))
  16401. (substring ans (match-end 7))))))
  16402. (setq tl (parse-time-string ans)
  16403. day (or (nth 3 tl) (nth 3 defdecode))
  16404. month (or (nth 4 tl)
  16405. (if (and org-read-date-prefer-future
  16406. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16407. (1+ (nth 4 defdecode))
  16408. (nth 4 defdecode)))
  16409. year (or (nth 5 tl)
  16410. (if (and org-read-date-prefer-future
  16411. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16412. (1+ (nth 5 defdecode))
  16413. (nth 5 defdecode)))
  16414. hour (or (nth 2 tl) (nth 2 defdecode))
  16415. minute (or (nth 1 tl) (nth 1 defdecode))
  16416. second (or (nth 0 tl) 0)
  16417. wday (nth 6 tl))
  16418. (when deltan
  16419. (unless deltadef
  16420. (let ((now (decode-time (current-time))))
  16421. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16422. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16423. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16424. ((equal deltaw "m") (setq month (+ month deltan)))
  16425. ((equal deltaw "y") (setq year (+ year deltan)))))
  16426. (when (and wday (not (nth 3 tl)))
  16427. ;; Weekday was given, but no day, so pick that day in the week
  16428. ;; on or after the derived date.
  16429. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16430. (unless (equal wday wday1)
  16431. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16432. (if (and (boundp 'org-time-was-given)
  16433. (nth 2 tl))
  16434. (setq org-time-was-given t))
  16435. (if (< year 100) (setq year (+ 2000 year)))
  16436. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16437. (list second minute hour day month year)))
  16438. (defvar parse-time-weekdays)
  16439. (defun org-read-date-get-relative (s today default)
  16440. "Check string S for special relative date string.
  16441. TODAY and DEFAULT are internal times, for today and for a default.
  16442. Return shift list (N what def-flag)
  16443. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16444. N is the number of WHATs to shift.
  16445. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16446. the DEFAULT date rather than TODAY."
  16447. (when (string-match
  16448. (concat
  16449. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16450. "\\([0-9]+\\)?"
  16451. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16452. "\\([ \t]\\|$\\)") s)
  16453. (let* ((dir (if (match-end 1)
  16454. (string-to-char (substring (match-string 1 s) -1))
  16455. ?+))
  16456. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16457. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16458. (what (if (match-end 3) (match-string 3 s) "d"))
  16459. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16460. (date (if rel default today))
  16461. (wday (nth 6 (decode-time date)))
  16462. delta)
  16463. (if wday1
  16464. (progn
  16465. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16466. (if (= dir ?-) (setq delta (- delta 7)))
  16467. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16468. (list delta "d" rel))
  16469. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16470. (defun org-eval-in-calendar (form &optional keepdate)
  16471. "Eval FORM in the calendar window and return to current window.
  16472. Also, store the cursor date in variable org-ans2."
  16473. (let ((sw (selected-window)))
  16474. (select-window (get-buffer-window "*Calendar*"))
  16475. (eval form)
  16476. (when (and (not keepdate) (calendar-cursor-to-date))
  16477. (let* ((date (calendar-cursor-to-date))
  16478. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16479. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16480. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16481. (select-window sw)))
  16482. ; ;; Update the prompt to show new default date
  16483. ; (save-excursion
  16484. ; (goto-char (point-min))
  16485. ; (when (and org-ans2
  16486. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16487. ; (get-text-property (match-end 0) 'field))
  16488. ; (let ((inhibit-read-only t))
  16489. ; (replace-match (concat "[" org-ans2 "]") t t)
  16490. ; (add-text-properties (point-min) (1+ (match-end 0))
  16491. ; (text-properties-at (1+ (point-min)))))))))
  16492. (defun org-calendar-select ()
  16493. "Return to `org-read-date' with the date currently selected.
  16494. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16495. (interactive)
  16496. (when (calendar-cursor-to-date)
  16497. (let* ((date (calendar-cursor-to-date))
  16498. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16499. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16500. (if (active-minibuffer-window) (exit-minibuffer))))
  16501. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16502. "Insert a date stamp for the date given by the internal TIME.
  16503. WITH-HM means, use the stamp format that includes the time of the day.
  16504. INACTIVE means use square brackets instead of angular ones, so that the
  16505. stamp will not contribute to the agenda.
  16506. PRE and POST are optional strings to be inserted before and after the
  16507. stamp.
  16508. The command returns the inserted time stamp."
  16509. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16510. stamp)
  16511. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16512. (insert-before-markers (or pre ""))
  16513. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16514. (when (listp extra)
  16515. (setq extra (car extra))
  16516. (if (and (stringp extra)
  16517. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16518. (setq extra (format "-%02d:%02d"
  16519. (string-to-number (match-string 1 extra))
  16520. (string-to-number (match-string 2 extra))))
  16521. (setq extra nil)))
  16522. (when extra
  16523. (backward-char 1)
  16524. (insert-before-markers extra)
  16525. (forward-char 1))
  16526. (insert-before-markers (or post ""))
  16527. stamp))
  16528. (defun org-toggle-time-stamp-overlays ()
  16529. "Toggle the use of custom time stamp formats."
  16530. (interactive)
  16531. (setq org-display-custom-times (not org-display-custom-times))
  16532. (unless org-display-custom-times
  16533. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16534. (while (setq p (next-single-property-change p 'display))
  16535. (if (and (get-text-property p 'display)
  16536. (eq (get-text-property p 'face) 'org-date))
  16537. (remove-text-properties
  16538. p (setq p (next-single-property-change p 'display))
  16539. '(display t))))
  16540. (set-buffer-modified-p bmp)))
  16541. (if (featurep 'xemacs)
  16542. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16543. (org-restart-font-lock)
  16544. (setq org-table-may-need-update t)
  16545. (if org-display-custom-times
  16546. (message "Time stamps are overlayed with custom format")
  16547. (message "Time stamp overlays removed")))
  16548. (defun org-display-custom-time (beg end)
  16549. "Overlay modified time stamp format over timestamp between BED and END."
  16550. (let* ((ts (buffer-substring beg end))
  16551. t1 w1 with-hm tf time str w2 (off 0))
  16552. (save-match-data
  16553. (setq t1 (org-parse-time-string ts t))
  16554. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16555. (setq off (- (match-end 0) (match-beginning 0)))))
  16556. (setq end (- end off))
  16557. (setq w1 (- end beg)
  16558. with-hm (and (nth 1 t1) (nth 2 t1))
  16559. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16560. time (org-fix-decoded-time t1)
  16561. str (org-add-props
  16562. (format-time-string
  16563. (substring tf 1 -1) (apply 'encode-time time))
  16564. nil 'mouse-face 'highlight)
  16565. w2 (length str))
  16566. (if (not (= w2 w1))
  16567. (add-text-properties (1+ beg) (+ 2 beg)
  16568. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16569. (if (featurep 'xemacs)
  16570. (progn
  16571. (put-text-property beg end 'invisible t)
  16572. (put-text-property beg end 'end-glyph (make-glyph str)))
  16573. (put-text-property beg end 'display str))))
  16574. (defun org-translate-time (string)
  16575. "Translate all timestamps in STRING to custom format.
  16576. But do this only if the variable `org-display-custom-times' is set."
  16577. (when org-display-custom-times
  16578. (save-match-data
  16579. (let* ((start 0)
  16580. (re org-ts-regexp-both)
  16581. t1 with-hm inactive tf time str beg end)
  16582. (while (setq start (string-match re string start))
  16583. (setq beg (match-beginning 0)
  16584. end (match-end 0)
  16585. t1 (save-match-data
  16586. (org-parse-time-string (substring string beg end) t))
  16587. with-hm (and (nth 1 t1) (nth 2 t1))
  16588. inactive (equal (substring string beg (1+ beg)) "[")
  16589. tf (funcall (if with-hm 'cdr 'car)
  16590. org-time-stamp-custom-formats)
  16591. time (org-fix-decoded-time t1)
  16592. str (format-time-string
  16593. (concat
  16594. (if inactive "[" "<") (substring tf 1 -1)
  16595. (if inactive "]" ">"))
  16596. (apply 'encode-time time))
  16597. string (replace-match str t t string)
  16598. start (+ start (length str)))))))
  16599. string)
  16600. (defun org-fix-decoded-time (time)
  16601. "Set 0 instead of nil for the first 6 elements of time.
  16602. Don't touch the rest."
  16603. (let ((n 0))
  16604. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16605. (defun org-days-to-time (timestamp-string)
  16606. "Difference between TIMESTAMP-STRING and now in days."
  16607. (- (time-to-days (org-time-string-to-time timestamp-string))
  16608. (time-to-days (current-time))))
  16609. (defun org-deadline-close (timestamp-string &optional ndays)
  16610. "Is the time in TIMESTAMP-STRING close to the current date?"
  16611. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16612. (and (< (org-days-to-time timestamp-string) ndays)
  16613. (not (org-entry-is-done-p))))
  16614. (defun org-get-wdays (ts)
  16615. "Get the deadline lead time appropriate for timestring TS."
  16616. (cond
  16617. ((<= org-deadline-warning-days 0)
  16618. ;; 0 or negative, enforce this value no matter what
  16619. (- org-deadline-warning-days))
  16620. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16621. ;; lead time is specified.
  16622. (floor (* (string-to-number (match-string 1 ts))
  16623. (cdr (assoc (match-string 2 ts)
  16624. '(("d" . 1) ("w" . 7)
  16625. ("m" . 30.4) ("y" . 365.25)))))))
  16626. ;; go for the default.
  16627. (t org-deadline-warning-days)))
  16628. (defun org-calendar-select-mouse (ev)
  16629. "Return to `org-read-date' with the date currently selected.
  16630. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16631. (interactive "e")
  16632. (mouse-set-point ev)
  16633. (when (calendar-cursor-to-date)
  16634. (let* ((date (calendar-cursor-to-date))
  16635. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16636. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16637. (if (active-minibuffer-window) (exit-minibuffer))))
  16638. (defun org-check-deadlines (ndays)
  16639. "Check if there are any deadlines due or past due.
  16640. A deadline is considered due if it happens within `org-deadline-warning-days'
  16641. days from today's date. If the deadline appears in an entry marked DONE,
  16642. it is not shown. The prefix arg NDAYS can be used to test that many
  16643. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16644. (interactive "P")
  16645. (let* ((org-warn-days
  16646. (cond
  16647. ((equal ndays '(4)) 100000)
  16648. (ndays (prefix-numeric-value ndays))
  16649. (t (abs org-deadline-warning-days))))
  16650. (case-fold-search nil)
  16651. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16652. (callback
  16653. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16654. (message "%d deadlines past-due or due within %d days"
  16655. (org-occur regexp nil callback)
  16656. org-warn-days)))
  16657. (defun org-check-before-date (date)
  16658. "Check if there are deadlines or scheduled entries before DATE."
  16659. (interactive (list (org-read-date)))
  16660. (let ((case-fold-search nil)
  16661. (regexp (concat "\\<\\(" org-deadline-string
  16662. "\\|" org-scheduled-string
  16663. "\\) *<\\([^>]+\\)>"))
  16664. (callback
  16665. (lambda () (time-less-p
  16666. (org-time-string-to-time (match-string 2))
  16667. (org-time-string-to-time date)))))
  16668. (message "%d entries before %s"
  16669. (org-occur regexp nil callback) date)))
  16670. (defun org-evaluate-time-range (&optional to-buffer)
  16671. "Evaluate a time range by computing the difference between start and end.
  16672. Normally the result is just printed in the echo area, but with prefix arg
  16673. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16674. If the time range is actually in a table, the result is inserted into the
  16675. next column.
  16676. For time difference computation, a year is assumed to be exactly 365
  16677. days in order to avoid rounding problems."
  16678. (interactive "P")
  16679. (or
  16680. (org-clock-update-time-maybe)
  16681. (save-excursion
  16682. (unless (org-at-date-range-p t)
  16683. (goto-char (point-at-bol))
  16684. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16685. (if (not (org-at-date-range-p t))
  16686. (error "Not at a time-stamp range, and none found in current line")))
  16687. (let* ((ts1 (match-string 1))
  16688. (ts2 (match-string 2))
  16689. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16690. (match-end (match-end 0))
  16691. (time1 (org-time-string-to-time ts1))
  16692. (time2 (org-time-string-to-time ts2))
  16693. (t1 (time-to-seconds time1))
  16694. (t2 (time-to-seconds time2))
  16695. (diff (abs (- t2 t1)))
  16696. (negative (< (- t2 t1) 0))
  16697. ;; (ys (floor (* 365 24 60 60)))
  16698. (ds (* 24 60 60))
  16699. (hs (* 60 60))
  16700. (fy "%dy %dd %02d:%02d")
  16701. (fy1 "%dy %dd")
  16702. (fd "%dd %02d:%02d")
  16703. (fd1 "%dd")
  16704. (fh "%02d:%02d")
  16705. y d h m align)
  16706. (if havetime
  16707. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16708. y 0
  16709. d (floor (/ diff ds)) diff (mod diff ds)
  16710. h (floor (/ diff hs)) diff (mod diff hs)
  16711. m (floor (/ diff 60)))
  16712. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16713. y 0
  16714. d (floor (+ (/ diff ds) 0.5))
  16715. h 0 m 0))
  16716. (if (not to-buffer)
  16717. (message "%s" (org-make-tdiff-string y d h m))
  16718. (if (org-at-table-p)
  16719. (progn
  16720. (goto-char match-end)
  16721. (setq align t)
  16722. (and (looking-at " *|") (goto-char (match-end 0))))
  16723. (goto-char match-end))
  16724. (if (looking-at
  16725. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16726. (replace-match ""))
  16727. (if negative (insert " -"))
  16728. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16729. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16730. (insert " " (format fh h m))))
  16731. (if align (org-table-align))
  16732. (message "Time difference inserted")))))
  16733. (defun org-make-tdiff-string (y d h m)
  16734. (let ((fmt "")
  16735. (l nil))
  16736. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16737. l (push y l)))
  16738. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16739. l (push d l)))
  16740. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16741. l (push h l)))
  16742. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16743. l (push m l)))
  16744. (apply 'format fmt (nreverse l))))
  16745. (defun org-time-string-to-time (s)
  16746. (apply 'encode-time (org-parse-time-string s)))
  16747. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16748. "Convert a time stamp to an absolute day number.
  16749. If there is a specifyer for a cyclic time stamp, get the closest date to
  16750. DAYNR."
  16751. (cond
  16752. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16753. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16754. daynr
  16755. (+ daynr 1000)))
  16756. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16757. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16758. (time-to-days (current-time))) (match-string 0 s)
  16759. prefer))
  16760. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16761. (defun org-time-from-absolute (d)
  16762. "Return the time corresponding to date D.
  16763. D may be an absolute day number, or a calendar-type list (month day year)."
  16764. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16765. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16766. (defun org-calendar-holiday ()
  16767. "List of holidays, for Diary display in Org-mode."
  16768. (require 'holidays)
  16769. (let ((hl (funcall
  16770. (if (fboundp 'calendar-check-holidays)
  16771. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16772. (if hl (mapconcat 'identity hl "; "))))
  16773. (defun org-diary-sexp-entry (sexp entry date)
  16774. "Process a SEXP diary ENTRY for DATE."
  16775. (require 'diary-lib)
  16776. (let ((result (if calendar-debug-sexp
  16777. (let ((stack-trace-on-error t))
  16778. (eval (car (read-from-string sexp))))
  16779. (condition-case nil
  16780. (eval (car (read-from-string sexp)))
  16781. (error
  16782. (beep)
  16783. (message "Bad sexp at line %d in %s: %s"
  16784. (org-current-line)
  16785. (buffer-file-name) sexp)
  16786. (sleep-for 2))))))
  16787. (cond ((stringp result) result)
  16788. ((and (consp result)
  16789. (stringp (cdr result))) (cdr result))
  16790. (result entry)
  16791. (t nil))))
  16792. (defun org-diary-to-ical-string (frombuf)
  16793. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16794. This uses the icalendar.el library."
  16795. (let* ((tmpdir (if (featurep 'xemacs)
  16796. (temp-directory)
  16797. temporary-file-directory))
  16798. (tmpfile (make-temp-name
  16799. (expand-file-name "orgics" tmpdir)))
  16800. buf rtn b e)
  16801. (save-excursion
  16802. (set-buffer frombuf)
  16803. (icalendar-export-region (point-min) (point-max) tmpfile)
  16804. (setq buf (find-buffer-visiting tmpfile))
  16805. (set-buffer buf)
  16806. (goto-char (point-min))
  16807. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16808. (setq b (match-beginning 0)))
  16809. (goto-char (point-max))
  16810. (if (re-search-backward "^END:VEVENT" nil t)
  16811. (setq e (match-end 0)))
  16812. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16813. (kill-buffer buf)
  16814. (kill-buffer frombuf)
  16815. (delete-file tmpfile)
  16816. rtn))
  16817. (defun org-closest-date (start current change prefer)
  16818. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16819. When PREFER is `past' return a date that is either CURRENT or past.
  16820. When PREFER is `future', return a date that is either CURRENT or future."
  16821. ;; Make the proper lists from the dates
  16822. (catch 'exit
  16823. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16824. dn dw sday cday n1 n2
  16825. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16826. (setq start (org-date-to-gregorian start)
  16827. current (org-date-to-gregorian
  16828. (if org-agenda-repeating-timestamp-show-all
  16829. current
  16830. (time-to-days (current-time))))
  16831. sday (calendar-absolute-from-gregorian start)
  16832. cday (calendar-absolute-from-gregorian current))
  16833. (if (<= cday sday) (throw 'exit sday))
  16834. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16835. (setq dn (string-to-number (match-string 1 change))
  16836. dw (cdr (assoc (match-string 2 change) a1)))
  16837. (error "Invalid change specifyer: %s" change))
  16838. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16839. (cond
  16840. ((eq dw 'day)
  16841. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16842. n2 (+ n1 dn)))
  16843. ((eq dw 'year)
  16844. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16845. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16846. (setq date1 (list m d y1)
  16847. n1 (calendar-absolute-from-gregorian date1)
  16848. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16849. n2 (calendar-absolute-from-gregorian date2)))
  16850. ((eq dw 'month)
  16851. ;; approx number of month between the tow dates
  16852. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16853. ;; How often does dn fit in there?
  16854. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16855. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16856. m (+ m nm)
  16857. ny (floor (/ m 12))
  16858. y (+ y ny)
  16859. m (- m (* ny 12)))
  16860. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16861. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16862. (setq m2 (+ m dn) y2 y)
  16863. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16864. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16865. (while (< n2 cday)
  16866. (setq n1 n2 m m2 y y2)
  16867. (setq m2 (+ m dn) y2 y)
  16868. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16869. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16870. (if org-agenda-repeating-timestamp-show-all
  16871. (cond
  16872. ((eq prefer 'past) n1)
  16873. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16874. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16875. (cond
  16876. ((eq prefer 'past) n1)
  16877. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16878. (t (if (= cday n1) n1 n2)))))))
  16879. (defun org-date-to-gregorian (date)
  16880. "Turn any specification of DATE into a gregorian date for the calendar."
  16881. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16882. ((and (listp date) (= (length date) 3)) date)
  16883. ((stringp date)
  16884. (setq date (org-parse-time-string date))
  16885. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16886. ((listp date)
  16887. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16888. (defun org-parse-time-string (s &optional nodefault)
  16889. "Parse the standard Org-mode time string.
  16890. This should be a lot faster than the normal `parse-time-string'.
  16891. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16892. hour and minute fields will be nil if not given."
  16893. (if (string-match org-ts-regexp0 s)
  16894. (list 0
  16895. (if (or (match-beginning 8) (not nodefault))
  16896. (string-to-number (or (match-string 8 s) "0")))
  16897. (if (or (match-beginning 7) (not nodefault))
  16898. (string-to-number (or (match-string 7 s) "0")))
  16899. (string-to-number (match-string 4 s))
  16900. (string-to-number (match-string 3 s))
  16901. (string-to-number (match-string 2 s))
  16902. nil nil nil)
  16903. (make-list 9 0)))
  16904. (defun org-timestamp-up (&optional arg)
  16905. "Increase the date item at the cursor by one.
  16906. If the cursor is on the year, change the year. If it is on the month or
  16907. the day, change that.
  16908. With prefix ARG, change by that many units."
  16909. (interactive "p")
  16910. (org-timestamp-change (prefix-numeric-value arg)))
  16911. (defun org-timestamp-down (&optional arg)
  16912. "Decrease the date item at the cursor by one.
  16913. If the cursor is on the year, change the year. If it is on the month or
  16914. the day, change that.
  16915. With prefix ARG, change by that many units."
  16916. (interactive "p")
  16917. (org-timestamp-change (- (prefix-numeric-value arg))))
  16918. (defun org-timestamp-up-day (&optional arg)
  16919. "Increase the date in the time stamp by one day.
  16920. With prefix ARG, change that many days."
  16921. (interactive "p")
  16922. (if (and (not (org-at-timestamp-p t))
  16923. (org-on-heading-p))
  16924. (org-todo 'up)
  16925. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16926. (defun org-timestamp-down-day (&optional arg)
  16927. "Decrease the date in the time stamp by one day.
  16928. With prefix ARG, change that many days."
  16929. (interactive "p")
  16930. (if (and (not (org-at-timestamp-p t))
  16931. (org-on-heading-p))
  16932. (org-todo 'down)
  16933. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16934. (defsubst org-pos-in-match-range (pos n)
  16935. (and (match-beginning n)
  16936. (<= (match-beginning n) pos)
  16937. (>= (match-end n) pos)))
  16938. (defun org-at-timestamp-p (&optional inactive-ok)
  16939. "Determine if the cursor is in or at a timestamp."
  16940. (interactive)
  16941. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16942. (pos (point))
  16943. (ans (or (looking-at tsr)
  16944. (save-excursion
  16945. (skip-chars-backward "^[<\n\r\t")
  16946. (if (> (point) (point-min)) (backward-char 1))
  16947. (and (looking-at tsr)
  16948. (> (- (match-end 0) pos) -1))))))
  16949. (and ans
  16950. (boundp 'org-ts-what)
  16951. (setq org-ts-what
  16952. (cond
  16953. ((= pos (match-beginning 0)) 'bracket)
  16954. ((= pos (1- (match-end 0))) 'bracket)
  16955. ((org-pos-in-match-range pos 2) 'year)
  16956. ((org-pos-in-match-range pos 3) 'month)
  16957. ((org-pos-in-match-range pos 7) 'hour)
  16958. ((org-pos-in-match-range pos 8) 'minute)
  16959. ((or (org-pos-in-match-range pos 4)
  16960. (org-pos-in-match-range pos 5)) 'day)
  16961. ((and (> pos (or (match-end 8) (match-end 5)))
  16962. (< pos (match-end 0)))
  16963. (- pos (or (match-end 8) (match-end 5))))
  16964. (t 'day))))
  16965. ans))
  16966. (defun org-toggle-timestamp-type ()
  16967. ""
  16968. (interactive)
  16969. (when (org-at-timestamp-p t)
  16970. (save-excursion
  16971. (goto-char (match-beginning 0))
  16972. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16973. (goto-char (1- (match-end 0)))
  16974. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16975. (message "Timestamp is now %sactive"
  16976. (if (equal (char-before) ?>) "in" ""))))
  16977. (defun org-timestamp-change (n &optional what)
  16978. "Change the date in the time stamp at point.
  16979. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16980. `year', `minute', `second'. If WHAT is not given, the cursor position
  16981. in the timestamp determines what will be changed."
  16982. (let ((pos (point))
  16983. with-hm inactive
  16984. org-ts-what
  16985. extra
  16986. ts time time0)
  16987. (if (not (org-at-timestamp-p t))
  16988. (error "Not at a timestamp"))
  16989. (if (and (not what) (eq org-ts-what 'bracket))
  16990. (org-toggle-timestamp-type)
  16991. (if (and (not what) (not (eq org-ts-what 'day))
  16992. org-display-custom-times
  16993. (get-text-property (point) 'display)
  16994. (not (get-text-property (1- (point)) 'display)))
  16995. (setq org-ts-what 'day))
  16996. (setq org-ts-what (or what org-ts-what)
  16997. inactive (= (char-after (match-beginning 0)) ?\[)
  16998. ts (match-string 0))
  16999. (replace-match "")
  17000. (if (string-match
  17001. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17002. ts)
  17003. (setq extra (match-string 1 ts)))
  17004. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17005. (setq with-hm t))
  17006. (setq time0 (org-parse-time-string ts))
  17007. (setq time
  17008. (encode-time (or (car time0) 0)
  17009. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17010. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17011. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17012. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17013. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17014. (nthcdr 6 time0)))
  17015. (when (integerp org-ts-what)
  17016. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17017. (if (eq what 'calendar)
  17018. (let ((cal-date (org-get-date-from-calendar)))
  17019. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17020. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17021. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17022. (setcar time0 (or (car time0) 0))
  17023. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17024. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17025. (setq time (apply 'encode-time time0))))
  17026. (setq org-last-changed-timestamp
  17027. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17028. (org-clock-update-time-maybe)
  17029. (goto-char pos)
  17030. ;; Try to recenter the calendar window, if any
  17031. (if (and org-calendar-follow-timestamp-change
  17032. (get-buffer-window "*Calendar*" t)
  17033. (memq org-ts-what '(day month year)))
  17034. (org-recenter-calendar (time-to-days time))))))
  17035. ;; FIXME: does not yet work for lead times
  17036. (defun org-modify-ts-extra (s pos n)
  17037. "Change the different parts of the lead-time and repeat fields in timestamp."
  17038. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17039. ng h m new)
  17040. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17041. (cond
  17042. ((or (org-pos-in-match-range pos 2)
  17043. (org-pos-in-match-range pos 3))
  17044. (setq m (string-to-number (match-string 3 s))
  17045. h (string-to-number (match-string 2 s)))
  17046. (if (org-pos-in-match-range pos 2)
  17047. (setq h (+ h n))
  17048. (setq m (+ m n)))
  17049. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17050. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17051. (setq h (min 24 (max 0 h)))
  17052. (setq ng 1 new (format "-%02d:%02d" h m)))
  17053. ((org-pos-in-match-range pos 6)
  17054. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17055. ((org-pos-in-match-range pos 5)
  17056. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17057. (when ng
  17058. (setq s (concat
  17059. (substring s 0 (match-beginning ng))
  17060. new
  17061. (substring s (match-end ng))))))
  17062. s))
  17063. (defun org-recenter-calendar (date)
  17064. "If the calendar is visible, recenter it to DATE."
  17065. (let* ((win (selected-window))
  17066. (cwin (get-buffer-window "*Calendar*" t))
  17067. (calendar-move-hook nil))
  17068. (when cwin
  17069. (select-window cwin)
  17070. (calendar-goto-date (if (listp date) date
  17071. (calendar-gregorian-from-absolute date)))
  17072. (select-window win))))
  17073. (defun org-goto-calendar (&optional arg)
  17074. "Go to the Emacs calendar at the current date.
  17075. If there is a time stamp in the current line, go to that date.
  17076. A prefix ARG can be used to force the current date."
  17077. (interactive "P")
  17078. (let ((tsr org-ts-regexp) diff
  17079. (calendar-move-hook nil)
  17080. (view-calendar-holidays-initially nil)
  17081. (view-diary-entries-initially nil))
  17082. (if (or (org-at-timestamp-p)
  17083. (save-excursion
  17084. (beginning-of-line 1)
  17085. (looking-at (concat ".*" tsr))))
  17086. (let ((d1 (time-to-days (current-time)))
  17087. (d2 (time-to-days
  17088. (org-time-string-to-time (match-string 1)))))
  17089. (setq diff (- d2 d1))))
  17090. (calendar)
  17091. (calendar-goto-today)
  17092. (if (and diff (not arg)) (calendar-forward-day diff))))
  17093. (defun org-get-date-from-calendar ()
  17094. "Return a list (month day year) of date at point in calendar."
  17095. (with-current-buffer "*Calendar*"
  17096. (save-match-data
  17097. (calendar-cursor-to-date))))
  17098. (defun org-date-from-calendar ()
  17099. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17100. If there is already a time stamp at the cursor position, update it."
  17101. (interactive)
  17102. (if (org-at-timestamp-p t)
  17103. (org-timestamp-change 0 'calendar)
  17104. (let ((cal-date (org-get-date-from-calendar)))
  17105. (org-insert-time-stamp
  17106. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17107. (defvar appt-time-msg-list)
  17108. ;;;###autoload
  17109. (defun org-agenda-to-appt (&optional refresh filter)
  17110. "Activate appointments found in `org-agenda-files'.
  17111. With a \\[universal-argument] prefix, refresh the list of
  17112. appointements.
  17113. If FILTER is t, interactively prompt the user for a regular
  17114. expression, and filter out entries that don't match it.
  17115. If FILTER is a string, use this string as a regular expression
  17116. for filtering entries out.
  17117. FILTER can also be an alist with the car of each cell being
  17118. either 'headline or 'category. For example:
  17119. '((headline \"IMPORTANT\")
  17120. (category \"Work\"))
  17121. will only add headlines containing IMPORTANT or headlines
  17122. belonging to the \"Work\" category."
  17123. (interactive "P")
  17124. (require 'calendar)
  17125. (if refresh (setq appt-time-msg-list nil))
  17126. (if (eq filter t)
  17127. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17128. (let* ((cnt 0) ; count added events
  17129. (org-agenda-new-buffers nil)
  17130. (org-deadline-warning-days 0)
  17131. (today (org-date-to-gregorian
  17132. (time-to-days (current-time))))
  17133. (files (org-agenda-files)) entries file)
  17134. ;; Get all entries which may contain an appt
  17135. (while (setq file (pop files))
  17136. (setq entries
  17137. (append entries
  17138. (org-agenda-get-day-entries
  17139. file today :timestamp :scheduled :deadline))))
  17140. (setq entries (delq nil entries))
  17141. ;; Map thru entries and find if we should filter them out
  17142. (mapc
  17143. (lambda(x)
  17144. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17145. (cat (get-text-property 1 'org-category x))
  17146. (tod (get-text-property 1 'time-of-day x))
  17147. (ok (or (null filter)
  17148. (and (stringp filter) (string-match filter evt))
  17149. (and (listp filter)
  17150. (or (string-match
  17151. (cadr (assoc 'category filter)) cat)
  17152. (string-match
  17153. (cadr (assoc 'headline filter)) evt))))))
  17154. ;; FIXME: Shall we remove text-properties for the appt text?
  17155. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17156. (when (and ok tod)
  17157. (setq tod (number-to-string tod)
  17158. tod (when (string-match
  17159. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17160. (concat (match-string 1 tod) ":"
  17161. (match-string 2 tod))))
  17162. (appt-add tod evt)
  17163. (setq cnt (1+ cnt))))) entries)
  17164. (org-release-buffers org-agenda-new-buffers)
  17165. (if (eq cnt 0)
  17166. (message "No event to add")
  17167. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17168. ;;; The clock for measuring work time.
  17169. (defvar org-mode-line-string "")
  17170. (put 'org-mode-line-string 'risky-local-variable t)
  17171. (defvar org-mode-line-timer nil)
  17172. (defvar org-clock-heading "")
  17173. (defvar org-clock-start-time "")
  17174. (defun org-update-mode-line ()
  17175. (let* ((delta (- (time-to-seconds (current-time))
  17176. (time-to-seconds org-clock-start-time)))
  17177. (h (floor delta 3600))
  17178. (m (floor (- delta (* 3600 h)) 60)))
  17179. (setq org-mode-line-string
  17180. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17181. 'help-echo "Org-mode clock is running"))
  17182. (force-mode-line-update)))
  17183. (defvar org-clock-marker (make-marker)
  17184. "Marker recording the last clock-in.")
  17185. (defvar org-clock-mode-line-entry nil
  17186. "Information for the modeline about the running clock.")
  17187. (defun org-clock-in ()
  17188. "Start the clock on the current item.
  17189. If necessary, clock-out of the currently active clock."
  17190. (interactive)
  17191. (org-clock-out t)
  17192. (let (ts)
  17193. (save-excursion
  17194. (org-back-to-heading t)
  17195. (when (and org-clock-in-switch-to-state
  17196. (not (looking-at (concat outline-regexp "[ \t]*"
  17197. org-clock-in-switch-to-state
  17198. "\\>"))))
  17199. (org-todo org-clock-in-switch-to-state))
  17200. (if (and org-clock-heading-function
  17201. (functionp org-clock-heading-function))
  17202. (setq org-clock-heading (funcall org-clock-heading-function))
  17203. (if (looking-at org-complex-heading-regexp)
  17204. (setq org-clock-heading (match-string 4))
  17205. (setq org-clock-heading "???")))
  17206. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17207. (org-clock-find-position)
  17208. (insert "\n") (backward-char 1)
  17209. (indent-relative)
  17210. (insert org-clock-string " ")
  17211. (setq org-clock-start-time (current-time))
  17212. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17213. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17214. (or global-mode-string (setq global-mode-string '("")))
  17215. (or (memq 'org-mode-line-string global-mode-string)
  17216. (setq global-mode-string
  17217. (append global-mode-string '(org-mode-line-string))))
  17218. (org-update-mode-line)
  17219. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17220. (message "Clock started at %s" ts))))
  17221. (defun org-clock-find-position ()
  17222. "Find the location where the next clock line should be inserted."
  17223. (org-back-to-heading t)
  17224. (catch 'exit
  17225. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17226. (re (concat "^[ \t]*" org-clock-string))
  17227. (cnt 0)
  17228. first last)
  17229. (goto-char beg)
  17230. (when (eobp) (newline) (setq end (max (point) end)))
  17231. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17232. ;; we seem to have a CLOCK drawer, so go there.
  17233. (beginning-of-line 2)
  17234. (throw 'exit t))
  17235. ;; Lets count the CLOCK lines
  17236. (goto-char beg)
  17237. (while (re-search-forward re end t)
  17238. (setq first (or first (match-beginning 0))
  17239. last (match-beginning 0)
  17240. cnt (1+ cnt)))
  17241. (when (and (integerp org-clock-into-drawer)
  17242. (>= (1+ cnt) org-clock-into-drawer))
  17243. ;; Wrap current entries into a new drawer
  17244. (goto-char last)
  17245. (beginning-of-line 2)
  17246. (if (org-at-item-p) (org-end-of-item))
  17247. (insert ":END:\n")
  17248. (beginning-of-line 0)
  17249. (org-indent-line-function)
  17250. (goto-char first)
  17251. (insert ":CLOCK:\n")
  17252. (beginning-of-line 0)
  17253. (org-indent-line-function)
  17254. (org-flag-drawer t)
  17255. (beginning-of-line 2)
  17256. (throw 'exit nil))
  17257. (goto-char beg)
  17258. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17259. (not (equal (match-string 1) org-clock-string)))
  17260. ;; Planning info, skip to after it
  17261. (beginning-of-line 2)
  17262. (or (bolp) (newline)))
  17263. (when (eq t org-clock-into-drawer)
  17264. (insert ":CLOCK:\n:END:\n")
  17265. (beginning-of-line -1)
  17266. (org-indent-line-function)
  17267. (org-flag-drawer t)
  17268. (beginning-of-line 2)
  17269. (org-indent-line-function)))))
  17270. (defun org-clock-out (&optional fail-quietly)
  17271. "Stop the currently running clock.
  17272. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17273. (interactive)
  17274. (catch 'exit
  17275. (if (not (marker-buffer org-clock-marker))
  17276. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17277. (let (ts te s h m)
  17278. (save-excursion
  17279. (set-buffer (marker-buffer org-clock-marker))
  17280. (goto-char org-clock-marker)
  17281. (beginning-of-line 1)
  17282. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17283. (equal (match-string 1) org-clock-string))
  17284. (setq ts (match-string 2))
  17285. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17286. (goto-char (match-end 0))
  17287. (delete-region (point) (point-at-eol))
  17288. (insert "--")
  17289. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17290. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17291. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17292. h (floor (/ s 3600))
  17293. s (- s (* 3600 h))
  17294. m (floor (/ s 60))
  17295. s (- s (* 60 s)))
  17296. (insert " => " (format "%2d:%02d" h m))
  17297. (move-marker org-clock-marker nil)
  17298. (when org-log-note-clock-out
  17299. (org-add-log-maybe 'clock-out))
  17300. (when org-mode-line-timer
  17301. (cancel-timer org-mode-line-timer)
  17302. (setq org-mode-line-timer nil))
  17303. (setq global-mode-string
  17304. (delq 'org-mode-line-string global-mode-string))
  17305. (force-mode-line-update)
  17306. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17307. (defun org-clock-cancel ()
  17308. "Cancel the running clock be removing the start timestamp."
  17309. (interactive)
  17310. (if (not (marker-buffer org-clock-marker))
  17311. (error "No active clock"))
  17312. (save-excursion
  17313. (set-buffer (marker-buffer org-clock-marker))
  17314. (goto-char org-clock-marker)
  17315. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17316. (setq global-mode-string
  17317. (delq 'org-mode-line-string global-mode-string))
  17318. (force-mode-line-update)
  17319. (message "Clock canceled"))
  17320. (defun org-clock-goto (&optional delete-windows)
  17321. "Go to the currently clocked-in entry."
  17322. (interactive "P")
  17323. (if (not (marker-buffer org-clock-marker))
  17324. (error "No active clock"))
  17325. (switch-to-buffer-other-window
  17326. (marker-buffer org-clock-marker))
  17327. (if delete-windows (delete-other-windows))
  17328. (goto-char org-clock-marker)
  17329. (org-show-entry)
  17330. (org-back-to-heading)
  17331. (recenter))
  17332. (defvar org-clock-file-total-minutes nil
  17333. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17334. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17335. (defun org-clock-sum (&optional tstart tend)
  17336. "Sum the times for each subtree.
  17337. Puts the resulting times in minutes as a text property on each headline."
  17338. (interactive)
  17339. (let* ((bmp (buffer-modified-p))
  17340. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17341. org-clock-string
  17342. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17343. (lmax 30)
  17344. (ltimes (make-vector lmax 0))
  17345. (t1 0)
  17346. (level 0)
  17347. ts te dt
  17348. time)
  17349. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17350. (save-excursion
  17351. (goto-char (point-max))
  17352. (while (re-search-backward re nil t)
  17353. (cond
  17354. ((match-end 2)
  17355. ;; Two time stamps
  17356. (setq ts (match-string 2)
  17357. te (match-string 3)
  17358. ts (time-to-seconds
  17359. (apply 'encode-time (org-parse-time-string ts)))
  17360. te (time-to-seconds
  17361. (apply 'encode-time (org-parse-time-string te)))
  17362. ts (if tstart (max ts tstart) ts)
  17363. te (if tend (min te tend) te)
  17364. dt (- te ts)
  17365. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17366. ((match-end 4)
  17367. ;; A naket time
  17368. (setq t1 (+ t1 (string-to-number (match-string 5))
  17369. (* 60 (string-to-number (match-string 4))))))
  17370. (t ;; A headline
  17371. (setq level (- (match-end 1) (match-beginning 1)))
  17372. (when (or (> t1 0) (> (aref ltimes level) 0))
  17373. (loop for l from 0 to level do
  17374. (aset ltimes l (+ (aref ltimes l) t1)))
  17375. (setq t1 0 time (aref ltimes level))
  17376. (loop for l from level to (1- lmax) do
  17377. (aset ltimes l 0))
  17378. (goto-char (match-beginning 0))
  17379. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17380. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17381. (set-buffer-modified-p bmp)))
  17382. (defun org-clock-display (&optional total-only)
  17383. "Show subtree times in the entire buffer.
  17384. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17385. in the echo area."
  17386. (interactive)
  17387. (org-remove-clock-overlays)
  17388. (let (time h m p)
  17389. (org-clock-sum)
  17390. (unless total-only
  17391. (save-excursion
  17392. (goto-char (point-min))
  17393. (while (or (and (equal (setq p (point)) (point-min))
  17394. (get-text-property p :org-clock-minutes))
  17395. (setq p (next-single-property-change
  17396. (point) :org-clock-minutes)))
  17397. (goto-char p)
  17398. (when (setq time (get-text-property p :org-clock-minutes))
  17399. (org-put-clock-overlay time (funcall outline-level))))
  17400. (setq h (/ org-clock-file-total-minutes 60)
  17401. m (- org-clock-file-total-minutes (* 60 h)))
  17402. ;; Arrange to remove the overlays upon next change.
  17403. (when org-remove-highlights-with-change
  17404. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17405. nil 'local))))
  17406. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17407. (defvar org-clock-overlays nil)
  17408. (make-variable-buffer-local 'org-clock-overlays)
  17409. (defun org-put-clock-overlay (time &optional level)
  17410. "Put an overlays on the current line, displaying TIME.
  17411. If LEVEL is given, prefix time with a corresponding number of stars.
  17412. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17413. will be easy to remove."
  17414. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17415. (l (if level (org-get-legal-level level 0) 0))
  17416. (off 0)
  17417. ov tx)
  17418. (move-to-column c)
  17419. (unless (eolp) (skip-chars-backward "^ \t"))
  17420. (skip-chars-backward " \t")
  17421. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17422. tx (concat (buffer-substring (1- (point)) (point))
  17423. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17424. (org-add-props (format "%s %2d:%02d%s"
  17425. (make-string l ?*) h m
  17426. (make-string (- 16 l) ?\ ))
  17427. '(face secondary-selection))
  17428. ""))
  17429. (if (not (featurep 'xemacs))
  17430. (org-overlay-put ov 'display tx)
  17431. (org-overlay-put ov 'invisible t)
  17432. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17433. (push ov org-clock-overlays)))
  17434. (defun org-remove-clock-overlays (&optional beg end noremove)
  17435. "Remove the occur highlights from the buffer.
  17436. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17437. from the `before-change-functions' in the current buffer."
  17438. (interactive)
  17439. (unless org-inhibit-highlight-removal
  17440. (mapc 'org-delete-overlay org-clock-overlays)
  17441. (setq org-clock-overlays nil)
  17442. (unless noremove
  17443. (remove-hook 'before-change-functions
  17444. 'org-remove-clock-overlays 'local))))
  17445. (defun org-clock-out-if-current ()
  17446. "Clock out if the current entry contains the running clock.
  17447. This is used to stop the clock after a TODO entry is marked DONE,
  17448. and is only done if the variable `org-clock-out-when-done' is not nil."
  17449. (when (and org-clock-out-when-done
  17450. (member state org-done-keywords)
  17451. (equal (marker-buffer org-clock-marker) (current-buffer))
  17452. (< (point) org-clock-marker)
  17453. (> (save-excursion (outline-next-heading) (point))
  17454. org-clock-marker))
  17455. ;; Clock out, but don't accept a logging message for this.
  17456. (let ((org-log-note-clock-out nil))
  17457. (org-clock-out))))
  17458. (add-hook 'org-after-todo-state-change-hook
  17459. 'org-clock-out-if-current)
  17460. (defun org-check-running-clock ()
  17461. "Check if the current buffer contains the running clock.
  17462. If yes, offer to stop it and to save the buffer with the changes."
  17463. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17464. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17465. (buffer-name))))
  17466. (org-clock-out)
  17467. (when (y-or-n-p "Save changed buffer?")
  17468. (save-buffer))))
  17469. (defun org-clock-report (&optional arg)
  17470. "Create a table containing a report about clocked time.
  17471. If the cursor is inside an existing clocktable block, then the table
  17472. will be updated. If not, a new clocktable will be inserted.
  17473. When called with a prefix argument, move to the first clock table in the
  17474. buffer and update it."
  17475. (interactive "P")
  17476. (org-remove-clock-overlays)
  17477. (when arg
  17478. (org-find-dblock "clocktable")
  17479. (org-show-entry))
  17480. (if (org-in-clocktable-p)
  17481. (goto-char (org-in-clocktable-p))
  17482. (org-create-dblock (list :name "clocktable"
  17483. :maxlevel 2 :scope 'file)))
  17484. (org-update-dblock))
  17485. (defun org-in-clocktable-p ()
  17486. "Check if the cursor is in a clocktable."
  17487. (let ((pos (point)) start)
  17488. (save-excursion
  17489. (end-of-line 1)
  17490. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17491. (setq start (match-beginning 0))
  17492. (re-search-forward "^#\\+END:.*" nil t)
  17493. (>= (match-end 0) pos)
  17494. start))))
  17495. (defun org-clock-update-time-maybe ()
  17496. "If this is a CLOCK line, update it and return t.
  17497. Otherwise, return nil."
  17498. (interactive)
  17499. (save-excursion
  17500. (beginning-of-line 1)
  17501. (skip-chars-forward " \t")
  17502. (when (looking-at org-clock-string)
  17503. (let ((re (concat "[ \t]*" org-clock-string
  17504. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17505. "\\([ \t]*=>.*\\)?"))
  17506. ts te h m s)
  17507. (if (not (looking-at re))
  17508. nil
  17509. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17510. (end-of-line 1)
  17511. (setq ts (match-string 1)
  17512. te (match-string 2))
  17513. (setq s (- (time-to-seconds
  17514. (apply 'encode-time (org-parse-time-string te)))
  17515. (time-to-seconds
  17516. (apply 'encode-time (org-parse-time-string ts))))
  17517. h (floor (/ s 3600))
  17518. s (- s (* 3600 h))
  17519. m (floor (/ s 60))
  17520. s (- s (* 60 s)))
  17521. (insert " => " (format "%2d:%02d" h m))
  17522. t)))))
  17523. (defun org-clock-special-range (key &optional time as-strings)
  17524. "Return two times bordering a special time range.
  17525. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17526. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17527. A week starts Monday 0:00 and ends Sunday 24:00.
  17528. The range is determined relative to TIME. TIME defaults to the current time.
  17529. The return value is a cons cell with two internal times like the ones
  17530. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17531. the returned times will be formatted strings."
  17532. (let* ((tm (decode-time (or time (current-time))))
  17533. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17534. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17535. (dow (nth 6 tm))
  17536. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17537. (cond
  17538. ((eq key 'today)
  17539. (setq h 0 m 0 h1 24 m1 0))
  17540. ((eq key 'yesterday)
  17541. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17542. ((eq key 'thisweek)
  17543. (setq diff (if (= dow 0) 6 (1- dow))
  17544. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17545. ((eq key 'lastweek)
  17546. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17547. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17548. ((eq key 'thismonth)
  17549. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17550. ((eq key 'lastmonth)
  17551. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17552. ((eq key 'thisyear)
  17553. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17554. ((eq key 'lastyear)
  17555. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17556. (t (error "No such time block %s" key)))
  17557. (setq ts (encode-time s m h d month y)
  17558. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17559. (or d1 d) (or month1 month) (or y1 y)))
  17560. (setq fm (cdr org-time-stamp-formats))
  17561. (if as-strings
  17562. (cons (format-time-string fm ts) (format-time-string fm te))
  17563. (cons ts te))))
  17564. (defun org-dblock-write:clocktable (params)
  17565. "Write the standard clocktable."
  17566. (catch 'exit
  17567. (let* ((hlchars '((1 . "*") (2 . "/")))
  17568. (ins (make-marker))
  17569. (total-time nil)
  17570. (scope (plist-get params :scope))
  17571. (tostring (plist-get params :tostring))
  17572. (multifile (plist-get params :multifile))
  17573. (header (plist-get params :header))
  17574. (maxlevel (or (plist-get params :maxlevel) 3))
  17575. (step (plist-get params :step))
  17576. (emph (plist-get params :emphasize))
  17577. (ts (plist-get params :tstart))
  17578. (te (plist-get params :tend))
  17579. (block (plist-get params :block))
  17580. ipos time h m p level hlc hdl
  17581. cc beg end pos tbl)
  17582. (when step
  17583. (org-clocktable-steps params)
  17584. (throw 'exit nil))
  17585. (when block
  17586. (setq cc (org-clock-special-range block nil t)
  17587. ts (car cc) te (cdr cc)))
  17588. (if ts (setq ts (time-to-seconds
  17589. (apply 'encode-time (org-parse-time-string ts)))))
  17590. (if te (setq te (time-to-seconds
  17591. (apply 'encode-time (org-parse-time-string te)))))
  17592. (move-marker ins (point))
  17593. (setq ipos (point))
  17594. ;; Get the right scope
  17595. (setq pos (point))
  17596. (save-restriction
  17597. (cond
  17598. ((not scope))
  17599. ((eq scope 'file) (widen))
  17600. ((eq scope 'subtree) (org-narrow-to-subtree))
  17601. ((eq scope 'tree)
  17602. (while (org-up-heading-safe))
  17603. (org-narrow-to-subtree))
  17604. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17605. (symbol-name scope)))
  17606. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17607. (catch 'exit
  17608. (while (org-up-heading-safe)
  17609. (looking-at outline-regexp)
  17610. (if (<= (org-reduced-level (funcall outline-level)) level)
  17611. (throw 'exit nil))))
  17612. (org-narrow-to-subtree))
  17613. ((or (listp scope) (eq scope 'agenda))
  17614. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17615. (scope 'agenda)
  17616. (p1 (copy-sequence params))
  17617. file)
  17618. (plist-put p1 :tostring t)
  17619. (plist-put p1 :multifile t)
  17620. (plist-put p1 :scope 'file)
  17621. (org-prepare-agenda-buffers files)
  17622. (while (setq file (pop files))
  17623. (with-current-buffer (find-buffer-visiting file)
  17624. (push (org-clocktable-add-file
  17625. file (org-dblock-write:clocktable p1)) tbl)
  17626. (setq total-time (+ (or total-time 0)
  17627. org-clock-file-total-minutes)))))))
  17628. (goto-char pos)
  17629. (unless (eq scope 'agenda)
  17630. (org-clock-sum ts te)
  17631. (goto-char (point-min))
  17632. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17633. (goto-char p)
  17634. (when (setq time (get-text-property p :org-clock-minutes))
  17635. (save-excursion
  17636. (beginning-of-line 1)
  17637. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17638. (setq level (org-reduced-level
  17639. (- (match-end 1) (match-beginning 1))))
  17640. (<= level maxlevel))
  17641. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17642. hdl (match-string 2)
  17643. h (/ time 60)
  17644. m (- time (* 60 h)))
  17645. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17646. (push (concat
  17647. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17648. (make-string (1- level) ?|)
  17649. hlc (format "%d:%02d" h m) hlc
  17650. " |") tbl))))))
  17651. (setq tbl (nreverse tbl))
  17652. (if tostring
  17653. (if tbl (mapconcat 'identity tbl "\n") nil)
  17654. (goto-char ins)
  17655. (insert-before-markers
  17656. (or header
  17657. (concat
  17658. "Clock summary at ["
  17659. (substring
  17660. (format-time-string (cdr org-time-stamp-formats))
  17661. 1 -1)
  17662. "]."
  17663. (if block
  17664. (format " Considered range is /%s/." block)
  17665. "")
  17666. "\n\n"))
  17667. (if (eq scope 'agenda) "|File" "")
  17668. "|L|Headline|Time|\n")
  17669. (setq total-time (or total-time org-clock-file-total-minutes)
  17670. h (/ total-time 60)
  17671. m (- total-time (* 60 h)))
  17672. (insert-before-markers
  17673. "|-\n|"
  17674. (if (eq scope 'agenda) "|" "")
  17675. "|"
  17676. "*Total time*| "
  17677. (format "*%d:%02d*" h m)
  17678. "|\n|-\n")
  17679. (setq tbl (delq nil tbl))
  17680. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17681. (equal (substring (car tbl) 0 2) "|-"))
  17682. (pop tbl))
  17683. (insert-before-markers (mapconcat
  17684. 'identity (delq nil tbl)
  17685. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17686. (backward-delete-char 1)
  17687. (goto-char ipos)
  17688. (skip-chars-forward "^|")
  17689. (org-table-align))))))
  17690. (defun org-clocktable-steps (params)
  17691. (let* ((p1 (copy-sequence params))
  17692. (ts (plist-get p1 :tstart))
  17693. (te (plist-get p1 :tend))
  17694. (step0 (plist-get p1 :step))
  17695. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17696. (block (plist-get p1 :block))
  17697. cc)
  17698. (when block
  17699. (setq cc (org-clock-special-range block nil t)
  17700. ts (car cc) te (cdr cc)))
  17701. (if ts (setq ts (time-to-seconds
  17702. (apply 'encode-time (org-parse-time-string ts)))))
  17703. (if te (setq te (time-to-seconds
  17704. (apply 'encode-time (org-parse-time-string te)))))
  17705. (plist-put p1 :header "")
  17706. (plist-put p1 :step nil)
  17707. (plist-put p1 :block nil)
  17708. (while (< ts te)
  17709. (or (bolp) (insert "\n"))
  17710. (plist-put p1 :tstart (format-time-string
  17711. (car org-time-stamp-formats)
  17712. (seconds-to-time ts)))
  17713. (plist-put p1 :tend (format-time-string
  17714. (car org-time-stamp-formats)
  17715. (seconds-to-time (setq ts (+ ts step)))))
  17716. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17717. (plist-get p1 :tstart) "\n")
  17718. (org-dblock-write:clocktable p1)
  17719. (re-search-forward "#\\+END:")
  17720. (end-of-line 0))))
  17721. (defun org-clocktable-add-file (file table)
  17722. (if table
  17723. (let ((lines (org-split-string table "\n"))
  17724. (ff (file-name-nondirectory file)))
  17725. (mapconcat 'identity
  17726. (mapcar (lambda (x)
  17727. (if (string-match org-table-dataline-regexp x)
  17728. (concat "|" ff x)
  17729. x))
  17730. lines)
  17731. "\n"))))
  17732. ;; FIXME: I don't think anybody uses this, ask David
  17733. (defun org-collect-clock-time-entries ()
  17734. "Return an internal list with clocking information.
  17735. This list has one entry for each CLOCK interval.
  17736. FIXME: describe the elements."
  17737. (interactive)
  17738. (let ((re (concat "^[ \t]*" org-clock-string
  17739. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17740. rtn beg end next cont level title total closedp leafp
  17741. clockpos titlepos h m donep)
  17742. (save-excursion
  17743. (org-clock-sum)
  17744. (goto-char (point-min))
  17745. (while (re-search-forward re nil t)
  17746. (setq clockpos (match-beginning 0)
  17747. beg (match-string 1) end (match-string 2)
  17748. cont (match-end 0))
  17749. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17750. end (apply 'encode-time (org-parse-time-string end)))
  17751. (org-back-to-heading t)
  17752. (setq donep (org-entry-is-done-p))
  17753. (setq titlepos (point)
  17754. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17755. h (/ total 60) m (- total (* 60 h))
  17756. total (cons h m))
  17757. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17758. (setq level (- (match-end 1) (match-beginning 1))
  17759. title (org-match-string-no-properties 2))
  17760. (save-excursion (outline-next-heading) (setq next (point)))
  17761. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17762. (goto-char next)
  17763. (setq leafp (and (looking-at "^\\*+ ")
  17764. (<= (- (match-end 0) (point)) level)))
  17765. (push (list beg end clockpos closedp donep
  17766. total title titlepos level leafp)
  17767. rtn)
  17768. (goto-char cont)))
  17769. (nreverse rtn)))
  17770. ;;;; Agenda, and Diary Integration
  17771. ;;; Define the Org-agenda-mode
  17772. (defvar org-agenda-mode-map (make-sparse-keymap)
  17773. "Keymap for `org-agenda-mode'.")
  17774. (defvar org-agenda-menu) ; defined later in this file.
  17775. (defvar org-agenda-follow-mode nil)
  17776. (defvar org-agenda-show-log nil)
  17777. (defvar org-agenda-redo-command nil)
  17778. (defvar org-agenda-mode-hook nil)
  17779. (defvar org-agenda-type nil)
  17780. (defvar org-agenda-force-single-file nil)
  17781. (defun org-agenda-mode ()
  17782. "Mode for time-sorted view on action items in Org-mode files.
  17783. The following commands are available:
  17784. \\{org-agenda-mode-map}"
  17785. (interactive)
  17786. (kill-all-local-variables)
  17787. (setq org-agenda-undo-list nil
  17788. org-agenda-pending-undo-list nil)
  17789. (setq major-mode 'org-agenda-mode)
  17790. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17791. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17792. (setq mode-name "Org-Agenda")
  17793. (use-local-map org-agenda-mode-map)
  17794. (easy-menu-add org-agenda-menu)
  17795. (if org-startup-truncated (setq truncate-lines t))
  17796. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17797. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17798. ;; Make sure properties are removed when copying text
  17799. (when (boundp 'buffer-substring-filters)
  17800. (org-set-local 'buffer-substring-filters
  17801. (cons (lambda (x)
  17802. (set-text-properties 0 (length x) nil x) x)
  17803. buffer-substring-filters)))
  17804. (unless org-agenda-keep-modes
  17805. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17806. org-agenda-show-log nil))
  17807. (easy-menu-change
  17808. '("Agenda") "Agenda Files"
  17809. (append
  17810. (list
  17811. (vector
  17812. (if (get 'org-agenda-files 'org-restrict)
  17813. "Restricted to single file"
  17814. "Edit File List")
  17815. '(org-edit-agenda-file-list)
  17816. (not (get 'org-agenda-files 'org-restrict)))
  17817. "--")
  17818. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17819. (org-agenda-set-mode-name)
  17820. (apply
  17821. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17822. (list 'org-agenda-mode-hook)))
  17823. (substitute-key-definition 'undo 'org-agenda-undo
  17824. org-agenda-mode-map global-map)
  17825. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17826. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17827. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17828. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17829. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17830. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17831. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17832. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17833. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17834. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17835. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17836. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17837. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17838. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17839. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17840. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17841. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17842. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17843. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17844. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17845. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17846. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17847. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17848. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17849. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17850. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17851. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17852. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17853. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17854. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17855. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17856. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17857. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17858. (while l (org-defkey org-agenda-mode-map
  17859. (int-to-string (pop l)) 'digit-argument)))
  17860. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17861. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17862. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17863. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17864. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17865. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17866. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17867. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17868. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17869. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17870. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17871. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17872. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17873. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17874. (org-defkey org-agenda-mode-map "n" 'next-line)
  17875. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17876. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17877. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17878. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17879. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17880. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17881. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17882. (eval-after-load "calendar"
  17883. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17884. 'org-calendar-goto-agenda))
  17885. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17886. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17887. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17888. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17889. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17890. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17891. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17892. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17893. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17894. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17895. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17896. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17897. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17898. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17899. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17900. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17901. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17902. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17903. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17904. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17905. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17906. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17907. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17908. "Local keymap for agenda entries from Org-mode.")
  17909. (org-defkey org-agenda-keymap
  17910. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17911. (org-defkey org-agenda-keymap
  17912. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17913. (when org-agenda-mouse-1-follows-link
  17914. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17915. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17916. '("Agenda"
  17917. ("Agenda Files")
  17918. "--"
  17919. ["Show" org-agenda-show t]
  17920. ["Go To (other window)" org-agenda-goto t]
  17921. ["Go To (this window)" org-agenda-switch-to t]
  17922. ["Follow Mode" org-agenda-follow-mode
  17923. :style toggle :selected org-agenda-follow-mode :active t]
  17924. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17925. "--"
  17926. ["Cycle TODO" org-agenda-todo t]
  17927. ["Archive subtree" org-agenda-archive t]
  17928. ["Delete subtree" org-agenda-kill t]
  17929. "--"
  17930. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17931. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17932. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17933. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17934. "--"
  17935. ("Tags and Properties"
  17936. ["Show all Tags" org-agenda-show-tags t]
  17937. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17938. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17939. "--"
  17940. ["Column View" org-columns t])
  17941. ("Date/Schedule"
  17942. ["Schedule" org-agenda-schedule t]
  17943. ["Set Deadline" org-agenda-deadline t]
  17944. "--"
  17945. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17946. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17947. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17948. ("Clock"
  17949. ["Clock in" org-agenda-clock-in t]
  17950. ["Clock out" org-agenda-clock-out t]
  17951. ["Clock cancel" org-agenda-clock-cancel t]
  17952. ["Goto running clock" org-clock-goto t])
  17953. ("Priority"
  17954. ["Set Priority" org-agenda-priority t]
  17955. ["Increase Priority" org-agenda-priority-up t]
  17956. ["Decrease Priority" org-agenda-priority-down t]
  17957. ["Show Priority" org-agenda-show-priority t])
  17958. ("Calendar/Diary"
  17959. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17960. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17961. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17962. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17963. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17964. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17965. "--"
  17966. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17967. "--"
  17968. ("View"
  17969. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17970. :style radio :selected (equal org-agenda-ndays 1)]
  17971. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17972. :style radio :selected (equal org-agenda-ndays 7)]
  17973. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17974. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17975. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17976. :style radio :selected (member org-agenda-ndays '(365 366))]
  17977. "--"
  17978. ["Show Logbook entries" org-agenda-log-mode
  17979. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17980. ["Include Diary" org-agenda-toggle-diary
  17981. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17982. ["Use Time Grid" org-agenda-toggle-time-grid
  17983. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17984. ["Write view to file" org-write-agenda t]
  17985. ["Rebuild buffer" org-agenda-redo t]
  17986. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17987. "--"
  17988. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17989. "--"
  17990. ["Quit" org-agenda-quit t]
  17991. ["Exit and Release Buffers" org-agenda-exit t]
  17992. ))
  17993. ;;; Agenda undo
  17994. (defvar org-agenda-allow-remote-undo t
  17995. "Non-nil means, allow remote undo from the agenda buffer.")
  17996. (defvar org-agenda-undo-list nil
  17997. "List of undoable operations in the agenda since last refresh.")
  17998. (defvar org-agenda-undo-has-started-in nil
  17999. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18000. (defvar org-agenda-pending-undo-list nil
  18001. "In a series of undo commands, this is the list of remaning undo items.")
  18002. (defmacro org-if-unprotected (&rest body)
  18003. "Execute BODY if there is no `org-protected' text property at point."
  18004. (declare (debug t))
  18005. `(unless (get-text-property (point) 'org-protected)
  18006. ,@body))
  18007. (defmacro org-with-remote-undo (_buffer &rest _body)
  18008. "Execute BODY while recording undo information in two buffers."
  18009. (declare (indent 1) (debug t))
  18010. `(let ((_cline (org-current-line))
  18011. (_cmd this-command)
  18012. (_buf1 (current-buffer))
  18013. (_buf2 ,_buffer)
  18014. (_undo1 buffer-undo-list)
  18015. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18016. _c1 _c2)
  18017. ,@_body
  18018. (when org-agenda-allow-remote-undo
  18019. (setq _c1 (org-verify-change-for-undo
  18020. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18021. _c2 (org-verify-change-for-undo
  18022. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18023. (when (or _c1 _c2)
  18024. ;; make sure there are undo boundaries
  18025. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18026. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18027. ;; remember which buffer to undo
  18028. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18029. org-agenda-undo-list)))))
  18030. (defun org-agenda-undo ()
  18031. "Undo a remote editing step in the agenda.
  18032. This undoes changes both in the agenda buffer and in the remote buffer
  18033. that have been changed along."
  18034. (interactive)
  18035. (or org-agenda-allow-remote-undo
  18036. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18037. (if (not (eq this-command last-command))
  18038. (setq org-agenda-undo-has-started-in nil
  18039. org-agenda-pending-undo-list org-agenda-undo-list))
  18040. (if (not org-agenda-pending-undo-list)
  18041. (error "No further undo information"))
  18042. (let* ((entry (pop org-agenda-pending-undo-list))
  18043. buf line cmd rembuf)
  18044. (setq cmd (pop entry) line (pop entry))
  18045. (setq rembuf (nth 2 entry))
  18046. (org-with-remote-undo rembuf
  18047. (while (bufferp (setq buf (pop entry)))
  18048. (if (pop entry)
  18049. (with-current-buffer buf
  18050. (let ((last-undo-buffer buf)
  18051. (inhibit-read-only t))
  18052. (unless (memq buf org-agenda-undo-has-started-in)
  18053. (push buf org-agenda-undo-has-started-in)
  18054. (make-local-variable 'pending-undo-list)
  18055. (undo-start))
  18056. (while (and pending-undo-list
  18057. (listp pending-undo-list)
  18058. (not (car pending-undo-list)))
  18059. (pop pending-undo-list))
  18060. (undo-more 1))))))
  18061. (goto-line line)
  18062. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18063. (defun org-verify-change-for-undo (l1 l2)
  18064. "Verify that a real change occurred between the undo lists L1 and L2."
  18065. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18066. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18067. (not (eq l1 l2)))
  18068. ;;; Agenda dispatch
  18069. (defvar org-agenda-restrict nil)
  18070. (defvar org-agenda-restrict-begin (make-marker))
  18071. (defvar org-agenda-restrict-end (make-marker))
  18072. (defvar org-agenda-last-dispatch-buffer nil)
  18073. (defvar org-agenda-overriding-restriction nil)
  18074. ;;;###autoload
  18075. (defun org-agenda (arg &optional keys restriction)
  18076. "Dispatch agenda commands to collect entries to the agenda buffer.
  18077. Prompts for a command to execute. Any prefix arg will be passed
  18078. on to the selected command. The default selections are:
  18079. a Call `org-agenda-list' to display the agenda for current day or week.
  18080. t Call `org-todo-list' to display the global todo list.
  18081. T Call `org-todo-list' to display the global todo list, select only
  18082. entries with a specific TODO keyword (the user gets a prompt).
  18083. m Call `org-tags-view' to display headlines with tags matching
  18084. a condition (the user is prompted for the condition).
  18085. M Like `m', but select only TODO entries, no ordinary headlines.
  18086. L Create a timeline for the current buffer.
  18087. e Export views to associated files.
  18088. More commands can be added by configuring the variable
  18089. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18090. searches can be pre-defined in this way.
  18091. If the current buffer is in Org-mode and visiting a file, you can also
  18092. first press `<' once to indicate that the agenda should be temporarily
  18093. \(until the next use of \\[org-agenda]) restricted to the current file.
  18094. Pressing `<' twice means to restrict to the current subtree or region
  18095. \(if active)."
  18096. (interactive "P")
  18097. (catch 'exit
  18098. (let* ((prefix-descriptions nil)
  18099. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18100. (org-agenda-custom-commands
  18101. ;; normalize different versions
  18102. (delq nil
  18103. (mapcar
  18104. (lambda (x)
  18105. (cond ((stringp (cdr x))
  18106. (push x prefix-descriptions)
  18107. nil)
  18108. ((stringp (nth 1 x)) x)
  18109. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18110. (t (cons (car x) (cons "" (cdr x))))))
  18111. org-agenda-custom-commands)))
  18112. (buf (current-buffer))
  18113. (bfn (buffer-file-name (buffer-base-buffer)))
  18114. entry key type match lprops ans)
  18115. ;; Turn off restriction unless there is an overriding one
  18116. (unless org-agenda-overriding-restriction
  18117. (put 'org-agenda-files 'org-restrict nil)
  18118. (setq org-agenda-restrict nil)
  18119. (move-marker org-agenda-restrict-begin nil)
  18120. (move-marker org-agenda-restrict-end nil))
  18121. ;; Delete old local properties
  18122. (put 'org-agenda-redo-command 'org-lprops nil)
  18123. ;; Remember where this call originated
  18124. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18125. (unless keys
  18126. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18127. keys (car ans)
  18128. restriction (cdr ans)))
  18129. ;; Estabish the restriction, if any
  18130. (when (and (not org-agenda-overriding-restriction) restriction)
  18131. (put 'org-agenda-files 'org-restrict (list bfn))
  18132. (cond
  18133. ((eq restriction 'region)
  18134. (setq org-agenda-restrict t)
  18135. (move-marker org-agenda-restrict-begin (region-beginning))
  18136. (move-marker org-agenda-restrict-end (region-end)))
  18137. ((eq restriction 'subtree)
  18138. (save-excursion
  18139. (setq org-agenda-restrict t)
  18140. (org-back-to-heading t)
  18141. (move-marker org-agenda-restrict-begin (point))
  18142. (move-marker org-agenda-restrict-end
  18143. (progn (org-end-of-subtree t)))))))
  18144. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18145. ;; For example the todo list should not need it (but does...)
  18146. (cond
  18147. ((setq entry (assoc keys org-agenda-custom-commands))
  18148. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18149. (progn
  18150. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18151. (put 'org-agenda-redo-command 'org-lprops lprops)
  18152. (cond
  18153. ((eq type 'agenda)
  18154. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18155. ((eq type 'alltodo)
  18156. (org-let lprops '(org-todo-list current-prefix-arg)))
  18157. ((eq type 'search)
  18158. (org-let lprops '(org-search-view current-prefix-arg match)))
  18159. ((eq type 'stuck)
  18160. (org-let lprops '(org-agenda-list-stuck-projects
  18161. current-prefix-arg)))
  18162. ((eq type 'tags)
  18163. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18164. ((eq type 'tags-todo)
  18165. (org-let lprops '(org-tags-view '(4) match)))
  18166. ((eq type 'todo)
  18167. (org-let lprops '(org-todo-list match)))
  18168. ((eq type 'tags-tree)
  18169. (org-check-for-org-mode)
  18170. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18171. ((eq type 'todo-tree)
  18172. (org-check-for-org-mode)
  18173. (org-let lprops
  18174. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18175. (regexp-quote match) "\\>"))))
  18176. ((eq type 'occur-tree)
  18177. (org-check-for-org-mode)
  18178. (org-let lprops '(org-occur match)))
  18179. ((functionp type)
  18180. (org-let lprops '(funcall type match)))
  18181. ((fboundp type)
  18182. (org-let lprops '(funcall type match)))
  18183. (t (error "Invalid custom agenda command type %s" type))))
  18184. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18185. ((equal keys "C")
  18186. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18187. (customize-variable 'org-agenda-custom-commands))
  18188. ((equal keys "a") (call-interactively 'org-agenda-list))
  18189. ((equal keys "s") (call-interactively 'org-search-view))
  18190. ((equal keys "t") (call-interactively 'org-todo-list))
  18191. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18192. ((equal keys "m") (call-interactively 'org-tags-view))
  18193. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18194. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18195. ((equal keys "L")
  18196. (unless (org-mode-p)
  18197. (error "This is not an Org-mode file"))
  18198. (unless restriction
  18199. (put 'org-agenda-files 'org-restrict (list bfn))
  18200. (org-call-with-arg 'org-timeline arg)))
  18201. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18202. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18203. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18204. (t (error "Invalid agenda key"))))))
  18205. (defun org-agenda-normalize-custom-commands (cmds)
  18206. (delq nil
  18207. (mapcar
  18208. (lambda (x)
  18209. (cond ((stringp (cdr x)) nil)
  18210. ((stringp (nth 1 x)) x)
  18211. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18212. (t (cons (car x) (cons "" (cdr x))))))
  18213. cmds)))
  18214. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18215. "The user interface for selecting an agenda command."
  18216. (catch 'exit
  18217. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18218. (restrict-ok (and bfn (org-mode-p)))
  18219. (region-p (org-region-active-p))
  18220. (custom org-agenda-custom-commands)
  18221. (selstring "")
  18222. restriction second-time
  18223. c entry key type match prefixes rmheader header-end custom1 desc)
  18224. (save-window-excursion
  18225. (delete-other-windows)
  18226. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18227. (erase-buffer)
  18228. (insert (eval-when-compile
  18229. (let ((header
  18230. "
  18231. Press key for an agenda command: < Buffer,subtree/region restriction
  18232. -------------------------------- > Remove restriction
  18233. a Agenda for current week or day e Export agenda views
  18234. t List of all TODO entries T Entries with special TODO kwd
  18235. m Match a TAGS query M Like m, but only TODO entries
  18236. L Timeline for current buffer # List stuck projects (!=configure)
  18237. s Search for keywords C Configure custom agenda commands
  18238. / Multi-occur
  18239. ")
  18240. (start 0))
  18241. (while (string-match
  18242. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18243. header start)
  18244. (setq start (match-end 0))
  18245. (add-text-properties (match-beginning 2) (match-end 2)
  18246. '(face bold) header))
  18247. header)))
  18248. (setq header-end (move-marker (make-marker) (point)))
  18249. (while t
  18250. (setq custom1 custom)
  18251. (when (eq rmheader t)
  18252. (goto-line 1)
  18253. (re-search-forward ":" nil t)
  18254. (delete-region (match-end 0) (point-at-eol))
  18255. (forward-char 1)
  18256. (looking-at "-+")
  18257. (delete-region (match-end 0) (point-at-eol))
  18258. (move-marker header-end (match-end 0)))
  18259. (goto-char header-end)
  18260. (delete-region (point) (point-max))
  18261. (while (setq entry (pop custom1))
  18262. (setq key (car entry) desc (nth 1 entry)
  18263. type (nth 2 entry) match (nth 3 entry))
  18264. (if (> (length key) 1)
  18265. (add-to-list 'prefixes (string-to-char key))
  18266. (insert
  18267. (format
  18268. "\n%-4s%-14s: %s"
  18269. (org-add-props (copy-sequence key)
  18270. '(face bold))
  18271. (cond
  18272. ((string-match "\\S-" desc) desc)
  18273. ((eq type 'agenda) "Agenda for current week or day")
  18274. ((eq type 'alltodo) "List of all TODO entries")
  18275. ((eq type 'search) "Word search")
  18276. ((eq type 'stuck) "List of stuck projects")
  18277. ((eq type 'todo) "TODO keyword")
  18278. ((eq type 'tags) "Tags query")
  18279. ((eq type 'tags-todo) "Tags (TODO)")
  18280. ((eq type 'tags-tree) "Tags tree")
  18281. ((eq type 'todo-tree) "TODO kwd tree")
  18282. ((eq type 'occur-tree) "Occur tree")
  18283. ((functionp type) (if (symbolp type)
  18284. (symbol-name type)
  18285. "Lambda expression"))
  18286. (t "???"))
  18287. (cond
  18288. ((stringp match)
  18289. (org-add-props match nil 'face 'org-warning))
  18290. (match
  18291. (format "set of %d commands" (length match)))
  18292. (t ""))))))
  18293. (when prefixes
  18294. (mapc (lambda (x)
  18295. (insert
  18296. (format "\n%s %s"
  18297. (org-add-props (char-to-string x)
  18298. nil 'face 'bold)
  18299. (or (cdr (assoc (concat selstring (char-to-string x))
  18300. prefix-descriptions))
  18301. "Prefix key"))))
  18302. prefixes))
  18303. (goto-char (point-min))
  18304. (when (fboundp 'fit-window-to-buffer)
  18305. (if second-time
  18306. (if (not (pos-visible-in-window-p (point-max)))
  18307. (fit-window-to-buffer))
  18308. (setq second-time t)
  18309. (fit-window-to-buffer)))
  18310. (message "Press key for agenda command%s:"
  18311. (if (or restrict-ok org-agenda-overriding-restriction)
  18312. (if org-agenda-overriding-restriction
  18313. " (restriction lock active)"
  18314. (if restriction
  18315. (format " (restricted to %s)" restriction)
  18316. " (unrestricted)"))
  18317. ""))
  18318. (setq c (read-char-exclusive))
  18319. (message "")
  18320. (cond
  18321. ((assoc (char-to-string c) custom)
  18322. (setq selstring (concat selstring (char-to-string c)))
  18323. (throw 'exit (cons selstring restriction)))
  18324. ((memq c prefixes)
  18325. (setq selstring (concat selstring (char-to-string c))
  18326. prefixes nil
  18327. rmheader (or rmheader t)
  18328. custom (delq nil (mapcar
  18329. (lambda (x)
  18330. (if (or (= (length (car x)) 1)
  18331. (/= (string-to-char (car x)) c))
  18332. nil
  18333. (cons (substring (car x) 1) (cdr x))))
  18334. custom))))
  18335. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18336. (message "Restriction is only possible in Org-mode buffers")
  18337. (ding) (sit-for 1))
  18338. ((eq c ?1)
  18339. (org-agenda-remove-restriction-lock 'noupdate)
  18340. (setq restriction 'buffer))
  18341. ((eq c ?0)
  18342. (org-agenda-remove-restriction-lock 'noupdate)
  18343. (setq restriction (if region-p 'region 'subtree)))
  18344. ((eq c ?<)
  18345. (org-agenda-remove-restriction-lock 'noupdate)
  18346. (setq restriction
  18347. (cond
  18348. ((eq restriction 'buffer)
  18349. (if region-p 'region 'subtree))
  18350. ((memq restriction '(subtree region))
  18351. nil)
  18352. (t 'buffer))))
  18353. ((eq c ?>)
  18354. (org-agenda-remove-restriction-lock 'noupdate)
  18355. (setq restriction nil))
  18356. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18357. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18358. ((and (> (length selstring) 0) (eq c ?\d))
  18359. (delete-window)
  18360. (org-agenda-get-restriction-and-command prefix-descriptions))
  18361. ((equal c ?q) (error "Abort"))
  18362. (t (error "Invalid key %c" c))))))))
  18363. (defun org-run-agenda-series (name series)
  18364. (org-prepare-agenda name)
  18365. (let* ((org-agenda-multi t)
  18366. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18367. (cmds (car series))
  18368. (gprops (nth 1 series))
  18369. match ;; The byte compiler incorrectly complains about this. Keep it!
  18370. cmd type lprops)
  18371. (while (setq cmd (pop cmds))
  18372. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18373. (cond
  18374. ((eq type 'agenda)
  18375. (org-let2 gprops lprops
  18376. '(call-interactively 'org-agenda-list)))
  18377. ((eq type 'alltodo)
  18378. (org-let2 gprops lprops
  18379. '(call-interactively 'org-todo-list)))
  18380. ((eq type 'search)
  18381. (org-let2 gprops lprops
  18382. '(org-search-view current-prefix-arg match)))
  18383. ((eq type 'stuck)
  18384. (org-let2 gprops lprops
  18385. '(call-interactively 'org-agenda-list-stuck-projects)))
  18386. ((eq type 'tags)
  18387. (org-let2 gprops lprops
  18388. '(org-tags-view current-prefix-arg match)))
  18389. ((eq type 'tags-todo)
  18390. (org-let2 gprops lprops
  18391. '(org-tags-view '(4) match)))
  18392. ((eq type 'todo)
  18393. (org-let2 gprops lprops
  18394. '(org-todo-list match)))
  18395. ((fboundp type)
  18396. (org-let2 gprops lprops
  18397. '(funcall type match)))
  18398. (t (error "Invalid type in command series"))))
  18399. (widen)
  18400. (setq org-agenda-redo-command redo)
  18401. (goto-char (point-min)))
  18402. (org-finalize-agenda))
  18403. ;;;###autoload
  18404. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18405. "Run an agenda command in batch mode and send the result to STDOUT.
  18406. If CMD-KEY is a string of length 1, it is used as a key in
  18407. `org-agenda-custom-commands' and triggers this command. If it is a
  18408. longer string it is used as a tags/todo match string.
  18409. Paramters are alternating variable names and values that will be bound
  18410. before running the agenda command."
  18411. (let (pars)
  18412. (while parameters
  18413. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18414. (if (> (length cmd-key) 2)
  18415. (eval (list 'let (nreverse pars)
  18416. (list 'org-tags-view nil cmd-key)))
  18417. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18418. (set-buffer org-agenda-buffer-name)
  18419. (princ (org-encode-for-stdout (buffer-string)))))
  18420. (defun org-encode-for-stdout (string)
  18421. (if (fboundp 'encode-coding-string)
  18422. (encode-coding-string string buffer-file-coding-system)
  18423. string))
  18424. (defvar org-agenda-info nil)
  18425. ;;;###autoload
  18426. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18427. "Run an agenda command in batch mode and send the result to STDOUT.
  18428. If CMD-KEY is a string of length 1, it is used as a key in
  18429. `org-agenda-custom-commands' and triggers this command. If it is a
  18430. longer string it is used as a tags/todo match string.
  18431. Paramters are alternating variable names and values that will be bound
  18432. before running the agenda command.
  18433. The output gives a line for each selected agenda item. Each
  18434. item is a list of comma-separated values, like this:
  18435. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18436. category The category of the item
  18437. head The headline, without TODO kwd, TAGS and PRIORITY
  18438. type The type of the agenda entry, can be
  18439. todo selected in TODO match
  18440. tagsmatch selected in tags match
  18441. diary imported from diary
  18442. deadline a deadline on given date
  18443. scheduled scheduled on given date
  18444. timestamp entry has timestamp on given date
  18445. closed entry was closed on given date
  18446. upcoming-deadline warning about deadline
  18447. past-scheduled forwarded scheduled item
  18448. block entry has date block including g. date
  18449. todo The todo keyword, if any
  18450. tags All tags including inherited ones, separated by colons
  18451. date The relevant date, like 2007-2-14
  18452. time The time, like 15:00-16:50
  18453. extra Sting with extra planning info
  18454. priority-l The priority letter if any was given
  18455. priority-n The computed numerical priority
  18456. agenda-day The day in the agenda where this is listed"
  18457. (let (pars)
  18458. (while parameters
  18459. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18460. (push (list 'org-agenda-remove-tags t) pars)
  18461. (if (> (length cmd-key) 2)
  18462. (eval (list 'let (nreverse pars)
  18463. (list 'org-tags-view nil cmd-key)))
  18464. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18465. (set-buffer org-agenda-buffer-name)
  18466. (let* ((lines (org-split-string (buffer-string) "\n"))
  18467. line)
  18468. (while (setq line (pop lines))
  18469. (catch 'next
  18470. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18471. (setq org-agenda-info
  18472. (org-fix-agenda-info (text-properties-at 0 line)))
  18473. (princ
  18474. (org-encode-for-stdout
  18475. (mapconcat 'org-agenda-export-csv-mapper
  18476. '(org-category txt type todo tags date time-of-day extra
  18477. priority-letter priority agenda-day)
  18478. ",")))
  18479. (princ "\n"))))))
  18480. (defun org-fix-agenda-info (props)
  18481. "Make sure all properties on an agenda item have a canonical form,
  18482. so the export commands can easily use it."
  18483. (let (tmp re)
  18484. (when (setq tmp (plist-get props 'tags))
  18485. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18486. (when (setq tmp (plist-get props 'date))
  18487. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18488. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18489. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18490. (setq tmp (calendar-date-string tmp)))
  18491. (setq props (plist-put props 'date tmp)))
  18492. (when (setq tmp (plist-get props 'day))
  18493. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18494. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18495. (setq tmp (calendar-date-string tmp)))
  18496. (setq props (plist-put props 'day tmp))
  18497. (setq props (plist-put props 'agenda-day tmp)))
  18498. (when (setq tmp (plist-get props 'txt))
  18499. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18500. (plist-put props 'priority-letter (match-string 1 tmp))
  18501. (setq tmp (replace-match "" t t tmp)))
  18502. (when (and (setq re (plist-get props 'org-todo-regexp))
  18503. (setq re (concat "\\`\\.*" re " ?"))
  18504. (string-match re tmp))
  18505. (plist-put props 'todo (match-string 1 tmp))
  18506. (setq tmp (replace-match "" t t tmp)))
  18507. (plist-put props 'txt tmp)))
  18508. props)
  18509. (defun org-agenda-export-csv-mapper (prop)
  18510. (let ((res (plist-get org-agenda-info prop)))
  18511. (setq res
  18512. (cond
  18513. ((not res) "")
  18514. ((stringp res) res)
  18515. (t (prin1-to-string res))))
  18516. (while (string-match "," res)
  18517. (setq res (replace-match ";" t t res)))
  18518. (org-trim res)))
  18519. ;;;###autoload
  18520. (defun org-store-agenda-views (&rest parameters)
  18521. (interactive)
  18522. (eval (list 'org-batch-store-agenda-views)))
  18523. ;; FIXME, why is this a macro?????
  18524. ;;;###autoload
  18525. (defmacro org-batch-store-agenda-views (&rest parameters)
  18526. "Run all custom agenda commands that have a file argument."
  18527. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18528. (pop-up-frames nil)
  18529. (dir default-directory)
  18530. pars cmd thiscmdkey files opts)
  18531. (while parameters
  18532. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18533. (setq pars (reverse pars))
  18534. (save-window-excursion
  18535. (while cmds
  18536. (setq cmd (pop cmds)
  18537. thiscmdkey (car cmd)
  18538. opts (nth 4 cmd)
  18539. files (nth 5 cmd))
  18540. (if (stringp files) (setq files (list files)))
  18541. (when files
  18542. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18543. (list 'org-agenda nil thiscmdkey)))
  18544. (set-buffer org-agenda-buffer-name)
  18545. (while files
  18546. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18547. (list 'org-write-agenda
  18548. (expand-file-name (pop files) dir) t))))
  18549. (and (get-buffer org-agenda-buffer-name)
  18550. (kill-buffer org-agenda-buffer-name)))))))
  18551. (defun org-write-agenda (file &optional nosettings)
  18552. "Write the current buffer (an agenda view) as a file.
  18553. Depending on the extension of the file name, plain text (.txt),
  18554. HTML (.html or .htm) or Postscript (.ps) is produced.
  18555. If NOSETTINGS is given, do not scope the settings of
  18556. `org-agenda-exporter-settings' into the export commands. This is used when
  18557. the settings have already been scoped and we do not wish to overrule other,
  18558. higher priority settings."
  18559. (interactive "FWrite agenda to file: ")
  18560. (if (not (file-writable-p file))
  18561. (error "Cannot write agenda to file %s" file))
  18562. (cond
  18563. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18564. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18565. (org-let (if nosettings nil org-agenda-exporter-settings)
  18566. '(save-excursion
  18567. (save-window-excursion
  18568. (cond
  18569. ((string-match "\\.html?\\'" file)
  18570. (set-buffer (htmlize-buffer (current-buffer)))
  18571. (when (and org-agenda-export-html-style
  18572. (string-match "<style>" org-agenda-export-html-style))
  18573. ;; replace <style> section with org-agenda-export-html-style
  18574. (goto-char (point-min))
  18575. (kill-region (- (search-forward "<style") 6)
  18576. (search-forward "</style>"))
  18577. (insert org-agenda-export-html-style))
  18578. (write-file file)
  18579. (kill-buffer (current-buffer))
  18580. (message "HTML written to %s" file))
  18581. ((string-match "\\.ps\\'" file)
  18582. (ps-print-buffer-with-faces file)
  18583. (message "Postscript written to %s" file))
  18584. (t
  18585. (let ((bs (buffer-string)))
  18586. (find-file file)
  18587. (insert bs)
  18588. (save-buffer 0)
  18589. (kill-buffer (current-buffer))
  18590. (message "Plain text written to %s" file))))))
  18591. (set-buffer org-agenda-buffer-name)))
  18592. (defmacro org-no-read-only (&rest body)
  18593. "Inhibit read-only for BODY."
  18594. `(let ((inhibit-read-only t)) ,@body))
  18595. (defun org-check-for-org-mode ()
  18596. "Make sure current buffer is in org-mode. Error if not."
  18597. (or (org-mode-p)
  18598. (error "Cannot execute org-mode agenda command on buffer in %s."
  18599. major-mode)))
  18600. (defun org-fit-agenda-window ()
  18601. "Fit the window to the buffer size."
  18602. (and (memq org-agenda-window-setup '(reorganize-frame))
  18603. (fboundp 'fit-window-to-buffer)
  18604. (fit-window-to-buffer
  18605. nil
  18606. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18607. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18608. ;;; Agenda file list
  18609. (defun org-agenda-files (&optional unrestricted)
  18610. "Get the list of agenda files.
  18611. Optional UNRESTRICTED means return the full list even if a restriction
  18612. is currently in place."
  18613. (let ((files
  18614. (cond
  18615. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18616. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18617. ((listp org-agenda-files) org-agenda-files)
  18618. (t (error "Invalid value of `org-agenda-files'")))))
  18619. (setq files (apply 'append
  18620. (mapcar (lambda (f)
  18621. (if (file-directory-p f)
  18622. (directory-files f t
  18623. org-agenda-file-regexp)
  18624. (list f)))
  18625. files)))
  18626. (if org-agenda-skip-unavailable-files
  18627. (delq nil
  18628. (mapcar (function
  18629. (lambda (file)
  18630. (and (file-readable-p file) file)))
  18631. files))
  18632. files))) ; `org-check-agenda-file' will remove them from the list
  18633. (defun org-edit-agenda-file-list ()
  18634. "Edit the list of agenda files.
  18635. Depending on setup, this either uses customize to edit the variable
  18636. `org-agenda-files', or it visits the file that is holding the list. In the
  18637. latter case, the buffer is set up in a way that saving it automatically kills
  18638. the buffer and restores the previous window configuration."
  18639. (interactive)
  18640. (if (stringp org-agenda-files)
  18641. (let ((cw (current-window-configuration)))
  18642. (find-file org-agenda-files)
  18643. (org-set-local 'org-window-configuration cw)
  18644. (org-add-hook 'after-save-hook
  18645. (lambda ()
  18646. (set-window-configuration
  18647. (prog1 org-window-configuration
  18648. (kill-buffer (current-buffer))))
  18649. (org-install-agenda-files-menu)
  18650. (message "New agenda file list installed"))
  18651. nil 'local)
  18652. (message "%s" (substitute-command-keys
  18653. "Edit list and finish with \\[save-buffer]")))
  18654. (customize-variable 'org-agenda-files)))
  18655. (defun org-store-new-agenda-file-list (list)
  18656. "Set new value for the agenda file list and save it correcly."
  18657. (if (stringp org-agenda-files)
  18658. (let ((f org-agenda-files) b)
  18659. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18660. (with-temp-file f
  18661. (insert (mapconcat 'identity list "\n") "\n")))
  18662. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18663. (setq org-agenda-files list)
  18664. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18665. (defun org-read-agenda-file-list ()
  18666. "Read the list of agenda files from a file."
  18667. (when (stringp org-agenda-files)
  18668. (with-temp-buffer
  18669. (insert-file-contents org-agenda-files)
  18670. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18671. ;;;###autoload
  18672. (defun org-cycle-agenda-files ()
  18673. "Cycle through the files in `org-agenda-files'.
  18674. If the current buffer visits an agenda file, find the next one in the list.
  18675. If the current buffer does not, find the first agenda file."
  18676. (interactive)
  18677. (let* ((fs (org-agenda-files t))
  18678. (files (append fs (list (car fs))))
  18679. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18680. file)
  18681. (unless files (error "No agenda files"))
  18682. (catch 'exit
  18683. (while (setq file (pop files))
  18684. (if (equal (file-truename file) tcf)
  18685. (when (car files)
  18686. (find-file (car files))
  18687. (throw 'exit t))))
  18688. (find-file (car fs)))
  18689. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18690. (defun org-agenda-file-to-front (&optional to-end)
  18691. "Move/add the current file to the top of the agenda file list.
  18692. If the file is not present in the list, it is added to the front. If it is
  18693. present, it is moved there. With optional argument TO-END, add/move to the
  18694. end of the list."
  18695. (interactive "P")
  18696. (let ((org-agenda-skip-unavailable-files nil)
  18697. (file-alist (mapcar (lambda (x)
  18698. (cons (file-truename x) x))
  18699. (org-agenda-files t)))
  18700. (ctf (file-truename buffer-file-name))
  18701. x had)
  18702. (setq x (assoc ctf file-alist) had x)
  18703. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18704. (if to-end
  18705. (setq file-alist (append (delq x file-alist) (list x)))
  18706. (setq file-alist (cons x (delq x file-alist))))
  18707. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18708. (org-install-agenda-files-menu)
  18709. (message "File %s to %s of agenda file list"
  18710. (if had "moved" "added") (if to-end "end" "front"))))
  18711. (defun org-remove-file (&optional file)
  18712. "Remove current file from the list of files in variable `org-agenda-files'.
  18713. These are the files which are being checked for agenda entries.
  18714. Optional argument FILE means, use this file instead of the current."
  18715. (interactive)
  18716. (let* ((org-agenda-skip-unavailable-files nil)
  18717. (file (or file buffer-file-name))
  18718. (true-file (file-truename file))
  18719. (afile (abbreviate-file-name file))
  18720. (files (delq nil (mapcar
  18721. (lambda (x)
  18722. (if (equal true-file
  18723. (file-truename x))
  18724. nil x))
  18725. (org-agenda-files t)))))
  18726. (if (not (= (length files) (length (org-agenda-files t))))
  18727. (progn
  18728. (org-store-new-agenda-file-list files)
  18729. (org-install-agenda-files-menu)
  18730. (message "Removed file: %s" afile))
  18731. (message "File was not in list: %s (not removed)" afile))))
  18732. (defun org-file-menu-entry (file)
  18733. (vector file (list 'find-file file) t))
  18734. (defun org-check-agenda-file (file)
  18735. "Make sure FILE exists. If not, ask user what to do."
  18736. (when (not (file-exists-p file))
  18737. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18738. (abbreviate-file-name file))
  18739. (let ((r (downcase (read-char-exclusive))))
  18740. (cond
  18741. ((equal r ?r)
  18742. (org-remove-file file)
  18743. (throw 'nextfile t))
  18744. (t (error "Abort"))))))
  18745. ;;; Agenda prepare and finalize
  18746. (defvar org-agenda-multi nil) ; dynammically scoped
  18747. (defvar org-agenda-buffer-name "*Org Agenda*")
  18748. (defvar org-pre-agenda-window-conf nil)
  18749. (defvar org-agenda-name nil)
  18750. (defun org-prepare-agenda (&optional name)
  18751. (setq org-todo-keywords-for-agenda nil)
  18752. (setq org-done-keywords-for-agenda nil)
  18753. (if org-agenda-multi
  18754. (progn
  18755. (setq buffer-read-only nil)
  18756. (goto-char (point-max))
  18757. (unless (or (bobp) org-agenda-compact-blocks)
  18758. (insert "\n" (make-string (window-width) ?=) "\n"))
  18759. (narrow-to-region (point) (point-max)))
  18760. (org-agenda-reset-markers)
  18761. (org-prepare-agenda-buffers (org-agenda-files))
  18762. (setq org-todo-keywords-for-agenda
  18763. (org-uniquify org-todo-keywords-for-agenda))
  18764. (setq org-done-keywords-for-agenda
  18765. (org-uniquify org-done-keywords-for-agenda))
  18766. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18767. (awin (get-buffer-window abuf)))
  18768. (cond
  18769. ((equal (current-buffer) abuf) nil)
  18770. (awin (select-window awin))
  18771. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18772. ((equal org-agenda-window-setup 'current-window)
  18773. (switch-to-buffer abuf))
  18774. ((equal org-agenda-window-setup 'other-window)
  18775. (org-switch-to-buffer-other-window abuf))
  18776. ((equal org-agenda-window-setup 'other-frame)
  18777. (switch-to-buffer-other-frame abuf))
  18778. ((equal org-agenda-window-setup 'reorganize-frame)
  18779. (delete-other-windows)
  18780. (org-switch-to-buffer-other-window abuf))))
  18781. (setq buffer-read-only nil)
  18782. (erase-buffer)
  18783. (org-agenda-mode)
  18784. (and name (not org-agenda-name)
  18785. (org-set-local 'org-agenda-name name)))
  18786. (setq buffer-read-only nil))
  18787. (defun org-finalize-agenda ()
  18788. "Finishing touch for the agenda buffer, called just before displaying it."
  18789. (unless org-agenda-multi
  18790. (save-excursion
  18791. (let ((inhibit-read-only t))
  18792. (goto-char (point-min))
  18793. (while (org-activate-bracket-links (point-max))
  18794. (add-text-properties (match-beginning 0) (match-end 0)
  18795. '(face org-link)))
  18796. (org-agenda-align-tags)
  18797. (unless org-agenda-with-colors
  18798. (remove-text-properties (point-min) (point-max) '(face nil))))
  18799. (if (and (boundp 'org-overriding-columns-format)
  18800. org-overriding-columns-format)
  18801. (org-set-local 'org-overriding-columns-format
  18802. org-overriding-columns-format))
  18803. (if (and (boundp 'org-agenda-view-columns-initially)
  18804. org-agenda-view-columns-initially)
  18805. (org-agenda-columns))
  18806. (when org-agenda-fontify-priorities
  18807. (org-fontify-priorities))
  18808. (run-hooks 'org-finalize-agenda-hook)
  18809. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18810. )))
  18811. (defun org-fontify-priorities ()
  18812. "Make highest priority lines bold, and lowest italic."
  18813. (interactive)
  18814. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18815. (org-delete-overlay o)))
  18816. (org-overlays-in (point-min) (point-max)))
  18817. (save-excursion
  18818. (let ((inhibit-read-only t)
  18819. b e p ov h l)
  18820. (goto-char (point-min))
  18821. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18822. (setq h (or (get-char-property (point) 'org-highest-priority)
  18823. org-highest-priority)
  18824. l (or (get-char-property (point) 'org-lowest-priority)
  18825. org-lowest-priority)
  18826. p (string-to-char (match-string 1))
  18827. b (match-beginning 0) e (point-at-eol)
  18828. ov (org-make-overlay b e))
  18829. (org-overlay-put
  18830. ov 'face
  18831. (cond ((listp org-agenda-fontify-priorities)
  18832. (cdr (assoc p org-agenda-fontify-priorities)))
  18833. ((equal p l) 'italic)
  18834. ((equal p h) 'bold)))
  18835. (org-overlay-put ov 'org-type 'org-priority)))))
  18836. (defun org-prepare-agenda-buffers (files)
  18837. "Create buffers for all agenda files, protect archived trees and comments."
  18838. (interactive)
  18839. (let ((pa '(:org-archived t))
  18840. (pc '(:org-comment t))
  18841. (pall '(:org-archived t :org-comment t))
  18842. (inhibit-read-only t)
  18843. (rea (concat ":" org-archive-tag ":"))
  18844. bmp file re)
  18845. (save-excursion
  18846. (save-restriction
  18847. (while (setq file (pop files))
  18848. (if (bufferp file)
  18849. (set-buffer file)
  18850. (org-check-agenda-file file)
  18851. (set-buffer (org-get-agenda-file-buffer file)))
  18852. (widen)
  18853. (setq bmp (buffer-modified-p))
  18854. (org-refresh-category-properties)
  18855. (setq org-todo-keywords-for-agenda
  18856. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18857. (setq org-done-keywords-for-agenda
  18858. (append org-done-keywords-for-agenda org-done-keywords))
  18859. (save-excursion
  18860. (remove-text-properties (point-min) (point-max) pall)
  18861. (when org-agenda-skip-archived-trees
  18862. (goto-char (point-min))
  18863. (while (re-search-forward rea nil t)
  18864. (if (org-on-heading-p t)
  18865. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18866. (goto-char (point-min))
  18867. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18868. (while (re-search-forward re nil t)
  18869. (add-text-properties
  18870. (match-beginning 0) (org-end-of-subtree t) pc)))
  18871. (set-buffer-modified-p bmp))))))
  18872. (defvar org-agenda-skip-function nil
  18873. "Function to be called at each match during agenda construction.
  18874. If this function returns nil, the current match should not be skipped.
  18875. Otherwise, the function must return a position from where the search
  18876. should be continued.
  18877. This may also be a Lisp form, it will be evaluated.
  18878. Never set this variable using `setq' or so, because then it will apply
  18879. to all future agenda commands. Instead, bind it with `let' to scope
  18880. it dynamically into the agenda-constructing command. A good way to set
  18881. it is through options in org-agenda-custom-commands.")
  18882. (defun org-agenda-skip ()
  18883. "Throw to `:skip' in places that should be skipped.
  18884. Also moves point to the end of the skipped region, so that search can
  18885. continue from there."
  18886. (let ((p (point-at-bol)) to fp)
  18887. (and org-agenda-skip-archived-trees
  18888. (get-text-property p :org-archived)
  18889. (org-end-of-subtree t)
  18890. (throw :skip t))
  18891. (and (get-text-property p :org-comment)
  18892. (org-end-of-subtree t)
  18893. (throw :skip t))
  18894. (if (equal (char-after p) ?#) (throw :skip t))
  18895. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18896. (consp org-agenda-skip-function))
  18897. (setq to (save-excursion
  18898. (save-match-data
  18899. (if fp
  18900. (funcall org-agenda-skip-function)
  18901. (eval org-agenda-skip-function))))))
  18902. (goto-char to)
  18903. (throw :skip t))))
  18904. (defvar org-agenda-markers nil
  18905. "List of all currently active markers created by `org-agenda'.")
  18906. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18907. "Creation time of the last agenda marker.")
  18908. (defun org-agenda-new-marker (&optional pos)
  18909. "Return a new agenda marker.
  18910. Org-mode keeps a list of these markers and resets them when they are
  18911. no longer in use."
  18912. (let ((m (copy-marker (or pos (point)))))
  18913. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18914. (push m org-agenda-markers)
  18915. m))
  18916. (defun org-agenda-reset-markers ()
  18917. "Reset markers created by `org-agenda'."
  18918. (while org-agenda-markers
  18919. (move-marker (pop org-agenda-markers) nil)))
  18920. (defun org-get-agenda-file-buffer (file)
  18921. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18922. it to the list of buffers which might be released later."
  18923. (let ((buf (org-find-base-buffer-visiting file)))
  18924. (if buf
  18925. buf ; just return it
  18926. ;; Make a new buffer and remember it
  18927. (setq buf (find-file-noselect file))
  18928. (if buf (push buf org-agenda-new-buffers))
  18929. buf)))
  18930. (defun org-release-buffers (blist)
  18931. "Release all buffers in list, asking the user for confirmation when needed.
  18932. When a buffer is unmodified, it is just killed. When modified, it is saved
  18933. \(if the user agrees) and then killed."
  18934. (let (buf file)
  18935. (while (setq buf (pop blist))
  18936. (setq file (buffer-file-name buf))
  18937. (when (and (buffer-modified-p buf)
  18938. file
  18939. (y-or-n-p (format "Save file %s? " file)))
  18940. (with-current-buffer buf (save-buffer)))
  18941. (kill-buffer buf))))
  18942. (defun org-get-category (&optional pos)
  18943. "Get the category applying to position POS."
  18944. (get-text-property (or pos (point)) 'org-category))
  18945. ;;; Agenda timeline
  18946. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18947. (defun org-timeline (&optional include-all)
  18948. "Show a time-sorted view of the entries in the current org file.
  18949. Only entries with a time stamp of today or later will be listed. With
  18950. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18951. under the current date.
  18952. If the buffer contains an active region, only check the region for
  18953. dates."
  18954. (interactive "P")
  18955. (require 'calendar)
  18956. (org-compile-prefix-format 'timeline)
  18957. (org-set-sorting-strategy 'timeline)
  18958. (let* ((dopast t)
  18959. (dotodo include-all)
  18960. (doclosed org-agenda-show-log)
  18961. (entry buffer-file-name)
  18962. (date (calendar-current-date))
  18963. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18964. (end (if (org-region-active-p) (region-end) (point-max)))
  18965. (day-numbers (org-get-all-dates beg end 'no-ranges
  18966. t doclosed ; always include today
  18967. org-timeline-show-empty-dates))
  18968. (org-deadline-warning-days 0)
  18969. (org-agenda-only-exact-dates t)
  18970. (today (time-to-days (current-time)))
  18971. (past t)
  18972. args
  18973. s e rtn d emptyp)
  18974. (setq org-agenda-redo-command
  18975. (list 'progn
  18976. (list 'org-switch-to-buffer-other-window (current-buffer))
  18977. (list 'org-timeline (list 'quote include-all))))
  18978. (if (not dopast)
  18979. ;; Remove past dates from the list of dates.
  18980. (setq day-numbers (delq nil (mapcar (lambda(x)
  18981. (if (>= x today) x nil))
  18982. day-numbers))))
  18983. (org-prepare-agenda (concat "Timeline "
  18984. (file-name-nondirectory buffer-file-name)))
  18985. (if doclosed (push :closed args))
  18986. (push :timestamp args)
  18987. (push :deadline args)
  18988. (push :scheduled args)
  18989. (push :sexp args)
  18990. (if dotodo (push :todo args))
  18991. (while (setq d (pop day-numbers))
  18992. (if (and (listp d) (eq (car d) :omitted))
  18993. (progn
  18994. (setq s (point))
  18995. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18996. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18997. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18998. (if (and (>= d today)
  18999. dopast
  19000. past)
  19001. (progn
  19002. (setq past nil)
  19003. (insert (make-string 79 ?-) "\n")))
  19004. (setq date (calendar-gregorian-from-absolute d))
  19005. (setq s (point))
  19006. (setq rtn (and (not emptyp)
  19007. (apply 'org-agenda-get-day-entries entry
  19008. date args)))
  19009. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19010. (progn
  19011. (insert
  19012. (if (stringp org-agenda-format-date)
  19013. (format-time-string org-agenda-format-date
  19014. (org-time-from-absolute date))
  19015. (funcall org-agenda-format-date date))
  19016. "\n")
  19017. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19018. (put-text-property s (1- (point)) 'org-date-line t)
  19019. (if (equal d today)
  19020. (put-text-property s (1- (point)) 'org-today t))
  19021. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19022. (put-text-property s (1- (point)) 'day d)))))
  19023. (goto-char (point-min))
  19024. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19025. (point-min)))
  19026. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19027. (org-finalize-agenda)
  19028. (setq buffer-read-only t)))
  19029. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19030. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19031. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19032. not every single day in the range. If FORCE-TODAY is non-nil, make
  19033. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19034. inactive time stamps (those in square brackets) are included.
  19035. When EMPTY is non-nil, also include days without any entries."
  19036. (let ((re (concat
  19037. (if pre-re pre-re "")
  19038. (if inactive org-ts-regexp-both org-ts-regexp)))
  19039. dates dates1 date day day1 day2 ts1 ts2)
  19040. (if force-today
  19041. (setq dates (list (time-to-days (current-time)))))
  19042. (save-excursion
  19043. (goto-char beg)
  19044. (while (re-search-forward re end t)
  19045. (setq day (time-to-days (org-time-string-to-time
  19046. (substring (match-string 1) 0 10))))
  19047. (or (memq day dates) (push day dates)))
  19048. (unless no-ranges
  19049. (goto-char beg)
  19050. (while (re-search-forward org-tr-regexp end t)
  19051. (setq ts1 (substring (match-string 1) 0 10)
  19052. ts2 (substring (match-string 2) 0 10)
  19053. day1 (time-to-days (org-time-string-to-time ts1))
  19054. day2 (time-to-days (org-time-string-to-time ts2)))
  19055. (while (< (setq day1 (1+ day1)) day2)
  19056. (or (memq day1 dates) (push day1 dates)))))
  19057. (setq dates (sort dates '<))
  19058. (when empty
  19059. (while (setq day (pop dates))
  19060. (setq day2 (car dates))
  19061. (push day dates1)
  19062. (when (and day2 empty)
  19063. (if (or (eq empty t)
  19064. (and (numberp empty) (<= (- day2 day) empty)))
  19065. (while (< (setq day (1+ day)) day2)
  19066. (push (list day) dates1))
  19067. (push (cons :omitted (- day2 day)) dates1))))
  19068. (setq dates (nreverse dates1)))
  19069. dates)))
  19070. ;;; Agenda Daily/Weekly
  19071. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19072. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19073. (defvar org-agenda-last-arguments nil
  19074. "The arguments of the previous call to org-agenda")
  19075. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19076. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19077. (defvar org-include-all-loc nil) ; local variable
  19078. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19079. ;;;###autoload
  19080. (defun org-agenda-list (&optional include-all start-day ndays)
  19081. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19082. The view will be for the current day or week, but from the overview buffer
  19083. you will be able to go to other days/weeks.
  19084. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19085. all unfinished TODO items will also be shown, before the agenda.
  19086. This feature is considered obsolete, please use the TODO list or a block
  19087. agenda instead.
  19088. With a numeric prefix argument in an interactive call, the agenda will
  19089. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19090. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19091. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19092. given in `org-agenda-start-on-weekday'."
  19093. (interactive "P")
  19094. (if (and (integerp include-all) (> include-all 0))
  19095. (setq ndays include-all include-all nil))
  19096. (setq ndays (or ndays org-agenda-ndays)
  19097. start-day (or start-day org-agenda-start-day))
  19098. (if org-agenda-overriding-arguments
  19099. (setq include-all (car org-agenda-overriding-arguments)
  19100. start-day (nth 1 org-agenda-overriding-arguments)
  19101. ndays (nth 2 org-agenda-overriding-arguments)))
  19102. (if (stringp start-day)
  19103. ;; Convert to an absolute day number
  19104. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19105. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19106. (org-compile-prefix-format 'agenda)
  19107. (org-set-sorting-strategy 'agenda)
  19108. (require 'calendar)
  19109. (let* ((org-agenda-start-on-weekday
  19110. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19111. org-agenda-start-on-weekday nil))
  19112. (thefiles (org-agenda-files))
  19113. (files thefiles)
  19114. (today (time-to-days
  19115. (time-subtract (current-time)
  19116. (list 0 (* 3600 org-extend-today-until) 0))))
  19117. (sd (or start-day today))
  19118. (start (if (or (null org-agenda-start-on-weekday)
  19119. (< org-agenda-ndays 7))
  19120. sd
  19121. (let* ((nt (calendar-day-of-week
  19122. (calendar-gregorian-from-absolute sd)))
  19123. (n1 org-agenda-start-on-weekday)
  19124. (d (- nt n1)))
  19125. (- sd (+ (if (< d 0) 7 0) d)))))
  19126. (day-numbers (list start))
  19127. (day-cnt 0)
  19128. (inhibit-redisplay (not debug-on-error))
  19129. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19130. (setq org-agenda-redo-command
  19131. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19132. ;; Make the list of days
  19133. (setq ndays (or ndays org-agenda-ndays)
  19134. nd ndays)
  19135. (while (> ndays 1)
  19136. (push (1+ (car day-numbers)) day-numbers)
  19137. (setq ndays (1- ndays)))
  19138. (setq day-numbers (nreverse day-numbers))
  19139. (org-prepare-agenda "Day/Week")
  19140. (org-set-local 'org-starting-day (car day-numbers))
  19141. (org-set-local 'org-include-all-loc include-all)
  19142. (org-set-local 'org-agenda-span
  19143. (org-agenda-ndays-to-span nd))
  19144. (when (and (or include-all org-agenda-include-all-todo)
  19145. (member today day-numbers))
  19146. (setq files thefiles
  19147. rtnall nil)
  19148. (while (setq file (pop files))
  19149. (catch 'nextfile
  19150. (org-check-agenda-file file)
  19151. (setq date (calendar-gregorian-from-absolute today)
  19152. rtn (org-agenda-get-day-entries
  19153. file date :todo))
  19154. (setq rtnall (append rtnall rtn))))
  19155. (when rtnall
  19156. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19157. (add-text-properties (point-min) (1- (point))
  19158. (list 'face 'org-agenda-structure))
  19159. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19160. (unless org-agenda-compact-blocks
  19161. (setq s (point))
  19162. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19163. "-agenda:\n")
  19164. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19165. 'org-date-line t)))
  19166. (while (setq d (pop day-numbers))
  19167. (setq date (calendar-gregorian-from-absolute d)
  19168. s (point))
  19169. (if (or (setq todayp (= d today))
  19170. (and (not start-pos) (= d sd)))
  19171. (setq start-pos (point))
  19172. (if (and start-pos (not end-pos))
  19173. (setq end-pos (point))))
  19174. (setq files thefiles
  19175. rtnall nil)
  19176. (while (setq file (pop files))
  19177. (catch 'nextfile
  19178. (org-check-agenda-file file)
  19179. (if org-agenda-show-log
  19180. (setq rtn (org-agenda-get-day-entries
  19181. file date
  19182. :deadline :scheduled :timestamp :sexp :closed))
  19183. (setq rtn (org-agenda-get-day-entries
  19184. file date
  19185. :deadline :scheduled :sexp :timestamp)))
  19186. (setq rtnall (append rtnall rtn))))
  19187. (if org-agenda-include-diary
  19188. (progn
  19189. (require 'diary-lib)
  19190. (setq rtn (org-get-entries-from-diary date))
  19191. (setq rtnall (append rtnall rtn))))
  19192. (if (or rtnall org-agenda-show-all-dates)
  19193. (progn
  19194. (setq day-cnt (1+ day-cnt))
  19195. (insert
  19196. (if (stringp org-agenda-format-date)
  19197. (format-time-string org-agenda-format-date
  19198. (org-time-from-absolute date))
  19199. (funcall org-agenda-format-date date))
  19200. "\n")
  19201. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19202. (put-text-property s (1- (point)) 'org-date-line t)
  19203. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19204. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19205. (if rtnall (insert
  19206. (org-finalize-agenda-entries
  19207. (org-agenda-add-time-grid-maybe
  19208. rtnall nd todayp))
  19209. "\n"))
  19210. (put-text-property s (1- (point)) 'day d)
  19211. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19212. (goto-char (point-min))
  19213. (org-fit-agenda-window)
  19214. (unless (and (pos-visible-in-window-p (point-min))
  19215. (pos-visible-in-window-p (point-max)))
  19216. (goto-char (1- (point-max)))
  19217. (recenter -1)
  19218. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19219. (progn
  19220. (goto-char (or start-pos 1))
  19221. (recenter 1))))
  19222. (goto-char (or start-pos 1))
  19223. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19224. (org-finalize-agenda)
  19225. (setq buffer-read-only t)
  19226. (message "")))
  19227. (defun org-agenda-ndays-to-span (n)
  19228. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19229. ;;; Agenda word search
  19230. (defvar org-agenda-search-history nil)
  19231. ;;;###autoload
  19232. (defun org-search-view (&optional arg string)
  19233. "Show all entries that contain the words given in the search string.
  19234. If the search string starts with an asterisks, only search in headlines."
  19235. (interactive "P")
  19236. (org-compile-prefix-format 'search)
  19237. (org-set-sorting-strategy 'search)
  19238. (org-prepare-agenda "SEARCH")
  19239. (let* ((props (list 'face nil
  19240. 'done-face 'org-done
  19241. 'org-not-done-regexp org-not-done-regexp
  19242. 'org-todo-regexp org-todo-regexp
  19243. 'mouse-face 'highlight
  19244. 'keymap org-agenda-keymap
  19245. 'help-echo (format "mouse-2 or RET jump to location")))
  19246. ;; FIXME: get rid of the \n at some point but watch out
  19247. (regexp (concat "^" org-outline-regexp))
  19248. rtn rtnall files file pos
  19249. marker priority category tags
  19250. ee txt beg end words word-regexps hdl-only buffer beg1 str)
  19251. (unless (and (stringp string)
  19252. (string-match "\\S-" string))
  19253. (setq string (read-string "Word Search: " nil
  19254. 'org-agenda-search-history)))
  19255. (setq org-agenda-redo-command
  19256. (list 'org-search-view 'current-prefix-arg string))
  19257. (if (equal (string-to-char string) ?*)
  19258. (setq hdl-only t
  19259. words (substring string 1))
  19260. (setq words string))
  19261. (setq words (org-split-string words)
  19262. word-regexps
  19263. (mapcar (lambda (w) (concat "\\<" (regexp-quote (downcase w)) "\\>"))
  19264. words))
  19265. (setq files (org-agenda-files)
  19266. rtnall nil)
  19267. (while (setq file (pop files))
  19268. (setq ee nil)
  19269. (catch 'nextfile
  19270. (org-check-agenda-file file)
  19271. (setq buffer (if (file-exists-p file)
  19272. (org-get-agenda-file-buffer file)
  19273. (error "No such file %s" file)))
  19274. (if (not buffer)
  19275. ;; If file does not exist, make sure an error message is sent
  19276. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19277. file))))
  19278. (with-current-buffer buffer
  19279. (unless (org-mode-p)
  19280. (error "Agenda file %s is not in `org-mode'" file))
  19281. (let ((case-fold-search t))
  19282. (save-excursion
  19283. (save-restriction
  19284. (if org-agenda-restrict
  19285. (narrow-to-region org-agenda-restrict-begin
  19286. org-agenda-restrict-end)
  19287. (widen))
  19288. (goto-char (point-min))
  19289. (while (re-search-forward regexp nil t)
  19290. (catch :skip
  19291. (setq beg (point-at-bol)
  19292. beg1 (match-end 0)
  19293. end (progn (outline-next-heading) (point)))
  19294. (goto-char beg)
  19295. (org-agenda-skip)
  19296. (setq str (buffer-substring-no-properties
  19297. (point-at-bol)
  19298. (if hdl-only (point-at-eol) end)))
  19299. (mapc
  19300. (lambda (wr)
  19301. (unless (string-match wr str)
  19302. (goto-char (1- end))
  19303. (throw :skip t)))
  19304. word-regexps)
  19305. (goto-char beg)
  19306. (setq marker (org-agenda-new-marker (point))
  19307. category (org-get-category)
  19308. tags (org-get-tags-at (point))
  19309. txt (org-format-agenda-item
  19310. ""
  19311. (buffer-substring-no-properties
  19312. beg1 (point-at-eol))
  19313. category tags))
  19314. (org-add-props txt props
  19315. 'org-marker marker 'org-hd-marker marker
  19316. 'priority 1000 'org-category category
  19317. 'type "search")
  19318. (push txt ee)
  19319. (goto-char (1- end)))))))))
  19320. (setq rtn ee)
  19321. (setq rtnall (append rtnall rtn)))
  19322. (if org-agenda-overriding-header
  19323. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19324. nil 'face 'org-agenda-structure) "\n")
  19325. (insert "Search words: ")
  19326. (add-text-properties (point-min) (1- (point))
  19327. (list 'face 'org-agenda-structure))
  19328. (setq pos (point))
  19329. (insert string "\n")
  19330. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19331. (setq pos (point)))
  19332. (when rtnall
  19333. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19334. (goto-char (point-min))
  19335. (org-fit-agenda-window)
  19336. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19337. (org-finalize-agenda)
  19338. (setq buffer-read-only t)))
  19339. ;;; Agenda TODO list
  19340. (defvar org-select-this-todo-keyword nil)
  19341. (defvar org-last-arg nil)
  19342. ;;;###autoload
  19343. (defun org-todo-list (arg)
  19344. "Show all TODO entries from all agenda file in a single list.
  19345. The prefix arg can be used to select a specific TODO keyword and limit
  19346. the list to these. When using \\[universal-argument], you will be prompted
  19347. for a keyword. A numeric prefix directly selects the Nth keyword in
  19348. `org-todo-keywords-1'."
  19349. (interactive "P")
  19350. (require 'calendar)
  19351. (org-compile-prefix-format 'todo)
  19352. (org-set-sorting-strategy 'todo)
  19353. (org-prepare-agenda "TODO")
  19354. (let* ((today (time-to-days (current-time)))
  19355. (date (calendar-gregorian-from-absolute today))
  19356. (kwds org-todo-keywords-for-agenda)
  19357. (completion-ignore-case t)
  19358. (org-select-this-todo-keyword
  19359. (if (stringp arg) arg
  19360. (and arg (integerp arg) (> arg 0)
  19361. (nth (1- arg) kwds))))
  19362. rtn rtnall files file pos)
  19363. (when (equal arg '(4))
  19364. (setq org-select-this-todo-keyword
  19365. (completing-read "Keyword (or KWD1|K2D2|...): "
  19366. (mapcar 'list kwds) nil nil)))
  19367. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19368. (org-set-local 'org-last-arg arg)
  19369. (setq org-agenda-redo-command
  19370. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19371. (setq files (org-agenda-files)
  19372. rtnall nil)
  19373. (while (setq file (pop files))
  19374. (catch 'nextfile
  19375. (org-check-agenda-file file)
  19376. (setq rtn (org-agenda-get-day-entries file date :todo))
  19377. (setq rtnall (append rtnall rtn))))
  19378. (if org-agenda-overriding-header
  19379. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19380. nil 'face 'org-agenda-structure) "\n")
  19381. (insert "Global list of TODO items of type: ")
  19382. (add-text-properties (point-min) (1- (point))
  19383. (list 'face 'org-agenda-structure))
  19384. (setq pos (point))
  19385. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19386. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19387. (setq pos (point))
  19388. (unless org-agenda-multi
  19389. (insert "Available with `N r': (0)ALL")
  19390. (let ((n 0) s)
  19391. (mapc (lambda (x)
  19392. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19393. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19394. (insert "\n "))
  19395. (insert " " s))
  19396. kwds))
  19397. (insert "\n"))
  19398. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19399. (when rtnall
  19400. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19401. (goto-char (point-min))
  19402. (org-fit-agenda-window)
  19403. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19404. (org-finalize-agenda)
  19405. (setq buffer-read-only t)))
  19406. ;;; Agenda tags match
  19407. ;;;###autoload
  19408. (defun org-tags-view (&optional todo-only match)
  19409. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19410. The prefix arg TODO-ONLY limits the search to TODO entries."
  19411. (interactive "P")
  19412. (org-compile-prefix-format 'tags)
  19413. (org-set-sorting-strategy 'tags)
  19414. (let* ((org-tags-match-list-sublevels
  19415. (if todo-only t org-tags-match-list-sublevels))
  19416. (completion-ignore-case t)
  19417. rtn rtnall files file pos matcher
  19418. buffer)
  19419. (setq matcher (org-make-tags-matcher match)
  19420. match (car matcher) matcher (cdr matcher))
  19421. (org-prepare-agenda (concat "TAGS " match))
  19422. (setq org-agenda-redo-command
  19423. (list 'org-tags-view (list 'quote todo-only)
  19424. (list 'if 'current-prefix-arg nil match)))
  19425. (setq files (org-agenda-files)
  19426. rtnall nil)
  19427. (while (setq file (pop files))
  19428. (catch 'nextfile
  19429. (org-check-agenda-file file)
  19430. (setq buffer (if (file-exists-p file)
  19431. (org-get-agenda-file-buffer file)
  19432. (error "No such file %s" file)))
  19433. (if (not buffer)
  19434. ;; If file does not exist, merror message to agenda
  19435. (setq rtn (list
  19436. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19437. rtnall (append rtnall rtn))
  19438. (with-current-buffer buffer
  19439. (unless (org-mode-p)
  19440. (error "Agenda file %s is not in `org-mode'" file))
  19441. (save-excursion
  19442. (save-restriction
  19443. (if org-agenda-restrict
  19444. (narrow-to-region org-agenda-restrict-begin
  19445. org-agenda-restrict-end)
  19446. (widen))
  19447. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19448. (setq rtnall (append rtnall rtn))))))))
  19449. (if org-agenda-overriding-header
  19450. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19451. nil 'face 'org-agenda-structure) "\n")
  19452. (insert "Headlines with TAGS match: ")
  19453. (add-text-properties (point-min) (1- (point))
  19454. (list 'face 'org-agenda-structure))
  19455. (setq pos (point))
  19456. (insert match "\n")
  19457. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19458. (setq pos (point))
  19459. (unless org-agenda-multi
  19460. (insert "Press `C-u r' to search again with new search string\n"))
  19461. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19462. (when rtnall
  19463. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19464. (goto-char (point-min))
  19465. (org-fit-agenda-window)
  19466. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19467. (org-finalize-agenda)
  19468. (setq buffer-read-only t)))
  19469. ;;; Agenda Finding stuck projects
  19470. (defvar org-agenda-skip-regexp nil
  19471. "Regular expression used in skipping subtrees for the agenda.
  19472. This is basically a temporary global variable that can be set and then
  19473. used by user-defined selections using `org-agenda-skip-function'.")
  19474. (defvar org-agenda-overriding-header nil
  19475. "When this is set during todo and tags searches, will replace header.")
  19476. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19477. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19478. If yes, it returns the end position of this tree, causing agenda commands
  19479. to skip this subtree. This is a function that can be put into
  19480. `org-agenda-skip-function' for the duration of a command."
  19481. (let ((end (save-excursion (org-end-of-subtree t)))
  19482. skip)
  19483. (save-excursion
  19484. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19485. (and skip end)))
  19486. (defun org-agenda-skip-entry-if (&rest conditions)
  19487. "Skip entry if any of CONDITIONS is true.
  19488. See `org-agenda-skip-if' for details."
  19489. (org-agenda-skip-if nil conditions))
  19490. (defun org-agenda-skip-subtree-if (&rest conditions)
  19491. "Skip entry if any of CONDITIONS is true.
  19492. See `org-agenda-skip-if' for details."
  19493. (org-agenda-skip-if t conditions))
  19494. (defun org-agenda-skip-if (subtree conditions)
  19495. "Checks current entity for CONDITIONS.
  19496. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19497. the entry, i.e. the text before the next heading is checked.
  19498. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19499. from different tests. Valid conditions are:
  19500. scheduled Check if there is a scheduled cookie
  19501. notscheduled Check if there is no scheduled cookie
  19502. deadline Check if there is a deadline
  19503. notdeadline Check if there is no deadline
  19504. regexp Check if regexp matches
  19505. notregexp Check if regexp does not match.
  19506. The regexp is taken from the conditions list, it must come right after
  19507. the `regexp' or `notregexp' element.
  19508. If any of these conditions is met, this function returns the end point of
  19509. the entity, causing the search to continue from there. This is a function
  19510. that can be put into `org-agenda-skip-function' for the duration of a command."
  19511. (let (beg end m)
  19512. (org-back-to-heading t)
  19513. (setq beg (point)
  19514. end (if subtree
  19515. (progn (org-end-of-subtree t) (point))
  19516. (progn (outline-next-heading) (1- (point)))))
  19517. (goto-char beg)
  19518. (and
  19519. (or
  19520. (and (memq 'scheduled conditions)
  19521. (re-search-forward org-scheduled-time-regexp end t))
  19522. (and (memq 'notscheduled conditions)
  19523. (not (re-search-forward org-scheduled-time-regexp end t)))
  19524. (and (memq 'deadline conditions)
  19525. (re-search-forward org-deadline-time-regexp end t))
  19526. (and (memq 'notdeadline conditions)
  19527. (not (re-search-forward org-deadline-time-regexp end t)))
  19528. (and (setq m (memq 'regexp conditions))
  19529. (stringp (nth 1 m))
  19530. (re-search-forward (nth 1 m) end t))
  19531. (and (setq m (memq 'notregexp conditions))
  19532. (stringp (nth 1 m))
  19533. (not (re-search-forward (nth 1 m) end t))))
  19534. end)))
  19535. ;;;###autoload
  19536. (defun org-agenda-list-stuck-projects (&rest ignore)
  19537. "Create agenda view for projects that are stuck.
  19538. Stuck projects are project that have no next actions. For the definitions
  19539. of what a project is and how to check if it stuck, customize the variable
  19540. `org-stuck-projects'.
  19541. MATCH is being ignored."
  19542. (interactive)
  19543. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19544. ;; FIXME: we could have used org-agenda-skip-if here.
  19545. (org-agenda-overriding-header "List of stuck projects: ")
  19546. (matcher (nth 0 org-stuck-projects))
  19547. (todo (nth 1 org-stuck-projects))
  19548. (todo-wds (if (member "*" todo)
  19549. (progn
  19550. (org-prepare-agenda-buffers (org-agenda-files))
  19551. (org-delete-all
  19552. org-done-keywords-for-agenda
  19553. (copy-sequence org-todo-keywords-for-agenda)))
  19554. todo))
  19555. (todo-re (concat "^\\*+[ \t]+\\("
  19556. (mapconcat 'identity todo-wds "\\|")
  19557. "\\)\\>"))
  19558. (tags (nth 2 org-stuck-projects))
  19559. (tags-re (if (member "*" tags)
  19560. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19561. (concat "^\\*+ .*:\\("
  19562. (mapconcat 'identity tags "\\|")
  19563. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19564. (gen-re (nth 3 org-stuck-projects))
  19565. (re-list
  19566. (delq nil
  19567. (list
  19568. (if todo todo-re)
  19569. (if tags tags-re)
  19570. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19571. gen-re)))))
  19572. (setq org-agenda-skip-regexp
  19573. (if re-list
  19574. (mapconcat 'identity re-list "\\|")
  19575. (error "No information how to identify unstuck projects")))
  19576. (org-tags-view nil matcher)
  19577. (with-current-buffer org-agenda-buffer-name
  19578. (setq org-agenda-redo-command
  19579. '(org-agenda-list-stuck-projects
  19580. (or current-prefix-arg org-last-arg))))))
  19581. ;;; Diary integration
  19582. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19583. (defun org-get-entries-from-diary (date)
  19584. "Get the (Emacs Calendar) diary entries for DATE."
  19585. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19586. (diary-display-hook '(fancy-diary-display))
  19587. (pop-up-frames nil)
  19588. (list-diary-entries-hook
  19589. (cons 'org-diary-default-entry list-diary-entries-hook))
  19590. (diary-file-name-prefix-function nil) ; turn this feature off
  19591. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19592. entries
  19593. (org-disable-agenda-to-diary t))
  19594. (save-excursion
  19595. (save-window-excursion
  19596. (funcall (if (fboundp 'diary-list-entries)
  19597. 'diary-list-entries 'list-diary-entries)
  19598. date 1)))
  19599. (if (not (get-buffer fancy-diary-buffer))
  19600. (setq entries nil)
  19601. (with-current-buffer fancy-diary-buffer
  19602. (setq buffer-read-only nil)
  19603. (if (zerop (buffer-size))
  19604. ;; No entries
  19605. (setq entries nil)
  19606. ;; Omit the date and other unnecessary stuff
  19607. (org-agenda-cleanup-fancy-diary)
  19608. ;; Add prefix to each line and extend the text properties
  19609. (if (zerop (buffer-size))
  19610. (setq entries nil)
  19611. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19612. (set-buffer-modified-p nil)
  19613. (kill-buffer fancy-diary-buffer)))
  19614. (when entries
  19615. (setq entries (org-split-string entries "\n"))
  19616. (setq entries
  19617. (mapcar
  19618. (lambda (x)
  19619. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19620. ;; Extend the text properties to the beginning of the line
  19621. (org-add-props x (text-properties-at (1- (length x)) x)
  19622. 'type "diary" 'date date))
  19623. entries)))))
  19624. (defun org-agenda-cleanup-fancy-diary ()
  19625. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19626. This gets rid of the date, the underline under the date, and
  19627. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19628. date. It also removes lines that contain only whitespace."
  19629. (goto-char (point-min))
  19630. (if (looking-at ".*?:[ \t]*")
  19631. (progn
  19632. (replace-match "")
  19633. (re-search-forward "\n=+$" nil t)
  19634. (replace-match "")
  19635. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19636. (re-search-forward "\n=+$" nil t)
  19637. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19638. (goto-char (point-min))
  19639. (while (re-search-forward "^ +\n" nil t)
  19640. (replace-match ""))
  19641. (goto-char (point-min))
  19642. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19643. (replace-match "")))
  19644. ;; Make sure entries from the diary have the right text properties.
  19645. (eval-after-load "diary-lib"
  19646. '(if (boundp 'diary-modify-entry-list-string-function)
  19647. ;; We can rely on the hook, nothing to do
  19648. nil
  19649. ;; Hook not avaiable, must use advice to make this work
  19650. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19651. "Make the position visible."
  19652. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19653. (stringp string)
  19654. buffer-file-name)
  19655. (setq string (org-modify-diary-entry-string string))))))
  19656. (defun org-modify-diary-entry-string (string)
  19657. "Add text properties to string, allowing org-mode to act on it."
  19658. (org-add-props string nil
  19659. 'mouse-face 'highlight
  19660. 'keymap org-agenda-keymap
  19661. 'help-echo (if buffer-file-name
  19662. (format "mouse-2 or RET jump to diary file %s"
  19663. (abbreviate-file-name buffer-file-name))
  19664. "")
  19665. 'org-agenda-diary-link t
  19666. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19667. (defun org-diary-default-entry ()
  19668. "Add a dummy entry to the diary.
  19669. Needed to avoid empty dates which mess up holiday display."
  19670. ;; Catch the error if dealing with the new add-to-diary-alist
  19671. (when org-disable-agenda-to-diary
  19672. (condition-case nil
  19673. (add-to-diary-list original-date "Org-mode dummy" "")
  19674. (error
  19675. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19676. ;;;###autoload
  19677. (defun org-diary (&rest args)
  19678. "Return diary information from org-files.
  19679. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19680. It accesses org files and extracts information from those files to be
  19681. listed in the diary. The function accepts arguments specifying what
  19682. items should be listed. The following arguments are allowed:
  19683. :timestamp List the headlines of items containing a date stamp or
  19684. date range matching the selected date. Deadlines will
  19685. also be listed, on the expiration day.
  19686. :sexp List entries resulting from diary-like sexps.
  19687. :deadline List any deadlines past due, or due within
  19688. `org-deadline-warning-days'. The listing occurs only
  19689. in the diary for *today*, not at any other date. If
  19690. an entry is marked DONE, it is no longer listed.
  19691. :scheduled List all items which are scheduled for the given date.
  19692. The diary for *today* also contains items which were
  19693. scheduled earlier and are not yet marked DONE.
  19694. :todo List all TODO items from the org-file. This may be a
  19695. long list - so this is not turned on by default.
  19696. Like deadlines, these entries only show up in the
  19697. diary for *today*, not at any other date.
  19698. The call in the diary file should look like this:
  19699. &%%(org-diary) ~/path/to/some/orgfile.org
  19700. Use a separate line for each org file to check. Or, if you omit the file name,
  19701. all files listed in `org-agenda-files' will be checked automatically:
  19702. &%%(org-diary)
  19703. If you don't give any arguments (as in the example above), the default
  19704. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19705. So the example above may also be written as
  19706. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19707. The function expects the lisp variables `entry' and `date' to be provided
  19708. by the caller, because this is how the calendar works. Don't use this
  19709. function from a program - use `org-agenda-get-day-entries' instead."
  19710. (when (> (- (time-to-seconds (current-time))
  19711. org-agenda-last-marker-time)
  19712. 5)
  19713. (org-agenda-reset-markers))
  19714. (org-compile-prefix-format 'agenda)
  19715. (org-set-sorting-strategy 'agenda)
  19716. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19717. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19718. (list entry)
  19719. (org-agenda-files t)))
  19720. file rtn results)
  19721. (org-prepare-agenda-buffers files)
  19722. ;; If this is called during org-agenda, don't return any entries to
  19723. ;; the calendar. Org Agenda will list these entries itself.
  19724. (if org-disable-agenda-to-diary (setq files nil))
  19725. (while (setq file (pop files))
  19726. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19727. (setq results (append results rtn)))
  19728. (if results
  19729. (concat (org-finalize-agenda-entries results) "\n"))))
  19730. ;;; Agenda entry finders
  19731. (defun org-agenda-get-day-entries (file date &rest args)
  19732. "Does the work for `org-diary' and `org-agenda'.
  19733. FILE is the path to a file to be checked for entries. DATE is date like
  19734. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19735. which kind of entries should be extracted. For details about these, see
  19736. the documentation of `org-diary'."
  19737. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19738. (let* ((org-startup-folded nil)
  19739. (org-startup-align-all-tables nil)
  19740. (buffer (if (file-exists-p file)
  19741. (org-get-agenda-file-buffer file)
  19742. (error "No such file %s" file)))
  19743. arg results rtn)
  19744. (if (not buffer)
  19745. ;; If file does not exist, make sure an error message ends up in diary
  19746. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19747. (with-current-buffer buffer
  19748. (unless (org-mode-p)
  19749. (error "Agenda file %s is not in `org-mode'" file))
  19750. (let ((case-fold-search nil))
  19751. (save-excursion
  19752. (save-restriction
  19753. (if org-agenda-restrict
  19754. (narrow-to-region org-agenda-restrict-begin
  19755. org-agenda-restrict-end)
  19756. (widen))
  19757. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19758. (while (setq arg (pop args))
  19759. (cond
  19760. ((and (eq arg :todo)
  19761. (equal date (calendar-current-date)))
  19762. (setq rtn (org-agenda-get-todos))
  19763. (setq results (append results rtn)))
  19764. ((eq arg :timestamp)
  19765. (setq rtn (org-agenda-get-blocks))
  19766. (setq results (append results rtn))
  19767. (setq rtn (org-agenda-get-timestamps))
  19768. (setq results (append results rtn)))
  19769. ((eq arg :sexp)
  19770. (setq rtn (org-agenda-get-sexps))
  19771. (setq results (append results rtn)))
  19772. ((eq arg :scheduled)
  19773. (setq rtn (org-agenda-get-scheduled))
  19774. (setq results (append results rtn)))
  19775. ((eq arg :closed)
  19776. (setq rtn (org-agenda-get-closed))
  19777. (setq results (append results rtn)))
  19778. ((eq arg :deadline)
  19779. (setq rtn (org-agenda-get-deadlines))
  19780. (setq results (append results rtn))))))))
  19781. results))))
  19782. (defun org-entry-is-todo-p ()
  19783. (member (org-get-todo-state) org-not-done-keywords))
  19784. (defun org-entry-is-done-p ()
  19785. (member (org-get-todo-state) org-done-keywords))
  19786. (defun org-get-todo-state ()
  19787. (save-excursion
  19788. (org-back-to-heading t)
  19789. (and (looking-at org-todo-line-regexp)
  19790. (match-end 2)
  19791. (match-string 2))))
  19792. (defun org-at-date-range-p (&optional inactive-ok)
  19793. "Is the cursor inside a date range?"
  19794. (interactive)
  19795. (save-excursion
  19796. (catch 'exit
  19797. (let ((pos (point)))
  19798. (skip-chars-backward "^[<\r\n")
  19799. (skip-chars-backward "<[")
  19800. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19801. (>= (match-end 0) pos)
  19802. (throw 'exit t))
  19803. (skip-chars-backward "^<[\r\n")
  19804. (skip-chars-backward "<[")
  19805. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19806. (>= (match-end 0) pos)
  19807. (throw 'exit t)))
  19808. nil)))
  19809. (defun org-agenda-get-todos ()
  19810. "Return the TODO information for agenda display."
  19811. (let* ((props (list 'face nil
  19812. 'done-face 'org-done
  19813. 'org-not-done-regexp org-not-done-regexp
  19814. 'org-todo-regexp org-todo-regexp
  19815. 'mouse-face 'highlight
  19816. 'keymap org-agenda-keymap
  19817. 'help-echo
  19818. (format "mouse-2 or RET jump to org file %s"
  19819. (abbreviate-file-name buffer-file-name))))
  19820. ;; FIXME: get rid of the \n at some point but watch out
  19821. (regexp (concat "^\\*+[ \t]+\\("
  19822. (if org-select-this-todo-keyword
  19823. (if (equal org-select-this-todo-keyword "*")
  19824. org-todo-regexp
  19825. (concat "\\<\\("
  19826. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19827. "\\)\\>"))
  19828. org-not-done-regexp)
  19829. "[^\n\r]*\\)"))
  19830. marker priority category tags
  19831. ee txt beg end)
  19832. (goto-char (point-min))
  19833. (while (re-search-forward regexp nil t)
  19834. (catch :skip
  19835. (save-match-data
  19836. (beginning-of-line)
  19837. (setq beg (point) end (progn (outline-next-heading) (point)))
  19838. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19839. (re-search-forward org-ts-regexp end t))
  19840. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19841. (re-search-forward org-scheduled-time-regexp end t))
  19842. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19843. (re-search-forward org-deadline-time-regexp end t)
  19844. (org-deadline-close (match-string 1))))
  19845. (goto-char (1+ beg))
  19846. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19847. (throw :skip nil)))
  19848. (goto-char beg)
  19849. (org-agenda-skip)
  19850. (goto-char (match-beginning 1))
  19851. (setq marker (org-agenda-new-marker (match-beginning 0))
  19852. category (org-get-category)
  19853. tags (org-get-tags-at (point))
  19854. txt (org-format-agenda-item "" (match-string 1) category tags)
  19855. priority (1+ (org-get-priority txt)))
  19856. (org-add-props txt props
  19857. 'org-marker marker 'org-hd-marker marker
  19858. 'priority priority 'org-category category
  19859. 'type "todo")
  19860. (push txt ee)
  19861. (if org-agenda-todo-list-sublevels
  19862. (goto-char (match-end 1))
  19863. (org-end-of-subtree 'invisible))))
  19864. (nreverse ee)))
  19865. (defconst org-agenda-no-heading-message
  19866. "No heading for this item in buffer or region.")
  19867. (defun org-agenda-get-timestamps ()
  19868. "Return the date stamp information for agenda display."
  19869. (let* ((props (list 'face nil
  19870. 'org-not-done-regexp org-not-done-regexp
  19871. 'org-todo-regexp org-todo-regexp
  19872. 'mouse-face 'highlight
  19873. 'keymap org-agenda-keymap
  19874. 'help-echo
  19875. (format "mouse-2 or RET jump to org file %s"
  19876. (abbreviate-file-name buffer-file-name))))
  19877. (d1 (calendar-absolute-from-gregorian date))
  19878. (remove-re
  19879. (concat
  19880. (regexp-quote
  19881. (format-time-string
  19882. "<%Y-%m-%d"
  19883. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19884. ".*?>"))
  19885. (regexp
  19886. (concat
  19887. (regexp-quote
  19888. (substring
  19889. (format-time-string
  19890. (car org-time-stamp-formats)
  19891. (apply 'encode-time ; DATE bound by calendar
  19892. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19893. 0 11))
  19894. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19895. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19896. marker hdmarker deadlinep scheduledp donep tmp priority category
  19897. ee txt timestr tags b0 b3 e3 head)
  19898. (goto-char (point-min))
  19899. (while (re-search-forward regexp nil t)
  19900. (setq b0 (match-beginning 0)
  19901. b3 (match-beginning 3) e3 (match-end 3))
  19902. (catch :skip
  19903. (and (org-at-date-range-p) (throw :skip nil))
  19904. (org-agenda-skip)
  19905. (if (and (match-end 1)
  19906. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19907. (throw :skip nil))
  19908. (if (and e3
  19909. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19910. (throw :skip nil))
  19911. (setq marker (org-agenda-new-marker b0)
  19912. category (org-get-category b0)
  19913. tmp (buffer-substring (max (point-min)
  19914. (- b0 org-ds-keyword-length))
  19915. b0)
  19916. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19917. deadlinep (string-match org-deadline-regexp tmp)
  19918. scheduledp (string-match org-scheduled-regexp tmp)
  19919. donep (org-entry-is-done-p))
  19920. (if (or scheduledp deadlinep) (throw :skip t))
  19921. (if (string-match ">" timestr)
  19922. ;; substring should only run to end of time stamp
  19923. (setq timestr (substring timestr 0 (match-end 0))))
  19924. (save-excursion
  19925. (if (re-search-backward "^\\*+ " nil t)
  19926. (progn
  19927. (goto-char (match-beginning 0))
  19928. (setq hdmarker (org-agenda-new-marker)
  19929. tags (org-get-tags-at))
  19930. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19931. (setq head (match-string 1))
  19932. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19933. (setq txt (org-format-agenda-item
  19934. nil head category tags timestr nil
  19935. remove-re)))
  19936. (setq txt org-agenda-no-heading-message))
  19937. (setq priority (org-get-priority txt))
  19938. (org-add-props txt props
  19939. 'org-marker marker 'org-hd-marker hdmarker)
  19940. (org-add-props txt nil 'priority priority
  19941. 'org-category category 'date date
  19942. 'type "timestamp")
  19943. (push txt ee))
  19944. (outline-next-heading)))
  19945. (nreverse ee)))
  19946. (defun org-agenda-get-sexps ()
  19947. "Return the sexp information for agenda display."
  19948. (require 'diary-lib)
  19949. (let* ((props (list 'face nil
  19950. 'mouse-face 'highlight
  19951. 'keymap org-agenda-keymap
  19952. 'help-echo
  19953. (format "mouse-2 or RET jump to org file %s"
  19954. (abbreviate-file-name buffer-file-name))))
  19955. (regexp "^&?%%(")
  19956. marker category ee txt tags entry result beg b sexp sexp-entry)
  19957. (goto-char (point-min))
  19958. (while (re-search-forward regexp nil t)
  19959. (catch :skip
  19960. (org-agenda-skip)
  19961. (setq beg (match-beginning 0))
  19962. (goto-char (1- (match-end 0)))
  19963. (setq b (point))
  19964. (forward-sexp 1)
  19965. (setq sexp (buffer-substring b (point)))
  19966. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19967. (org-trim (match-string 1))
  19968. ""))
  19969. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19970. (when result
  19971. (setq marker (org-agenda-new-marker beg)
  19972. category (org-get-category beg))
  19973. (if (string-match "\\S-" result)
  19974. (setq txt result)
  19975. (setq txt "SEXP entry returned empty string"))
  19976. (setq txt (org-format-agenda-item
  19977. "" txt category tags 'time))
  19978. (org-add-props txt props 'org-marker marker)
  19979. (org-add-props txt nil
  19980. 'org-category category 'date date
  19981. 'type "sexp")
  19982. (push txt ee))))
  19983. (nreverse ee)))
  19984. (defun org-agenda-get-closed ()
  19985. "Return the logged TODO entries for agenda display."
  19986. (let* ((props (list 'mouse-face 'highlight
  19987. 'org-not-done-regexp org-not-done-regexp
  19988. 'org-todo-regexp org-todo-regexp
  19989. 'keymap org-agenda-keymap
  19990. 'help-echo
  19991. (format "mouse-2 or RET jump to org file %s"
  19992. (abbreviate-file-name buffer-file-name))))
  19993. (regexp (concat
  19994. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19995. (regexp-quote
  19996. (substring
  19997. (format-time-string
  19998. (car org-time-stamp-formats)
  19999. (apply 'encode-time ; DATE bound by calendar
  20000. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20001. 1 11))))
  20002. marker hdmarker priority category tags closedp
  20003. ee txt timestr)
  20004. (goto-char (point-min))
  20005. (while (re-search-forward regexp nil t)
  20006. (catch :skip
  20007. (org-agenda-skip)
  20008. (setq marker (org-agenda-new-marker (match-beginning 0))
  20009. closedp (equal (match-string 1) org-closed-string)
  20010. category (org-get-category (match-beginning 0))
  20011. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20012. ;; donep (org-entry-is-done-p)
  20013. )
  20014. (if (string-match "\\]" timestr)
  20015. ;; substring should only run to end of time stamp
  20016. (setq timestr (substring timestr 0 (match-end 0))))
  20017. (save-excursion
  20018. (if (re-search-backward "^\\*+ " nil t)
  20019. (progn
  20020. (goto-char (match-beginning 0))
  20021. (setq hdmarker (org-agenda-new-marker)
  20022. tags (org-get-tags-at))
  20023. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20024. (setq txt (org-format-agenda-item
  20025. (if closedp "Closed: " "Clocked: ")
  20026. (match-string 1) category tags timestr)))
  20027. (setq txt org-agenda-no-heading-message))
  20028. (setq priority 100000)
  20029. (org-add-props txt props
  20030. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20031. 'priority priority 'org-category category
  20032. 'type "closed" 'date date
  20033. 'undone-face 'org-warning 'done-face 'org-done)
  20034. (push txt ee))
  20035. (goto-char (point-at-eol))))
  20036. (nreverse ee)))
  20037. (defun org-agenda-get-deadlines ()
  20038. "Return the deadline information for agenda display."
  20039. (let* ((props (list 'mouse-face 'highlight
  20040. 'org-not-done-regexp org-not-done-regexp
  20041. 'org-todo-regexp org-todo-regexp
  20042. 'keymap org-agenda-keymap
  20043. 'help-echo
  20044. (format "mouse-2 or RET jump to org file %s"
  20045. (abbreviate-file-name buffer-file-name))))
  20046. (regexp org-deadline-time-regexp)
  20047. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20048. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20049. d2 diff dfrac wdays pos pos1 category tags
  20050. ee txt head face s upcomingp donep timestr)
  20051. (goto-char (point-min))
  20052. (while (re-search-forward regexp nil t)
  20053. (catch :skip
  20054. (org-agenda-skip)
  20055. (setq s (match-string 1)
  20056. pos (1- (match-beginning 1))
  20057. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20058. diff (- d2 d1)
  20059. wdays (org-get-wdays s)
  20060. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20061. upcomingp (and todayp (> diff 0)))
  20062. ;; When to show a deadline in the calendar:
  20063. ;; If the expiration is within wdays warning time.
  20064. ;; Past-due deadlines are only shown on the current date
  20065. (if (or (and (<= diff wdays)
  20066. (and todayp (not org-agenda-only-exact-dates)))
  20067. (= diff 0))
  20068. (save-excursion
  20069. (setq category (org-get-category))
  20070. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20071. (progn
  20072. (goto-char (match-end 0))
  20073. (setq pos1 (match-beginning 0))
  20074. (setq tags (org-get-tags-at pos1))
  20075. (setq head (buffer-substring-no-properties
  20076. (point)
  20077. (progn (skip-chars-forward "^\r\n")
  20078. (point))))
  20079. (setq donep (string-match org-looking-at-done-regexp head))
  20080. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20081. (setq timestr
  20082. (concat (substring s (match-beginning 1)) " "))
  20083. (setq timestr 'time))
  20084. (if (and donep
  20085. (or org-agenda-skip-deadline-if-done
  20086. (not (= diff 0))))
  20087. (setq txt nil)
  20088. (setq txt (org-format-agenda-item
  20089. (if (= diff 0)
  20090. (car org-agenda-deadline-leaders)
  20091. (format (nth 1 org-agenda-deadline-leaders)
  20092. diff))
  20093. head category tags timestr))))
  20094. (setq txt org-agenda-no-heading-message))
  20095. (when txt
  20096. (setq face (org-agenda-deadline-face dfrac))
  20097. (org-add-props txt props
  20098. 'org-marker (org-agenda-new-marker pos)
  20099. 'org-hd-marker (org-agenda-new-marker pos1)
  20100. 'priority (+ (- diff)
  20101. (org-get-priority txt))
  20102. 'org-category category
  20103. 'type (if upcomingp "upcoming-deadline" "deadline")
  20104. 'date (if upcomingp date d2)
  20105. 'face (if donep 'org-done face)
  20106. 'undone-face face 'done-face 'org-done)
  20107. (push txt ee))))))
  20108. (nreverse ee)))
  20109. (defun org-agenda-deadline-face (fraction)
  20110. "Return the face to displaying a deadline item.
  20111. FRACTION is what fraction of the head-warning time has passed."
  20112. (let ((faces org-agenda-deadline-faces) f)
  20113. (catch 'exit
  20114. (while (setq f (pop faces))
  20115. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20116. (defun org-agenda-get-scheduled ()
  20117. "Return the scheduled information for agenda display."
  20118. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20119. 'org-todo-regexp org-todo-regexp
  20120. 'done-face 'org-done
  20121. 'mouse-face 'highlight
  20122. 'keymap org-agenda-keymap
  20123. 'help-echo
  20124. (format "mouse-2 or RET jump to org file %s"
  20125. (abbreviate-file-name buffer-file-name))))
  20126. (regexp org-scheduled-time-regexp)
  20127. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20128. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20129. d2 diff pos pos1 category tags
  20130. ee txt head pastschedp donep face timestr s)
  20131. (goto-char (point-min))
  20132. (while (re-search-forward regexp nil t)
  20133. (catch :skip
  20134. (org-agenda-skip)
  20135. (setq s (match-string 1)
  20136. pos (1- (match-beginning 1))
  20137. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20138. ;;; is this right?
  20139. ;;; do we need to do this for deadleine too????
  20140. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20141. diff (- d2 d1))
  20142. (setq pastschedp (and todayp (< diff 0)))
  20143. ;; When to show a scheduled item in the calendar:
  20144. ;; If it is on or past the date.
  20145. (if (or (and (< diff 0)
  20146. (and todayp (not org-agenda-only-exact-dates)))
  20147. (= diff 0))
  20148. (save-excursion
  20149. (setq category (org-get-category))
  20150. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20151. (progn
  20152. (goto-char (match-end 0))
  20153. (setq pos1 (match-beginning 0))
  20154. (setq tags (org-get-tags-at))
  20155. (setq head (buffer-substring-no-properties
  20156. (point)
  20157. (progn (skip-chars-forward "^\r\n") (point))))
  20158. (setq donep (string-match org-looking-at-done-regexp head))
  20159. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20160. (setq timestr
  20161. (concat (substring s (match-beginning 1)) " "))
  20162. (setq timestr 'time))
  20163. (if (and donep
  20164. (or org-agenda-skip-scheduled-if-done
  20165. (not (= diff 0))))
  20166. (setq txt nil)
  20167. (setq txt (org-format-agenda-item
  20168. (if (= diff 0)
  20169. (car org-agenda-scheduled-leaders)
  20170. (format (nth 1 org-agenda-scheduled-leaders)
  20171. (- 1 diff)))
  20172. head category tags timestr))))
  20173. (setq txt org-agenda-no-heading-message))
  20174. (when txt
  20175. (setq face (if pastschedp
  20176. 'org-scheduled-previously
  20177. 'org-scheduled-today))
  20178. (org-add-props txt props
  20179. 'undone-face face
  20180. 'face (if donep 'org-done face)
  20181. 'org-marker (org-agenda-new-marker pos)
  20182. 'org-hd-marker (org-agenda-new-marker pos1)
  20183. 'type (if pastschedp "past-scheduled" "scheduled")
  20184. 'date (if pastschedp d2 date)
  20185. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20186. 'org-category category)
  20187. (push txt ee))))))
  20188. (nreverse ee)))
  20189. (defun org-agenda-get-blocks ()
  20190. "Return the date-range information for agenda display."
  20191. (let* ((props (list 'face nil
  20192. 'org-not-done-regexp org-not-done-regexp
  20193. 'org-todo-regexp org-todo-regexp
  20194. 'mouse-face 'highlight
  20195. 'keymap org-agenda-keymap
  20196. 'help-echo
  20197. (format "mouse-2 or RET jump to org file %s"
  20198. (abbreviate-file-name buffer-file-name))))
  20199. (regexp org-tr-regexp)
  20200. (d0 (calendar-absolute-from-gregorian date))
  20201. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20202. donep head)
  20203. (goto-char (point-min))
  20204. (while (re-search-forward regexp nil t)
  20205. (catch :skip
  20206. (org-agenda-skip)
  20207. (setq pos (point))
  20208. (setq timestr (match-string 0)
  20209. s1 (match-string 1)
  20210. s2 (match-string 2)
  20211. d1 (time-to-days (org-time-string-to-time s1))
  20212. d2 (time-to-days (org-time-string-to-time s2)))
  20213. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20214. ;; Only allow days between the limits, because the normal
  20215. ;; date stamps will catch the limits.
  20216. (save-excursion
  20217. (setq marker (org-agenda-new-marker (point)))
  20218. (setq category (org-get-category))
  20219. (if (re-search-backward "^\\*+ " nil t)
  20220. (progn
  20221. (goto-char (match-beginning 0))
  20222. (setq hdmarker (org-agenda-new-marker (point)))
  20223. (setq tags (org-get-tags-at))
  20224. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20225. (setq head (match-string 1))
  20226. (and org-agenda-skip-timestamp-if-done
  20227. (org-entry-is-done-p)
  20228. (throw :skip t))
  20229. (setq txt (org-format-agenda-item
  20230. (format (if (= d1 d2) "" "(%d/%d): ")
  20231. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20232. head category tags
  20233. (if (= d0 d1) timestr))))
  20234. (setq txt org-agenda-no-heading-message))
  20235. (org-add-props txt props
  20236. 'org-marker marker 'org-hd-marker hdmarker
  20237. 'type "block" 'date date
  20238. 'priority (org-get-priority txt) 'org-category category)
  20239. (push txt ee)))
  20240. (goto-char pos)))
  20241. ;; Sort the entries by expiration date.
  20242. (nreverse ee)))
  20243. ;;; Agenda presentation and sorting
  20244. (defconst org-plain-time-of-day-regexp
  20245. (concat
  20246. "\\(\\<[012]?[0-9]"
  20247. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20248. "\\(--?"
  20249. "\\(\\<[012]?[0-9]"
  20250. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20251. "\\)?")
  20252. "Regular expression to match a plain time or time range.
  20253. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20254. groups carry important information:
  20255. 0 the full match
  20256. 1 the first time, range or not
  20257. 8 the second time, if it is a range.")
  20258. (defconst org-plain-time-extension-regexp
  20259. (concat
  20260. "\\(\\<[012]?[0-9]"
  20261. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20262. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20263. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20264. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20265. groups carry important information:
  20266. 0 the full match
  20267. 7 hours of duration
  20268. 9 minutes of duration")
  20269. (defconst org-stamp-time-of-day-regexp
  20270. (concat
  20271. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20272. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20273. "\\(--?"
  20274. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20275. "Regular expression to match a timestamp time or time range.
  20276. After a match, the following groups carry important information:
  20277. 0 the full match
  20278. 1 date plus weekday, for backreferencing to make sure both times on same day
  20279. 2 the first time, range or not
  20280. 4 the second time, if it is a range.")
  20281. (defvar org-prefix-has-time nil
  20282. "A flag, set by `org-compile-prefix-format'.
  20283. The flag is set if the currently compiled format contains a `%t'.")
  20284. (defvar org-prefix-has-tag nil
  20285. "A flag, set by `org-compile-prefix-format'.
  20286. The flag is set if the currently compiled format contains a `%T'.")
  20287. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20288. noprefix remove-re)
  20289. "Format TXT to be inserted into the agenda buffer.
  20290. In particular, it adds the prefix and corresponding text properties. EXTRA
  20291. must be a string and replaces the `%s' specifier in the prefix format.
  20292. CATEGORY (string, symbol or nil) may be used to overrule the default
  20293. category taken from local variable or file name. It will replace the `%c'
  20294. specifier in the format. DOTIME, when non-nil, indicates that a
  20295. time-of-day should be extracted from TXT for sorting of this entry, and for
  20296. the `%t' specifier in the format. When DOTIME is a string, this string is
  20297. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20298. only the correctly processes TXT should be returned - this is used by
  20299. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20300. Any match of REMOVE-RE will be removed from TXT."
  20301. (save-match-data
  20302. ;; Diary entries sometimes have extra whitespace at the beginning
  20303. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20304. (let* ((category (or category
  20305. org-category
  20306. (if buffer-file-name
  20307. (file-name-sans-extension
  20308. (file-name-nondirectory buffer-file-name))
  20309. "")))
  20310. (tag (if tags (nth (1- (length tags)) tags) ""))
  20311. time ; time and tag are needed for the eval of the prefix format
  20312. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20313. (time-of-day (and dotime (org-get-time-of-day ts)))
  20314. stamp plain s0 s1 s2 rtn srp)
  20315. (when (and dotime time-of-day org-prefix-has-time)
  20316. ;; Extract starting and ending time and move them to prefix
  20317. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20318. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20319. (setq s0 (match-string 0 ts)
  20320. srp (and stamp (match-end 3))
  20321. s1 (match-string (if plain 1 2) ts)
  20322. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20323. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20324. ;; them, we might want to remove them there to avoid duplication.
  20325. ;; The user can turn this off with a variable.
  20326. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20327. (string-match (concat (regexp-quote s0) " *") txt)
  20328. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20329. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20330. (= (match-beginning 0) 0)
  20331. t))
  20332. (setq txt (replace-match "" nil nil txt))))
  20333. ;; Normalize the time(s) to 24 hour
  20334. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20335. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20336. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20337. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20338. (let ((m (+ (string-to-number (match-string 2 s1))
  20339. (* 60 (string-to-number (match-string 1 s1)))
  20340. org-agenda-default-appointment-duration))
  20341. h)
  20342. (setq h (/ m 60) m (- m (* h 60)))
  20343. (setq s2 (format "%02d:%02d" h m))))
  20344. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20345. txt)
  20346. ;; Tags are in the string
  20347. (if (or (eq org-agenda-remove-tags t)
  20348. (and org-agenda-remove-tags
  20349. org-prefix-has-tag))
  20350. (setq txt (replace-match "" t t txt))
  20351. (setq txt (replace-match
  20352. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20353. (match-string 2 txt))
  20354. t t txt))))
  20355. (when remove-re
  20356. (while (string-match remove-re txt)
  20357. (setq txt (replace-match "" t t txt))))
  20358. ;; Create the final string
  20359. (if noprefix
  20360. (setq rtn txt)
  20361. ;; Prepare the variables needed in the eval of the compiled format
  20362. (setq time (cond (s2 (concat s1 "-" s2))
  20363. (s1 (concat s1 "......"))
  20364. (t ""))
  20365. extra (or extra "")
  20366. category (if (symbolp category) (symbol-name category) category))
  20367. ;; Evaluate the compiled format
  20368. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20369. ;; And finally add the text properties
  20370. (org-add-props rtn nil
  20371. 'org-category (downcase category) 'tags tags
  20372. 'org-highest-priority org-highest-priority
  20373. 'org-lowest-priority org-lowest-priority
  20374. 'prefix-length (- (length rtn) (length txt))
  20375. 'time-of-day time-of-day
  20376. 'txt txt
  20377. 'time time
  20378. 'extra extra
  20379. 'dotime dotime))))
  20380. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20381. (defvar org-agenda-sorting-strategy-selected nil)
  20382. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20383. (catch 'exit
  20384. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20385. ((and todayp (member 'today (car org-agenda-time-grid))))
  20386. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20387. ((member 'weekly (car org-agenda-time-grid)))
  20388. (t (throw 'exit list)))
  20389. (let* ((have (delq nil (mapcar
  20390. (lambda (x) (get-text-property 1 'time-of-day x))
  20391. list)))
  20392. (string (nth 1 org-agenda-time-grid))
  20393. (gridtimes (nth 2 org-agenda-time-grid))
  20394. (req (car org-agenda-time-grid))
  20395. (remove (member 'remove-match req))
  20396. new time)
  20397. (if (and (member 'require-timed req) (not have))
  20398. ;; don't show empty grid
  20399. (throw 'exit list))
  20400. (while (setq time (pop gridtimes))
  20401. (unless (and remove (member time have))
  20402. (setq time (int-to-string time))
  20403. (push (org-format-agenda-item
  20404. nil string "" nil
  20405. (concat (substring time 0 -2) ":" (substring time -2)))
  20406. new)
  20407. (put-text-property
  20408. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20409. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20410. (append new list)
  20411. (append list new)))))
  20412. (defun org-compile-prefix-format (key)
  20413. "Compile the prefix format into a Lisp form that can be evaluated.
  20414. The resulting form is returned and stored in the variable
  20415. `org-prefix-format-compiled'."
  20416. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20417. (let ((s (cond
  20418. ((stringp org-agenda-prefix-format)
  20419. org-agenda-prefix-format)
  20420. ((assq key org-agenda-prefix-format)
  20421. (cdr (assq key org-agenda-prefix-format)))
  20422. (t " %-12:c%?-12t% s")))
  20423. (start 0)
  20424. varform vars var e c f opt)
  20425. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20426. s start)
  20427. (setq var (cdr (assoc (match-string 4 s)
  20428. '(("c" . category) ("t" . time) ("s" . extra)
  20429. ("T" . tag))))
  20430. c (or (match-string 3 s) "")
  20431. opt (match-beginning 1)
  20432. start (1+ (match-beginning 0)))
  20433. (if (equal var 'time) (setq org-prefix-has-time t))
  20434. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20435. (setq f (concat "%" (match-string 2 s) "s"))
  20436. (if opt
  20437. (setq varform
  20438. `(if (equal "" ,var)
  20439. ""
  20440. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20441. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20442. (setq s (replace-match "%s" t nil s))
  20443. (push varform vars))
  20444. (setq vars (nreverse vars))
  20445. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20446. (defun org-set-sorting-strategy (key)
  20447. (if (symbolp (car org-agenda-sorting-strategy))
  20448. ;; the old format
  20449. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20450. (setq org-agenda-sorting-strategy-selected
  20451. (or (cdr (assq key org-agenda-sorting-strategy))
  20452. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20453. '(time-up category-keep priority-down)))))
  20454. (defun org-get-time-of-day (s &optional string mod24)
  20455. "Check string S for a time of day.
  20456. If found, return it as a military time number between 0 and 2400.
  20457. If not found, return nil.
  20458. The optional STRING argument forces conversion into a 5 character wide string
  20459. HH:MM."
  20460. (save-match-data
  20461. (when
  20462. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20463. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20464. (let* ((h (string-to-number (match-string 1 s)))
  20465. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20466. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20467. (am-p (equal ampm "am"))
  20468. (h1 (cond ((not ampm) h)
  20469. ((= h 12) (if am-p 0 12))
  20470. (t (+ h (if am-p 0 12)))))
  20471. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20472. (mod h1 24) h1))
  20473. (t0 (+ (* 100 h2) m))
  20474. (t1 (concat (if (>= h1 24) "+" " ")
  20475. (if (< t0 100) "0" "")
  20476. (if (< t0 10) "0" "")
  20477. (int-to-string t0))))
  20478. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20479. (defun org-finalize-agenda-entries (list &optional nosort)
  20480. "Sort and concatenate the agenda items."
  20481. (setq list (mapcar 'org-agenda-highlight-todo list))
  20482. (if nosort
  20483. list
  20484. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20485. (defun org-agenda-highlight-todo (x)
  20486. (let (re pl)
  20487. (if (eq x 'line)
  20488. (save-excursion
  20489. (beginning-of-line 1)
  20490. (setq re (get-text-property (point) 'org-todo-regexp))
  20491. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20492. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20493. (add-text-properties (match-beginning 0) (match-end 0)
  20494. (list 'face (org-get-todo-face 0)))
  20495. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20496. (delete-region (match-beginning 1) (1- (match-end 0)))
  20497. (goto-char (match-beginning 1))
  20498. (insert (format org-agenda-todo-keyword-format s)))))
  20499. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20500. pl (get-text-property 0 'prefix-length x))
  20501. (when (and re
  20502. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20503. x (or pl 0)) pl))
  20504. (add-text-properties
  20505. (or (match-end 1) (match-end 0)) (match-end 0)
  20506. (list 'face (org-get-todo-face (match-string 2 x)))
  20507. x)
  20508. (setq x (concat (substring x 0 (match-end 1))
  20509. (format org-agenda-todo-keyword-format
  20510. (match-string 2 x))
  20511. " "
  20512. (substring x (match-end 3)))))
  20513. x)))
  20514. (defsubst org-cmp-priority (a b)
  20515. "Compare the priorities of string A and B."
  20516. (let ((pa (or (get-text-property 1 'priority a) 0))
  20517. (pb (or (get-text-property 1 'priority b) 0)))
  20518. (cond ((> pa pb) +1)
  20519. ((< pa pb) -1)
  20520. (t nil))))
  20521. (defsubst org-cmp-category (a b)
  20522. "Compare the string values of categories of strings A and B."
  20523. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20524. (cb (or (get-text-property 1 'org-category b) "")))
  20525. (cond ((string-lessp ca cb) -1)
  20526. ((string-lessp cb ca) +1)
  20527. (t nil))))
  20528. (defsubst org-cmp-tag (a b)
  20529. "Compare the string values of categories of strings A and B."
  20530. (let ((ta (car (last (get-text-property 1 'tags a))))
  20531. (tb (car (last (get-text-property 1 'tags b)))))
  20532. (cond ((not ta) +1)
  20533. ((not tb) -1)
  20534. ((string-lessp ta tb) -1)
  20535. ((string-lessp tb ta) +1)
  20536. (t nil))))
  20537. (defsubst org-cmp-time (a b)
  20538. "Compare the time-of-day values of strings A and B."
  20539. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20540. (ta (or (get-text-property 1 'time-of-day a) def))
  20541. (tb (or (get-text-property 1 'time-of-day b) def)))
  20542. (cond ((< ta tb) -1)
  20543. ((< tb ta) +1)
  20544. (t nil))))
  20545. (defun org-entries-lessp (a b)
  20546. "Predicate for sorting agenda entries."
  20547. ;; The following variables will be used when the form is evaluated.
  20548. ;; So even though the compiler complains, keep them.
  20549. (let* ((time-up (org-cmp-time a b))
  20550. (time-down (if time-up (- time-up) nil))
  20551. (priority-up (org-cmp-priority a b))
  20552. (priority-down (if priority-up (- priority-up) nil))
  20553. (category-up (org-cmp-category a b))
  20554. (category-down (if category-up (- category-up) nil))
  20555. (category-keep (if category-up +1 nil))
  20556. (tag-up (org-cmp-tag a b))
  20557. (tag-down (if tag-up (- tag-up) nil)))
  20558. (cdr (assoc
  20559. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20560. '((-1 . t) (1 . nil) (nil . nil))))))
  20561. ;;; Agenda restriction lock
  20562. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20563. "Overlay to mark the headline to which arenda commands are restricted.")
  20564. (org-overlay-put org-agenda-restriction-lock-overlay
  20565. 'face 'org-agenda-restriction-lock)
  20566. (org-overlay-put org-agenda-restriction-lock-overlay
  20567. 'help-echo "Agendas are currently limited to this subtree.")
  20568. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20569. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20570. "Overlay marking the agenda restriction line in speedbar.")
  20571. (org-overlay-put org-speedbar-restriction-lock-overlay
  20572. 'face 'org-agenda-restriction-lock)
  20573. (org-overlay-put org-speedbar-restriction-lock-overlay
  20574. 'help-echo "Agendas are currently limited to this item.")
  20575. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20576. (defun org-agenda-set-restriction-lock (&optional type)
  20577. "Set restriction lock for agenda, to current subtree or file.
  20578. Restriction will be the file if TYPE is `file', or if type is the
  20579. universal prefix '(4), or if the cursor is before the first headline
  20580. in the file. Otherwise, restriction will be to the current subtree."
  20581. (interactive "P")
  20582. (and (equal type '(4)) (setq type 'file))
  20583. (setq type (cond
  20584. (type type)
  20585. ((org-at-heading-p) 'subtree)
  20586. ((condition-case nil (org-back-to-heading t) (error nil))
  20587. 'subtree)
  20588. (t 'file)))
  20589. (if (eq type 'subtree)
  20590. (progn
  20591. (setq org-agenda-restrict t)
  20592. (setq org-agenda-overriding-restriction 'subtree)
  20593. (put 'org-agenda-files 'org-restrict
  20594. (list (buffer-file-name (buffer-base-buffer))))
  20595. (org-back-to-heading t)
  20596. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20597. (move-marker org-agenda-restrict-begin (point))
  20598. (move-marker org-agenda-restrict-end
  20599. (save-excursion (org-end-of-subtree t)))
  20600. (message "Locking agenda restriction to subtree"))
  20601. (put 'org-agenda-files 'org-restrict
  20602. (list (buffer-file-name (buffer-base-buffer))))
  20603. (setq org-agenda-restrict nil)
  20604. (setq org-agenda-overriding-restriction 'file)
  20605. (move-marker org-agenda-restrict-begin nil)
  20606. (move-marker org-agenda-restrict-end nil)
  20607. (message "Locking agenda restriction to file"))
  20608. (setq current-prefix-arg nil)
  20609. (org-agenda-maybe-redo))
  20610. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20611. "Remove the agenda restriction lock."
  20612. (interactive "P")
  20613. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20614. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20615. (setq org-agenda-overriding-restriction nil)
  20616. (setq org-agenda-restrict nil)
  20617. (put 'org-agenda-files 'org-restrict nil)
  20618. (move-marker org-agenda-restrict-begin nil)
  20619. (move-marker org-agenda-restrict-end nil)
  20620. (setq current-prefix-arg nil)
  20621. (message "Agenda restriction lock removed")
  20622. (or noupdate (org-agenda-maybe-redo)))
  20623. (defun org-agenda-maybe-redo ()
  20624. "If there is any window showing the agenda view, update it."
  20625. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20626. (w0 (selected-window)))
  20627. (when w
  20628. (select-window w)
  20629. (org-agenda-redo)
  20630. (select-window w0)
  20631. (if org-agenda-overriding-restriction
  20632. (message "Agenda view shifted to new %s restriction"
  20633. org-agenda-overriding-restriction)
  20634. (message "Agenda restriction lock removed")))))
  20635. ;;; Agenda commands
  20636. (defun org-agenda-check-type (error &rest types)
  20637. "Check if agenda buffer is of allowed type.
  20638. If ERROR is non-nil, throw an error, otherwise just return nil."
  20639. (if (memq org-agenda-type types)
  20640. t
  20641. (if error
  20642. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20643. nil)))
  20644. (defun org-agenda-quit ()
  20645. "Exit agenda by removing the window or the buffer."
  20646. (interactive)
  20647. (let ((buf (current-buffer)))
  20648. (if (not (one-window-p)) (delete-window))
  20649. (kill-buffer buf)
  20650. (org-agenda-reset-markers)
  20651. (org-columns-remove-overlays))
  20652. ;; Maybe restore the pre-agenda window configuration.
  20653. (and org-agenda-restore-windows-after-quit
  20654. (not (eq org-agenda-window-setup 'other-frame))
  20655. org-pre-agenda-window-conf
  20656. (set-window-configuration org-pre-agenda-window-conf)))
  20657. (defun org-agenda-exit ()
  20658. "Exit agenda by removing the window or the buffer.
  20659. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20660. Org-mode buffers visited directly by the user will not be touched."
  20661. (interactive)
  20662. (org-release-buffers org-agenda-new-buffers)
  20663. (setq org-agenda-new-buffers nil)
  20664. (org-agenda-quit))
  20665. (defun org-agenda-execute (arg)
  20666. "Execute another agenda command, keeping same window.\\<global-map>
  20667. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20668. (interactive "P")
  20669. (let ((org-agenda-window-setup 'current-window))
  20670. (org-agenda arg)))
  20671. (defun org-save-all-org-buffers ()
  20672. "Save all Org-mode buffers without user confirmation."
  20673. (interactive)
  20674. (message "Saving all Org-mode buffers...")
  20675. (save-some-buffers t 'org-mode-p)
  20676. (message "Saving all Org-mode buffers... done"))
  20677. (defun org-agenda-redo ()
  20678. "Rebuild Agenda.
  20679. When this is the global TODO list, a prefix argument will be interpreted."
  20680. (interactive)
  20681. (let* ((org-agenda-keep-modes t)
  20682. (line (org-current-line))
  20683. (window-line (- line (org-current-line (window-start))))
  20684. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20685. (message "Rebuilding agenda buffer...")
  20686. (org-let lprops '(eval org-agenda-redo-command))
  20687. (setq org-agenda-undo-list nil
  20688. org-agenda-pending-undo-list nil)
  20689. (message "Rebuilding agenda buffer...done")
  20690. (goto-line line)
  20691. (recenter window-line)))
  20692. (defun org-agenda-goto-date (date)
  20693. "Jump to DATE in agenda."
  20694. (interactive (list (org-read-date)))
  20695. (org-agenda-list nil date))
  20696. (defun org-agenda-goto-today ()
  20697. "Go to today."
  20698. (interactive)
  20699. (org-agenda-check-type t 'timeline 'agenda)
  20700. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20701. (cond
  20702. (tdpos (goto-char tdpos))
  20703. ((eq org-agenda-type 'agenda)
  20704. (let* ((sd (time-to-days
  20705. (time-subtract (current-time)
  20706. (list 0 (* 3600 org-extend-today-until) 0))))
  20707. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20708. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20709. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20710. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20711. (org-agenda-redo)
  20712. (org-agenda-find-same-or-today-or-agenda)))
  20713. (t (error "Cannot find today")))))
  20714. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20715. (goto-char
  20716. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20717. (text-property-any (point-min) (point-max) 'org-today t)
  20718. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20719. (point-min))))
  20720. (defun org-agenda-later (arg)
  20721. "Go forward in time by thee current span.
  20722. With prefix ARG, go forward that many times the current span."
  20723. (interactive "p")
  20724. (org-agenda-check-type t 'agenda)
  20725. (let* ((span org-agenda-span)
  20726. (sd org-starting-day)
  20727. (greg (calendar-gregorian-from-absolute sd))
  20728. (cnt (get-text-property (point) 'org-day-cnt))
  20729. greg2 nd)
  20730. (cond
  20731. ((eq span 'day)
  20732. (setq sd (+ arg sd) nd 1))
  20733. ((eq span 'week)
  20734. (setq sd (+ (* 7 arg) sd) nd 7))
  20735. ((eq span 'month)
  20736. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20737. sd (calendar-absolute-from-gregorian greg2))
  20738. (setcar greg2 (1+ (car greg2)))
  20739. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20740. ((eq span 'year)
  20741. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20742. sd (calendar-absolute-from-gregorian greg2))
  20743. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20744. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20745. (let ((org-agenda-overriding-arguments
  20746. (list (car org-agenda-last-arguments) sd nd t)))
  20747. (org-agenda-redo)
  20748. (org-agenda-find-same-or-today-or-agenda cnt))))
  20749. (defun org-agenda-earlier (arg)
  20750. "Go backward in time by the current span.
  20751. With prefix ARG, go backward that many times the current span."
  20752. (interactive "p")
  20753. (org-agenda-later (- arg)))
  20754. (defun org-agenda-day-view ()
  20755. "Switch to daily view for agenda."
  20756. (interactive)
  20757. (setq org-agenda-ndays 1)
  20758. (org-agenda-change-time-span 'day))
  20759. (defun org-agenda-week-view ()
  20760. "Switch to daily view for agenda."
  20761. (interactive)
  20762. (setq org-agenda-ndays 7)
  20763. (org-agenda-change-time-span 'week))
  20764. (defun org-agenda-month-view ()
  20765. "Switch to daily view for agenda."
  20766. (interactive)
  20767. (org-agenda-change-time-span 'month))
  20768. (defun org-agenda-year-view ()
  20769. "Switch to daily view for agenda."
  20770. (interactive)
  20771. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20772. (org-agenda-change-time-span 'year)
  20773. (error "Abort")))
  20774. (defun org-agenda-change-time-span (span)
  20775. "Change the agenda view to SPAN.
  20776. SPAN may be `day', `week', `month', `year'."
  20777. (org-agenda-check-type t 'agenda)
  20778. (if (equal org-agenda-span span)
  20779. (error "Viewing span is already \"%s\"" span))
  20780. (let* ((sd (or (get-text-property (point) 'day)
  20781. org-starting-day))
  20782. (computed (org-agenda-compute-time-span sd span))
  20783. (org-agenda-overriding-arguments
  20784. (list (car org-agenda-last-arguments)
  20785. (car computed) (cdr computed) t)))
  20786. (org-agenda-redo)
  20787. (org-agenda-find-same-or-today-or-agenda))
  20788. (org-agenda-set-mode-name)
  20789. (message "Switched to %s view" span))
  20790. (defun org-agenda-compute-time-span (sd span)
  20791. "Compute starting date and number of days for agenda.
  20792. SPAN may be `day', `week', `month', `year'. The return value
  20793. is a cons cell with the starting date and the number of days,
  20794. so that the date SD will be in that range."
  20795. (let* ((greg (calendar-gregorian-from-absolute sd))
  20796. nd)
  20797. (cond
  20798. ((eq span 'day)
  20799. (setq nd 1))
  20800. ((eq span 'week)
  20801. (let* ((nt (calendar-day-of-week
  20802. (calendar-gregorian-from-absolute sd)))
  20803. (d (if org-agenda-start-on-weekday
  20804. (- nt org-agenda-start-on-weekday)
  20805. 0)))
  20806. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20807. (setq nd 7)))
  20808. ((eq span 'month)
  20809. (setq sd (calendar-absolute-from-gregorian
  20810. (list (car greg) 1 (nth 2 greg)))
  20811. nd (- (calendar-absolute-from-gregorian
  20812. (list (1+ (car greg)) 1 (nth 2 greg)))
  20813. sd)))
  20814. ((eq span 'year)
  20815. (setq sd (calendar-absolute-from-gregorian
  20816. (list 1 1 (nth 2 greg)))
  20817. nd (- (calendar-absolute-from-gregorian
  20818. (list 1 1 (1+ (nth 2 greg))))
  20819. sd))))
  20820. (cons sd nd)))
  20821. ;; FIXME: does not work if user makes date format that starts with a blank
  20822. (defun org-agenda-next-date-line (&optional arg)
  20823. "Jump to the next line indicating a date in agenda buffer."
  20824. (interactive "p")
  20825. (org-agenda-check-type t 'agenda 'timeline)
  20826. (beginning-of-line 1)
  20827. (if (looking-at "^\\S-") (forward-char 1))
  20828. (if (not (re-search-forward "^\\S-" nil t arg))
  20829. (progn
  20830. (backward-char 1)
  20831. (error "No next date after this line in this buffer")))
  20832. (goto-char (match-beginning 0)))
  20833. (defun org-agenda-previous-date-line (&optional arg)
  20834. "Jump to the previous line indicating a date in agenda buffer."
  20835. (interactive "p")
  20836. (org-agenda-check-type t 'agenda 'timeline)
  20837. (beginning-of-line 1)
  20838. (if (not (re-search-backward "^\\S-" nil t arg))
  20839. (error "No previous date before this line in this buffer")))
  20840. ;; Initialize the highlight
  20841. (defvar org-hl (org-make-overlay 1 1))
  20842. (org-overlay-put org-hl 'face 'highlight)
  20843. (defun org-highlight (begin end &optional buffer)
  20844. "Highlight a region with overlay."
  20845. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20846. org-hl begin end (or buffer (current-buffer))))
  20847. (defun org-unhighlight ()
  20848. "Detach overlay INDEX."
  20849. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20850. ;; FIXME this is currently not used.
  20851. (defun org-highlight-until-next-command (beg end &optional buffer)
  20852. (org-highlight beg end buffer)
  20853. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20854. (defun org-unhighlight-once ()
  20855. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20856. (org-unhighlight))
  20857. (defun org-agenda-follow-mode ()
  20858. "Toggle follow mode in an agenda buffer."
  20859. (interactive)
  20860. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20861. (org-agenda-set-mode-name)
  20862. (message "Follow mode is %s"
  20863. (if org-agenda-follow-mode "on" "off")))
  20864. (defun org-agenda-log-mode ()
  20865. "Toggle log mode in an agenda buffer."
  20866. (interactive)
  20867. (org-agenda-check-type t 'agenda 'timeline)
  20868. (setq org-agenda-show-log (not org-agenda-show-log))
  20869. (org-agenda-set-mode-name)
  20870. (org-agenda-redo)
  20871. (message "Log mode is %s"
  20872. (if org-agenda-show-log "on" "off")))
  20873. (defun org-agenda-toggle-diary ()
  20874. "Toggle diary inclusion in an agenda buffer."
  20875. (interactive)
  20876. (org-agenda-check-type t 'agenda)
  20877. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20878. (org-agenda-redo)
  20879. (org-agenda-set-mode-name)
  20880. (message "Diary inclusion turned %s"
  20881. (if org-agenda-include-diary "on" "off")))
  20882. (defun org-agenda-toggle-time-grid ()
  20883. "Toggle time grid in an agenda buffer."
  20884. (interactive)
  20885. (org-agenda-check-type t 'agenda)
  20886. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20887. (org-agenda-redo)
  20888. (org-agenda-set-mode-name)
  20889. (message "Time-grid turned %s"
  20890. (if org-agenda-use-time-grid "on" "off")))
  20891. (defun org-agenda-set-mode-name ()
  20892. "Set the mode name to indicate all the small mode settings."
  20893. (setq mode-name
  20894. (concat "Org-Agenda"
  20895. (if (equal org-agenda-ndays 1) " Day" "")
  20896. (if (equal org-agenda-ndays 7) " Week" "")
  20897. (if org-agenda-follow-mode " Follow" "")
  20898. (if org-agenda-include-diary " Diary" "")
  20899. (if org-agenda-use-time-grid " Grid" "")
  20900. (if org-agenda-show-log " Log" "")))
  20901. (force-mode-line-update))
  20902. (defun org-agenda-post-command-hook ()
  20903. (and (eolp) (not (bolp)) (backward-char 1))
  20904. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20905. (if (and org-agenda-follow-mode
  20906. (get-text-property (point) 'org-marker))
  20907. (org-agenda-show)))
  20908. (defun org-agenda-show-priority ()
  20909. "Show the priority of the current item.
  20910. This priority is composed of the main priority given with the [#A] cookies,
  20911. and by additional input from the age of a schedules or deadline entry."
  20912. (interactive)
  20913. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20914. (message "Priority is %d" (if pri pri -1000))))
  20915. (defun org-agenda-show-tags ()
  20916. "Show the tags applicable to the current item."
  20917. (interactive)
  20918. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20919. (if tags
  20920. (message "Tags are :%s:"
  20921. (org-no-properties (mapconcat 'identity tags ":")))
  20922. (message "No tags associated with this line"))))
  20923. (defun org-agenda-goto (&optional highlight)
  20924. "Go to the Org-mode file which contains the item at point."
  20925. (interactive)
  20926. (let* ((marker (or (get-text-property (point) 'org-marker)
  20927. (org-agenda-error)))
  20928. (buffer (marker-buffer marker))
  20929. (pos (marker-position marker)))
  20930. (switch-to-buffer-other-window buffer)
  20931. (widen)
  20932. (goto-char pos)
  20933. (when (org-mode-p)
  20934. (org-show-context 'agenda)
  20935. (save-excursion
  20936. (and (outline-next-heading)
  20937. (org-flag-heading nil)))) ; show the next heading
  20938. (recenter (/ (window-height) 2))
  20939. (run-hooks 'org-agenda-after-show-hook)
  20940. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20941. (defvar org-agenda-after-show-hook nil
  20942. "Normal hook run after an item has been shown from the agenda.
  20943. Point is in the buffer where the item originated.")
  20944. (defun org-agenda-kill ()
  20945. "Kill the entry or subtree belonging to the current agenda entry."
  20946. (interactive)
  20947. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20948. (let* ((marker (or (get-text-property (point) 'org-marker)
  20949. (org-agenda-error)))
  20950. (buffer (marker-buffer marker))
  20951. (pos (marker-position marker))
  20952. (type (get-text-property (point) 'type))
  20953. dbeg dend (n 0) conf)
  20954. (org-with-remote-undo buffer
  20955. (with-current-buffer buffer
  20956. (save-excursion
  20957. (goto-char pos)
  20958. (if (and (org-mode-p) (not (member type '("sexp"))))
  20959. (setq dbeg (progn (org-back-to-heading t) (point))
  20960. dend (org-end-of-subtree t t))
  20961. (setq dbeg (point-at-bol)
  20962. dend (min (point-max) (1+ (point-at-eol)))))
  20963. (goto-char dbeg)
  20964. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20965. (setq conf (or (eq t org-agenda-confirm-kill)
  20966. (and (numberp org-agenda-confirm-kill)
  20967. (> n org-agenda-confirm-kill))))
  20968. (and conf
  20969. (not (y-or-n-p
  20970. (format "Delete entry with %d lines in buffer \"%s\"? "
  20971. n (buffer-name buffer))))
  20972. (error "Abort"))
  20973. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20974. (with-current-buffer buffer (delete-region dbeg dend))
  20975. (message "Agenda item and source killed"))))
  20976. (defun org-agenda-archive ()
  20977. "Kill the entry or subtree belonging to the current agenda entry."
  20978. (interactive)
  20979. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20980. (let* ((marker (or (get-text-property (point) 'org-marker)
  20981. (org-agenda-error)))
  20982. (buffer (marker-buffer marker))
  20983. (pos (marker-position marker)))
  20984. (org-with-remote-undo buffer
  20985. (with-current-buffer buffer
  20986. (if (org-mode-p)
  20987. (save-excursion
  20988. (goto-char pos)
  20989. (org-remove-subtree-entries-from-agenda)
  20990. (org-back-to-heading t)
  20991. (org-archive-subtree))
  20992. (error "Archiving works only in Org-mode files"))))))
  20993. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20994. "Remove all lines in the agenda that correspond to a given subtree.
  20995. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20996. If this information is not given, the function uses the tree at point."
  20997. (let ((buf (or buf (current-buffer))) m p)
  20998. (save-excursion
  20999. (unless (and beg end)
  21000. (org-back-to-heading t)
  21001. (setq beg (point))
  21002. (org-end-of-subtree t)
  21003. (setq end (point)))
  21004. (set-buffer (get-buffer org-agenda-buffer-name))
  21005. (save-excursion
  21006. (goto-char (point-max))
  21007. (beginning-of-line 1)
  21008. (while (not (bobp))
  21009. (when (and (setq m (get-text-property (point) 'org-marker))
  21010. (equal buf (marker-buffer m))
  21011. (setq p (marker-position m))
  21012. (>= p beg)
  21013. (<= p end))
  21014. (let ((inhibit-read-only t))
  21015. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21016. (beginning-of-line 0))))))
  21017. (defun org-agenda-open-link ()
  21018. "Follow the link in the current line, if any."
  21019. (interactive)
  21020. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21021. (save-excursion
  21022. (save-restriction
  21023. (narrow-to-region (point-at-bol) (point-at-eol))
  21024. (org-open-at-point))))
  21025. (defun org-agenda-copy-local-variable (var)
  21026. "Get a variable from a referenced buffer and install it here."
  21027. (let ((m (get-text-property (point) 'org-marker)))
  21028. (when (and m (buffer-live-p (marker-buffer m)))
  21029. (org-set-local var (with-current-buffer (marker-buffer m)
  21030. (symbol-value var))))))
  21031. (defun org-agenda-switch-to (&optional delete-other-windows)
  21032. "Go to the Org-mode file which contains the item at point."
  21033. (interactive)
  21034. (let* ((marker (or (get-text-property (point) 'org-marker)
  21035. (org-agenda-error)))
  21036. (buffer (marker-buffer marker))
  21037. (pos (marker-position marker)))
  21038. (switch-to-buffer buffer)
  21039. (and delete-other-windows (delete-other-windows))
  21040. (widen)
  21041. (goto-char pos)
  21042. (when (org-mode-p)
  21043. (org-show-context 'agenda)
  21044. (save-excursion
  21045. (and (outline-next-heading)
  21046. (org-flag-heading nil)))))) ; show the next heading
  21047. (defun org-agenda-goto-mouse (ev)
  21048. "Go to the Org-mode file which contains the item at the mouse click."
  21049. (interactive "e")
  21050. (mouse-set-point ev)
  21051. (org-agenda-goto))
  21052. (defun org-agenda-show ()
  21053. "Display the Org-mode file which contains the item at point."
  21054. (interactive)
  21055. (let ((win (selected-window)))
  21056. (org-agenda-goto t)
  21057. (select-window win)))
  21058. (defun org-agenda-recenter (arg)
  21059. "Display the Org-mode file which contains the item at point and recenter."
  21060. (interactive "P")
  21061. (let ((win (selected-window)))
  21062. (org-agenda-goto t)
  21063. (recenter arg)
  21064. (select-window win)))
  21065. (defun org-agenda-show-mouse (ev)
  21066. "Display the Org-mode file which contains the item at the mouse click."
  21067. (interactive "e")
  21068. (mouse-set-point ev)
  21069. (org-agenda-show))
  21070. (defun org-agenda-check-no-diary ()
  21071. "Check if the entry is a diary link and abort if yes."
  21072. (if (get-text-property (point) 'org-agenda-diary-link)
  21073. (org-agenda-error)))
  21074. (defun org-agenda-error ()
  21075. (error "Command not allowed in this line"))
  21076. (defun org-agenda-tree-to-indirect-buffer ()
  21077. "Show the subtree corresponding to the current entry in an indirect buffer.
  21078. This calls the command `org-tree-to-indirect-buffer' from the original
  21079. Org-mode buffer.
  21080. With numerical prefix arg ARG, go up to this level and then take that tree.
  21081. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21082. dedicated frame)."
  21083. (interactive)
  21084. (org-agenda-check-no-diary)
  21085. (let* ((marker (or (get-text-property (point) 'org-marker)
  21086. (org-agenda-error)))
  21087. (buffer (marker-buffer marker))
  21088. (pos (marker-position marker)))
  21089. (with-current-buffer buffer
  21090. (save-excursion
  21091. (goto-char pos)
  21092. (call-interactively 'org-tree-to-indirect-buffer)))))
  21093. (defvar org-last-heading-marker (make-marker)
  21094. "Marker pointing to the headline that last changed its TODO state
  21095. by a remote command from the agenda.")
  21096. (defun org-agenda-todo-nextset ()
  21097. "Switch TODO entry to next sequence."
  21098. (interactive)
  21099. (org-agenda-todo 'nextset))
  21100. (defun org-agenda-todo-previousset ()
  21101. "Switch TODO entry to previous sequence."
  21102. (interactive)
  21103. (org-agenda-todo 'previousset))
  21104. (defun org-agenda-todo (&optional arg)
  21105. "Cycle TODO state of line at point, also in Org-mode file.
  21106. This changes the line at point, all other lines in the agenda referring to
  21107. the same tree node, and the headline of the tree node in the Org-mode file."
  21108. (interactive "P")
  21109. (org-agenda-check-no-diary)
  21110. (let* ((col (current-column))
  21111. (marker (or (get-text-property (point) 'org-marker)
  21112. (org-agenda-error)))
  21113. (buffer (marker-buffer marker))
  21114. (pos (marker-position marker))
  21115. (hdmarker (get-text-property (point) 'org-hd-marker))
  21116. (inhibit-read-only t)
  21117. newhead)
  21118. (org-with-remote-undo buffer
  21119. (with-current-buffer buffer
  21120. (widen)
  21121. (goto-char pos)
  21122. (org-show-context 'agenda)
  21123. (save-excursion
  21124. (and (outline-next-heading)
  21125. (org-flag-heading nil))) ; show the next heading
  21126. (org-todo arg)
  21127. (and (bolp) (forward-char 1))
  21128. (setq newhead (org-get-heading))
  21129. (save-excursion
  21130. (org-back-to-heading)
  21131. (move-marker org-last-heading-marker (point))))
  21132. (beginning-of-line 1)
  21133. (save-excursion
  21134. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21135. (move-to-column col))))
  21136. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21137. "Change all lines in the agenda buffer which match HDMARKER.
  21138. The new content of the line will be NEWHEAD (as modified by
  21139. `org-format-agenda-item'). HDMARKER is checked with
  21140. `equal' against all `org-hd-marker' text properties in the file.
  21141. If FIXFACE is non-nil, the face of each item is modified acording to
  21142. the new TODO state."
  21143. (let* ((inhibit-read-only t)
  21144. props m pl undone-face done-face finish new dotime cat tags)
  21145. (save-excursion
  21146. (goto-char (point-max))
  21147. (beginning-of-line 1)
  21148. (while (not finish)
  21149. (setq finish (bobp))
  21150. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21151. (equal m hdmarker))
  21152. (setq props (text-properties-at (point))
  21153. dotime (get-text-property (point) 'dotime)
  21154. cat (get-text-property (point) 'org-category)
  21155. tags (get-text-property (point) 'tags)
  21156. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21157. pl (get-text-property (point) 'prefix-length)
  21158. undone-face (get-text-property (point) 'undone-face)
  21159. done-face (get-text-property (point) 'done-face))
  21160. (move-to-column pl)
  21161. (cond
  21162. ((equal new "")
  21163. (beginning-of-line 1)
  21164. (and (looking-at ".*\n?") (replace-match "")))
  21165. ((looking-at ".*")
  21166. (replace-match new t t)
  21167. (beginning-of-line 1)
  21168. (add-text-properties (point-at-bol) (point-at-eol) props)
  21169. (when fixface
  21170. (add-text-properties
  21171. (point-at-bol) (point-at-eol)
  21172. (list 'face
  21173. (if org-last-todo-state-is-todo
  21174. undone-face done-face))))
  21175. (org-agenda-highlight-todo 'line)
  21176. (beginning-of-line 1))
  21177. (t (error "Line update did not work"))))
  21178. (beginning-of-line 0)))
  21179. (org-finalize-agenda)))
  21180. (defun org-agenda-align-tags (&optional line)
  21181. "Align all tags in agenda items to `org-agenda-tags-column'."
  21182. (let ((inhibit-read-only t) l c)
  21183. (save-excursion
  21184. (goto-char (if line (point-at-bol) (point-min)))
  21185. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21186. (if line (point-at-eol) nil) t)
  21187. (add-text-properties
  21188. (match-beginning 2) (match-end 2)
  21189. (list 'face (list 'org-tag (get-text-property
  21190. (match-beginning 2) 'face))))
  21191. (setq l (- (match-end 2) (match-beginning 2))
  21192. c (if (< org-agenda-tags-column 0)
  21193. (- (abs org-agenda-tags-column) l)
  21194. org-agenda-tags-column))
  21195. (delete-region (match-beginning 1) (match-end 1))
  21196. (goto-char (match-beginning 1))
  21197. (insert (org-add-props
  21198. (make-string (max 1 (- c (current-column))) ?\ )
  21199. (text-properties-at (point))))))))
  21200. (defun org-agenda-priority-up ()
  21201. "Increase the priority of line at point, also in Org-mode file."
  21202. (interactive)
  21203. (org-agenda-priority 'up))
  21204. (defun org-agenda-priority-down ()
  21205. "Decrease the priority of line at point, also in Org-mode file."
  21206. (interactive)
  21207. (org-agenda-priority 'down))
  21208. (defun org-agenda-priority (&optional force-direction)
  21209. "Set the priority of line at point, also in Org-mode file.
  21210. This changes the line at point, all other lines in the agenda referring to
  21211. the same tree node, and the headline of the tree node in the Org-mode file."
  21212. (interactive)
  21213. (org-agenda-check-no-diary)
  21214. (let* ((marker (or (get-text-property (point) 'org-marker)
  21215. (org-agenda-error)))
  21216. (hdmarker (get-text-property (point) 'org-hd-marker))
  21217. (buffer (marker-buffer hdmarker))
  21218. (pos (marker-position hdmarker))
  21219. (inhibit-read-only t)
  21220. newhead)
  21221. (org-with-remote-undo buffer
  21222. (with-current-buffer buffer
  21223. (widen)
  21224. (goto-char pos)
  21225. (org-show-context 'agenda)
  21226. (save-excursion
  21227. (and (outline-next-heading)
  21228. (org-flag-heading nil))) ; show the next heading
  21229. (funcall 'org-priority force-direction)
  21230. (end-of-line 1)
  21231. (setq newhead (org-get-heading)))
  21232. (org-agenda-change-all-lines newhead hdmarker)
  21233. (beginning-of-line 1))))
  21234. (defun org-get-tags-at (&optional pos)
  21235. "Get a list of all headline tags applicable at POS.
  21236. POS defaults to point. If tags are inherited, the list contains
  21237. the targets in the same sequence as the headlines appear, i.e.
  21238. the tags of the current headline come last."
  21239. (interactive)
  21240. (let (tags lastpos)
  21241. (save-excursion
  21242. (save-restriction
  21243. (widen)
  21244. (goto-char (or pos (point)))
  21245. (save-match-data
  21246. (org-back-to-heading t)
  21247. (condition-case nil
  21248. (while (not (equal lastpos (point)))
  21249. (setq lastpos (point))
  21250. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21251. (setq tags (append (org-split-string
  21252. (org-match-string-no-properties 1) ":")
  21253. tags)))
  21254. (or org-use-tag-inheritance (error ""))
  21255. (org-up-heading-all 1))
  21256. (error nil))))
  21257. tags)))
  21258. ;; FIXME: should fix the tags property of the agenda line.
  21259. (defun org-agenda-set-tags ()
  21260. "Set tags for the current headline."
  21261. (interactive)
  21262. (org-agenda-check-no-diary)
  21263. (if (and (org-region-active-p) (interactive-p))
  21264. (call-interactively 'org-change-tag-in-region)
  21265. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21266. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21267. (org-agenda-error)))
  21268. (buffer (marker-buffer hdmarker))
  21269. (pos (marker-position hdmarker))
  21270. (inhibit-read-only t)
  21271. newhead)
  21272. (org-with-remote-undo buffer
  21273. (with-current-buffer buffer
  21274. (widen)
  21275. (goto-char pos)
  21276. (save-excursion
  21277. (org-show-context 'agenda))
  21278. (save-excursion
  21279. (and (outline-next-heading)
  21280. (org-flag-heading nil))) ; show the next heading
  21281. (goto-char pos)
  21282. (call-interactively 'org-set-tags)
  21283. (end-of-line 1)
  21284. (setq newhead (org-get-heading)))
  21285. (org-agenda-change-all-lines newhead hdmarker)
  21286. (beginning-of-line 1)))))
  21287. (defun org-agenda-toggle-archive-tag ()
  21288. "Toggle the archive tag for the current entry."
  21289. (interactive)
  21290. (org-agenda-check-no-diary)
  21291. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21292. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21293. (org-agenda-error)))
  21294. (buffer (marker-buffer hdmarker))
  21295. (pos (marker-position hdmarker))
  21296. (inhibit-read-only t)
  21297. newhead)
  21298. (org-with-remote-undo buffer
  21299. (with-current-buffer buffer
  21300. (widen)
  21301. (goto-char pos)
  21302. (org-show-context 'agenda)
  21303. (save-excursion
  21304. (and (outline-next-heading)
  21305. (org-flag-heading nil))) ; show the next heading
  21306. (call-interactively 'org-toggle-archive-tag)
  21307. (end-of-line 1)
  21308. (setq newhead (org-get-heading)))
  21309. (org-agenda-change-all-lines newhead hdmarker)
  21310. (beginning-of-line 1))))
  21311. (defun org-agenda-date-later (arg &optional what)
  21312. "Change the date of this item to one day later."
  21313. (interactive "p")
  21314. (org-agenda-check-type t 'agenda 'timeline)
  21315. (org-agenda-check-no-diary)
  21316. (let* ((marker (or (get-text-property (point) 'org-marker)
  21317. (org-agenda-error)))
  21318. (buffer (marker-buffer marker))
  21319. (pos (marker-position marker)))
  21320. (org-with-remote-undo buffer
  21321. (with-current-buffer buffer
  21322. (widen)
  21323. (goto-char pos)
  21324. (if (not (org-at-timestamp-p))
  21325. (error "Cannot find time stamp"))
  21326. (org-timestamp-change arg (or what 'day)))
  21327. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21328. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21329. (defun org-agenda-date-earlier (arg &optional what)
  21330. "Change the date of this item to one day earlier."
  21331. (interactive "p")
  21332. (org-agenda-date-later (- arg) what))
  21333. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21334. "Show new date stamp via text properties."
  21335. ;; We use text properties to make this undoable
  21336. (let ((inhibit-read-only t))
  21337. (setq stamp (concat " " prefix " => " stamp))
  21338. (save-excursion
  21339. (goto-char (point-max))
  21340. (while (not (bobp))
  21341. (when (equal marker (get-text-property (point) 'org-marker))
  21342. (move-to-column (- (window-width) (length stamp)) t)
  21343. (if (featurep 'xemacs)
  21344. ;; Use `duplicable' property to trigger undo recording
  21345. (let ((ex (make-extent nil nil))
  21346. (gl (make-glyph stamp)))
  21347. (set-glyph-face gl 'secondary-selection)
  21348. (set-extent-properties
  21349. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21350. (insert-extent ex (1- (point)) (point-at-eol)))
  21351. (add-text-properties
  21352. (1- (point)) (point-at-eol)
  21353. (list 'display (org-add-props stamp nil
  21354. 'face 'secondary-selection))))
  21355. (beginning-of-line 1))
  21356. (beginning-of-line 0)))))
  21357. (defun org-agenda-date-prompt (arg)
  21358. "Change the date of this item. Date is prompted for, with default today.
  21359. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21360. be used to request time specification in the time stamp."
  21361. (interactive "P")
  21362. (org-agenda-check-type t 'agenda 'timeline)
  21363. (org-agenda-check-no-diary)
  21364. (let* ((marker (or (get-text-property (point) 'org-marker)
  21365. (org-agenda-error)))
  21366. (buffer (marker-buffer marker))
  21367. (pos (marker-position marker)))
  21368. (org-with-remote-undo buffer
  21369. (with-current-buffer buffer
  21370. (widen)
  21371. (goto-char pos)
  21372. (if (not (org-at-timestamp-p))
  21373. (error "Cannot find time stamp"))
  21374. (org-time-stamp arg)
  21375. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21376. (defun org-agenda-schedule (arg)
  21377. "Schedule the item at point."
  21378. (interactive "P")
  21379. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21380. (org-agenda-check-no-diary)
  21381. (let* ((marker (or (get-text-property (point) 'org-marker)
  21382. (org-agenda-error)))
  21383. (buffer (marker-buffer marker))
  21384. (pos (marker-position marker))
  21385. (org-insert-labeled-timestamps-at-point nil)
  21386. ts)
  21387. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21388. (set-marker-insertion-type marker t)
  21389. (org-with-remote-undo buffer
  21390. (with-current-buffer buffer
  21391. (widen)
  21392. (goto-char pos)
  21393. (setq ts (org-schedule arg)))
  21394. (org-agenda-show-new-time marker ts "S"))
  21395. (message "Item scheduled for %s" ts)))
  21396. (defun org-agenda-deadline (arg)
  21397. "Schedule the item at point."
  21398. (interactive "P")
  21399. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21400. (org-agenda-check-no-diary)
  21401. (let* ((marker (or (get-text-property (point) 'org-marker)
  21402. (org-agenda-error)))
  21403. (buffer (marker-buffer marker))
  21404. (pos (marker-position marker))
  21405. (org-insert-labeled-timestamps-at-point nil)
  21406. ts)
  21407. (org-with-remote-undo buffer
  21408. (with-current-buffer buffer
  21409. (widen)
  21410. (goto-char pos)
  21411. (setq ts (org-deadline arg)))
  21412. (org-agenda-show-new-time marker ts "S"))
  21413. (message "Deadline for this item set to %s" ts)))
  21414. (defun org-get-heading (&optional no-tags)
  21415. "Return the heading of the current entry, without the stars."
  21416. (save-excursion
  21417. (org-back-to-heading t)
  21418. (if (looking-at
  21419. (if no-tags
  21420. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21421. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21422. (match-string 1) "")))
  21423. (defun org-agenda-clock-in (&optional arg)
  21424. "Start the clock on the currently selected item."
  21425. (interactive "P")
  21426. (org-agenda-check-no-diary)
  21427. (let* ((marker (or (get-text-property (point) 'org-marker)
  21428. (org-agenda-error)))
  21429. (pos (marker-position marker)))
  21430. (org-with-remote-undo (marker-buffer marker)
  21431. (with-current-buffer (marker-buffer marker)
  21432. (widen)
  21433. (goto-char pos)
  21434. (org-clock-in)))))
  21435. (defun org-agenda-clock-out (&optional arg)
  21436. "Stop the currently running clock."
  21437. (interactive "P")
  21438. (unless (marker-buffer org-clock-marker)
  21439. (error "No running clock"))
  21440. (org-with-remote-undo (marker-buffer org-clock-marker)
  21441. (org-clock-out)))
  21442. (defun org-agenda-clock-cancel (&optional arg)
  21443. "Cancel the currently running clock."
  21444. (interactive "P")
  21445. (unless (marker-buffer org-clock-marker)
  21446. (error "No running clock"))
  21447. (org-with-remote-undo (marker-buffer org-clock-marker)
  21448. (org-clock-cancel)))
  21449. (defun org-agenda-diary-entry ()
  21450. "Make a diary entry, like the `i' command from the calendar.
  21451. All the standard commands work: block, weekly etc."
  21452. (interactive)
  21453. (org-agenda-check-type t 'agenda 'timeline)
  21454. (require 'diary-lib)
  21455. (let* ((char (progn
  21456. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21457. (read-char-exclusive)))
  21458. (cmd (cdr (assoc char
  21459. '((?d . insert-diary-entry)
  21460. (?w . insert-weekly-diary-entry)
  21461. (?m . insert-monthly-diary-entry)
  21462. (?y . insert-yearly-diary-entry)
  21463. (?a . insert-anniversary-diary-entry)
  21464. (?b . insert-block-diary-entry)
  21465. (?c . insert-cyclic-diary-entry)))))
  21466. (oldf (symbol-function 'calendar-cursor-to-date))
  21467. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21468. (point (point))
  21469. (mark (or (mark t) (point))))
  21470. (unless cmd
  21471. (error "No command associated with <%c>" char))
  21472. (unless (and (get-text-property point 'day)
  21473. (or (not (equal ?b char))
  21474. (get-text-property mark 'day)))
  21475. (error "Don't know which date to use for diary entry"))
  21476. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21477. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21478. (let ((calendar-mark-ring
  21479. (list (calendar-gregorian-from-absolute
  21480. (or (get-text-property mark 'day)
  21481. (get-text-property point 'day))))))
  21482. (unwind-protect
  21483. (progn
  21484. (fset 'calendar-cursor-to-date
  21485. (lambda (&optional error)
  21486. (calendar-gregorian-from-absolute
  21487. (get-text-property point 'day))))
  21488. (call-interactively cmd))
  21489. (fset 'calendar-cursor-to-date oldf)))))
  21490. (defun org-agenda-execute-calendar-command (cmd)
  21491. "Execute a calendar command from the agenda, with the date associated to
  21492. the cursor position."
  21493. (org-agenda-check-type t 'agenda 'timeline)
  21494. (require 'diary-lib)
  21495. (unless (get-text-property (point) 'day)
  21496. (error "Don't know which date to use for calendar command"))
  21497. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21498. (point (point))
  21499. (date (calendar-gregorian-from-absolute
  21500. (get-text-property point 'day)))
  21501. ;; the following 3 vars are needed in the calendar
  21502. (displayed-day (extract-calendar-day date))
  21503. (displayed-month (extract-calendar-month date))
  21504. (displayed-year (extract-calendar-year date)))
  21505. (unwind-protect
  21506. (progn
  21507. (fset 'calendar-cursor-to-date
  21508. (lambda (&optional error)
  21509. (calendar-gregorian-from-absolute
  21510. (get-text-property point 'day))))
  21511. (call-interactively cmd))
  21512. (fset 'calendar-cursor-to-date oldf))))
  21513. (defun org-agenda-phases-of-moon ()
  21514. "Display the phases of the moon for the 3 months around the cursor date."
  21515. (interactive)
  21516. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21517. (defun org-agenda-holidays ()
  21518. "Display the holidays for the 3 months around the cursor date."
  21519. (interactive)
  21520. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21521. (defun org-agenda-sunrise-sunset (arg)
  21522. "Display sunrise and sunset for the cursor date.
  21523. Latitude and longitude can be specified with the variables
  21524. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21525. argument, latitude and longitude will be prompted for."
  21526. (interactive "P")
  21527. (let ((calendar-longitude (if arg nil calendar-longitude))
  21528. (calendar-latitude (if arg nil calendar-latitude))
  21529. (calendar-location-name
  21530. (if arg "the given coordinates" calendar-location-name)))
  21531. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21532. (defun org-agenda-goto-calendar ()
  21533. "Open the Emacs calendar with the date at the cursor."
  21534. (interactive)
  21535. (org-agenda-check-type t 'agenda 'timeline)
  21536. (let* ((day (or (get-text-property (point) 'day)
  21537. (error "Don't know which date to open in calendar")))
  21538. (date (calendar-gregorian-from-absolute day))
  21539. (calendar-move-hook nil)
  21540. (view-calendar-holidays-initially nil)
  21541. (view-diary-entries-initially nil))
  21542. (calendar)
  21543. (calendar-goto-date date)))
  21544. (defun org-calendar-goto-agenda ()
  21545. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21546. This is a command that has to be installed in `calendar-mode-map'."
  21547. (interactive)
  21548. (org-agenda-list nil (calendar-absolute-from-gregorian
  21549. (calendar-cursor-to-date))
  21550. nil))
  21551. (defun org-agenda-convert-date ()
  21552. (interactive)
  21553. (org-agenda-check-type t 'agenda 'timeline)
  21554. (let ((day (get-text-property (point) 'day))
  21555. date s)
  21556. (unless day
  21557. (error "Don't know which date to convert"))
  21558. (setq date (calendar-gregorian-from-absolute day))
  21559. (setq s (concat
  21560. "Gregorian: " (calendar-date-string date) "\n"
  21561. "ISO: " (calendar-iso-date-string date) "\n"
  21562. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21563. "Julian: " (calendar-julian-date-string date) "\n"
  21564. "Astron. JD: " (calendar-astro-date-string date)
  21565. " (Julian date number at noon UTC)\n"
  21566. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21567. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21568. "French: " (calendar-french-date-string date) "\n"
  21569. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21570. "Mayan: " (calendar-mayan-date-string date) "\n"
  21571. "Coptic: " (calendar-coptic-date-string date) "\n"
  21572. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21573. "Persian: " (calendar-persian-date-string date) "\n"
  21574. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21575. (with-output-to-temp-buffer "*Dates*"
  21576. (princ s))
  21577. (if (fboundp 'fit-window-to-buffer)
  21578. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21579. ;;;; Embedded LaTeX
  21580. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21581. "Keymap for the minor `org-cdlatex-mode'.")
  21582. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21583. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21584. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21585. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21586. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21587. (defvar org-cdlatex-texmathp-advice-is-done nil
  21588. "Flag remembering if we have applied the advice to texmathp already.")
  21589. (define-minor-mode org-cdlatex-mode
  21590. "Toggle the minor `org-cdlatex-mode'.
  21591. This mode supports entering LaTeX environment and math in LaTeX fragments
  21592. in Org-mode.
  21593. \\{org-cdlatex-mode-map}"
  21594. nil " OCDL" nil
  21595. (when org-cdlatex-mode (require 'cdlatex))
  21596. (unless org-cdlatex-texmathp-advice-is-done
  21597. (setq org-cdlatex-texmathp-advice-is-done t)
  21598. (defadvice texmathp (around org-math-always-on activate)
  21599. "Always return t in org-mode buffers.
  21600. This is because we want to insert math symbols without dollars even outside
  21601. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21602. en embedded LaTeX fragement, let texmathp do its job.
  21603. \\[org-cdlatex-mode-map]"
  21604. (interactive)
  21605. (let (p)
  21606. (cond
  21607. ((not (org-mode-p)) ad-do-it)
  21608. ((eq this-command 'cdlatex-math-symbol)
  21609. (setq ad-return-value t
  21610. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21611. (t
  21612. (let ((p (org-inside-LaTeX-fragment-p)))
  21613. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21614. (setq ad-return-value t
  21615. texmathp-why '("Org-mode embedded math" . 0))
  21616. (if p ad-do-it)))))))))
  21617. (defun turn-on-org-cdlatex ()
  21618. "Unconditionally turn on `org-cdlatex-mode'."
  21619. (org-cdlatex-mode 1))
  21620. (defun org-inside-LaTeX-fragment-p ()
  21621. "Test if point is inside a LaTeX fragment.
  21622. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21623. sequence appearing also before point.
  21624. Even though the matchers for math are configurable, this function assumes
  21625. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21626. delimiters are skipped when they have been removed by customization.
  21627. The return value is nil, or a cons cell with the delimiter and
  21628. and the position of this delimiter.
  21629. This function does a reasonably good job, but can locally be fooled by
  21630. for example currency specifications. For example it will assume being in
  21631. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21632. fragments that are properly closed, but during editing, we have to live
  21633. with the uncertainty caused by missing closing delimiters. This function
  21634. looks only before point, not after."
  21635. (catch 'exit
  21636. (let ((pos (point))
  21637. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21638. (lim (progn
  21639. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21640. (point)))
  21641. dd-on str (start 0) m re)
  21642. (goto-char pos)
  21643. (when dodollar
  21644. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21645. re (nth 1 (assoc "$" org-latex-regexps)))
  21646. (while (string-match re str start)
  21647. (cond
  21648. ((= (match-end 0) (length str))
  21649. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21650. ((= (match-end 0) (- (length str) 5))
  21651. (throw 'exit nil))
  21652. (t (setq start (match-end 0))))))
  21653. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21654. (goto-char pos)
  21655. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21656. (and (match-beginning 2) (throw 'exit nil))
  21657. ;; count $$
  21658. (while (re-search-backward "\\$\\$" lim t)
  21659. (setq dd-on (not dd-on)))
  21660. (goto-char pos)
  21661. (if dd-on (cons "$$" m))))))
  21662. (defun org-try-cdlatex-tab ()
  21663. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21664. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21665. - inside a LaTeX fragment, or
  21666. - after the first word in a line, where an abbreviation expansion could
  21667. insert a LaTeX environment."
  21668. (when org-cdlatex-mode
  21669. (cond
  21670. ((save-excursion
  21671. (skip-chars-backward "a-zA-Z0-9*")
  21672. (skip-chars-backward " \t")
  21673. (bolp))
  21674. (cdlatex-tab) t)
  21675. ((org-inside-LaTeX-fragment-p)
  21676. (cdlatex-tab) t)
  21677. (t nil))))
  21678. (defun org-cdlatex-underscore-caret (&optional arg)
  21679. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21680. Revert to the normal definition outside of these fragments."
  21681. (interactive "P")
  21682. (if (org-inside-LaTeX-fragment-p)
  21683. (call-interactively 'cdlatex-sub-superscript)
  21684. (let (org-cdlatex-mode)
  21685. (call-interactively (key-binding (vector last-input-event))))))
  21686. (defun org-cdlatex-math-modify (&optional arg)
  21687. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21688. Revert to the normal definition outside of these fragments."
  21689. (interactive "P")
  21690. (if (org-inside-LaTeX-fragment-p)
  21691. (call-interactively 'cdlatex-math-modify)
  21692. (let (org-cdlatex-mode)
  21693. (call-interactively (key-binding (vector last-input-event))))))
  21694. (defvar org-latex-fragment-image-overlays nil
  21695. "List of overlays carrying the images of latex fragments.")
  21696. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21697. (defun org-remove-latex-fragment-image-overlays ()
  21698. "Remove all overlays with LaTeX fragment images in current buffer."
  21699. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21700. (setq org-latex-fragment-image-overlays nil))
  21701. (defun org-preview-latex-fragment (&optional subtree)
  21702. "Preview the LaTeX fragment at point, or all locally or globally.
  21703. If the cursor is in a LaTeX fragment, create the image and overlay
  21704. it over the source code. If there is no fragment at point, display
  21705. all fragments in the current text, from one headline to the next. With
  21706. prefix SUBTREE, display all fragments in the current subtree. With a
  21707. double prefix `C-u C-u', or when the cursor is before the first headline,
  21708. display all fragments in the buffer.
  21709. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21710. (interactive "P")
  21711. (org-remove-latex-fragment-image-overlays)
  21712. (save-excursion
  21713. (save-restriction
  21714. (let (beg end at msg)
  21715. (cond
  21716. ((or (equal subtree '(16))
  21717. (not (save-excursion
  21718. (re-search-backward (concat "^" outline-regexp) nil t))))
  21719. (setq beg (point-min) end (point-max)
  21720. msg "Creating images for buffer...%s"))
  21721. ((equal subtree '(4))
  21722. (org-back-to-heading)
  21723. (setq beg (point) end (org-end-of-subtree t)
  21724. msg "Creating images for subtree...%s"))
  21725. (t
  21726. (if (setq at (org-inside-LaTeX-fragment-p))
  21727. (goto-char (max (point-min) (- (cdr at) 2)))
  21728. (org-back-to-heading))
  21729. (setq beg (point) end (progn (outline-next-heading) (point))
  21730. msg (if at "Creating image...%s"
  21731. "Creating images for entry...%s"))))
  21732. (message msg "")
  21733. (narrow-to-region beg end)
  21734. (goto-char beg)
  21735. (org-format-latex
  21736. (concat "ltxpng/" (file-name-sans-extension
  21737. (file-name-nondirectory
  21738. buffer-file-name)))
  21739. default-directory 'overlays msg at 'forbuffer)
  21740. (message msg "done. Use `C-c C-c' to remove images.")))))
  21741. (defvar org-latex-regexps
  21742. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21743. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21744. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21745. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21746. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21747. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21748. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21749. "Regular expressions for matching embedded LaTeX.")
  21750. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21751. "Replace LaTeX fragments with links to an image, and produce images."
  21752. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21753. (let* ((prefixnodir (file-name-nondirectory prefix))
  21754. (absprefix (expand-file-name prefix dir))
  21755. (todir (file-name-directory absprefix))
  21756. (opt org-format-latex-options)
  21757. (matchers (plist-get opt :matchers))
  21758. (re-list org-latex-regexps)
  21759. (cnt 0) txt link beg end re e checkdir
  21760. m n block linkfile movefile ov)
  21761. ;; Check if there are old images files with this prefix, and remove them
  21762. (when (file-directory-p todir)
  21763. (mapc 'delete-file
  21764. (directory-files
  21765. todir 'full
  21766. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21767. ;; Check the different regular expressions
  21768. (while (setq e (pop re-list))
  21769. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21770. block (if (nth 3 e) "\n\n" ""))
  21771. (when (member m matchers)
  21772. (goto-char (point-min))
  21773. (while (re-search-forward re nil t)
  21774. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21775. (setq txt (match-string n)
  21776. beg (match-beginning n) end (match-end n)
  21777. cnt (1+ cnt)
  21778. linkfile (format "%s_%04d.png" prefix cnt)
  21779. movefile (format "%s_%04d.png" absprefix cnt)
  21780. link (concat block "[[file:" linkfile "]]" block))
  21781. (if msg (message msg cnt))
  21782. (goto-char beg)
  21783. (unless checkdir ; make sure the directory exists
  21784. (setq checkdir t)
  21785. (or (file-directory-p todir) (make-directory todir)))
  21786. (org-create-formula-image
  21787. txt movefile opt forbuffer)
  21788. (if overlays
  21789. (progn
  21790. (setq ov (org-make-overlay beg end))
  21791. (if (featurep 'xemacs)
  21792. (progn
  21793. (org-overlay-put ov 'invisible t)
  21794. (org-overlay-put
  21795. ov 'end-glyph
  21796. (make-glyph (vector 'png :file movefile))))
  21797. (org-overlay-put
  21798. ov 'display
  21799. (list 'image :type 'png :file movefile :ascent 'center)))
  21800. (push ov org-latex-fragment-image-overlays)
  21801. (goto-char end))
  21802. (delete-region beg end)
  21803. (insert link))))))))
  21804. ;; This function borrows from Ganesh Swami's latex2png.el
  21805. (defun org-create-formula-image (string tofile options buffer)
  21806. (let* ((tmpdir (if (featurep 'xemacs)
  21807. (temp-directory)
  21808. temporary-file-directory))
  21809. (texfilebase (make-temp-name
  21810. (expand-file-name "orgtex" tmpdir)))
  21811. (texfile (concat texfilebase ".tex"))
  21812. (dvifile (concat texfilebase ".dvi"))
  21813. (pngfile (concat texfilebase ".png"))
  21814. (fnh (face-attribute 'default :height nil))
  21815. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21816. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21817. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21818. "Black"))
  21819. (bg (or (plist-get options (if buffer :background :html-background))
  21820. "Transparent")))
  21821. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21822. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21823. (with-temp-file texfile
  21824. (insert org-format-latex-header
  21825. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21826. (let ((dir default-directory))
  21827. (condition-case nil
  21828. (progn
  21829. (cd tmpdir)
  21830. (call-process "latex" nil nil nil texfile))
  21831. (error nil))
  21832. (cd dir))
  21833. (if (not (file-exists-p dvifile))
  21834. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21835. (call-process "dvipng" nil nil nil
  21836. "-E" "-fg" fg "-bg" bg
  21837. "-D" dpi
  21838. ;;"-x" scale "-y" scale
  21839. "-T" "tight"
  21840. "-o" pngfile
  21841. dvifile)
  21842. (if (not (file-exists-p pngfile))
  21843. (progn (message "Failed to create png file from %s" texfile) nil)
  21844. ;; Use the requested file name and clean up
  21845. (copy-file pngfile tofile 'replace)
  21846. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21847. (delete-file (concat texfilebase e)))
  21848. pngfile))))
  21849. (defun org-dvipng-color (attr)
  21850. "Return an rgb color specification for dvipng."
  21851. (apply 'format "rgb %s %s %s"
  21852. (mapcar 'org-normalize-color
  21853. (color-values (face-attribute 'default attr nil)))))
  21854. (defun org-normalize-color (value)
  21855. "Return string to be used as color value for an RGB component."
  21856. (format "%g" (/ value 65535.0)))
  21857. ;;;; Exporting
  21858. ;;; Variables, constants, and parameter plists
  21859. (defconst org-level-max 20)
  21860. (defvar org-export-html-preamble nil
  21861. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21862. (defvar org-export-html-postamble nil
  21863. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21864. (defvar org-export-html-auto-preamble t
  21865. "Should default preamble be inserted? Set by publishing functions.")
  21866. (defvar org-export-html-auto-postamble t
  21867. "Should default postamble be inserted? Set by publishing functions.")
  21868. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21869. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21870. (defconst org-export-plist-vars
  21871. '((:language . org-export-default-language)
  21872. (:customtime . org-display-custom-times)
  21873. (:headline-levels . org-export-headline-levels)
  21874. (:section-numbers . org-export-with-section-numbers)
  21875. (:table-of-contents . org-export-with-toc)
  21876. (:preserve-breaks . org-export-preserve-breaks)
  21877. (:archived-trees . org-export-with-archived-trees)
  21878. (:emphasize . org-export-with-emphasize)
  21879. (:sub-superscript . org-export-with-sub-superscripts)
  21880. (:special-strings . org-export-with-special-strings)
  21881. (:footnotes . org-export-with-footnotes)
  21882. (:drawers . org-export-with-drawers)
  21883. (:tags . org-export-with-tags)
  21884. (:TeX-macros . org-export-with-TeX-macros)
  21885. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21886. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21887. (:fixed-width . org-export-with-fixed-width)
  21888. (:timestamps . org-export-with-timestamps)
  21889. (:author-info . org-export-author-info)
  21890. (:time-stamp-file . org-export-time-stamp-file)
  21891. (:tables . org-export-with-tables)
  21892. (:table-auto-headline . org-export-highlight-first-table-line)
  21893. (:style . org-export-html-style)
  21894. (:agenda-style . org-agenda-export-html-style)
  21895. (:convert-org-links . org-export-html-link-org-files-as-html)
  21896. (:inline-images . org-export-html-inline-images)
  21897. (:html-extension . org-export-html-extension)
  21898. (:html-table-tag . org-export-html-table-tag)
  21899. (:expand-quoted-html . org-export-html-expand)
  21900. (:timestamp . org-export-html-with-timestamp)
  21901. (:publishing-directory . org-export-publishing-directory)
  21902. (:preamble . org-export-html-preamble)
  21903. (:postamble . org-export-html-postamble)
  21904. (:auto-preamble . org-export-html-auto-preamble)
  21905. (:auto-postamble . org-export-html-auto-postamble)
  21906. (:author . user-full-name)
  21907. (:email . user-mail-address)))
  21908. (defun org-default-export-plist ()
  21909. "Return the property list with default settings for the export variables."
  21910. (let ((l org-export-plist-vars) rtn e)
  21911. (while (setq e (pop l))
  21912. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21913. rtn))
  21914. (defun org-infile-export-plist ()
  21915. "Return the property list with file-local settings for export."
  21916. (save-excursion
  21917. (save-restriction
  21918. (widen)
  21919. (goto-char 0)
  21920. (let ((re (org-make-options-regexp
  21921. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21922. p key val text options)
  21923. (while (re-search-forward re nil t)
  21924. (setq key (org-match-string-no-properties 1)
  21925. val (org-match-string-no-properties 2))
  21926. (cond
  21927. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21928. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21929. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21930. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21931. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21932. ((string-equal key "TEXT")
  21933. (setq text (if text (concat text "\n" val) val)))
  21934. ((string-equal key "OPTIONS") (setq options val))))
  21935. (setq p (plist-put p :text text))
  21936. (when options
  21937. (let ((op '(("H" . :headline-levels)
  21938. ("num" . :section-numbers)
  21939. ("toc" . :table-of-contents)
  21940. ("\\n" . :preserve-breaks)
  21941. ("@" . :expand-quoted-html)
  21942. (":" . :fixed-width)
  21943. ("|" . :tables)
  21944. ("^" . :sub-superscript)
  21945. ("-" . :special-strings)
  21946. ("f" . :footnotes)
  21947. ("d" . :drawers)
  21948. ("tags" . :tags)
  21949. ("*" . :emphasize)
  21950. ("TeX" . :TeX-macros)
  21951. ("LaTeX" . :LaTeX-fragments)
  21952. ("skip" . :skip-before-1st-heading)
  21953. ("author" . :author-info)
  21954. ("timestamp" . :time-stamp-file)))
  21955. o)
  21956. (while (setq o (pop op))
  21957. (if (string-match (concat (regexp-quote (car o))
  21958. ":\\([^ \t\n\r;,.]*\\)")
  21959. options)
  21960. (setq p (plist-put p (cdr o)
  21961. (car (read-from-string
  21962. (match-string 1 options)))))))))
  21963. p))))
  21964. (defun org-export-directory (type plist)
  21965. (let* ((val (plist-get plist :publishing-directory))
  21966. (dir (if (listp val)
  21967. (or (cdr (assoc type val)) ".")
  21968. val)))
  21969. dir))
  21970. (defun org-skip-comments (lines)
  21971. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21972. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21973. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21974. (case-fold-search nil)
  21975. rtn line level)
  21976. (while (setq line (pop lines))
  21977. (cond
  21978. ((and (string-match re1 line)
  21979. (setq level (- (match-end 1) (match-beginning 1))))
  21980. ;; Beginning of a COMMENT subtree. Skip it.
  21981. (while (and (setq line (pop lines))
  21982. (or (not (string-match re2 line))
  21983. (> (- (match-end 1) (match-beginning 1)) level))))
  21984. (setq lines (cons line lines)))
  21985. ((string-match "^#" line)
  21986. ;; an ordinary comment line
  21987. )
  21988. ((and org-export-table-remove-special-lines
  21989. (string-match "^[ \t]*|" line)
  21990. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21991. (and (string-match "| *<[0-9]+> *|" line)
  21992. (not (string-match "| *[^ <|]" line)))))
  21993. ;; a special table line that should be removed
  21994. )
  21995. (t (setq rtn (cons line rtn)))))
  21996. (nreverse rtn)))
  21997. (defun org-export (&optional arg)
  21998. (interactive)
  21999. (let ((help "[t] insert the export option template
  22000. \[v] limit export to visible part of outline tree
  22001. \[a] export as ASCII
  22002. \[h] export as HTML
  22003. \[H] export as HTML to temporary buffer
  22004. \[R] export region as HTML
  22005. \[b] export as HTML and browse immediately
  22006. \[x] export as XOXO
  22007. \[l] export as LaTeX
  22008. \[L] export as LaTeX to temporary buffer
  22009. \[i] export current file as iCalendar file
  22010. \[I] export all agenda files as iCalendar files
  22011. \[c] export agenda files into combined iCalendar file
  22012. \[F] publish current file
  22013. \[P] publish current project
  22014. \[X] publish... (project will be prompted for)
  22015. \[A] publish all projects")
  22016. (cmds
  22017. '((?t . org-insert-export-options-template)
  22018. (?v . org-export-visible)
  22019. (?a . org-export-as-ascii)
  22020. (?h . org-export-as-html)
  22021. (?b . org-export-as-html-and-open)
  22022. (?H . org-export-as-html-to-buffer)
  22023. (?R . org-export-region-as-html)
  22024. (?x . org-export-as-xoxo)
  22025. (?l . org-export-as-latex)
  22026. (?L . org-export-as-latex-to-buffer)
  22027. (?i . org-export-icalendar-this-file)
  22028. (?I . org-export-icalendar-all-agenda-files)
  22029. (?c . org-export-icalendar-combine-agenda-files)
  22030. (?F . org-publish-current-file)
  22031. (?P . org-publish-current-project)
  22032. (?X . org-publish)
  22033. (?A . org-publish-all)))
  22034. r1 r2 ass)
  22035. (save-window-excursion
  22036. (delete-other-windows)
  22037. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22038. (princ help))
  22039. (message "Select command: ")
  22040. (setq r1 (read-char-exclusive)))
  22041. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22042. (if (setq ass (assq r2 cmds))
  22043. (call-interactively (cdr ass))
  22044. (error "No command associated with key %c" r1))))
  22045. (defconst org-html-entities
  22046. '(("nbsp")
  22047. ("iexcl")
  22048. ("cent")
  22049. ("pound")
  22050. ("curren")
  22051. ("yen")
  22052. ("brvbar")
  22053. ("vert" . "&#124;")
  22054. ("sect")
  22055. ("uml")
  22056. ("copy")
  22057. ("ordf")
  22058. ("laquo")
  22059. ("not")
  22060. ("shy")
  22061. ("reg")
  22062. ("macr")
  22063. ("deg")
  22064. ("plusmn")
  22065. ("sup2")
  22066. ("sup3")
  22067. ("acute")
  22068. ("micro")
  22069. ("para")
  22070. ("middot")
  22071. ("odot"."o")
  22072. ("star"."*")
  22073. ("cedil")
  22074. ("sup1")
  22075. ("ordm")
  22076. ("raquo")
  22077. ("frac14")
  22078. ("frac12")
  22079. ("frac34")
  22080. ("iquest")
  22081. ("Agrave")
  22082. ("Aacute")
  22083. ("Acirc")
  22084. ("Atilde")
  22085. ("Auml")
  22086. ("Aring") ("AA"."&Aring;")
  22087. ("AElig")
  22088. ("Ccedil")
  22089. ("Egrave")
  22090. ("Eacute")
  22091. ("Ecirc")
  22092. ("Euml")
  22093. ("Igrave")
  22094. ("Iacute")
  22095. ("Icirc")
  22096. ("Iuml")
  22097. ("ETH")
  22098. ("Ntilde")
  22099. ("Ograve")
  22100. ("Oacute")
  22101. ("Ocirc")
  22102. ("Otilde")
  22103. ("Ouml")
  22104. ("times")
  22105. ("Oslash")
  22106. ("Ugrave")
  22107. ("Uacute")
  22108. ("Ucirc")
  22109. ("Uuml")
  22110. ("Yacute")
  22111. ("THORN")
  22112. ("szlig")
  22113. ("agrave")
  22114. ("aacute")
  22115. ("acirc")
  22116. ("atilde")
  22117. ("auml")
  22118. ("aring")
  22119. ("aelig")
  22120. ("ccedil")
  22121. ("egrave")
  22122. ("eacute")
  22123. ("ecirc")
  22124. ("euml")
  22125. ("igrave")
  22126. ("iacute")
  22127. ("icirc")
  22128. ("iuml")
  22129. ("eth")
  22130. ("ntilde")
  22131. ("ograve")
  22132. ("oacute")
  22133. ("ocirc")
  22134. ("otilde")
  22135. ("ouml")
  22136. ("divide")
  22137. ("oslash")
  22138. ("ugrave")
  22139. ("uacute")
  22140. ("ucirc")
  22141. ("uuml")
  22142. ("yacute")
  22143. ("thorn")
  22144. ("yuml")
  22145. ("fnof")
  22146. ("Alpha")
  22147. ("Beta")
  22148. ("Gamma")
  22149. ("Delta")
  22150. ("Epsilon")
  22151. ("Zeta")
  22152. ("Eta")
  22153. ("Theta")
  22154. ("Iota")
  22155. ("Kappa")
  22156. ("Lambda")
  22157. ("Mu")
  22158. ("Nu")
  22159. ("Xi")
  22160. ("Omicron")
  22161. ("Pi")
  22162. ("Rho")
  22163. ("Sigma")
  22164. ("Tau")
  22165. ("Upsilon")
  22166. ("Phi")
  22167. ("Chi")
  22168. ("Psi")
  22169. ("Omega")
  22170. ("alpha")
  22171. ("beta")
  22172. ("gamma")
  22173. ("delta")
  22174. ("epsilon")
  22175. ("varepsilon"."&epsilon;")
  22176. ("zeta")
  22177. ("eta")
  22178. ("theta")
  22179. ("iota")
  22180. ("kappa")
  22181. ("lambda")
  22182. ("mu")
  22183. ("nu")
  22184. ("xi")
  22185. ("omicron")
  22186. ("pi")
  22187. ("rho")
  22188. ("sigmaf") ("varsigma"."&sigmaf;")
  22189. ("sigma")
  22190. ("tau")
  22191. ("upsilon")
  22192. ("phi")
  22193. ("chi")
  22194. ("psi")
  22195. ("omega")
  22196. ("thetasym") ("vartheta"."&thetasym;")
  22197. ("upsih")
  22198. ("piv")
  22199. ("bull") ("bullet"."&bull;")
  22200. ("hellip") ("dots"."&hellip;")
  22201. ("prime")
  22202. ("Prime")
  22203. ("oline")
  22204. ("frasl")
  22205. ("weierp")
  22206. ("image")
  22207. ("real")
  22208. ("trade")
  22209. ("alefsym")
  22210. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22211. ("uarr") ("uparrow"."&uarr;")
  22212. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22213. ("darr")("downarrow"."&darr;")
  22214. ("harr") ("leftrightarrow"."&harr;")
  22215. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22216. ("lArr") ("Leftarrow"."&lArr;")
  22217. ("uArr") ("Uparrow"."&uArr;")
  22218. ("rArr") ("Rightarrow"."&rArr;")
  22219. ("dArr") ("Downarrow"."&dArr;")
  22220. ("hArr") ("Leftrightarrow"."&hArr;")
  22221. ("forall")
  22222. ("part") ("partial"."&part;")
  22223. ("exist") ("exists"."&exist;")
  22224. ("empty") ("emptyset"."&empty;")
  22225. ("nabla")
  22226. ("isin") ("in"."&isin;")
  22227. ("notin")
  22228. ("ni")
  22229. ("prod")
  22230. ("sum")
  22231. ("minus")
  22232. ("lowast") ("ast"."&lowast;")
  22233. ("radic")
  22234. ("prop") ("proptp"."&prop;")
  22235. ("infin") ("infty"."&infin;")
  22236. ("ang") ("angle"."&ang;")
  22237. ("and") ("wedge"."&and;")
  22238. ("or") ("vee"."&or;")
  22239. ("cap")
  22240. ("cup")
  22241. ("int")
  22242. ("there4")
  22243. ("sim")
  22244. ("cong") ("simeq"."&cong;")
  22245. ("asymp")("approx"."&asymp;")
  22246. ("ne") ("neq"."&ne;")
  22247. ("equiv")
  22248. ("le")
  22249. ("ge")
  22250. ("sub") ("subset"."&sub;")
  22251. ("sup") ("supset"."&sup;")
  22252. ("nsub")
  22253. ("sube")
  22254. ("supe")
  22255. ("oplus")
  22256. ("otimes")
  22257. ("perp")
  22258. ("sdot") ("cdot"."&sdot;")
  22259. ("lceil")
  22260. ("rceil")
  22261. ("lfloor")
  22262. ("rfloor")
  22263. ("lang")
  22264. ("rang")
  22265. ("loz") ("Diamond"."&loz;")
  22266. ("spades") ("spadesuit"."&spades;")
  22267. ("clubs") ("clubsuit"."&clubs;")
  22268. ("hearts") ("diamondsuit"."&hearts;")
  22269. ("diams") ("diamondsuit"."&diams;")
  22270. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22271. ("quot")
  22272. ("amp")
  22273. ("lt")
  22274. ("gt")
  22275. ("OElig")
  22276. ("oelig")
  22277. ("Scaron")
  22278. ("scaron")
  22279. ("Yuml")
  22280. ("circ")
  22281. ("tilde")
  22282. ("ensp")
  22283. ("emsp")
  22284. ("thinsp")
  22285. ("zwnj")
  22286. ("zwj")
  22287. ("lrm")
  22288. ("rlm")
  22289. ("ndash")
  22290. ("mdash")
  22291. ("lsquo")
  22292. ("rsquo")
  22293. ("sbquo")
  22294. ("ldquo")
  22295. ("rdquo")
  22296. ("bdquo")
  22297. ("dagger")
  22298. ("Dagger")
  22299. ("permil")
  22300. ("lsaquo")
  22301. ("rsaquo")
  22302. ("euro")
  22303. ("arccos"."arccos")
  22304. ("arcsin"."arcsin")
  22305. ("arctan"."arctan")
  22306. ("arg"."arg")
  22307. ("cos"."cos")
  22308. ("cosh"."cosh")
  22309. ("cot"."cot")
  22310. ("coth"."coth")
  22311. ("csc"."csc")
  22312. ("deg"."deg")
  22313. ("det"."det")
  22314. ("dim"."dim")
  22315. ("exp"."exp")
  22316. ("gcd"."gcd")
  22317. ("hom"."hom")
  22318. ("inf"."inf")
  22319. ("ker"."ker")
  22320. ("lg"."lg")
  22321. ("lim"."lim")
  22322. ("liminf"."liminf")
  22323. ("limsup"."limsup")
  22324. ("ln"."ln")
  22325. ("log"."log")
  22326. ("max"."max")
  22327. ("min"."min")
  22328. ("Pr"."Pr")
  22329. ("sec"."sec")
  22330. ("sin"."sin")
  22331. ("sinh"."sinh")
  22332. ("sup"."sup")
  22333. ("tan"."tan")
  22334. ("tanh"."tanh")
  22335. )
  22336. "Entities for TeX->HTML translation.
  22337. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22338. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22339. In that case, \"\\ent\" will be translated to \"&other;\".
  22340. The list contains HTML entities for Latin-1, Greek and other symbols.
  22341. It is supplemented by a number of commonly used TeX macros with appropriate
  22342. translations. There is currently no way for users to extend this.")
  22343. ;;; General functions for all backends
  22344. (defun org-cleaned-string-for-export (string &rest parameters)
  22345. "Cleanup a buffer STRING so that links can be created safely."
  22346. (interactive)
  22347. (let* ((re-radio (and org-target-link-regexp
  22348. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22349. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22350. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22351. (re-archive (concat ":" org-archive-tag ":"))
  22352. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22353. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22354. (htmlp (plist-get parameters :for-html))
  22355. (asciip (plist-get parameters :for-ascii))
  22356. (latexp (plist-get parameters :for-LaTeX))
  22357. (commentsp (plist-get parameters :comments))
  22358. (archived-trees (plist-get parameters :archived-trees))
  22359. (inhibit-read-only t)
  22360. (drawers org-drawers)
  22361. (exp-drawers (plist-get parameters :drawers))
  22362. (outline-regexp "\\*+ ")
  22363. a b xx
  22364. rtn p)
  22365. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22366. (erase-buffer)
  22367. (insert string)
  22368. ;; Remove license-to-kill stuff
  22369. (while (setq p (text-property-any (point-min) (point-max)
  22370. :org-license-to-kill t))
  22371. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22372. (let ((org-inhibit-startup t)) (org-mode))
  22373. (untabify (point-min) (point-max))
  22374. ;; Get rid of drawers
  22375. (unless (eq t exp-drawers)
  22376. (goto-char (point-min))
  22377. (let ((re (concat "^[ \t]*:\\("
  22378. (mapconcat
  22379. 'identity
  22380. (org-delete-all exp-drawers
  22381. (copy-sequence drawers))
  22382. "\\|")
  22383. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22384. (while (re-search-forward re nil t)
  22385. (replace-match ""))))
  22386. ;; Get the correct stuff before the first headline
  22387. (when (plist-get parameters :skip-before-1st-heading)
  22388. (goto-char (point-min))
  22389. (when (re-search-forward "^\\*+[ \t]" nil t)
  22390. (delete-region (point-min) (match-beginning 0))
  22391. (goto-char (point-min))
  22392. (insert "\n")))
  22393. (when (plist-get parameters :add-text)
  22394. (goto-char (point-min))
  22395. (insert (plist-get parameters :add-text) "\n"))
  22396. ;; Get rid of archived trees
  22397. (when (not (eq archived-trees t))
  22398. (goto-char (point-min))
  22399. (while (re-search-forward re-archive nil t)
  22400. (if (not (org-on-heading-p t))
  22401. (org-end-of-subtree t)
  22402. (beginning-of-line 1)
  22403. (setq a (if archived-trees
  22404. (1+ (point-at-eol)) (point))
  22405. b (org-end-of-subtree t))
  22406. (if (> b a) (delete-region a b)))))
  22407. ;; Find targets in comments and move them out of comments,
  22408. ;; but mark them as targets that should be invisible
  22409. (goto-char (point-min))
  22410. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22411. (replace-match "\\1(INVISIBLE)"))
  22412. ;; Protect backend specific stuff, throw away the others.
  22413. (let ((formatters
  22414. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22415. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22416. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22417. fmt)
  22418. (goto-char (point-min))
  22419. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22420. (goto-char (match-end 0))
  22421. (while (not (looking-at "#\\+END_EXAMPLE"))
  22422. (insert ": ")
  22423. (beginning-of-line 2)))
  22424. (goto-char (point-min))
  22425. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22426. (add-text-properties (match-beginning 0) (match-end 0)
  22427. '(org-protected t)))
  22428. (while formatters
  22429. (setq fmt (pop formatters))
  22430. (when (car fmt)
  22431. (goto-char (point-min))
  22432. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22433. ":[ \t]*\\(.*\\)") nil t)
  22434. (replace-match "\\1" t)
  22435. (add-text-properties
  22436. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22437. '(org-protected t))))
  22438. (goto-char (point-min))
  22439. (while (re-search-forward
  22440. (concat "^#\\+"
  22441. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22442. (cadddr fmt) "\\>.*\n?") nil t)
  22443. (if (car fmt)
  22444. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22445. '(org-protected t))
  22446. (delete-region (match-beginning 0) (match-end 0))))))
  22447. ;; Protect quoted subtrees
  22448. (goto-char (point-min))
  22449. (while (re-search-forward re-quote nil t)
  22450. (goto-char (match-beginning 0))
  22451. (end-of-line 1)
  22452. (add-text-properties (point) (org-end-of-subtree t)
  22453. '(org-protected t)))
  22454. ;; Protect verbatim elements
  22455. (goto-char (point-min))
  22456. (while (re-search-forward org-verbatim-re nil t)
  22457. (add-text-properties (match-beginning 4) (match-end 4)
  22458. '(org-protected t))
  22459. (goto-char (1+ (match-end 4))))
  22460. ;; Remove subtrees that are commented
  22461. (goto-char (point-min))
  22462. (while (re-search-forward re-commented nil t)
  22463. (goto-char (match-beginning 0))
  22464. (delete-region (point) (org-end-of-subtree t)))
  22465. ;; Remove special table lines
  22466. (when org-export-table-remove-special-lines
  22467. (goto-char (point-min))
  22468. (while (re-search-forward "^[ \t]*|" nil t)
  22469. (beginning-of-line 1)
  22470. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22471. (and (looking-at ".*?| *<[0-9]+> *|")
  22472. (not (looking-at ".*?| *[^ <|]"))))
  22473. (delete-region (max (point-min) (1- (point-at-bol)))
  22474. (point-at-eol))
  22475. (end-of-line 1))))
  22476. ;; Specific LaTeX stuff
  22477. (when latexp
  22478. (require 'org-export-latex nil)
  22479. (org-export-latex-cleaned-string))
  22480. (when asciip
  22481. (org-export-ascii-clean-string))
  22482. ;; Specific HTML stuff
  22483. (when htmlp
  22484. ;; Convert LaTeX fragments to images
  22485. (when (plist-get parameters :LaTeX-fragments)
  22486. (org-format-latex
  22487. (concat "ltxpng/" (file-name-sans-extension
  22488. (file-name-nondirectory
  22489. org-current-export-file)))
  22490. org-current-export-dir nil "Creating LaTeX image %s"))
  22491. (message "Exporting..."))
  22492. ;; Remove or replace comments
  22493. (goto-char (point-min))
  22494. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22495. (if commentsp
  22496. (progn (add-text-properties
  22497. (match-beginning 0) (match-end 0) '(org-protected t))
  22498. (replace-match (format commentsp (match-string 1)) t t))
  22499. (replace-match "")))
  22500. ;; Find matches for radio targets and turn them into internal links
  22501. (goto-char (point-min))
  22502. (when re-radio
  22503. (while (re-search-forward re-radio nil t)
  22504. (org-if-unprotected
  22505. (replace-match "\\1[[\\2]]"))))
  22506. ;; Find all links that contain a newline and put them into a single line
  22507. (goto-char (point-min))
  22508. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22509. (org-if-unprotected
  22510. (replace-match "\\1 \\3")
  22511. (goto-char (match-beginning 0))))
  22512. ;; Normalize links: Convert angle and plain links into bracket links
  22513. ;; Expand link abbreviations
  22514. (goto-char (point-min))
  22515. (while (re-search-forward re-plain-link nil t)
  22516. (goto-char (1- (match-end 0)))
  22517. (org-if-unprotected
  22518. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22519. ":" (match-string 3) "]]")))
  22520. ;; added 'org-link face to links
  22521. (put-text-property 0 (length s) 'face 'org-link s)
  22522. (replace-match s t t))))
  22523. (goto-char (point-min))
  22524. (while (re-search-forward re-angle-link nil t)
  22525. (goto-char (1- (match-end 0)))
  22526. (org-if-unprotected
  22527. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22528. ":" (match-string 3) "]]")))
  22529. (put-text-property 0 (length s) 'face 'org-link s)
  22530. (replace-match s t t))))
  22531. (goto-char (point-min))
  22532. (while (re-search-forward org-bracket-link-regexp nil t)
  22533. (org-if-unprotected
  22534. (let* ((s (concat "[[" (setq xx (save-match-data
  22535. (org-link-expand-abbrev (match-string 1))))
  22536. "]"
  22537. (if (match-end 3)
  22538. (match-string 2)
  22539. (concat "[" xx "]"))
  22540. "]")))
  22541. (put-text-property 0 (length s) 'face 'org-link s)
  22542. (replace-match s t t))))
  22543. ;; Find multiline emphasis and put them into single line
  22544. (when (plist-get parameters :emph-multiline)
  22545. (goto-char (point-min))
  22546. (while (re-search-forward org-emph-re nil t)
  22547. (if (not (= (char-after (match-beginning 3))
  22548. (char-after (match-beginning 4))))
  22549. (org-if-unprotected
  22550. (subst-char-in-region (match-beginning 0) (match-end 0)
  22551. ?\n ?\ t)
  22552. (goto-char (1- (match-end 0))))
  22553. (goto-char (1+ (match-beginning 0))))))
  22554. (setq rtn (buffer-string)))
  22555. (kill-buffer " org-mode-tmp")
  22556. rtn))
  22557. (defun org-export-grab-title-from-buffer ()
  22558. "Get a title for the current document, from looking at the buffer."
  22559. (let ((inhibit-read-only t))
  22560. (save-excursion
  22561. (goto-char (point-min))
  22562. (let ((end (save-excursion (outline-next-heading) (point))))
  22563. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22564. ;; Mark the line so that it will not be exported as normal text.
  22565. (org-unmodified
  22566. (add-text-properties (match-beginning 0) (match-end 0)
  22567. (list :org-license-to-kill t)))
  22568. ;; Return the title string
  22569. (org-trim (match-string 0)))))))
  22570. (defun org-export-get-title-from-subtree ()
  22571. "Return subtree title and exclude it from export."
  22572. (let (title (m (mark)))
  22573. (save-excursion
  22574. (goto-char (region-beginning))
  22575. (when (and (org-at-heading-p)
  22576. (>= (org-end-of-subtree t t) (region-end)))
  22577. ;; This is a subtree, we take the title from the first heading
  22578. (goto-char (region-beginning))
  22579. (looking-at org-todo-line-regexp)
  22580. (setq title (match-string 3))
  22581. (org-unmodified
  22582. (add-text-properties (point) (1+ (point-at-eol))
  22583. (list :org-license-to-kill t)))))
  22584. title))
  22585. (defun org-solidify-link-text (s &optional alist)
  22586. "Take link text and make a safe target out of it."
  22587. (save-match-data
  22588. (let* ((rtn
  22589. (mapconcat
  22590. 'identity
  22591. (org-split-string s "[ \t\r\n]+") "--"))
  22592. (a (assoc rtn alist)))
  22593. (or (cdr a) rtn))))
  22594. (defun org-get-min-level (lines)
  22595. "Get the minimum level in LINES."
  22596. (let ((re "^\\(\\*+\\) ") l min)
  22597. (catch 'exit
  22598. (while (setq l (pop lines))
  22599. (if (string-match re l)
  22600. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22601. 1)))
  22602. ;; Variable holding the vector with section numbers
  22603. (defvar org-section-numbers (make-vector org-level-max 0))
  22604. (defun org-init-section-numbers ()
  22605. "Initialize the vector for the section numbers."
  22606. (let* ((level -1)
  22607. (numbers (nreverse (org-split-string "" "\\.")))
  22608. (depth (1- (length org-section-numbers)))
  22609. (i depth) number-string)
  22610. (while (>= i 0)
  22611. (if (> i level)
  22612. (aset org-section-numbers i 0)
  22613. (setq number-string (or (car numbers) "0"))
  22614. (if (string-match "\\`[A-Z]\\'" number-string)
  22615. (aset org-section-numbers i
  22616. (- (string-to-char number-string) ?A -1))
  22617. (aset org-section-numbers i (string-to-number number-string)))
  22618. (pop numbers))
  22619. (setq i (1- i)))))
  22620. (defun org-section-number (&optional level)
  22621. "Return a string with the current section number.
  22622. When LEVEL is non-nil, increase section numbers on that level."
  22623. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22624. (when level
  22625. (when (> level -1)
  22626. (aset org-section-numbers
  22627. level (1+ (aref org-section-numbers level))))
  22628. (setq idx (1+ level))
  22629. (while (<= idx depth)
  22630. (if (not (= idx 1))
  22631. (aset org-section-numbers idx 0))
  22632. (setq idx (1+ idx))))
  22633. (setq idx 0)
  22634. (while (<= idx depth)
  22635. (setq n (aref org-section-numbers idx))
  22636. (setq string (concat string (if (not (string= string "")) "." "")
  22637. (int-to-string n)))
  22638. (setq idx (1+ idx)))
  22639. (save-match-data
  22640. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22641. (setq string (replace-match "" t nil string)))
  22642. (if (string-match "\\(\\.0\\)+\\'" string)
  22643. (setq string (replace-match "" t nil string))))
  22644. string))
  22645. ;;; ASCII export
  22646. (defvar org-last-level nil) ; dynamically scoped variable
  22647. (defvar org-min-level nil) ; dynamically scoped variable
  22648. (defvar org-levels-open nil) ; dynamically scoped parameter
  22649. (defvar org-ascii-current-indentation nil) ; For communication
  22650. (defun org-export-as-ascii (arg)
  22651. "Export the outline as a pretty ASCII file.
  22652. If there is an active region, export only the region.
  22653. The prefix ARG specifies how many levels of the outline should become
  22654. underlined headlines. The default is 3."
  22655. (interactive "P")
  22656. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22657. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22658. (org-infile-export-plist)))
  22659. (region-p (org-region-active-p))
  22660. (subtree-p
  22661. (when region-p
  22662. (save-excursion
  22663. (goto-char (region-beginning))
  22664. (and (org-at-heading-p)
  22665. (>= (org-end-of-subtree t t) (region-end))))))
  22666. (custom-times org-display-custom-times)
  22667. (org-ascii-current-indentation '(0 . 0))
  22668. (level 0) line txt
  22669. (umax nil)
  22670. (umax-toc nil)
  22671. (case-fold-search nil)
  22672. (filename (concat (file-name-as-directory
  22673. (org-export-directory :ascii opt-plist))
  22674. (file-name-sans-extension
  22675. (or (and subtree-p
  22676. (org-entry-get (region-beginning)
  22677. "EXPORT_FILE_NAME" t))
  22678. (file-name-nondirectory buffer-file-name)))
  22679. ".txt"))
  22680. (filename (if (equal (file-truename filename)
  22681. (file-truename buffer-file-name))
  22682. (concat filename ".txt")
  22683. filename))
  22684. (buffer (find-file-noselect filename))
  22685. (org-levels-open (make-vector org-level-max nil))
  22686. (odd org-odd-levels-only)
  22687. (date (plist-get opt-plist :date))
  22688. (author (plist-get opt-plist :author))
  22689. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22690. (plist-get opt-plist :title)
  22691. (and (not
  22692. (plist-get opt-plist :skip-before-1st-heading))
  22693. (org-export-grab-title-from-buffer))
  22694. (file-name-sans-extension
  22695. (file-name-nondirectory buffer-file-name))))
  22696. (email (plist-get opt-plist :email))
  22697. (language (plist-get opt-plist :language))
  22698. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22699. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22700. (todo nil)
  22701. (lang-words nil)
  22702. (region
  22703. (buffer-substring
  22704. (if (org-region-active-p) (region-beginning) (point-min))
  22705. (if (org-region-active-p) (region-end) (point-max))))
  22706. (lines (org-split-string
  22707. (org-cleaned-string-for-export
  22708. region
  22709. :for-ascii t
  22710. :skip-before-1st-heading
  22711. (plist-get opt-plist :skip-before-1st-heading)
  22712. :drawers (plist-get opt-plist :drawers)
  22713. :verbatim-multiline t
  22714. :archived-trees
  22715. (plist-get opt-plist :archived-trees)
  22716. :add-text (plist-get opt-plist :text))
  22717. "\n"))
  22718. thetoc have-headings first-heading-pos
  22719. table-open table-buffer)
  22720. (let ((inhibit-read-only t))
  22721. (org-unmodified
  22722. (remove-text-properties (point-min) (point-max)
  22723. '(:org-license-to-kill t))))
  22724. (setq org-min-level (org-get-min-level lines))
  22725. (setq org-last-level org-min-level)
  22726. (org-init-section-numbers)
  22727. (find-file-noselect filename)
  22728. (setq lang-words (or (assoc language org-export-language-setup)
  22729. (assoc "en" org-export-language-setup)))
  22730. (switch-to-buffer-other-window buffer)
  22731. (erase-buffer)
  22732. (fundamental-mode)
  22733. ;; create local variables for all options, to make sure all called
  22734. ;; functions get the correct information
  22735. (mapc (lambda (x)
  22736. (set (make-local-variable (cdr x))
  22737. (plist-get opt-plist (car x))))
  22738. org-export-plist-vars)
  22739. (org-set-local 'org-odd-levels-only odd)
  22740. (setq umax (if arg (prefix-numeric-value arg)
  22741. org-export-headline-levels))
  22742. (setq umax-toc (if (integerp org-export-with-toc)
  22743. (min org-export-with-toc umax)
  22744. umax))
  22745. ;; File header
  22746. (if title (org-insert-centered title ?=))
  22747. (insert "\n")
  22748. (if (and (or author email)
  22749. org-export-author-info)
  22750. (insert (concat (nth 1 lang-words) ": " (or author "")
  22751. (if email (concat " <" email ">") "")
  22752. "\n")))
  22753. (cond
  22754. ((and date (string-match "%" date))
  22755. (setq date (format-time-string date (current-time))))
  22756. (date)
  22757. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22758. (if (and date org-export-time-stamp-file)
  22759. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22760. (insert "\n\n")
  22761. (if org-export-with-toc
  22762. (progn
  22763. (push (concat (nth 3 lang-words) "\n") thetoc)
  22764. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22765. (mapc '(lambda (line)
  22766. (if (string-match org-todo-line-regexp
  22767. line)
  22768. ;; This is a headline
  22769. (progn
  22770. (setq have-headings t)
  22771. (setq level (- (match-end 1) (match-beginning 1))
  22772. level (org-tr-level level)
  22773. txt (match-string 3 line)
  22774. todo
  22775. (or (and org-export-mark-todo-in-toc
  22776. (match-beginning 2)
  22777. (not (member (match-string 2 line)
  22778. org-done-keywords)))
  22779. ; TODO, not DONE
  22780. (and org-export-mark-todo-in-toc
  22781. (= level umax-toc)
  22782. (org-search-todo-below
  22783. line lines level))))
  22784. (setq txt (org-html-expand-for-ascii txt))
  22785. (while (string-match org-bracket-link-regexp txt)
  22786. (setq txt
  22787. (replace-match
  22788. (match-string (if (match-end 2) 3 1) txt)
  22789. t t txt)))
  22790. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22791. (string-match
  22792. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22793. txt))
  22794. (setq txt (replace-match "" t t txt)))
  22795. (if (string-match quote-re0 txt)
  22796. (setq txt (replace-match "" t t txt)))
  22797. (if org-export-with-section-numbers
  22798. (setq txt (concat (org-section-number level)
  22799. " " txt)))
  22800. (if (<= level umax-toc)
  22801. (progn
  22802. (push
  22803. (concat
  22804. (make-string
  22805. (* (max 0 (- level org-min-level)) 4) ?\ )
  22806. (format (if todo "%s (*)\n" "%s\n") txt))
  22807. thetoc)
  22808. (setq org-last-level level))
  22809. ))))
  22810. lines)
  22811. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22812. (org-init-section-numbers)
  22813. (while (setq line (pop lines))
  22814. ;; Remove the quoted HTML tags.
  22815. (setq line (org-html-expand-for-ascii line))
  22816. ;; Remove targets
  22817. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22818. (setq line (replace-match "" t t line)))
  22819. ;; Replace internal links
  22820. (while (string-match org-bracket-link-regexp line)
  22821. (setq line (replace-match
  22822. (if (match-end 3) "[\\3]" "[\\1]")
  22823. t nil line)))
  22824. (when custom-times
  22825. (setq line (org-translate-time line)))
  22826. (cond
  22827. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22828. ;; a Headline
  22829. (setq first-heading-pos (or first-heading-pos (point)))
  22830. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22831. txt (match-string 2 line))
  22832. (org-ascii-level-start level txt umax lines))
  22833. ((and org-export-with-tables
  22834. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22835. (if (not table-open)
  22836. ;; New table starts
  22837. (setq table-open t table-buffer nil))
  22838. ;; Accumulate lines
  22839. (setq table-buffer (cons line table-buffer))
  22840. (when (or (not lines)
  22841. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22842. (car lines))))
  22843. (setq table-open nil
  22844. table-buffer (nreverse table-buffer))
  22845. (insert (mapconcat
  22846. (lambda (x)
  22847. (org-fix-indentation x org-ascii-current-indentation))
  22848. (org-format-table-ascii table-buffer)
  22849. "\n") "\n")))
  22850. (t
  22851. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22852. (if (and org-export-with-fixed-width
  22853. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22854. (setq line (replace-match "\\1" nil nil line)))
  22855. (insert line "\n"))))
  22856. (normal-mode)
  22857. ;; insert the table of contents
  22858. (when thetoc
  22859. (goto-char (point-min))
  22860. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22861. (progn
  22862. (goto-char (match-beginning 0))
  22863. (replace-match ""))
  22864. (goto-char first-heading-pos))
  22865. (mapc 'insert thetoc)
  22866. (or (looking-at "[ \t]*\n[ \t]*\n")
  22867. (insert "\n\n")))
  22868. ;; Convert whitespace place holders
  22869. (goto-char (point-min))
  22870. (let (beg end)
  22871. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22872. (setq end (next-single-property-change beg 'org-whitespace))
  22873. (goto-char beg)
  22874. (delete-region beg end)
  22875. (insert (make-string (- end beg) ?\ ))))
  22876. (save-buffer)
  22877. ;; remove display and invisible chars
  22878. (let (beg end)
  22879. (goto-char (point-min))
  22880. (while (setq beg (next-single-property-change (point) 'display))
  22881. (setq end (next-single-property-change beg 'display))
  22882. (delete-region beg end)
  22883. (goto-char beg)
  22884. (insert "=>"))
  22885. (goto-char (point-min))
  22886. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22887. (setq end (next-single-property-change beg 'org-cwidth))
  22888. (delete-region beg end)
  22889. (goto-char beg)))
  22890. (goto-char (point-min))))
  22891. (defun org-export-ascii-clean-string ()
  22892. "Do extra work for ASCII export"
  22893. (goto-char (point-min))
  22894. (while (re-search-forward org-verbatim-re nil t)
  22895. (goto-char (match-end 2))
  22896. (backward-delete-char 1) (insert "'")
  22897. (goto-char (match-beginning 2))
  22898. (delete-char 1) (insert "`")
  22899. (goto-char (match-end 2))))
  22900. (defun org-search-todo-below (line lines level)
  22901. "Search the subtree below LINE for any TODO entries."
  22902. (let ((rest (cdr (memq line lines)))
  22903. (re org-todo-line-regexp)
  22904. line lv todo)
  22905. (catch 'exit
  22906. (while (setq line (pop rest))
  22907. (if (string-match re line)
  22908. (progn
  22909. (setq lv (- (match-end 1) (match-beginning 1))
  22910. todo (and (match-beginning 2)
  22911. (not (member (match-string 2 line)
  22912. org-done-keywords))))
  22913. ; TODO, not DONE
  22914. (if (<= lv level) (throw 'exit nil))
  22915. (if todo (throw 'exit t))))))))
  22916. (defun org-html-expand-for-ascii (line)
  22917. "Handle quoted HTML for ASCII export."
  22918. (if org-export-html-expand
  22919. (while (string-match "@<[^<>\n]*>" line)
  22920. ;; We just remove the tags for now.
  22921. (setq line (replace-match "" nil nil line))))
  22922. line)
  22923. (defun org-insert-centered (s &optional underline)
  22924. "Insert the string S centered and underline it with character UNDERLINE."
  22925. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22926. (insert (make-string ind ?\ ) s "\n")
  22927. (if underline
  22928. (insert (make-string ind ?\ )
  22929. (make-string (string-width s) underline)
  22930. "\n"))))
  22931. (defun org-ascii-level-start (level title umax &optional lines)
  22932. "Insert a new level in ASCII export."
  22933. (let (char (n (- level umax 1)) (ind 0))
  22934. (if (> level umax)
  22935. (progn
  22936. (insert (make-string (* 2 n) ?\ )
  22937. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22938. org-export-ascii-bullets))
  22939. " " title "\n")
  22940. ;; find the indentation of the next non-empty line
  22941. (catch 'stop
  22942. (while lines
  22943. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22944. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22945. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22946. (pop lines)))
  22947. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22948. (if (or (not (equal (char-before) ?\n))
  22949. (not (equal (char-before (1- (point))) ?\n)))
  22950. (insert "\n"))
  22951. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22952. (unless org-export-with-tags
  22953. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22954. (setq title (replace-match "" t t title))))
  22955. (if org-export-with-section-numbers
  22956. (setq title (concat (org-section-number level) " " title)))
  22957. (insert title "\n" (make-string (string-width title) char) "\n")
  22958. (setq org-ascii-current-indentation '(0 . 0)))))
  22959. (defun org-export-visible (type arg)
  22960. "Create a copy of the visible part of the current buffer, and export it.
  22961. The copy is created in a temporary buffer and removed after use.
  22962. TYPE is the final key (as a string) that also select the export command in
  22963. the `C-c C-e' export dispatcher.
  22964. As a special case, if the you type SPC at the prompt, the temporary
  22965. org-mode file will not be removed but presented to you so that you can
  22966. continue to use it. The prefix arg ARG is passed through to the exporting
  22967. command."
  22968. (interactive
  22969. (list (progn
  22970. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22971. (read-char-exclusive))
  22972. current-prefix-arg))
  22973. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22974. (error "Invalid export key"))
  22975. (let* ((binding (cdr (assoc type
  22976. '((?a . org-export-as-ascii)
  22977. (?\C-a . org-export-as-ascii)
  22978. (?b . org-export-as-html-and-open)
  22979. (?\C-b . org-export-as-html-and-open)
  22980. (?h . org-export-as-html)
  22981. (?H . org-export-as-html-to-buffer)
  22982. (?R . org-export-region-as-html)
  22983. (?x . org-export-as-xoxo)))))
  22984. (keepp (equal type ?\ ))
  22985. (file buffer-file-name)
  22986. (buffer (get-buffer-create "*Org Export Visible*"))
  22987. s e)
  22988. ;; Need to hack the drawers here.
  22989. (save-excursion
  22990. (goto-char (point-min))
  22991. (while (re-search-forward org-drawer-regexp nil t)
  22992. (goto-char (match-beginning 1))
  22993. (or (org-invisible-p) (org-flag-drawer nil))))
  22994. (with-current-buffer buffer (erase-buffer))
  22995. (save-excursion
  22996. (setq s (goto-char (point-min)))
  22997. (while (not (= (point) (point-max)))
  22998. (goto-char (org-find-invisible))
  22999. (append-to-buffer buffer s (point))
  23000. (setq s (goto-char (org-find-visible))))
  23001. (org-cycle-hide-drawers 'all)
  23002. (goto-char (point-min))
  23003. (unless keepp
  23004. ;; Copy all comment lines to the end, to make sure #+ settings are
  23005. ;; still available for the second export step. Kind of a hack, but
  23006. ;; does do the trick.
  23007. (if (looking-at "#[^\r\n]*")
  23008. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23009. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23010. (append-to-buffer buffer (1+ (match-beginning 0))
  23011. (min (point-max) (1+ (match-end 0))))))
  23012. (set-buffer buffer)
  23013. (let ((buffer-file-name file)
  23014. (org-inhibit-startup t))
  23015. (org-mode)
  23016. (show-all)
  23017. (unless keepp (funcall binding arg))))
  23018. (if (not keepp)
  23019. (kill-buffer buffer)
  23020. (switch-to-buffer-other-window buffer)
  23021. (goto-char (point-min)))))
  23022. (defun org-find-visible ()
  23023. (let ((s (point)))
  23024. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23025. (get-char-property s 'invisible)))
  23026. s))
  23027. (defun org-find-invisible ()
  23028. (let ((s (point)))
  23029. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23030. (not (get-char-property s 'invisible))))
  23031. s))
  23032. ;;; HTML export
  23033. (defun org-get-current-options ()
  23034. "Return a string with current options as keyword options.
  23035. Does include HTML export options as well as TODO and CATEGORY stuff."
  23036. (format
  23037. "#+TITLE: %s
  23038. #+AUTHOR: %s
  23039. #+EMAIL: %s
  23040. #+LANGUAGE: %s
  23041. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23042. #+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
  23043. #+CATEGORY: %s
  23044. #+SEQ_TODO: %s
  23045. #+TYP_TODO: %s
  23046. #+PRIORITIES: %c %c %c
  23047. #+DRAWERS: %s
  23048. #+STARTUP: %s %s %s %s %s
  23049. #+TAGS: %s
  23050. #+ARCHIVE: %s
  23051. #+LINK: %s
  23052. "
  23053. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23054. org-export-headline-levels
  23055. org-export-with-section-numbers
  23056. org-export-with-toc
  23057. org-export-preserve-breaks
  23058. org-export-html-expand
  23059. org-export-with-fixed-width
  23060. org-export-with-tables
  23061. org-export-with-sub-superscripts
  23062. org-export-with-special-strings
  23063. org-export-with-footnotes
  23064. org-export-with-emphasize
  23065. org-export-with-TeX-macros
  23066. org-export-with-LaTeX-fragments
  23067. org-export-skip-text-before-1st-heading
  23068. org-export-with-drawers
  23069. org-export-with-tags
  23070. (file-name-nondirectory buffer-file-name)
  23071. "TODO FEEDBACK VERIFY DONE"
  23072. "Me Jason Marie DONE"
  23073. org-highest-priority org-lowest-priority org-default-priority
  23074. (mapconcat 'identity org-drawers " ")
  23075. (cdr (assoc org-startup-folded
  23076. '((nil . "showall") (t . "overview") (content . "content"))))
  23077. (if org-odd-levels-only "odd" "oddeven")
  23078. (if org-hide-leading-stars "hidestars" "showstars")
  23079. (if org-startup-align-all-tables "align" "noalign")
  23080. (cond ((eq org-log-done t) "logdone")
  23081. ((equal org-log-done 'note) "lognotedone")
  23082. ((not org-log-done) "nologdone"))
  23083. (or (mapconcat (lambda (x)
  23084. (cond
  23085. ((equal '(:startgroup) x) "{")
  23086. ((equal '(:endgroup) x) "}")
  23087. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23088. (t (car x))))
  23089. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23090. org-archive-location
  23091. "org file:~/org/%s.org"
  23092. ))
  23093. (defun org-insert-export-options-template ()
  23094. "Insert into the buffer a template with information for exporting."
  23095. (interactive)
  23096. (if (not (bolp)) (newline))
  23097. (let ((s (org-get-current-options)))
  23098. (and (string-match "#\\+CATEGORY" s)
  23099. (setq s (substring s 0 (match-beginning 0))))
  23100. (insert s)))
  23101. (defun org-toggle-fixed-width-section (arg)
  23102. "Toggle the fixed-width export.
  23103. If there is no active region, the QUOTE keyword at the current headline is
  23104. inserted or removed. When present, it causes the text between this headline
  23105. and the next to be exported as fixed-width text, and unmodified.
  23106. If there is an active region, this command adds or removes a colon as the
  23107. first character of this line. If the first character of a line is a colon,
  23108. this line is also exported in fixed-width font."
  23109. (interactive "P")
  23110. (let* ((cc 0)
  23111. (regionp (org-region-active-p))
  23112. (beg (if regionp (region-beginning) (point)))
  23113. (end (if regionp (region-end)))
  23114. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23115. (case-fold-search nil)
  23116. (re "[ \t]*\\(:\\)")
  23117. off)
  23118. (if regionp
  23119. (save-excursion
  23120. (goto-char beg)
  23121. (setq cc (current-column))
  23122. (beginning-of-line 1)
  23123. (setq off (looking-at re))
  23124. (while (> nlines 0)
  23125. (setq nlines (1- nlines))
  23126. (beginning-of-line 1)
  23127. (cond
  23128. (arg
  23129. (move-to-column cc t)
  23130. (insert ":\n")
  23131. (forward-line -1))
  23132. ((and off (looking-at re))
  23133. (replace-match "" t t nil 1))
  23134. ((not off) (move-to-column cc t) (insert ":")))
  23135. (forward-line 1)))
  23136. (save-excursion
  23137. (org-back-to-heading)
  23138. (if (looking-at (concat outline-regexp
  23139. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23140. (replace-match "" t t nil 1)
  23141. (if (looking-at outline-regexp)
  23142. (progn
  23143. (goto-char (match-end 0))
  23144. (insert org-quote-string " "))))))))
  23145. (defun org-export-as-html-and-open (arg)
  23146. "Export the outline as HTML and immediately open it with a browser.
  23147. If there is an active region, export only the region.
  23148. The prefix ARG specifies how many levels of the outline should become
  23149. headlines. The default is 3. Lower levels will become bulleted lists."
  23150. (interactive "P")
  23151. (org-export-as-html arg 'hidden)
  23152. (org-open-file buffer-file-name))
  23153. (defun org-export-as-html-batch ()
  23154. "Call `org-export-as-html', may be used in batch processing as
  23155. emacs --batch
  23156. --load=$HOME/lib/emacs/org.el
  23157. --eval \"(setq org-export-headline-levels 2)\"
  23158. --visit=MyFile --funcall org-export-as-html-batch"
  23159. (org-export-as-html org-export-headline-levels 'hidden))
  23160. (defun org-export-as-html-to-buffer (arg)
  23161. "Call `org-exort-as-html` with output to a temporary buffer.
  23162. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23163. (interactive "P")
  23164. (org-export-as-html arg nil nil "*Org HTML Export*")
  23165. (switch-to-buffer-other-window "*Org HTML Export*"))
  23166. (defun org-replace-region-by-html (beg end)
  23167. "Assume the current region has org-mode syntax, and convert it to HTML.
  23168. This can be used in any buffer. For example, you could write an
  23169. itemized list in org-mode syntax in an HTML buffer and then use this
  23170. command to convert it."
  23171. (interactive "r")
  23172. (let (reg html buf pop-up-frames)
  23173. (save-window-excursion
  23174. (if (org-mode-p)
  23175. (setq html (org-export-region-as-html
  23176. beg end t 'string))
  23177. (setq reg (buffer-substring beg end)
  23178. buf (get-buffer-create "*Org tmp*"))
  23179. (with-current-buffer buf
  23180. (erase-buffer)
  23181. (insert reg)
  23182. (org-mode)
  23183. (setq html (org-export-region-as-html
  23184. (point-min) (point-max) t 'string)))
  23185. (kill-buffer buf)))
  23186. (delete-region beg end)
  23187. (insert html)))
  23188. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23189. "Convert region from BEG to END in org-mode buffer to HTML.
  23190. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23191. contents, and only produce the region of converted text, useful for
  23192. cut-and-paste operations.
  23193. If BUFFER is a buffer or a string, use/create that buffer as a target
  23194. of the converted HTML. If BUFFER is the symbol `string', return the
  23195. produced HTML as a string and leave not buffer behind. For example,
  23196. a Lisp program could call this function in the following way:
  23197. (setq html (org-export-region-as-html beg end t 'string))
  23198. When called interactively, the output buffer is selected, and shown
  23199. in a window. A non-interactive call will only retunr the buffer."
  23200. (interactive "r\nP")
  23201. (when (interactive-p)
  23202. (setq buffer "*Org HTML Export*"))
  23203. (let ((transient-mark-mode t) (zmacs-regions t)
  23204. rtn)
  23205. (goto-char end)
  23206. (set-mark (point)) ;; to activate the region
  23207. (goto-char beg)
  23208. (setq rtn (org-export-as-html
  23209. nil nil nil
  23210. buffer body-only))
  23211. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23212. (if (and (interactive-p) (bufferp rtn))
  23213. (switch-to-buffer-other-window rtn)
  23214. rtn)))
  23215. (defvar html-table-tag nil) ; dynamically scoped into this.
  23216. (defun org-export-as-html (arg &optional hidden ext-plist
  23217. to-buffer body-only)
  23218. "Export the outline as a pretty HTML file.
  23219. If there is an active region, export only the region. The prefix
  23220. ARG specifies how many levels of the outline should become
  23221. headlines. The default is 3. Lower levels will become bulleted
  23222. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23223. EXT-PLIST is a property list with external parameters overriding
  23224. org-mode's default settings, but still inferior to file-local
  23225. settings. When TO-BUFFER is non-nil, create a buffer with that
  23226. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23227. don't leave any buffer behind but just return the resulting HTML as
  23228. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23229. simply return the content of <body>...</body>, without even
  23230. the body tags themselves."
  23231. (interactive "P")
  23232. ;; Make sure we have a file name when we need it.
  23233. (when (and (not (or to-buffer body-only))
  23234. (not buffer-file-name))
  23235. (if (buffer-base-buffer)
  23236. (org-set-local 'buffer-file-name
  23237. (with-current-buffer (buffer-base-buffer)
  23238. buffer-file-name))
  23239. (error "Need a file name to be able to export.")))
  23240. (message "Exporting...")
  23241. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23242. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23243. (setq-default org-done-keywords org-done-keywords)
  23244. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23245. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23246. ext-plist
  23247. (org-infile-export-plist)))
  23248. (style (plist-get opt-plist :style))
  23249. (html-extension (plist-get opt-plist :html-extension))
  23250. (link-validate (plist-get opt-plist :link-validation-function))
  23251. valid thetoc have-headings first-heading-pos
  23252. (odd org-odd-levels-only)
  23253. (region-p (org-region-active-p))
  23254. (subtree-p
  23255. (when region-p
  23256. (save-excursion
  23257. (goto-char (region-beginning))
  23258. (and (org-at-heading-p)
  23259. (>= (org-end-of-subtree t t) (region-end))))))
  23260. ;; The following two are dynamically scoped into other
  23261. ;; routines below.
  23262. (org-current-export-dir (org-export-directory :html opt-plist))
  23263. (org-current-export-file buffer-file-name)
  23264. (level 0) (line "") (origline "") txt todo
  23265. (umax nil)
  23266. (umax-toc nil)
  23267. (filename (if to-buffer nil
  23268. (expand-file-name
  23269. (concat
  23270. (file-name-sans-extension
  23271. (or (and subtree-p
  23272. (org-entry-get (region-beginning)
  23273. "EXPORT_FILE_NAME" t))
  23274. (file-name-nondirectory buffer-file-name)))
  23275. "." html-extension)
  23276. (file-name-as-directory
  23277. (org-export-directory :html opt-plist)))))
  23278. (current-dir (if buffer-file-name
  23279. (file-name-directory buffer-file-name)
  23280. default-directory))
  23281. (buffer (if to-buffer
  23282. (cond
  23283. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23284. (t (get-buffer-create to-buffer)))
  23285. (find-file-noselect filename)))
  23286. (org-levels-open (make-vector org-level-max nil))
  23287. (date (plist-get opt-plist :date))
  23288. (author (plist-get opt-plist :author))
  23289. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23290. (plist-get opt-plist :title)
  23291. (and (not
  23292. (plist-get opt-plist :skip-before-1st-heading))
  23293. (org-export-grab-title-from-buffer))
  23294. (and buffer-file-name
  23295. (file-name-sans-extension
  23296. (file-name-nondirectory buffer-file-name)))
  23297. "UNTITLED"))
  23298. (html-table-tag (plist-get opt-plist :html-table-tag))
  23299. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23300. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23301. (inquote nil)
  23302. (infixed nil)
  23303. (in-local-list nil)
  23304. (local-list-num nil)
  23305. (local-list-indent nil)
  23306. (llt org-plain-list-ordered-item-terminator)
  23307. (email (plist-get opt-plist :email))
  23308. (language (plist-get opt-plist :language))
  23309. (lang-words nil)
  23310. (target-alist nil) tg
  23311. (head-count 0) cnt
  23312. (start 0)
  23313. (coding-system (and (boundp 'buffer-file-coding-system)
  23314. buffer-file-coding-system))
  23315. (coding-system-for-write (or org-export-html-coding-system
  23316. coding-system))
  23317. (save-buffer-coding-system (or org-export-html-coding-system
  23318. coding-system))
  23319. (charset (and coding-system-for-write
  23320. (fboundp 'coding-system-get)
  23321. (coding-system-get coding-system-for-write
  23322. 'mime-charset)))
  23323. (region
  23324. (buffer-substring
  23325. (if region-p (region-beginning) (point-min))
  23326. (if region-p (region-end) (point-max))))
  23327. (lines
  23328. (org-split-string
  23329. (org-cleaned-string-for-export
  23330. region
  23331. :emph-multiline t
  23332. :for-html t
  23333. :skip-before-1st-heading
  23334. (plist-get opt-plist :skip-before-1st-heading)
  23335. :drawers (plist-get opt-plist :drawers)
  23336. :archived-trees
  23337. (plist-get opt-plist :archived-trees)
  23338. :add-text
  23339. (plist-get opt-plist :text)
  23340. :LaTeX-fragments
  23341. (plist-get opt-plist :LaTeX-fragments))
  23342. "[\r\n]"))
  23343. table-open type
  23344. table-buffer table-orig-buffer
  23345. ind start-is-num starter didclose
  23346. rpl path desc descp desc1 desc2 link
  23347. )
  23348. (let ((inhibit-read-only t))
  23349. (org-unmodified
  23350. (remove-text-properties (point-min) (point-max)
  23351. '(:org-license-to-kill t))))
  23352. (message "Exporting...")
  23353. (setq org-min-level (org-get-min-level lines))
  23354. (setq org-last-level org-min-level)
  23355. (org-init-section-numbers)
  23356. (cond
  23357. ((and date (string-match "%" date))
  23358. (setq date (format-time-string date (current-time))))
  23359. (date)
  23360. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23361. ;; Get the language-dependent settings
  23362. (setq lang-words (or (assoc language org-export-language-setup)
  23363. (assoc "en" org-export-language-setup)))
  23364. ;; Switch to the output buffer
  23365. (set-buffer buffer)
  23366. (let ((inhibit-read-only t)) (erase-buffer))
  23367. (fundamental-mode)
  23368. (and (fboundp 'set-buffer-file-coding-system)
  23369. (set-buffer-file-coding-system coding-system-for-write))
  23370. (let ((case-fold-search nil)
  23371. (org-odd-levels-only odd))
  23372. ;; create local variables for all options, to make sure all called
  23373. ;; functions get the correct information
  23374. (mapc (lambda (x)
  23375. (set (make-local-variable (cdr x))
  23376. (plist-get opt-plist (car x))))
  23377. org-export-plist-vars)
  23378. (setq umax (if arg (prefix-numeric-value arg)
  23379. org-export-headline-levels))
  23380. (setq umax-toc (if (integerp org-export-with-toc)
  23381. (min org-export-with-toc umax)
  23382. umax))
  23383. (unless body-only
  23384. ;; File header
  23385. (insert (format
  23386. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23387. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23388. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23389. lang=\"%s\" xml:lang=\"%s\">
  23390. <head>
  23391. <title>%s</title>
  23392. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23393. <meta name=\"generator\" content=\"Org-mode\"/>
  23394. <meta name=\"generated\" content=\"%s\"/>
  23395. <meta name=\"author\" content=\"%s\"/>
  23396. %s
  23397. </head><body>
  23398. "
  23399. language language (org-html-expand title)
  23400. (or charset "iso-8859-1") date author style))
  23401. (insert (or (plist-get opt-plist :preamble) ""))
  23402. (when (plist-get opt-plist :auto-preamble)
  23403. (if title (insert (format org-export-html-title-format
  23404. (org-html-expand title))))))
  23405. (if (and org-export-with-toc (not body-only))
  23406. (progn
  23407. (push (format "<h%d>%s</h%d>\n"
  23408. org-export-html-toplevel-hlevel
  23409. (nth 3 lang-words)
  23410. org-export-html-toplevel-hlevel)
  23411. thetoc)
  23412. (push "<ul>\n<li>" thetoc)
  23413. (setq lines
  23414. (mapcar '(lambda (line)
  23415. (if (string-match org-todo-line-regexp line)
  23416. ;; This is a headline
  23417. (progn
  23418. (setq have-headings t)
  23419. (setq level (- (match-end 1) (match-beginning 1))
  23420. level (org-tr-level level)
  23421. txt (save-match-data
  23422. (org-html-expand
  23423. (org-export-cleanup-toc-line
  23424. (match-string 3 line))))
  23425. todo
  23426. (or (and org-export-mark-todo-in-toc
  23427. (match-beginning 2)
  23428. (not (member (match-string 2 line)
  23429. org-done-keywords)))
  23430. ; TODO, not DONE
  23431. (and org-export-mark-todo-in-toc
  23432. (= level umax-toc)
  23433. (org-search-todo-below
  23434. line lines level))))
  23435. (if (string-match
  23436. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23437. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23438. (if (string-match quote-re0 txt)
  23439. (setq txt (replace-match "" t t txt)))
  23440. (if org-export-with-section-numbers
  23441. (setq txt (concat (org-section-number level)
  23442. " " txt)))
  23443. (if (<= level (max umax umax-toc))
  23444. (setq head-count (+ head-count 1)))
  23445. (if (<= level umax-toc)
  23446. (progn
  23447. (if (> level org-last-level)
  23448. (progn
  23449. (setq cnt (- level org-last-level))
  23450. (while (>= (setq cnt (1- cnt)) 0)
  23451. (push "\n<ul>\n<li>" thetoc))
  23452. (push "\n" thetoc)))
  23453. (if (< level org-last-level)
  23454. (progn
  23455. (setq cnt (- org-last-level level))
  23456. (while (>= (setq cnt (1- cnt)) 0)
  23457. (push "</li>\n</ul>" thetoc))
  23458. (push "\n" thetoc)))
  23459. ;; Check for targets
  23460. (while (string-match org-target-regexp line)
  23461. (setq tg (match-string 1 line)
  23462. line (replace-match
  23463. (concat "@<span class=\"target\">" tg "@</span> ")
  23464. t t line))
  23465. (push (cons (org-solidify-link-text tg)
  23466. (format "sec-%d" head-count))
  23467. target-alist))
  23468. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23469. (setq txt (replace-match "" t t txt)))
  23470. (push
  23471. (format
  23472. (if todo
  23473. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23474. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23475. head-count txt) thetoc)
  23476. (setq org-last-level level))
  23477. )))
  23478. line)
  23479. lines))
  23480. (while (> org-last-level (1- org-min-level))
  23481. (setq org-last-level (1- org-last-level))
  23482. (push "</li>\n</ul>\n" thetoc))
  23483. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23484. (setq head-count 0)
  23485. (org-init-section-numbers)
  23486. (while (setq line (pop lines) origline line)
  23487. (catch 'nextline
  23488. ;; end of quote section?
  23489. (when (and inquote (string-match "^\\*+ " line))
  23490. (insert "</pre>\n")
  23491. (setq inquote nil))
  23492. ;; inside a quote section?
  23493. (when inquote
  23494. (insert (org-html-protect line) "\n")
  23495. (throw 'nextline nil))
  23496. ;; verbatim lines
  23497. (when (and org-export-with-fixed-width
  23498. (string-match "^[ \t]*:\\(.*\\)" line))
  23499. (when (not infixed)
  23500. (setq infixed t)
  23501. (insert "<pre>\n"))
  23502. (insert (org-html-protect (match-string 1 line)) "\n")
  23503. (when (and lines
  23504. (not (string-match "^[ \t]*\\(:.*\\)"
  23505. (car lines))))
  23506. (setq infixed nil)
  23507. (insert "</pre>\n"))
  23508. (throw 'nextline nil))
  23509. ;; Protected HTML
  23510. (when (get-text-property 0 'org-protected line)
  23511. (let (par)
  23512. (when (re-search-backward
  23513. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23514. (setq par (match-string 1))
  23515. (replace-match "\\2\n"))
  23516. (insert line "\n")
  23517. (while (and lines
  23518. (or (= (length (car lines)) 0)
  23519. (get-text-property 0 'org-protected (car lines))))
  23520. (insert (pop lines) "\n"))
  23521. (and par (insert "<p>\n")))
  23522. (throw 'nextline nil))
  23523. ;; Horizontal line
  23524. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23525. (insert "\n<hr/>\n")
  23526. (throw 'nextline nil))
  23527. ;; make targets to anchors
  23528. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23529. (cond
  23530. ((match-end 2)
  23531. (setq line (replace-match
  23532. (concat "@<a name=\""
  23533. (org-solidify-link-text (match-string 1 line))
  23534. "\">\\nbsp@</a>")
  23535. t t line)))
  23536. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23537. (setq line (replace-match
  23538. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23539. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23540. t t line)))
  23541. (t
  23542. (setq line (replace-match
  23543. (concat "@<a name=\""
  23544. (org-solidify-link-text (match-string 1 line))
  23545. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23546. t t line)))))
  23547. (setq line (org-html-handle-time-stamps line))
  23548. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23549. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23550. ;; Also handle sub_superscripts and checkboxes
  23551. (or (string-match org-table-hline-regexp line)
  23552. (setq line (org-html-expand line)))
  23553. ;; Format the links
  23554. (setq start 0)
  23555. (while (string-match org-bracket-link-analytic-regexp line start)
  23556. (setq start (match-beginning 0))
  23557. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23558. (setq path (match-string 3 line))
  23559. (setq desc1 (if (match-end 5) (match-string 5 line))
  23560. desc2 (if (match-end 2) (concat type ":" path) path)
  23561. descp (and desc1 (not (equal desc1 desc2)))
  23562. desc (or desc1 desc2))
  23563. ;; Make an image out of the description if that is so wanted
  23564. (when (and descp (org-file-image-p desc))
  23565. (save-match-data
  23566. (if (string-match "^file:" desc)
  23567. (setq desc (substring desc (match-end 0)))))
  23568. (setq desc (concat "<img src=\"" desc "\"/>")))
  23569. ;; FIXME: do we need to unescape here somewhere?
  23570. (cond
  23571. ((equal type "internal")
  23572. (setq rpl
  23573. (concat
  23574. "<a href=\"#"
  23575. (org-solidify-link-text
  23576. (save-match-data (org-link-unescape path)) target-alist)
  23577. "\">" desc "</a>")))
  23578. ((member type '("http" "https"))
  23579. ;; standard URL, just check if we need to inline an image
  23580. (if (and (or (eq t org-export-html-inline-images)
  23581. (and org-export-html-inline-images (not descp)))
  23582. (org-file-image-p path))
  23583. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23584. (setq link (concat type ":" path))
  23585. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23586. ((member type '("ftp" "mailto" "news"))
  23587. ;; standard URL
  23588. (setq link (concat type ":" path))
  23589. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23590. ((string= type "file")
  23591. ;; FILE link
  23592. (let* ((filename path)
  23593. (abs-p (file-name-absolute-p filename))
  23594. thefile file-is-image-p search)
  23595. (save-match-data
  23596. (if (string-match "::\\(.*\\)" filename)
  23597. (setq search (match-string 1 filename)
  23598. filename (replace-match "" t nil filename)))
  23599. (setq valid
  23600. (if (functionp link-validate)
  23601. (funcall link-validate filename current-dir)
  23602. t))
  23603. (setq file-is-image-p (org-file-image-p filename))
  23604. (setq thefile (if abs-p (expand-file-name filename) filename))
  23605. (when (and org-export-html-link-org-files-as-html
  23606. (string-match "\\.org$" thefile))
  23607. (setq thefile (concat (substring thefile 0
  23608. (match-beginning 0))
  23609. "." html-extension))
  23610. (if (and search
  23611. ;; make sure this is can be used as target search
  23612. (not (string-match "^[0-9]*$" search))
  23613. (not (string-match "^\\*" search))
  23614. (not (string-match "^/.*/$" search)))
  23615. (setq thefile (concat thefile "#"
  23616. (org-solidify-link-text
  23617. (org-link-unescape search)))))
  23618. (when (string-match "^file:" desc)
  23619. (setq desc (replace-match "" t t desc))
  23620. (if (string-match "\\.org$" desc)
  23621. (setq desc (replace-match "" t t desc))))))
  23622. (setq rpl (if (and file-is-image-p
  23623. (or (eq t org-export-html-inline-images)
  23624. (and org-export-html-inline-images
  23625. (not descp))))
  23626. (concat "<img src=\"" thefile "\"/>")
  23627. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23628. (if (not valid) (setq rpl desc))))
  23629. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23630. (setq rpl (concat "<i>&lt;" type ":"
  23631. (save-match-data (org-link-unescape path))
  23632. "&gt;</i>"))))
  23633. (setq line (replace-match rpl t t line)
  23634. start (+ start (length rpl))))
  23635. ;; TODO items
  23636. (if (and (string-match org-todo-line-regexp line)
  23637. (match-beginning 2))
  23638. (setq line
  23639. (concat (substring line 0 (match-beginning 2))
  23640. "<span class=\""
  23641. (if (member (match-string 2 line)
  23642. org-done-keywords)
  23643. "done" "todo")
  23644. "\">" (match-string 2 line)
  23645. "</span>" (substring line (match-end 2)))))
  23646. ;; Does this contain a reference to a footnote?
  23647. (when org-export-with-footnotes
  23648. (setq start 0)
  23649. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23650. (if (get-text-property (match-beginning 2) 'org-protected line)
  23651. (setq start (match-end 2))
  23652. (let ((n (match-string 2 line)))
  23653. (setq line
  23654. (replace-match
  23655. (format
  23656. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23657. (match-string 1 line) n n n)
  23658. t t line))))))
  23659. (cond
  23660. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23661. ;; This is a headline
  23662. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23663. txt (match-string 2 line))
  23664. (if (string-match quote-re0 txt)
  23665. (setq txt (replace-match "" t t txt)))
  23666. (if (<= level (max umax umax-toc))
  23667. (setq head-count (+ head-count 1)))
  23668. (when in-local-list
  23669. ;; Close any local lists before inserting a new header line
  23670. (while local-list-num
  23671. (org-close-li)
  23672. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23673. (pop local-list-num))
  23674. (setq local-list-indent nil
  23675. in-local-list nil))
  23676. (setq first-heading-pos (or first-heading-pos (point)))
  23677. (org-html-level-start level txt umax
  23678. (and org-export-with-toc (<= level umax))
  23679. head-count)
  23680. ;; QUOTES
  23681. (when (string-match quote-re line)
  23682. (insert "<pre>")
  23683. (setq inquote t)))
  23684. ((and org-export-with-tables
  23685. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23686. (if (not table-open)
  23687. ;; New table starts
  23688. (setq table-open t table-buffer nil table-orig-buffer nil))
  23689. ;; Accumulate lines
  23690. (setq table-buffer (cons line table-buffer)
  23691. table-orig-buffer (cons origline table-orig-buffer))
  23692. (when (or (not lines)
  23693. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23694. (car lines))))
  23695. (setq table-open nil
  23696. table-buffer (nreverse table-buffer)
  23697. table-orig-buffer (nreverse table-orig-buffer))
  23698. (org-close-par-maybe)
  23699. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23700. (t
  23701. ;; Normal lines
  23702. (when (string-match
  23703. (cond
  23704. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23705. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23706. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23707. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23708. line)
  23709. (setq ind (org-get-string-indentation line)
  23710. start-is-num (match-beginning 4)
  23711. starter (if (match-beginning 2)
  23712. (substring (match-string 2 line) 0 -1))
  23713. line (substring line (match-beginning 5)))
  23714. (unless (string-match "[^ \t]" line)
  23715. ;; empty line. Pretend indentation is large.
  23716. (setq ind (if org-empty-line-terminates-plain-lists
  23717. 0
  23718. (1+ (or (car local-list-indent) 1)))))
  23719. (setq didclose nil)
  23720. (while (and in-local-list
  23721. (or (and (= ind (car local-list-indent))
  23722. (not starter))
  23723. (< ind (car local-list-indent))))
  23724. (setq didclose t)
  23725. (org-close-li)
  23726. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23727. (pop local-list-num) (pop local-list-indent)
  23728. (setq in-local-list local-list-indent))
  23729. (cond
  23730. ((and starter
  23731. (or (not in-local-list)
  23732. (> ind (car local-list-indent))))
  23733. ;; Start new (level of) list
  23734. (org-close-par-maybe)
  23735. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23736. (push start-is-num local-list-num)
  23737. (push ind local-list-indent)
  23738. (setq in-local-list t))
  23739. (starter
  23740. ;; continue current list
  23741. (org-close-li)
  23742. (insert "<li>\n"))
  23743. (didclose
  23744. ;; we did close a list, normal text follows: need <p>
  23745. (org-open-par)))
  23746. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23747. (setq line
  23748. (replace-match
  23749. (if (equal (match-string 1 line) "X")
  23750. "<b>[X]</b>"
  23751. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23752. t t line))))
  23753. ;; Empty lines start a new paragraph. If hand-formatted lists
  23754. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23755. ;; also start a new paragraph.
  23756. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23757. ;; Is this the start of a footnote?
  23758. (when org-export-with-footnotes
  23759. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23760. (org-close-par-maybe)
  23761. (let ((n (match-string 1 line)))
  23762. (setq line (replace-match
  23763. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23764. ;; Check if the line break needs to be conserved
  23765. (cond
  23766. ((string-match "\\\\\\\\[ \t]*$" line)
  23767. (setq line (replace-match "<br/>" t t line)))
  23768. (org-export-preserve-breaks
  23769. (setq line (concat line "<br/>"))))
  23770. (insert line "\n")))))
  23771. ;; Properly close all local lists and other lists
  23772. (when inquote (insert "</pre>\n"))
  23773. (when in-local-list
  23774. ;; Close any local lists before inserting a new header line
  23775. (while local-list-num
  23776. (org-close-li)
  23777. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23778. (pop local-list-num))
  23779. (setq local-list-indent nil
  23780. in-local-list nil))
  23781. (org-html-level-start 1 nil umax
  23782. (and org-export-with-toc (<= level umax))
  23783. head-count)
  23784. (unless body-only
  23785. (when (plist-get opt-plist :auto-postamble)
  23786. (insert "<div id=\"postamble\">")
  23787. (when (and org-export-author-info author)
  23788. (insert "<p class=\"author\"> "
  23789. (nth 1 lang-words) ": " author "\n")
  23790. (when email
  23791. (if (listp (split-string email ",+ *"))
  23792. (mapc (lambda(e)
  23793. (insert "<a href=\"mailto:" e "\">&lt;"
  23794. e "&gt;</a>\n"))
  23795. (split-string email ",+ *"))
  23796. (insert "<a href=\"mailto:" email "\">&lt;"
  23797. email "&gt;</a>\n")))
  23798. (insert "</p>\n"))
  23799. (when (and date org-export-time-stamp-file)
  23800. (insert "<p class=\"date\"> "
  23801. (nth 2 lang-words) ": "
  23802. date "</p>\n"))
  23803. (insert "</div>"))
  23804. (if org-export-html-with-timestamp
  23805. (insert org-export-html-html-helper-timestamp))
  23806. (insert (or (plist-get opt-plist :postamble) ""))
  23807. (insert "</body>\n</html>\n"))
  23808. (normal-mode)
  23809. (if (eq major-mode default-major-mode) (html-mode))
  23810. ;; insert the table of contents
  23811. (goto-char (point-min))
  23812. (when thetoc
  23813. (if (or (re-search-forward
  23814. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23815. (re-search-forward
  23816. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23817. (progn
  23818. (goto-char (match-beginning 0))
  23819. (replace-match ""))
  23820. (goto-char first-heading-pos)
  23821. (when (looking-at "\\s-*</p>")
  23822. (goto-char (match-end 0))
  23823. (insert "\n")))
  23824. (insert "<div id=\"table-of-contents\">\n")
  23825. (mapc 'insert thetoc)
  23826. (insert "</div>\n"))
  23827. ;; remove empty paragraphs and lists
  23828. (goto-char (point-min))
  23829. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23830. (replace-match ""))
  23831. (goto-char (point-min))
  23832. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23833. (replace-match ""))
  23834. (goto-char (point-min))
  23835. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23836. (replace-match ""))
  23837. ;; Convert whitespace place holders
  23838. (goto-char (point-min))
  23839. (let (beg end n)
  23840. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23841. (setq n (get-text-property beg 'org-whitespace)
  23842. end (next-single-property-change beg 'org-whitespace))
  23843. (goto-char beg)
  23844. (delete-region beg end)
  23845. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23846. (make-string n ?x)))))
  23847. (or to-buffer (save-buffer))
  23848. (goto-char (point-min))
  23849. (message "Exporting... done")
  23850. (if (eq to-buffer 'string)
  23851. (prog1 (buffer-substring (point-min) (point-max))
  23852. (kill-buffer (current-buffer)))
  23853. (current-buffer)))))
  23854. (defvar org-table-colgroup-info nil)
  23855. (defun org-format-table-ascii (lines)
  23856. "Format a table for ascii export."
  23857. (if (stringp lines)
  23858. (setq lines (org-split-string lines "\n")))
  23859. (if (not (string-match "^[ \t]*|" (car lines)))
  23860. ;; Table made by table.el - test for spanning
  23861. lines
  23862. ;; A normal org table
  23863. ;; Get rid of hlines at beginning and end
  23864. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23865. (setq lines (nreverse lines))
  23866. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23867. (setq lines (nreverse lines))
  23868. (when org-export-table-remove-special-lines
  23869. ;; Check if the table has a marking column. If yes remove the
  23870. ;; column and the special lines
  23871. (setq lines (org-table-clean-before-export lines)))
  23872. ;; Get rid of the vertical lines except for grouping
  23873. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23874. rtn line vl1 start)
  23875. (while (setq line (pop lines))
  23876. (if (string-match org-table-hline-regexp line)
  23877. (and (string-match "|\\(.*\\)|" line)
  23878. (setq line (replace-match " \\1" t nil line)))
  23879. (setq start 0 vl1 vl)
  23880. (while (string-match "|" line start)
  23881. (setq start (match-end 0))
  23882. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23883. (push line rtn))
  23884. (nreverse rtn))))
  23885. (defun org-colgroup-info-to-vline-list (info)
  23886. (let (vl new last)
  23887. (while info
  23888. (setq last new new (pop info))
  23889. (if (or (memq last '(:end :startend))
  23890. (memq new '(:start :startend)))
  23891. (push t vl)
  23892. (push nil vl)))
  23893. (setq vl (nreverse vl))
  23894. (and vl (setcar vl nil))
  23895. vl))
  23896. (defun org-format-table-html (lines olines)
  23897. "Find out which HTML converter to use and return the HTML code."
  23898. (if (stringp lines)
  23899. (setq lines (org-split-string lines "\n")))
  23900. (if (string-match "^[ \t]*|" (car lines))
  23901. ;; A normal org table
  23902. (org-format-org-table-html lines)
  23903. ;; Table made by table.el - test for spanning
  23904. (let* ((hlines (delq nil (mapcar
  23905. (lambda (x)
  23906. (if (string-match "^[ \t]*\\+-" x) x
  23907. nil))
  23908. lines)))
  23909. (first (car hlines))
  23910. (ll (and (string-match "\\S-+" first)
  23911. (match-string 0 first)))
  23912. (re (concat "^[ \t]*" (regexp-quote ll)))
  23913. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23914. hlines))))
  23915. (if (and (not spanning)
  23916. (not org-export-prefer-native-exporter-for-tables))
  23917. ;; We can use my own converter with HTML conversions
  23918. (org-format-table-table-html lines)
  23919. ;; Need to use the code generator in table.el, with the original text.
  23920. (org-format-table-table-html-using-table-generate-source olines)))))
  23921. (defun org-format-org-table-html (lines &optional splice)
  23922. "Format a table into HTML."
  23923. ;; Get rid of hlines at beginning and end
  23924. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23925. (setq lines (nreverse lines))
  23926. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23927. (setq lines (nreverse lines))
  23928. (when org-export-table-remove-special-lines
  23929. ;; Check if the table has a marking column. If yes remove the
  23930. ;; column and the special lines
  23931. (setq lines (org-table-clean-before-export lines)))
  23932. (let ((head (and org-export-highlight-first-table-line
  23933. (delq nil (mapcar
  23934. (lambda (x) (string-match "^[ \t]*|-" x))
  23935. (cdr lines)))))
  23936. (nlines 0) fnum i
  23937. tbopen line fields html gr colgropen)
  23938. (if splice (setq head nil))
  23939. (unless splice (push (if head "<thead>" "<tbody>") html))
  23940. (setq tbopen t)
  23941. (while (setq line (pop lines))
  23942. (catch 'next-line
  23943. (if (string-match "^[ \t]*|-" line)
  23944. (progn
  23945. (unless splice
  23946. (push (if head "</thead>" "</tbody>") html)
  23947. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23948. (setq head nil) ;; head ends here, first time around
  23949. ;; ignore this line
  23950. (throw 'next-line t)))
  23951. ;; Break the line into fields
  23952. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23953. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23954. (setq nlines (1+ nlines) i -1)
  23955. (push (concat "<tr>"
  23956. (mapconcat
  23957. (lambda (x)
  23958. (setq i (1+ i))
  23959. (if (and (< i nlines)
  23960. (string-match org-table-number-regexp x))
  23961. (incf (aref fnum i)))
  23962. (if head
  23963. (concat (car org-export-table-header-tags) x
  23964. (cdr org-export-table-header-tags))
  23965. (concat (car org-export-table-data-tags) x
  23966. (cdr org-export-table-data-tags))))
  23967. fields "")
  23968. "</tr>")
  23969. html)))
  23970. (unless splice (if tbopen (push "</tbody>" html)))
  23971. (unless splice (push "</table>\n" html))
  23972. (setq html (nreverse html))
  23973. (unless splice
  23974. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23975. (push (mapconcat
  23976. (lambda (x)
  23977. (setq gr (pop org-table-colgroup-info))
  23978. (format "%s<col align=\"%s\"></col>%s"
  23979. (if (memq gr '(:start :startend))
  23980. (prog1
  23981. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23982. (setq colgropen t))
  23983. "")
  23984. (if (> (/ (float x) nlines) org-table-number-fraction)
  23985. "right" "left")
  23986. (if (memq gr '(:end :startend))
  23987. (progn (setq colgropen nil) "</colgroup>")
  23988. "")))
  23989. fnum "")
  23990. html)
  23991. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23992. (push html-table-tag html))
  23993. (concat (mapconcat 'identity html "\n") "\n")))
  23994. (defun org-table-clean-before-export (lines)
  23995. "Check if the table has a marking column.
  23996. If yes remove the column and the special lines."
  23997. (setq org-table-colgroup-info nil)
  23998. (if (memq nil
  23999. (mapcar
  24000. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24001. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24002. lines))
  24003. (progn
  24004. (setq org-table-clean-did-remove-column nil)
  24005. (delq nil
  24006. (mapcar
  24007. (lambda (x)
  24008. (cond
  24009. ((string-match "^[ \t]*| */ *|" x)
  24010. (setq org-table-colgroup-info
  24011. (mapcar (lambda (x)
  24012. (cond ((member x '("<" "&lt;")) :start)
  24013. ((member x '(">" "&gt;")) :end)
  24014. ((member x '("<>" "&lt;&gt;")) :startend)
  24015. (t nil)))
  24016. (org-split-string x "[ \t]*|[ \t]*")))
  24017. nil)
  24018. (t x)))
  24019. lines)))
  24020. (setq org-table-clean-did-remove-column t)
  24021. (delq nil
  24022. (mapcar
  24023. (lambda (x)
  24024. (cond
  24025. ((string-match "^[ \t]*| */ *|" x)
  24026. (setq org-table-colgroup-info
  24027. (mapcar (lambda (x)
  24028. (cond ((member x '("<" "&lt;")) :start)
  24029. ((member x '(">" "&gt;")) :end)
  24030. ((member x '("<>" "&lt;&gt;")) :startend)
  24031. (t nil)))
  24032. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24033. nil)
  24034. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24035. nil) ; ignore this line
  24036. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24037. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24038. ;; remove the first column
  24039. (replace-match "\\1|" t nil x))))
  24040. lines))))
  24041. (defun org-format-table-table-html (lines)
  24042. "Format a table generated by table.el into HTML.
  24043. This conversion does *not* use `table-generate-source' from table.el.
  24044. This has the advantage that Org-mode's HTML conversions can be used.
  24045. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24046. (let (line field-buffer
  24047. (head org-export-highlight-first-table-line)
  24048. fields html empty)
  24049. (setq html (concat html-table-tag "\n"))
  24050. (while (setq line (pop lines))
  24051. (setq empty "&nbsp;")
  24052. (catch 'next-line
  24053. (if (string-match "^[ \t]*\\+-" line)
  24054. (progn
  24055. (if field-buffer
  24056. (progn
  24057. (setq
  24058. html
  24059. (concat
  24060. html
  24061. "<tr>"
  24062. (mapconcat
  24063. (lambda (x)
  24064. (if (equal x "") (setq x empty))
  24065. (if head
  24066. (concat (car org-export-table-header-tags) x
  24067. (cdr org-export-table-header-tags))
  24068. (concat (car org-export-table-data-tags) x
  24069. (cdr org-export-table-data-tags))))
  24070. field-buffer "\n")
  24071. "</tr>\n"))
  24072. (setq head nil)
  24073. (setq field-buffer nil)))
  24074. ;; Ignore this line
  24075. (throw 'next-line t)))
  24076. ;; Break the line into fields and store the fields
  24077. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24078. (if field-buffer
  24079. (setq field-buffer (mapcar
  24080. (lambda (x)
  24081. (concat x "<br/>" (pop fields)))
  24082. field-buffer))
  24083. (setq field-buffer fields))))
  24084. (setq html (concat html "</table>\n"))
  24085. html))
  24086. (defun org-format-table-table-html-using-table-generate-source (lines)
  24087. "Format a table into html, using `table-generate-source' from table.el.
  24088. This has the advantage that cell- or row-spanning is allowed.
  24089. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24090. (require 'table)
  24091. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24092. (erase-buffer)
  24093. (insert (mapconcat 'identity lines "\n"))
  24094. (goto-char (point-min))
  24095. (if (not (re-search-forward "|[^+]" nil t))
  24096. (error "Error processing table"))
  24097. (table-recognize-table)
  24098. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24099. (table-generate-source 'html " org-tmp2 ")
  24100. (set-buffer " org-tmp2 ")
  24101. (buffer-substring (point-min) (point-max))))
  24102. (defun org-html-handle-time-stamps (s)
  24103. "Format time stamps in string S, or remove them."
  24104. (catch 'exit
  24105. (let (r b)
  24106. (while (string-match org-maybe-keyword-time-regexp s)
  24107. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24108. ;; never export CLOCK
  24109. (throw 'exit ""))
  24110. (or b (setq b (substring s 0 (match-beginning 0))))
  24111. (if (not org-export-with-timestamps)
  24112. (setq r (concat r (substring s 0 (match-beginning 0)))
  24113. s (substring s (match-end 0)))
  24114. (setq r (concat
  24115. r (substring s 0 (match-beginning 0))
  24116. (if (match-end 1)
  24117. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24118. (match-string 1 s)))
  24119. (format " @<span class=\"timestamp\">%s@</span>"
  24120. (substring
  24121. (org-translate-time (match-string 3 s)) 1 -1)))
  24122. s (substring s (match-end 0)))))
  24123. ;; Line break if line started and ended with time stamp stuff
  24124. (if (not r)
  24125. s
  24126. (setq r (concat r s))
  24127. (unless (string-match "\\S-" (concat b s))
  24128. (setq r (concat r "@<br/>")))
  24129. r))))
  24130. (defun org-html-protect (s)
  24131. ;; convert & to &amp;, < to &lt; and > to &gt;
  24132. (let ((start 0))
  24133. (while (string-match "&" s start)
  24134. (setq s (replace-match "&amp;" t t s)
  24135. start (1+ (match-beginning 0))))
  24136. (while (string-match "<" s)
  24137. (setq s (replace-match "&lt;" t t s)))
  24138. (while (string-match ">" s)
  24139. (setq s (replace-match "&gt;" t t s))))
  24140. s)
  24141. (defun org-export-cleanup-toc-line (s)
  24142. "Remove tags and time staps from lines going into the toc."
  24143. (when (memq org-export-with-tags '(not-in-toc nil))
  24144. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24145. (setq s (replace-match "" t t s))))
  24146. (when org-export-remove-timestamps-from-toc
  24147. (while (string-match org-maybe-keyword-time-regexp s)
  24148. (setq s (replace-match "" t t s))))
  24149. (while (string-match org-bracket-link-regexp s)
  24150. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24151. t t s)))
  24152. s)
  24153. (defun org-html-expand (string)
  24154. "Prepare STRING for HTML export. Applies all active conversions.
  24155. If there are links in the string, don't modify these."
  24156. (let* ((re (concat org-bracket-link-regexp "\\|"
  24157. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24158. m s l res)
  24159. (while (setq m (string-match re string))
  24160. (setq s (substring string 0 m)
  24161. l (match-string 0 string)
  24162. string (substring string (match-end 0)))
  24163. (push (org-html-do-expand s) res)
  24164. (push l res))
  24165. (push (org-html-do-expand string) res)
  24166. (apply 'concat (nreverse res))))
  24167. (defun org-html-do-expand (s)
  24168. "Apply all active conversions to translate special ASCII to HTML."
  24169. (setq s (org-html-protect s))
  24170. (if org-export-html-expand
  24171. (let ((start 0))
  24172. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24173. (setq s (replace-match "<\\1>" t nil s)))))
  24174. (if org-export-with-emphasize
  24175. (setq s (org-export-html-convert-emphasize s)))
  24176. (if org-export-with-special-strings
  24177. (setq s (org-export-html-convert-special-strings s)))
  24178. (if org-export-with-sub-superscripts
  24179. (setq s (org-export-html-convert-sub-super s)))
  24180. (if org-export-with-TeX-macros
  24181. (let ((start 0) wd ass)
  24182. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24183. (if (get-text-property (match-beginning 0) 'org-protected s)
  24184. (setq start (match-end 0))
  24185. (setq wd (match-string 1 s))
  24186. (if (setq ass (assoc wd org-html-entities))
  24187. (setq s (replace-match (or (cdr ass)
  24188. (concat "&" (car ass) ";"))
  24189. t t s))
  24190. (setq start (+ start (length wd))))))))
  24191. s)
  24192. (defun org-create-multibrace-regexp (left right n)
  24193. "Create a regular expression which will match a balanced sexp.
  24194. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24195. as single character strings.
  24196. The regexp returned will match the entire expression including the
  24197. delimiters. It will also define a single group which contains the
  24198. match except for the outermost delimiters. The maximum depth of
  24199. stacked delimiters is N. Escaping delimiters is not possible."
  24200. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24201. (or "\\|")
  24202. (re nothing)
  24203. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24204. (while (> n 1)
  24205. (setq n (1- n)
  24206. re (concat re or next)
  24207. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24208. (concat left "\\(" re "\\)" right)))
  24209. (defvar org-match-substring-regexp
  24210. (concat
  24211. "\\([^\\]\\)\\([_^]\\)\\("
  24212. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24213. "\\|"
  24214. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24215. "\\|"
  24216. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24217. "The regular expression matching a sub- or superscript.")
  24218. (defvar org-match-substring-with-braces-regexp
  24219. (concat
  24220. "\\([^\\]\\)\\([_^]\\)\\("
  24221. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24222. "\\)")
  24223. "The regular expression matching a sub- or superscript, forcing braces.")
  24224. (defconst org-export-html-special-string-regexps
  24225. '(("\\\\-" . "&shy;")
  24226. ("---\\([^-]\\)" . "&mdash;\\1")
  24227. ("--\\([^-]\\)" . "&ndash;\\1")
  24228. ("\\.\\.\\." . "&hellip;"))
  24229. "Regular expressions for special string conversion.")
  24230. (defun org-export-html-convert-special-strings (string)
  24231. "Convert special characters in STRING to HTML."
  24232. (let ((all org-export-html-special-string-regexps)
  24233. e a re rpl start)
  24234. (while (setq a (pop all))
  24235. (setq re (car a) rpl (cdr a) start 0)
  24236. (while (string-match re string start)
  24237. (if (get-text-property (match-beginning 0) 'org-protected string)
  24238. (setq start (match-end 0))
  24239. (setq string (replace-match rpl t nil string)))))
  24240. string))
  24241. (defun org-export-html-convert-sub-super (string)
  24242. "Convert sub- and superscripts in STRING to HTML."
  24243. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24244. (while (string-match org-match-substring-regexp string s)
  24245. (cond
  24246. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24247. ((get-text-property (match-beginning 2) 'org-protected string)
  24248. (setq s (match-end 2)))
  24249. (t
  24250. (setq s (match-end 1)
  24251. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24252. c (or (match-string 8 string)
  24253. (match-string 6 string)
  24254. (match-string 5 string))
  24255. string (replace-match
  24256. (concat (match-string 1 string)
  24257. "<" key ">" c "</" key ">")
  24258. t t string)))))
  24259. (while (string-match "\\\\\\([_^]\\)" string)
  24260. (setq string (replace-match (match-string 1 string) t t string)))
  24261. string))
  24262. (defun org-export-html-convert-emphasize (string)
  24263. "Apply emphasis."
  24264. (let ((s 0) rpl)
  24265. (while (string-match org-emph-re string s)
  24266. (if (not (equal
  24267. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24268. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24269. (setq s (match-beginning 0)
  24270. rpl
  24271. (concat
  24272. (match-string 1 string)
  24273. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24274. (match-string 4 string)
  24275. (nth 3 (assoc (match-string 3 string)
  24276. org-emphasis-alist))
  24277. (match-string 5 string))
  24278. string (replace-match rpl t t string)
  24279. s (+ s (- (length rpl) 2)))
  24280. (setq s (1+ s))))
  24281. string))
  24282. (defvar org-par-open nil)
  24283. (defun org-open-par ()
  24284. "Insert <p>, but first close previous paragraph if any."
  24285. (org-close-par-maybe)
  24286. (insert "\n<p>")
  24287. (setq org-par-open t))
  24288. (defun org-close-par-maybe ()
  24289. "Close paragraph if there is one open."
  24290. (when org-par-open
  24291. (insert "</p>")
  24292. (setq org-par-open nil)))
  24293. (defun org-close-li ()
  24294. "Close <li> if necessary."
  24295. (org-close-par-maybe)
  24296. (insert "</li>\n"))
  24297. (defvar body-only) ; dynamically scoped into this.
  24298. (defun org-html-level-start (level title umax with-toc head-count)
  24299. "Insert a new level in HTML export.
  24300. When TITLE is nil, just close all open levels."
  24301. (org-close-par-maybe)
  24302. (let ((l org-level-max))
  24303. (while (>= l level)
  24304. (if (aref org-levels-open (1- l))
  24305. (progn
  24306. (org-html-level-close l umax)
  24307. (aset org-levels-open (1- l) nil)))
  24308. (setq l (1- l)))
  24309. (when title
  24310. ;; If title is nil, this means this function is called to close
  24311. ;; all levels, so the rest is done only if title is given
  24312. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24313. (setq title (replace-match
  24314. (if org-export-with-tags
  24315. (save-match-data
  24316. (concat
  24317. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24318. (mapconcat 'identity (org-split-string
  24319. (match-string 1 title) ":")
  24320. "&nbsp;")
  24321. "</span>"))
  24322. "")
  24323. t t title)))
  24324. (if (> level umax)
  24325. (progn
  24326. (if (aref org-levels-open (1- level))
  24327. (progn
  24328. (org-close-li)
  24329. (insert "<li>" title "<br/>\n"))
  24330. (aset org-levels-open (1- level) t)
  24331. (org-close-par-maybe)
  24332. (insert "<ul>\n<li>" title "<br/>\n")))
  24333. (aset org-levels-open (1- level) t)
  24334. (if (and org-export-with-section-numbers (not body-only))
  24335. (setq title (concat (org-section-number level) " " title)))
  24336. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24337. (if with-toc
  24338. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24339. level level head-count title level))
  24340. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24341. (org-open-par)))))
  24342. (defun org-html-level-close (level max-outline-level)
  24343. "Terminate one level in HTML export."
  24344. (if (<= level max-outline-level)
  24345. (insert "</div>\n")
  24346. (org-close-li)
  24347. (insert "</ul>\n")))
  24348. ;;; iCalendar export
  24349. ;;;###autoload
  24350. (defun org-export-icalendar-this-file ()
  24351. "Export current file as an iCalendar file.
  24352. The iCalendar file will be located in the same directory as the Org-mode
  24353. file, but with extension `.ics'."
  24354. (interactive)
  24355. (org-export-icalendar nil buffer-file-name))
  24356. ;;;###autoload
  24357. (defun org-export-icalendar-all-agenda-files ()
  24358. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24359. Each iCalendar file will be located in the same directory as the Org-mode
  24360. file, but with extension `.ics'."
  24361. (interactive)
  24362. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24363. ;;;###autoload
  24364. (defun org-export-icalendar-combine-agenda-files ()
  24365. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24366. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24367. (interactive)
  24368. (apply 'org-export-icalendar t (org-agenda-files t)))
  24369. (defun org-export-icalendar (combine &rest files)
  24370. "Create iCalendar files for all elements of FILES.
  24371. If COMBINE is non-nil, combine all calendar entries into a single large
  24372. file and store it under the name `org-combined-agenda-icalendar-file'."
  24373. (save-excursion
  24374. (org-prepare-agenda-buffers files)
  24375. (let* ((dir (org-export-directory
  24376. :ical (list :publishing-directory
  24377. org-export-publishing-directory)))
  24378. file ical-file ical-buffer category started org-agenda-new-buffers)
  24379. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24380. (when combine
  24381. (setq ical-file
  24382. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24383. org-combined-agenda-icalendar-file
  24384. (expand-file-name org-combined-agenda-icalendar-file dir))
  24385. ical-buffer (org-get-agenda-file-buffer ical-file))
  24386. (set-buffer ical-buffer) (erase-buffer))
  24387. (while (setq file (pop files))
  24388. (catch 'nextfile
  24389. (org-check-agenda-file file)
  24390. (set-buffer (org-get-agenda-file-buffer file))
  24391. (unless combine
  24392. (setq ical-file (concat (file-name-as-directory dir)
  24393. (file-name-sans-extension
  24394. (file-name-nondirectory buffer-file-name))
  24395. ".ics"))
  24396. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24397. (with-current-buffer ical-buffer (erase-buffer)))
  24398. (setq category (or org-category
  24399. (file-name-sans-extension
  24400. (file-name-nondirectory buffer-file-name))))
  24401. (if (symbolp category) (setq category (symbol-name category)))
  24402. (let ((standard-output ical-buffer))
  24403. (if combine
  24404. (and (not started) (setq started t)
  24405. (org-start-icalendar-file org-icalendar-combined-name))
  24406. (org-start-icalendar-file category))
  24407. (org-print-icalendar-entries combine)
  24408. (when (or (and combine (not files)) (not combine))
  24409. (org-finish-icalendar-file)
  24410. (set-buffer ical-buffer)
  24411. (save-buffer)
  24412. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24413. (org-release-buffers org-agenda-new-buffers))))
  24414. (defvar org-after-save-iCalendar-file-hook nil
  24415. "Hook run after an iCalendar file has been saved.
  24416. The iCalendar buffer is still current when this hook is run.
  24417. A good way to use this is to tell a desktop calenndar application to re-read
  24418. the iCalendar file.")
  24419. (defun org-print-icalendar-entries (&optional combine)
  24420. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24421. When COMBINE is non nil, add the category to each line."
  24422. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24423. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24424. (dts (org-ical-ts-to-string
  24425. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24426. "DTSTART"))
  24427. hd ts ts2 state status (inc t) pos b sexp rrule
  24428. scheduledp deadlinep tmp pri category entry location summary desc
  24429. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24430. (org-refresh-category-properties)
  24431. (save-excursion
  24432. (goto-char (point-min))
  24433. (while (re-search-forward re1 nil t)
  24434. (catch :skip
  24435. (org-agenda-skip)
  24436. (setq pos (match-beginning 0)
  24437. ts (match-string 0)
  24438. inc t
  24439. hd (org-get-heading)
  24440. summary (org-icalendar-cleanup-string
  24441. (org-entry-get nil "SUMMARY"))
  24442. desc (org-icalendar-cleanup-string
  24443. (or (org-entry-get nil "DESCRIPTION")
  24444. (and org-icalendar-include-body (org-get-entry)))
  24445. t org-icalendar-include-body)
  24446. location (org-icalendar-cleanup-string
  24447. (org-entry-get nil "LOCATION"))
  24448. category (org-get-category))
  24449. (if (looking-at re2)
  24450. (progn
  24451. (goto-char (match-end 0))
  24452. (setq ts2 (match-string 1) inc nil))
  24453. (setq tmp (buffer-substring (max (point-min)
  24454. (- pos org-ds-keyword-length))
  24455. pos)
  24456. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24457. (progn
  24458. (setq inc nil)
  24459. (replace-match "\\1" t nil ts))
  24460. ts)
  24461. deadlinep (string-match org-deadline-regexp tmp)
  24462. scheduledp (string-match org-scheduled-regexp tmp)
  24463. ;; donep (org-entry-is-done-p)
  24464. ))
  24465. (if (or (string-match org-tr-regexp hd)
  24466. (string-match org-ts-regexp hd))
  24467. (setq hd (replace-match "" t t hd)))
  24468. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24469. (setq rrule
  24470. (concat "\nRRULE:FREQ="
  24471. (cdr (assoc
  24472. (match-string 2 ts)
  24473. '(("d" . "DAILY")("w" . "WEEKLY")
  24474. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24475. ";INTERVAL=" (match-string 1 ts)))
  24476. (setq rrule ""))
  24477. (setq summary (or summary hd))
  24478. (if (string-match org-bracket-link-regexp summary)
  24479. (setq summary
  24480. (replace-match (if (match-end 3)
  24481. (match-string 3 summary)
  24482. (match-string 1 summary))
  24483. t t summary)))
  24484. (if deadlinep (setq summary (concat "DL: " summary)))
  24485. (if scheduledp (setq summary (concat "S: " summary)))
  24486. (if (string-match "\\`<%%" ts)
  24487. (with-current-buffer sexp-buffer
  24488. (insert (substring ts 1 -1) " " summary "\n"))
  24489. (princ (format "BEGIN:VEVENT
  24490. %s
  24491. %s%s
  24492. SUMMARY:%s%s%s
  24493. CATEGORIES:%s
  24494. END:VEVENT\n"
  24495. (org-ical-ts-to-string ts "DTSTART")
  24496. (org-ical-ts-to-string ts2 "DTEND" inc)
  24497. rrule summary
  24498. (if (and desc (string-match "\\S-" desc))
  24499. (concat "\nDESCRIPTION: " desc) "")
  24500. (if (and location (string-match "\\S-" location))
  24501. (concat "\nLOCATION: " location) "")
  24502. category)))))
  24503. (when (and org-icalendar-include-sexps
  24504. (condition-case nil (require 'icalendar) (error nil))
  24505. (fboundp 'icalendar-export-region))
  24506. ;; Get all the literal sexps
  24507. (goto-char (point-min))
  24508. (while (re-search-forward "^&?%%(" nil t)
  24509. (catch :skip
  24510. (org-agenda-skip)
  24511. (setq b (match-beginning 0))
  24512. (goto-char (1- (match-end 0)))
  24513. (forward-sexp 1)
  24514. (end-of-line 1)
  24515. (setq sexp (buffer-substring b (point)))
  24516. (with-current-buffer sexp-buffer
  24517. (insert sexp "\n"))
  24518. (princ (org-diary-to-ical-string sexp-buffer)))))
  24519. (when org-icalendar-include-todo
  24520. (goto-char (point-min))
  24521. (while (re-search-forward org-todo-line-regexp nil t)
  24522. (catch :skip
  24523. (org-agenda-skip)
  24524. (setq state (match-string 2))
  24525. (setq status (if (member state org-done-keywords)
  24526. "COMPLETED" "NEEDS-ACTION"))
  24527. (when (and state
  24528. (or (not (member state org-done-keywords))
  24529. (eq org-icalendar-include-todo 'all))
  24530. (not (member org-archive-tag (org-get-tags-at)))
  24531. )
  24532. (setq hd (match-string 3)
  24533. summary (org-icalendar-cleanup-string
  24534. (org-entry-get nil "SUMMARY"))
  24535. desc (org-icalendar-cleanup-string
  24536. (or (org-entry-get nil "DESCRIPTION")
  24537. (and org-icalendar-include-body (org-get-entry)))
  24538. t org-icalendar-include-body)
  24539. location (org-icalendar-cleanup-string
  24540. (org-entry-get nil "LOCATION")))
  24541. (if (string-match org-bracket-link-regexp hd)
  24542. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24543. (match-string 1 hd))
  24544. t t hd)))
  24545. (if (string-match org-priority-regexp hd)
  24546. (setq pri (string-to-char (match-string 2 hd))
  24547. hd (concat (substring hd 0 (match-beginning 1))
  24548. (substring hd (match-end 1))))
  24549. (setq pri org-default-priority))
  24550. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24551. (- org-lowest-priority org-highest-priority))))))
  24552. (princ (format "BEGIN:VTODO
  24553. %s
  24554. SUMMARY:%s%s%s
  24555. CATEGORIES:%s
  24556. SEQUENCE:1
  24557. PRIORITY:%d
  24558. STATUS:%s
  24559. END:VTODO\n"
  24560. dts
  24561. (or summary hd)
  24562. (if (and location (string-match "\\S-" location))
  24563. (concat "\nLOCATION: " location) "")
  24564. (if (and desc (string-match "\\S-" desc))
  24565. (concat "\nDESCRIPTION: " desc) "")
  24566. category pri status)))))))))
  24567. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24568. "Take out stuff and quote what needs to be quoted.
  24569. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24570. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24571. characters."
  24572. (if (not s)
  24573. nil
  24574. (when is-body
  24575. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24576. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24577. (while (string-match re s) (setq s (replace-match "" t t s)))
  24578. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24579. (let ((start 0))
  24580. (while (string-match "\\([,;\\]\\)" s start)
  24581. (setq start (+ (match-beginning 0) 2)
  24582. s (replace-match "\\\\\\1" nil nil s))))
  24583. (when is-body
  24584. (while (string-match "[ \t]*\n[ \t]*" s)
  24585. (setq s (replace-match "\\n" t t s))))
  24586. (setq s (org-trim s))
  24587. (if is-body
  24588. (if maxlength
  24589. (if (and (numberp maxlength)
  24590. (> (length s) maxlength))
  24591. (setq s (substring s 0 maxlength)))))
  24592. s))
  24593. (defun org-get-entry ()
  24594. "Clean-up description string."
  24595. (save-excursion
  24596. (org-back-to-heading t)
  24597. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24598. (defun org-start-icalendar-file (name)
  24599. "Start an iCalendar file by inserting the header."
  24600. (let ((user user-full-name)
  24601. (name (or name "unknown"))
  24602. (timezone (cadr (current-time-zone))))
  24603. (princ
  24604. (format "BEGIN:VCALENDAR
  24605. VERSION:2.0
  24606. X-WR-CALNAME:%s
  24607. PRODID:-//%s//Emacs with Org-mode//EN
  24608. X-WR-TIMEZONE:%s
  24609. CALSCALE:GREGORIAN\n" name user timezone))))
  24610. (defun org-finish-icalendar-file ()
  24611. "Finish an iCalendar file by inserting the END statement."
  24612. (princ "END:VCALENDAR\n"))
  24613. (defun org-ical-ts-to-string (s keyword &optional inc)
  24614. "Take a time string S and convert it to iCalendar format.
  24615. KEYWORD is added in front, to make a complete line like DTSTART....
  24616. When INC is non-nil, increase the hour by two (if time string contains
  24617. a time), or the day by one (if it does not contain a time)."
  24618. (let ((t1 (org-parse-time-string s 'nodefault))
  24619. t2 fmt have-time time)
  24620. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24621. (setq t2 t1 have-time t)
  24622. (setq t2 (org-parse-time-string s)))
  24623. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24624. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24625. (when inc
  24626. (if have-time
  24627. (if org-agenda-default-appointment-duration
  24628. (setq mi (+ org-agenda-default-appointment-duration mi))
  24629. (setq h (+ 2 h)))
  24630. (setq d (1+ d))))
  24631. (setq time (encode-time s mi h d m y)))
  24632. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24633. (concat keyword (format-time-string fmt time))))
  24634. ;;; XOXO export
  24635. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24636. (with-current-buffer buffer
  24637. (apply 'insert output)))
  24638. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24639. (defun org-export-as-xoxo (&optional buffer)
  24640. "Export the org buffer as XOXO.
  24641. The XOXO buffer is named *xoxo-<source buffer name>*"
  24642. (interactive (list (current-buffer)))
  24643. ;; A quickie abstraction
  24644. ;; Output everything as XOXO
  24645. (with-current-buffer (get-buffer buffer)
  24646. (let* ((pos (point))
  24647. (opt-plist (org-combine-plists (org-default-export-plist)
  24648. (org-infile-export-plist)))
  24649. (filename (concat (file-name-as-directory
  24650. (org-export-directory :xoxo opt-plist))
  24651. (file-name-sans-extension
  24652. (file-name-nondirectory buffer-file-name))
  24653. ".html"))
  24654. (out (find-file-noselect filename))
  24655. (last-level 1)
  24656. (hanging-li nil))
  24657. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24658. ;; Check the output buffer is empty.
  24659. (with-current-buffer out (erase-buffer))
  24660. ;; Kick off the output
  24661. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24662. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24663. (let* ((hd (match-string-no-properties 1))
  24664. (level (length hd))
  24665. (text (concat
  24666. (match-string-no-properties 2)
  24667. (save-excursion
  24668. (goto-char (match-end 0))
  24669. (let ((str ""))
  24670. (catch 'loop
  24671. (while 't
  24672. (forward-line)
  24673. (if (looking-at "^[ \t]\\(.*\\)")
  24674. (setq str (concat str (match-string-no-properties 1)))
  24675. (throw 'loop str)))))))))
  24676. ;; Handle level rendering
  24677. (cond
  24678. ((> level last-level)
  24679. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24680. ((< level last-level)
  24681. (dotimes (- (- last-level level) 1)
  24682. (if hanging-li
  24683. (org-export-as-xoxo-insert-into out "</li>\n"))
  24684. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24685. (when hanging-li
  24686. (org-export-as-xoxo-insert-into out "</li>\n")
  24687. (setq hanging-li nil)))
  24688. ((equal level last-level)
  24689. (if hanging-li
  24690. (org-export-as-xoxo-insert-into out "</li>\n")))
  24691. )
  24692. (setq last-level level)
  24693. ;; And output the new li
  24694. (setq hanging-li 't)
  24695. (if (equal ?+ (elt text 0))
  24696. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24697. (org-export-as-xoxo-insert-into out "<li>" text))))
  24698. ;; Finally finish off the ol
  24699. (dotimes (- last-level 1)
  24700. (if hanging-li
  24701. (org-export-as-xoxo-insert-into out "</li>\n"))
  24702. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24703. (goto-char pos)
  24704. ;; Finish the buffer off and clean it up.
  24705. (switch-to-buffer-other-window out)
  24706. (indent-region (point-min) (point-max) nil)
  24707. (save-buffer)
  24708. (goto-char (point-min))
  24709. )))
  24710. ;;;; Key bindings
  24711. ;; Make `C-c C-x' a prefix key
  24712. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24713. ;; TAB key with modifiers
  24714. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24715. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24716. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24717. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24718. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24719. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24720. ;; The following line is necessary under Suse GNU/Linux
  24721. (unless (featurep 'xemacs)
  24722. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24723. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24724. (define-key org-mode-map [backtab] 'org-shifttab)
  24725. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24726. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24727. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24728. ;; Cursor keys with modifiers
  24729. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24730. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24731. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24732. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24733. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24734. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24735. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24736. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24737. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24738. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24739. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24740. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24741. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24742. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24743. ;;; Extra keys for tty access.
  24744. ;; We only set them when really needed because otherwise the
  24745. ;; menus don't show the simple keys
  24746. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24747. (not window-system))
  24748. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24749. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24750. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24751. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24752. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24753. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24754. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24755. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24756. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24757. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24758. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24759. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24760. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24761. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24762. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24763. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24764. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24765. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24766. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24767. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24768. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24769. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24770. ;; All the other keys
  24771. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24772. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24773. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24774. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24775. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24776. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24777. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24778. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24779. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24780. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24781. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24782. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24783. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24784. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24785. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24786. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24787. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24788. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24789. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24790. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24791. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24792. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24793. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24794. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24795. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24796. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24797. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24798. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24799. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24800. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24801. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24802. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24803. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24804. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24805. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24806. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24807. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24808. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24809. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24810. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24811. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24812. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24813. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24814. (org-defkey org-mode-map "\C-m" 'org-return)
  24815. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24816. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24817. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24818. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24819. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24820. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24821. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24822. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24823. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24824. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24825. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24826. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24827. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24828. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24829. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24830. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24831. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24832. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24833. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24834. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24835. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24836. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24837. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24838. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24839. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24840. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24841. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24842. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24843. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24844. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24845. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24846. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24847. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24848. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24849. (when (featurep 'xemacs)
  24850. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24851. (defsubst org-table-p () (org-at-table-p))
  24852. (defun org-self-insert-command (N)
  24853. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24854. If the cursor is in a table looking at whitespace, the whitespace is
  24855. overwritten, and the table is not marked as requiring realignment."
  24856. (interactive "p")
  24857. (if (and (org-table-p)
  24858. (progn
  24859. ;; check if we blank the field, and if that triggers align
  24860. (and org-table-auto-blank-field
  24861. (member last-command
  24862. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24863. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24864. ;; got extra space, this field does not determine column width
  24865. (let (org-table-may-need-update) (org-table-blank-field))
  24866. ;; no extra space, this field may determine column width
  24867. (org-table-blank-field)))
  24868. t)
  24869. (eq N 1)
  24870. (looking-at "[^|\n]* |"))
  24871. (let (org-table-may-need-update)
  24872. (goto-char (1- (match-end 0)))
  24873. (delete-backward-char 1)
  24874. (goto-char (match-beginning 0))
  24875. (self-insert-command N))
  24876. (setq org-table-may-need-update t)
  24877. (self-insert-command N)
  24878. (org-fix-tags-on-the-fly)))
  24879. (defun org-fix-tags-on-the-fly ()
  24880. (when (and (equal (char-after (point-at-bol)) ?*)
  24881. (org-on-heading-p))
  24882. (org-align-tags-here org-tags-column)))
  24883. (defun org-delete-backward-char (N)
  24884. "Like `delete-backward-char', insert whitespace at field end in tables.
  24885. When deleting backwards, in tables this function will insert whitespace in
  24886. front of the next \"|\" separator, to keep the table aligned. The table will
  24887. still be marked for re-alignment if the field did fill the entire column,
  24888. because, in this case the deletion might narrow the column."
  24889. (interactive "p")
  24890. (if (and (org-table-p)
  24891. (eq N 1)
  24892. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24893. (looking-at ".*?|"))
  24894. (let ((pos (point))
  24895. (noalign (looking-at "[^|\n\r]* |"))
  24896. (c org-table-may-need-update))
  24897. (backward-delete-char N)
  24898. (skip-chars-forward "^|")
  24899. (insert " ")
  24900. (goto-char (1- pos))
  24901. ;; noalign: if there were two spaces at the end, this field
  24902. ;; does not determine the width of the column.
  24903. (if noalign (setq org-table-may-need-update c)))
  24904. (backward-delete-char N)
  24905. (org-fix-tags-on-the-fly)))
  24906. (defun org-delete-char (N)
  24907. "Like `delete-char', but insert whitespace at field end in tables.
  24908. When deleting characters, in tables this function will insert whitespace in
  24909. front of the next \"|\" separator, to keep the table aligned. The table will
  24910. still be marked for re-alignment if the field did fill the entire column,
  24911. because, in this case the deletion might narrow the column."
  24912. (interactive "p")
  24913. (if (and (org-table-p)
  24914. (not (bolp))
  24915. (not (= (char-after) ?|))
  24916. (eq N 1))
  24917. (if (looking-at ".*?|")
  24918. (let ((pos (point))
  24919. (noalign (looking-at "[^|\n\r]* |"))
  24920. (c org-table-may-need-update))
  24921. (replace-match (concat
  24922. (substring (match-string 0) 1 -1)
  24923. " |"))
  24924. (goto-char pos)
  24925. ;; noalign: if there were two spaces at the end, this field
  24926. ;; does not determine the width of the column.
  24927. (if noalign (setq org-table-may-need-update c)))
  24928. (delete-char N))
  24929. (delete-char N)
  24930. (org-fix-tags-on-the-fly)))
  24931. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24932. (put 'org-self-insert-command 'delete-selection t)
  24933. (put 'orgtbl-self-insert-command 'delete-selection t)
  24934. (put 'org-delete-char 'delete-selection 'supersede)
  24935. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24936. ;; Make `flyspell-mode' delay after some commands
  24937. (put 'org-self-insert-command 'flyspell-delayed t)
  24938. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24939. (put 'org-delete-char 'flyspell-delayed t)
  24940. (put 'org-delete-backward-char 'flyspell-delayed t)
  24941. ;; Make pabbrev-mode expand after org-mode commands
  24942. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24943. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24944. ;; How to do this: Measure non-white length of current string
  24945. ;; If equal to column width, we should realign.
  24946. (defun org-remap (map &rest commands)
  24947. "In MAP, remap the functions given in COMMANDS.
  24948. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24949. (let (new old)
  24950. (while commands
  24951. (setq old (pop commands) new (pop commands))
  24952. (if (fboundp 'command-remapping)
  24953. (org-defkey map (vector 'remap old) new)
  24954. (substitute-key-definition old new map global-map)))))
  24955. (when (eq org-enable-table-editor 'optimized)
  24956. ;; If the user wants maximum table support, we need to hijack
  24957. ;; some standard editing functions
  24958. (org-remap org-mode-map
  24959. 'self-insert-command 'org-self-insert-command
  24960. 'delete-char 'org-delete-char
  24961. 'delete-backward-char 'org-delete-backward-char)
  24962. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24963. (defun org-shiftcursor-error ()
  24964. "Throw an error because Shift-Cursor command was applied in wrong context."
  24965. (error "This command is active in special context like tables, headlines or timestamps"))
  24966. (defun org-shifttab (&optional arg)
  24967. "Global visibility cycling or move to previous table field.
  24968. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24969. on context.
  24970. See the individual commands for more information."
  24971. (interactive "P")
  24972. (cond
  24973. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24974. (arg (message "Content view to level: ")
  24975. (org-content (prefix-numeric-value arg))
  24976. (setq org-cycle-global-status 'overview))
  24977. (t (call-interactively 'org-global-cycle))))
  24978. (defun org-shiftmetaleft ()
  24979. "Promote subtree or delete table column.
  24980. Calls `org-promote-subtree', `org-outdent-item',
  24981. or `org-table-delete-column', depending on context.
  24982. See the individual commands for more information."
  24983. (interactive)
  24984. (cond
  24985. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24986. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24987. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24988. (t (org-shiftcursor-error))))
  24989. (defun org-shiftmetaright ()
  24990. "Demote subtree or insert table column.
  24991. Calls `org-demote-subtree', `org-indent-item',
  24992. or `org-table-insert-column', depending on context.
  24993. See the individual commands for more information."
  24994. (interactive)
  24995. (cond
  24996. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24997. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24998. ((org-at-item-p) (call-interactively 'org-indent-item))
  24999. (t (org-shiftcursor-error))))
  25000. (defun org-shiftmetaup (&optional arg)
  25001. "Move subtree up or kill table row.
  25002. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25003. `org-move-item-up' depending on context. See the individual commands
  25004. for more information."
  25005. (interactive "P")
  25006. (cond
  25007. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25008. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25009. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25010. (t (org-shiftcursor-error))))
  25011. (defun org-shiftmetadown (&optional arg)
  25012. "Move subtree down or insert table row.
  25013. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25014. `org-move-item-down', depending on context. See the individual
  25015. commands for more information."
  25016. (interactive "P")
  25017. (cond
  25018. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25019. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25020. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25021. (t (org-shiftcursor-error))))
  25022. (defun org-metaleft (&optional arg)
  25023. "Promote heading or move table column to left.
  25024. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25025. With no specific context, calls the Emacs default `backward-word'.
  25026. See the individual commands for more information."
  25027. (interactive "P")
  25028. (cond
  25029. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25030. ((or (org-on-heading-p) (org-region-active-p))
  25031. (call-interactively 'org-do-promote))
  25032. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25033. (t (call-interactively 'backward-word))))
  25034. (defun org-metaright (&optional arg)
  25035. "Demote subtree or move table column to right.
  25036. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25037. With no specific context, calls the Emacs default `forward-word'.
  25038. See the individual commands for more information."
  25039. (interactive "P")
  25040. (cond
  25041. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25042. ((or (org-on-heading-p) (org-region-active-p))
  25043. (call-interactively 'org-do-demote))
  25044. ((org-at-item-p) (call-interactively 'org-indent-item))
  25045. (t (call-interactively 'forward-word))))
  25046. (defun org-metaup (&optional arg)
  25047. "Move subtree up or move table row up.
  25048. Calls `org-move-subtree-up' or `org-table-move-row' or
  25049. `org-move-item-up', depending on context. See the individual commands
  25050. for more information."
  25051. (interactive "P")
  25052. (cond
  25053. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25054. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25055. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25056. (t (transpose-lines 1) (beginning-of-line -1))))
  25057. (defun org-metadown (&optional arg)
  25058. "Move subtree down or move table row down.
  25059. Calls `org-move-subtree-down' or `org-table-move-row' or
  25060. `org-move-item-down', depending on context. See the individual
  25061. commands for more information."
  25062. (interactive "P")
  25063. (cond
  25064. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25065. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25066. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25067. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25068. (defun org-shiftup (&optional arg)
  25069. "Increase item in timestamp or increase priority of current headline.
  25070. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25071. depending on context. See the individual commands for more information."
  25072. (interactive "P")
  25073. (cond
  25074. ((org-at-timestamp-p t)
  25075. (call-interactively (if org-edit-timestamp-down-means-later
  25076. 'org-timestamp-down 'org-timestamp-up)))
  25077. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25078. ((org-at-item-p) (call-interactively 'org-previous-item))
  25079. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25080. (defun org-shiftdown (&optional arg)
  25081. "Decrease item in timestamp or decrease priority of current headline.
  25082. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25083. depending on context. See the individual commands for more information."
  25084. (interactive "P")
  25085. (cond
  25086. ((org-at-timestamp-p t)
  25087. (call-interactively (if org-edit-timestamp-down-means-later
  25088. 'org-timestamp-up 'org-timestamp-down)))
  25089. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25090. (t (call-interactively 'org-next-item))))
  25091. (defun org-shiftright ()
  25092. "Next TODO keyword or timestamp one day later, depending on context."
  25093. (interactive)
  25094. (cond
  25095. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25096. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25097. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25098. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25099. (t (org-shiftcursor-error))))
  25100. (defun org-shiftleft ()
  25101. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25102. (interactive)
  25103. (cond
  25104. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25105. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25106. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25107. ((org-at-property-p)
  25108. (call-interactively 'org-property-previous-allowed-value))
  25109. (t (org-shiftcursor-error))))
  25110. (defun org-shiftcontrolright ()
  25111. "Switch to next TODO set."
  25112. (interactive)
  25113. (cond
  25114. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25115. (t (org-shiftcursor-error))))
  25116. (defun org-shiftcontrolleft ()
  25117. "Switch to previous TODO set."
  25118. (interactive)
  25119. (cond
  25120. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25121. (t (org-shiftcursor-error))))
  25122. (defun org-ctrl-c-ret ()
  25123. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25124. (interactive)
  25125. (cond
  25126. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25127. (t (call-interactively 'org-insert-heading))))
  25128. (defun org-copy-special ()
  25129. "Copy region in table or copy current subtree.
  25130. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25131. See the individual commands for more information."
  25132. (interactive)
  25133. (call-interactively
  25134. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25135. (defun org-cut-special ()
  25136. "Cut region in table or cut current subtree.
  25137. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25138. See the individual commands for more information."
  25139. (interactive)
  25140. (call-interactively
  25141. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25142. (defun org-paste-special (arg)
  25143. "Paste rectangular region into table, or past subtree relative to level.
  25144. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25145. See the individual commands for more information."
  25146. (interactive "P")
  25147. (if (org-at-table-p)
  25148. (org-table-paste-rectangle)
  25149. (org-paste-subtree arg)))
  25150. (defun org-ctrl-c-ctrl-c (&optional arg)
  25151. "Set tags in headline, or update according to changed information at point.
  25152. This command does many different things, depending on context:
  25153. - If the cursor is in a headline, prompt for tags and insert them
  25154. into the current line, aligned to `org-tags-column'. When called
  25155. with prefix arg, realign all tags in the current buffer.
  25156. - If the cursor is in one of the special #+KEYWORD lines, this
  25157. triggers scanning the buffer for these lines and updating the
  25158. information.
  25159. - If the cursor is inside a table, realign the table. This command
  25160. works even if the automatic table editor has been turned off.
  25161. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25162. the entire table.
  25163. - If the cursor is a the beginning of a dynamic block, update it.
  25164. - If the cursor is inside a table created by the table.el package,
  25165. activate that table.
  25166. - If the current buffer is a remember buffer, close note and file it.
  25167. with a prefix argument, file it without further interaction to the default
  25168. location.
  25169. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25170. links in this buffer.
  25171. - If the cursor is on a numbered item in a plain list, renumber the
  25172. ordered list.
  25173. - If the cursor is on a checkbox, toggle it."
  25174. (interactive "P")
  25175. (let ((org-enable-table-editor t))
  25176. (cond
  25177. ((or org-clock-overlays
  25178. org-occur-highlights
  25179. org-latex-fragment-image-overlays)
  25180. (org-remove-clock-overlays)
  25181. (org-remove-occur-highlights)
  25182. (org-remove-latex-fragment-image-overlays)
  25183. (message "Temporary highlights/overlays removed from current buffer"))
  25184. ((and (local-variable-p 'org-finish-function (current-buffer))
  25185. (fboundp org-finish-function))
  25186. (funcall org-finish-function))
  25187. ((org-at-property-p)
  25188. (call-interactively 'org-property-action))
  25189. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25190. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25191. ((org-at-table.el-p)
  25192. (require 'table)
  25193. (beginning-of-line 1)
  25194. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25195. (call-interactively 'table-recognize-table))
  25196. ((org-at-table-p)
  25197. (org-table-maybe-eval-formula)
  25198. (if arg
  25199. (call-interactively 'org-table-recalculate)
  25200. (org-table-maybe-recalculate-line))
  25201. (call-interactively 'org-table-align))
  25202. ((org-at-item-checkbox-p)
  25203. (call-interactively 'org-toggle-checkbox))
  25204. ((org-at-item-p)
  25205. (call-interactively 'org-maybe-renumber-ordered-list))
  25206. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25207. ;; Dynamic block
  25208. (beginning-of-line 1)
  25209. (org-update-dblock))
  25210. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25211. (cond
  25212. ((equal (match-string 1) "TBLFM")
  25213. ;; Recalculate the table before this line
  25214. (save-excursion
  25215. (beginning-of-line 1)
  25216. (skip-chars-backward " \r\n\t")
  25217. (if (org-at-table-p)
  25218. (org-call-with-arg 'org-table-recalculate t))))
  25219. (t
  25220. (call-interactively 'org-mode-restart))))
  25221. (t (error "C-c C-c can do nothing useful at this location.")))))
  25222. (defun org-mode-restart ()
  25223. "Restart Org-mode, to scan again for special lines.
  25224. Also updates the keyword regular expressions."
  25225. (interactive)
  25226. (let ((org-inhibit-startup t)) (org-mode))
  25227. (message "Org-mode restarted to refresh keyword and special line setup"))
  25228. (defun org-kill-note-or-show-branches ()
  25229. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25230. (interactive)
  25231. (if (not org-finish-function)
  25232. (call-interactively 'show-branches)
  25233. (let ((org-note-abort t))
  25234. (funcall org-finish-function))))
  25235. (defun org-return (&optional indent)
  25236. "Goto next table row or insert a newline.
  25237. Calls `org-table-next-row' or `newline', depending on context.
  25238. See the individual commands for more information."
  25239. (interactive)
  25240. (cond
  25241. ((bobp) (if indent (newline-and-indent) (newline)))
  25242. ((and (org-at-heading-p)
  25243. (looking-at
  25244. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25245. (org-show-entry)
  25246. (end-of-line 1)
  25247. (newline))
  25248. ((org-at-table-p)
  25249. (org-table-justify-field-maybe)
  25250. (call-interactively 'org-table-next-row))
  25251. (t (if indent (newline-and-indent) (newline)))))
  25252. (defun org-return-indent ()
  25253. (interactive)
  25254. "Goto next table row or insert a newline and indent.
  25255. Calls `org-table-next-row' or `newline-and-indent', depending on
  25256. context. See the individual commands for more information."
  25257. (org-return t))
  25258. (defun org-ctrl-c-minus ()
  25259. "Insert separator line in table or modify bullet type in list.
  25260. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25261. depending on context."
  25262. (interactive)
  25263. (cond
  25264. ((org-at-table-p)
  25265. (call-interactively 'org-table-insert-hline))
  25266. ((org-on-heading-p)
  25267. ;; Convert to item
  25268. (save-excursion
  25269. (beginning-of-line 1)
  25270. (if (looking-at "\\*+ ")
  25271. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25272. ((org-in-item-p)
  25273. (call-interactively 'org-cycle-list-bullet))
  25274. (t (error "`C-c -' does have no function here."))))
  25275. (defun org-meta-return (&optional arg)
  25276. "Insert a new heading or wrap a region in a table.
  25277. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25278. See the individual commands for more information."
  25279. (interactive "P")
  25280. (cond
  25281. ((org-at-table-p)
  25282. (call-interactively 'org-table-wrap-region))
  25283. (t (call-interactively 'org-insert-heading))))
  25284. ;;; Menu entries
  25285. ;; Define the Org-mode menus
  25286. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25287. '("Tbl"
  25288. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25289. ["Next Field" org-cycle (org-at-table-p)]
  25290. ["Previous Field" org-shifttab (org-at-table-p)]
  25291. ["Next Row" org-return (org-at-table-p)]
  25292. "--"
  25293. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25294. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25295. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25296. "--"
  25297. ("Column"
  25298. ["Move Column Left" org-metaleft (org-at-table-p)]
  25299. ["Move Column Right" org-metaright (org-at-table-p)]
  25300. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25301. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25302. ("Row"
  25303. ["Move Row Up" org-metaup (org-at-table-p)]
  25304. ["Move Row Down" org-metadown (org-at-table-p)]
  25305. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25306. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25307. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25308. "--"
  25309. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25310. ("Rectangle"
  25311. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25312. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25313. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25314. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25315. "--"
  25316. ("Calculate"
  25317. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25318. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25319. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25320. "--"
  25321. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25322. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25323. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25324. "--"
  25325. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25326. "--"
  25327. ["Sum Column/Rectangle" org-table-sum
  25328. (or (org-at-table-p) (org-region-active-p))]
  25329. ["Which Column?" org-table-current-column (org-at-table-p)])
  25330. ["Debug Formulas"
  25331. org-table-toggle-formula-debugger
  25332. :style toggle :selected org-table-formula-debug]
  25333. ["Show Col/Row Numbers"
  25334. org-table-toggle-coordinate-overlays
  25335. :style toggle :selected org-table-overlay-coordinates]
  25336. "--"
  25337. ["Create" org-table-create (and (not (org-at-table-p))
  25338. org-enable-table-editor)]
  25339. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25340. ["Import from File" org-table-import (not (org-at-table-p))]
  25341. ["Export to File" org-table-export (org-at-table-p)]
  25342. "--"
  25343. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25344. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25345. '("Org"
  25346. ("Show/Hide"
  25347. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25348. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25349. ["Sparse Tree" org-occur t]
  25350. ["Reveal Context" org-reveal t]
  25351. ["Show All" show-all t]
  25352. "--"
  25353. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25354. "--"
  25355. ["New Heading" org-insert-heading t]
  25356. ("Navigate Headings"
  25357. ["Up" outline-up-heading t]
  25358. ["Next" outline-next-visible-heading t]
  25359. ["Previous" outline-previous-visible-heading t]
  25360. ["Next Same Level" outline-forward-same-level t]
  25361. ["Previous Same Level" outline-backward-same-level t]
  25362. "--"
  25363. ["Jump" org-goto t])
  25364. ("Edit Structure"
  25365. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25366. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25367. "--"
  25368. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25369. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25370. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25371. "--"
  25372. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25373. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25374. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25375. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25376. "--"
  25377. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25378. "--"
  25379. ["Convert to odd levels" org-convert-to-odd-levels t]
  25380. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25381. ("Editing"
  25382. ["Emphasis..." org-emphasize t])
  25383. ("Archive"
  25384. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25385. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25386. ; :active t :keys "C-u C-c C-x C-a"]
  25387. ["Sparse trees open ARCHIVE trees"
  25388. (setq org-sparse-tree-open-archived-trees
  25389. (not org-sparse-tree-open-archived-trees))
  25390. :style toggle :selected org-sparse-tree-open-archived-trees]
  25391. ["Cycling opens ARCHIVE trees"
  25392. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25393. :style toggle :selected org-cycle-open-archived-trees]
  25394. ["Agenda includes ARCHIVE trees"
  25395. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25396. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25397. "--"
  25398. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25399. ; ["Check and Move Children" (org-archive-subtree '(4))
  25400. ; :active t :keys "C-u C-c C-x C-s"]
  25401. )
  25402. "--"
  25403. ("TODO Lists"
  25404. ["TODO/DONE/-" org-todo t]
  25405. ("Select keyword"
  25406. ["Next keyword" org-shiftright (org-on-heading-p)]
  25407. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25408. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25409. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25410. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25411. ["Show TODO Tree" org-show-todo-tree t]
  25412. ["Global TODO list" org-todo-list t]
  25413. "--"
  25414. ["Set Priority" org-priority t]
  25415. ["Priority Up" org-shiftup t]
  25416. ["Priority Down" org-shiftdown t])
  25417. ("TAGS and Properties"
  25418. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25419. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25420. "--"
  25421. ["Set property" 'org-set-property t]
  25422. ["Column view of properties" org-columns t]
  25423. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25424. ("Dates and Scheduling"
  25425. ["Timestamp" org-time-stamp t]
  25426. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25427. ("Change Date"
  25428. ["1 Day Later" org-shiftright t]
  25429. ["1 Day Earlier" org-shiftleft t]
  25430. ["1 ... Later" org-shiftup t]
  25431. ["1 ... Earlier" org-shiftdown t])
  25432. ["Compute Time Range" org-evaluate-time-range t]
  25433. ["Schedule Item" org-schedule t]
  25434. ["Deadline" org-deadline t]
  25435. "--"
  25436. ["Custom time format" org-toggle-time-stamp-overlays
  25437. :style radio :selected org-display-custom-times]
  25438. "--"
  25439. ["Goto Calendar" org-goto-calendar t]
  25440. ["Date from Calendar" org-date-from-calendar t])
  25441. ("Logging work"
  25442. ["Clock in" org-clock-in t]
  25443. ["Clock out" org-clock-out t]
  25444. ["Clock cancel" org-clock-cancel t]
  25445. ["Goto running clock" org-clock-goto t]
  25446. ["Display times" org-clock-display t]
  25447. ["Create clock table" org-clock-report t]
  25448. "--"
  25449. ["Record DONE time"
  25450. (progn (setq org-log-done (not org-log-done))
  25451. (message "Switching to %s will %s record a timestamp"
  25452. (car org-done-keywords)
  25453. (if org-log-done "automatically" "not")))
  25454. :style toggle :selected org-log-done])
  25455. "--"
  25456. ["Agenda Command..." org-agenda t]
  25457. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25458. ("File List for Agenda")
  25459. ("Special views current file"
  25460. ["TODO Tree" org-show-todo-tree t]
  25461. ["Check Deadlines" org-check-deadlines t]
  25462. ["Timeline" org-timeline t]
  25463. ["Tags Tree" org-tags-sparse-tree t])
  25464. "--"
  25465. ("Hyperlinks"
  25466. ["Store Link (Global)" org-store-link t]
  25467. ["Insert Link" org-insert-link t]
  25468. ["Follow Link" org-open-at-point t]
  25469. "--"
  25470. ["Next link" org-next-link t]
  25471. ["Previous link" org-previous-link t]
  25472. "--"
  25473. ["Descriptive Links"
  25474. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25475. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25476. ["Literal Links"
  25477. (progn
  25478. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25479. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25480. "--"
  25481. ["Export/Publish..." org-export t]
  25482. ("LaTeX"
  25483. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25484. :selected org-cdlatex-mode]
  25485. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25486. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25487. ["Modify math symbol" org-cdlatex-math-modify
  25488. (org-inside-LaTeX-fragment-p)]
  25489. ["Export LaTeX fragments as images"
  25490. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25491. :style toggle :selected org-export-with-LaTeX-fragments])
  25492. "--"
  25493. ("Documentation"
  25494. ["Show Version" org-version t]
  25495. ["Info Documentation" org-info t])
  25496. ("Customize"
  25497. ["Browse Org Group" org-customize t]
  25498. "--"
  25499. ["Expand This Menu" org-create-customize-menu
  25500. (fboundp 'customize-menu-create)])
  25501. "--"
  25502. ["Refresh setup" org-mode-restart t]
  25503. ))
  25504. (defun org-info (&optional node)
  25505. "Read documentation for Org-mode in the info system.
  25506. With optional NODE, go directly to that node."
  25507. (interactive)
  25508. (require 'info)
  25509. (Info-goto-node (format "(org)%s" (or node ""))))
  25510. (defun org-install-agenda-files-menu ()
  25511. (let ((bl (buffer-list)))
  25512. (save-excursion
  25513. (while bl
  25514. (set-buffer (pop bl))
  25515. (if (org-mode-p) (setq bl nil)))
  25516. (when (org-mode-p)
  25517. (easy-menu-change
  25518. '("Org") "File List for Agenda"
  25519. (append
  25520. (list
  25521. ["Edit File List" (org-edit-agenda-file-list) t]
  25522. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25523. ["Remove Current File from List" org-remove-file t]
  25524. ["Cycle through agenda files" org-cycle-agenda-files t]
  25525. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25526. "--")
  25527. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25528. ;;;; Documentation
  25529. (defun org-customize ()
  25530. "Call the customize function with org as argument."
  25531. (interactive)
  25532. (customize-browse 'org))
  25533. (defun org-create-customize-menu ()
  25534. "Create a full customization menu for Org-mode, insert it into the menu."
  25535. (interactive)
  25536. (if (fboundp 'customize-menu-create)
  25537. (progn
  25538. (easy-menu-change
  25539. '("Org") "Customize"
  25540. `(["Browse Org group" org-customize t]
  25541. "--"
  25542. ,(customize-menu-create 'org)
  25543. ["Set" Custom-set t]
  25544. ["Save" Custom-save t]
  25545. ["Reset to Current" Custom-reset-current t]
  25546. ["Reset to Saved" Custom-reset-saved t]
  25547. ["Reset to Standard Settings" Custom-reset-standard t]))
  25548. (message "\"Org\"-menu now contains full customization menu"))
  25549. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25550. ;;;; Miscellaneous stuff
  25551. ;;; Generally useful functions
  25552. (defun org-context ()
  25553. "Return a list of contexts of the current cursor position.
  25554. If several contexts apply, all are returned.
  25555. Each context entry is a list with a symbol naming the context, and
  25556. two positions indicating start and end of the context. Possible
  25557. contexts are:
  25558. :headline anywhere in a headline
  25559. :headline-stars on the leading stars in a headline
  25560. :todo-keyword on a TODO keyword (including DONE) in a headline
  25561. :tags on the TAGS in a headline
  25562. :priority on the priority cookie in a headline
  25563. :item on the first line of a plain list item
  25564. :item-bullet on the bullet/number of a plain list item
  25565. :checkbox on the checkbox in a plain list item
  25566. :table in an org-mode table
  25567. :table-special on a special filed in a table
  25568. :table-table in a table.el table
  25569. :link on a hyperlink
  25570. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25571. :target on a <<target>>
  25572. :radio-target on a <<<radio-target>>>
  25573. :latex-fragment on a LaTeX fragment
  25574. :latex-preview on a LaTeX fragment with overlayed preview image
  25575. This function expects the position to be visible because it uses font-lock
  25576. faces as a help to recognize the following contexts: :table-special, :link,
  25577. and :keyword."
  25578. (let* ((f (get-text-property (point) 'face))
  25579. (faces (if (listp f) f (list f)))
  25580. (p (point)) clist o)
  25581. ;; First the large context
  25582. (cond
  25583. ((org-on-heading-p t)
  25584. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25585. (when (progn
  25586. (beginning-of-line 1)
  25587. (looking-at org-todo-line-tags-regexp))
  25588. (push (org-point-in-group p 1 :headline-stars) clist)
  25589. (push (org-point-in-group p 2 :todo-keyword) clist)
  25590. (push (org-point-in-group p 4 :tags) clist))
  25591. (goto-char p)
  25592. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25593. (if (looking-at "\\[#[A-Z0-9]\\]")
  25594. (push (org-point-in-group p 0 :priority) clist)))
  25595. ((org-at-item-p)
  25596. (push (org-point-in-group p 2 :item-bullet) clist)
  25597. (push (list :item (point-at-bol)
  25598. (save-excursion (org-end-of-item) (point)))
  25599. clist)
  25600. (and (org-at-item-checkbox-p)
  25601. (push (org-point-in-group p 0 :checkbox) clist)))
  25602. ((org-at-table-p)
  25603. (push (list :table (org-table-begin) (org-table-end)) clist)
  25604. (if (memq 'org-formula faces)
  25605. (push (list :table-special
  25606. (previous-single-property-change p 'face)
  25607. (next-single-property-change p 'face)) clist)))
  25608. ((org-at-table-p 'any)
  25609. (push (list :table-table) clist)))
  25610. (goto-char p)
  25611. ;; Now the small context
  25612. (cond
  25613. ((org-at-timestamp-p)
  25614. (push (org-point-in-group p 0 :timestamp) clist))
  25615. ((memq 'org-link faces)
  25616. (push (list :link
  25617. (previous-single-property-change p 'face)
  25618. (next-single-property-change p 'face)) clist))
  25619. ((memq 'org-special-keyword faces)
  25620. (push (list :keyword
  25621. (previous-single-property-change p 'face)
  25622. (next-single-property-change p 'face)) clist))
  25623. ((org-on-target-p)
  25624. (push (org-point-in-group p 0 :target) clist)
  25625. (goto-char (1- (match-beginning 0)))
  25626. (if (looking-at org-radio-target-regexp)
  25627. (push (org-point-in-group p 0 :radio-target) clist))
  25628. (goto-char p))
  25629. ((setq o (car (delq nil
  25630. (mapcar
  25631. (lambda (x)
  25632. (if (memq x org-latex-fragment-image-overlays) x))
  25633. (org-overlays-at (point))))))
  25634. (push (list :latex-fragment
  25635. (org-overlay-start o) (org-overlay-end o)) clist)
  25636. (push (list :latex-preview
  25637. (org-overlay-start o) (org-overlay-end o)) clist))
  25638. ((org-inside-LaTeX-fragment-p)
  25639. ;; FIXME: positions wrong.
  25640. (push (list :latex-fragment (point) (point)) clist)))
  25641. (setq clist (nreverse (delq nil clist)))
  25642. clist))
  25643. ;; FIXME: Compare with at-regexp-p Do we need both?
  25644. (defun org-in-regexp (re &optional nlines visually)
  25645. "Check if point is inside a match of regexp.
  25646. Normally only the current line is checked, but you can include NLINES extra
  25647. lines both before and after point into the search.
  25648. If VISUALLY is set, require that the cursor is not after the match but
  25649. really on, so that the block visually is on the match."
  25650. (catch 'exit
  25651. (let ((pos (point))
  25652. (eol (point-at-eol (+ 1 (or nlines 0))))
  25653. (inc (if visually 1 0)))
  25654. (save-excursion
  25655. (beginning-of-line (- 1 (or nlines 0)))
  25656. (while (re-search-forward re eol t)
  25657. (if (and (<= (match-beginning 0) pos)
  25658. (>= (+ inc (match-end 0)) pos))
  25659. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25660. (defun org-at-regexp-p (regexp)
  25661. "Is point inside a match of REGEXP in the current line?"
  25662. (catch 'exit
  25663. (save-excursion
  25664. (let ((pos (point)) (end (point-at-eol)))
  25665. (beginning-of-line 1)
  25666. (while (re-search-forward regexp end t)
  25667. (if (and (<= (match-beginning 0) pos)
  25668. (>= (match-end 0) pos))
  25669. (throw 'exit t)))
  25670. nil))))
  25671. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25672. "Call `multi-occur' with buffers for all agenda files."
  25673. (interactive "sOrg-files matching: \np")
  25674. (let* ((files (org-agenda-files))
  25675. (tnames (mapcar 'file-truename files))
  25676. (extra org-agenda-multi-occur-extra-files)
  25677. f)
  25678. (while (setq f (pop extra))
  25679. (unless (member (file-truename f) tnames)
  25680. (add-to-list 'files f 'append)
  25681. (add-to-list 'tnames (file-truename f) 'append)))
  25682. (multi-occur
  25683. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25684. regexp)))
  25685. (if (boundp 'occur-mode-find-occurrence-hook)
  25686. ;; Emacs 23
  25687. (add-hook 'occur-mode-find-occurrence-hook
  25688. (lambda ()
  25689. (when (org-mode-p)
  25690. (org-reveal))))
  25691. ;; Emacs 22
  25692. (defadvice occur-mode-goto-occurrence
  25693. (after org-occur-reveal activate)
  25694. (and (org-mode-p) (org-reveal)))
  25695. (defadvice occur-mode-goto-occurrence-other-window
  25696. (after org-occur-reveal activate)
  25697. (and (org-mode-p) (org-reveal)))
  25698. (defadvice occur-mode-display-occurrence
  25699. (after org-occur-reveal activate)
  25700. (when (org-mode-p)
  25701. (let ((pos (occur-mode-find-occurrence)))
  25702. (with-current-buffer (marker-buffer pos)
  25703. (save-excursion
  25704. (goto-char pos)
  25705. (org-reveal)))))))
  25706. (defun org-uniquify (list)
  25707. "Remove duplicate elements from LIST."
  25708. (let (res)
  25709. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25710. res))
  25711. (defun org-delete-all (elts list)
  25712. "Remove all elements in ELTS from LIST."
  25713. (while elts
  25714. (setq list (delete (pop elts) list)))
  25715. list)
  25716. (defun org-back-over-empty-lines ()
  25717. "Move backwards over witespace, to the beginning of the first empty line.
  25718. Returns the number o empty lines passed."
  25719. (let ((pos (point)))
  25720. (skip-chars-backward " \t\n\r")
  25721. (beginning-of-line 2)
  25722. (goto-char (min (point) pos))
  25723. (count-lines (point) pos)))
  25724. (defun org-skip-whitespace ()
  25725. (skip-chars-forward " \t\n\r"))
  25726. (defun org-point-in-group (point group &optional context)
  25727. "Check if POINT is in match-group GROUP.
  25728. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25729. match. If the match group does ot exist or point is not inside it,
  25730. return nil."
  25731. (and (match-beginning group)
  25732. (>= point (match-beginning group))
  25733. (<= point (match-end group))
  25734. (if context
  25735. (list context (match-beginning group) (match-end group))
  25736. t)))
  25737. (defun org-switch-to-buffer-other-window (&rest args)
  25738. "Switch to buffer in a second window on the current frame.
  25739. In particular, do not allow pop-up frames."
  25740. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25741. special-display-function)
  25742. (apply 'switch-to-buffer-other-window args)))
  25743. (defun org-combine-plists (&rest plists)
  25744. "Create a single property list from all plists in PLISTS.
  25745. The process starts by copying the first list, and then setting properties
  25746. from the other lists. Settings in the last list are the most significant
  25747. ones and overrule settings in the other lists."
  25748. (let ((rtn (copy-sequence (pop plists)))
  25749. p v ls)
  25750. (while plists
  25751. (setq ls (pop plists))
  25752. (while ls
  25753. (setq p (pop ls) v (pop ls))
  25754. (setq rtn (plist-put rtn p v))))
  25755. rtn))
  25756. (defun org-move-line-down (arg)
  25757. "Move the current line down. With prefix argument, move it past ARG lines."
  25758. (interactive "p")
  25759. (let ((col (current-column))
  25760. beg end pos)
  25761. (beginning-of-line 1) (setq beg (point))
  25762. (beginning-of-line 2) (setq end (point))
  25763. (beginning-of-line (+ 1 arg))
  25764. (setq pos (move-marker (make-marker) (point)))
  25765. (insert (delete-and-extract-region beg end))
  25766. (goto-char pos)
  25767. (move-to-column col)))
  25768. (defun org-move-line-up (arg)
  25769. "Move the current line up. With prefix argument, move it past ARG lines."
  25770. (interactive "p")
  25771. (let ((col (current-column))
  25772. beg end pos)
  25773. (beginning-of-line 1) (setq beg (point))
  25774. (beginning-of-line 2) (setq end (point))
  25775. (beginning-of-line (- arg))
  25776. (setq pos (move-marker (make-marker) (point)))
  25777. (insert (delete-and-extract-region beg end))
  25778. (goto-char pos)
  25779. (move-to-column col)))
  25780. (defun org-replace-escapes (string table)
  25781. "Replace %-escapes in STRING with values in TABLE.
  25782. TABLE is an association list with keys like \"%a\" and string values.
  25783. The sequences in STRING may contain normal field width and padding information,
  25784. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25785. so values can contain further %-escapes if they are define later in TABLE."
  25786. (let ((case-fold-search nil)
  25787. e re rpl)
  25788. (while (setq e (pop table))
  25789. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25790. (while (string-match re string)
  25791. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25792. (cdr e)))
  25793. (setq string (replace-match rpl t t string))))
  25794. string))
  25795. (defun org-sublist (list start end)
  25796. "Return a section of LIST, from START to END.
  25797. Counting starts at 1."
  25798. (let (rtn (c start))
  25799. (setq list (nthcdr (1- start) list))
  25800. (while (and list (<= c end))
  25801. (push (pop list) rtn)
  25802. (setq c (1+ c)))
  25803. (nreverse rtn)))
  25804. (defun org-find-base-buffer-visiting (file)
  25805. "Like `find-buffer-visiting' but alway return the base buffer and
  25806. not an indirect buffer"
  25807. (let ((buf (find-buffer-visiting file)))
  25808. (if buf
  25809. (or (buffer-base-buffer buf) buf)
  25810. nil)))
  25811. (defun org-image-file-name-regexp ()
  25812. "Return regexp matching the file names of images."
  25813. (if (fboundp 'image-file-name-regexp)
  25814. (image-file-name-regexp)
  25815. (let ((image-file-name-extensions
  25816. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25817. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25818. (concat "\\."
  25819. (regexp-opt (nconc (mapcar 'upcase
  25820. image-file-name-extensions)
  25821. image-file-name-extensions)
  25822. t)
  25823. "\\'"))))
  25824. (defun org-file-image-p (file)
  25825. "Return non-nil if FILE is an image."
  25826. (save-match-data
  25827. (string-match (org-image-file-name-regexp) file)))
  25828. ;;; Paragraph filling stuff.
  25829. ;; We want this to be just right, so use the full arsenal.
  25830. (defun org-indent-line-function ()
  25831. "Indent line like previous, but further if previous was headline or item."
  25832. (interactive)
  25833. (let* ((pos (point))
  25834. (itemp (org-at-item-p))
  25835. column bpos bcol tpos tcol bullet btype bullet-type)
  25836. ;; Find the previous relevant line
  25837. (beginning-of-line 1)
  25838. (cond
  25839. ((looking-at "#") (setq column 0))
  25840. ((looking-at "\\*+ ") (setq column 0))
  25841. (t
  25842. (beginning-of-line 0)
  25843. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25844. (beginning-of-line 0))
  25845. (cond
  25846. ((looking-at "\\*+[ \t]+")
  25847. (goto-char (match-end 0))
  25848. (setq column (current-column)))
  25849. ((org-in-item-p)
  25850. (org-beginning-of-item)
  25851. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25852. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25853. (setq bpos (match-beginning 1) tpos (match-end 0)
  25854. bcol (progn (goto-char bpos) (current-column))
  25855. tcol (progn (goto-char tpos) (current-column))
  25856. bullet (match-string 1)
  25857. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25858. (if (not itemp)
  25859. (setq column tcol)
  25860. (goto-char pos)
  25861. (beginning-of-line 1)
  25862. (if (looking-at "\\S-")
  25863. (progn
  25864. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25865. (setq bullet (match-string 1)
  25866. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25867. (setq column (if (equal btype bullet-type) bcol tcol)))
  25868. (setq column (org-get-indentation)))))
  25869. (t (setq column (org-get-indentation))))))
  25870. (goto-char pos)
  25871. (if (<= (current-column) (current-indentation))
  25872. (indent-line-to column)
  25873. (save-excursion (indent-line-to column)))
  25874. (setq column (current-column))
  25875. (beginning-of-line 1)
  25876. (if (looking-at
  25877. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25878. (replace-match (concat "\\1" (format org-property-format
  25879. (match-string 2) (match-string 3)))
  25880. t nil))
  25881. (move-to-column column)))
  25882. (defun org-set-autofill-regexps ()
  25883. (interactive)
  25884. ;; In the paragraph separator we include headlines, because filling
  25885. ;; text in a line directly attached to a headline would otherwise
  25886. ;; fill the headline as well.
  25887. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25888. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25889. ;; The paragraph starter includes hand-formatted lists.
  25890. (org-set-local 'paragraph-start
  25891. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25892. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25893. ;; But only if the user has not turned off tables or fixed-width regions
  25894. (org-set-local
  25895. 'auto-fill-inhibit-regexp
  25896. (concat "\\*+ \\|#\\+"
  25897. "\\|[ \t]*" org-keyword-time-regexp
  25898. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25899. (concat
  25900. "\\|[ \t]*["
  25901. (if org-enable-table-editor "|" "")
  25902. (if org-enable-fixed-width-editor ":" "")
  25903. "]"))))
  25904. ;; We use our own fill-paragraph function, to make sure that tables
  25905. ;; and fixed-width regions are not wrapped. That function will pass
  25906. ;; through to `fill-paragraph' when appropriate.
  25907. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25908. ; Adaptive filling: To get full control, first make sure that
  25909. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25910. (org-set-local 'adaptive-fill-regexp "\000")
  25911. (org-set-local 'adaptive-fill-function
  25912. 'org-adaptive-fill-function)
  25913. (org-set-local
  25914. 'align-mode-rules-list
  25915. '((org-in-buffer-settings
  25916. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25917. (modes . '(org-mode))))))
  25918. (defun org-fill-paragraph (&optional justify)
  25919. "Re-align a table, pass through to fill-paragraph if no table."
  25920. (let ((table-p (org-at-table-p))
  25921. (table.el-p (org-at-table.el-p)))
  25922. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25923. (save-excursion (goto-char (point-at-bol))
  25924. (looking-at outline-regexp)))
  25925. t) ; skip headlines
  25926. (table.el-p t) ; skip table.el tables
  25927. (table-p (org-table-align) t) ; align org-mode tables
  25928. (t nil)))) ; call paragraph-fill
  25929. ;; For reference, this is the default value of adaptive-fill-regexp
  25930. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25931. (defun org-adaptive-fill-function ()
  25932. "Return a fill prefix for org-mode files.
  25933. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25934. work correctly."
  25935. (cond ((looking-at "#[ \t]+")
  25936. (match-string 0))
  25937. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25938. (save-excursion
  25939. (goto-char (match-end 0))
  25940. (make-string (current-column) ?\ )))
  25941. (t nil)))
  25942. ;;;; Functions extending outline functionality
  25943. (defun org-beginning-of-line (&optional arg)
  25944. "Go to the beginning of the current line. If that is invisible, continue
  25945. to a visible line beginning. This makes the function of C-a more intuitive.
  25946. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25947. first attempt, and only move to after the tags when the cursor is already
  25948. beyond the end of the headline."
  25949. (interactive "P")
  25950. (let ((pos (point)))
  25951. (beginning-of-line 1)
  25952. (if (bobp)
  25953. nil
  25954. (backward-char 1)
  25955. (if (org-invisible-p)
  25956. (while (and (not (bobp)) (org-invisible-p))
  25957. (backward-char 1)
  25958. (beginning-of-line 1))
  25959. (forward-char 1)))
  25960. (when org-special-ctrl-a/e
  25961. (cond
  25962. ((and (looking-at org-todo-line-regexp)
  25963. (= (char-after (match-end 1)) ?\ ))
  25964. (goto-char
  25965. (if (eq org-special-ctrl-a/e t)
  25966. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25967. ((= pos (point)) (match-beginning 3))
  25968. (t (point)))
  25969. (cond ((> pos (point)) (point))
  25970. ((not (eq last-command this-command)) (point))
  25971. (t (match-beginning 3))))))
  25972. ((org-at-item-p)
  25973. (goto-char
  25974. (if (eq org-special-ctrl-a/e t)
  25975. (cond ((> pos (match-end 4)) (match-end 4))
  25976. ((= pos (point)) (match-end 4))
  25977. (t (point)))
  25978. (cond ((> pos (point)) (point))
  25979. ((not (eq last-command this-command)) (point))
  25980. (t (match-end 4))))))))))
  25981. (defun org-end-of-line (&optional arg)
  25982. "Go to the end of the line.
  25983. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25984. first attempt, and only move to after the tags when the cursor is already
  25985. beyond the end of the headline."
  25986. (interactive "P")
  25987. (if (or (not org-special-ctrl-a/e)
  25988. (not (org-on-heading-p)))
  25989. (end-of-line arg)
  25990. (let ((pos (point)))
  25991. (beginning-of-line 1)
  25992. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25993. (if (eq org-special-ctrl-a/e t)
  25994. (if (or (< pos (match-beginning 1))
  25995. (= pos (match-end 0)))
  25996. (goto-char (match-beginning 1))
  25997. (goto-char (match-end 0)))
  25998. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25999. (goto-char (match-end 0))
  26000. (goto-char (match-beginning 1))))
  26001. (end-of-line arg)))))
  26002. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26003. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26004. (defun org-kill-line (&optional arg)
  26005. "Kill line, to tags or end of line."
  26006. (interactive "P")
  26007. (cond
  26008. ((or (not org-special-ctrl-k)
  26009. (bolp)
  26010. (not (org-on-heading-p)))
  26011. (call-interactively 'kill-line))
  26012. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26013. (kill-region (point) (match-beginning 1))
  26014. (org-set-tags nil t))
  26015. (t (kill-region (point) (point-at-eol)))))
  26016. (define-key org-mode-map "\C-k" 'org-kill-line)
  26017. (defun org-invisible-p ()
  26018. "Check if point is at a character currently not visible."
  26019. ;; Early versions of noutline don't have `outline-invisible-p'.
  26020. (if (fboundp 'outline-invisible-p)
  26021. (outline-invisible-p)
  26022. (get-char-property (point) 'invisible)))
  26023. (defun org-invisible-p2 ()
  26024. "Check if point is at a character currently not visible."
  26025. (save-excursion
  26026. (if (and (eolp) (not (bobp))) (backward-char 1))
  26027. ;; Early versions of noutline don't have `outline-invisible-p'.
  26028. (if (fboundp 'outline-invisible-p)
  26029. (outline-invisible-p)
  26030. (get-char-property (point) 'invisible))))
  26031. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26032. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26033. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26034. (defun org-at-heading-or-item-p ()
  26035. (or (org-on-heading-p) (org-at-item-p)))
  26036. (defun org-on-target-p ()
  26037. (or (org-in-regexp org-radio-target-regexp)
  26038. (org-in-regexp org-target-regexp)))
  26039. (defun org-up-heading-all (arg)
  26040. "Move to the heading line of which the present line is a subheading.
  26041. This function considers both visible and invisible heading lines.
  26042. With argument, move up ARG levels."
  26043. (if (fboundp 'outline-up-heading-all)
  26044. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26045. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26046. (defun org-up-heading-safe ()
  26047. "Move to the heading line of which the present line is a subheading.
  26048. This version will not throw an error. It will return the level of the
  26049. headline found, or nil if no higher level is found."
  26050. (let ((pos (point)) start-level level
  26051. (re (concat "^" outline-regexp)))
  26052. (catch 'exit
  26053. (outline-back-to-heading t)
  26054. (setq start-level (funcall outline-level))
  26055. (if (equal start-level 1) (throw 'exit nil))
  26056. (while (re-search-backward re nil t)
  26057. (setq level (funcall outline-level))
  26058. (if (< level start-level) (throw 'exit level)))
  26059. nil)))
  26060. (defun org-first-sibling-p ()
  26061. "Is this heading the first child of its parents?"
  26062. (interactive)
  26063. (let ((re (concat "^" outline-regexp))
  26064. level l)
  26065. (unless (org-at-heading-p t)
  26066. (error "Not at a heading"))
  26067. (setq level (funcall outline-level))
  26068. (save-excursion
  26069. (if (not (re-search-backward re nil t))
  26070. t
  26071. (setq l (funcall outline-level))
  26072. (< l level)))))
  26073. (defun org-goto-sibling (&optional previous)
  26074. "Goto the next sibling, even if it is invisible.
  26075. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26076. when a sibling was found. When none is found, return nil and don't
  26077. move point."
  26078. (let ((fun (if previous 're-search-backward 're-search-forward))
  26079. (pos (point))
  26080. (re (concat "^" outline-regexp))
  26081. level l)
  26082. (when (condition-case nil (org-back-to-heading t) (error nil))
  26083. (setq level (funcall outline-level))
  26084. (catch 'exit
  26085. (or previous (forward-char 1))
  26086. (while (funcall fun re nil t)
  26087. (setq l (funcall outline-level))
  26088. (when (< l level) (goto-char pos) (throw 'exit nil))
  26089. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26090. (goto-char pos)
  26091. nil))))
  26092. (defun org-show-siblings ()
  26093. "Show all siblings of the current headline."
  26094. (save-excursion
  26095. (while (org-goto-sibling) (org-flag-heading nil)))
  26096. (save-excursion
  26097. (while (org-goto-sibling 'previous)
  26098. (org-flag-heading nil))))
  26099. (defun org-show-hidden-entry ()
  26100. "Show an entry where even the heading is hidden."
  26101. (save-excursion
  26102. (org-show-entry)))
  26103. (defun org-flag-heading (flag &optional entry)
  26104. "Flag the current heading. FLAG non-nil means make invisible.
  26105. When ENTRY is non-nil, show the entire entry."
  26106. (save-excursion
  26107. (org-back-to-heading t)
  26108. ;; Check if we should show the entire entry
  26109. (if entry
  26110. (progn
  26111. (org-show-entry)
  26112. (save-excursion
  26113. (and (outline-next-heading)
  26114. (org-flag-heading nil))))
  26115. (outline-flag-region (max (point-min) (1- (point)))
  26116. (save-excursion (outline-end-of-heading) (point))
  26117. flag))))
  26118. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26119. ;; This is an exact copy of the original function, but it uses
  26120. ;; `org-back-to-heading', to make it work also in invisible
  26121. ;; trees. And is uses an invisible-OK argument.
  26122. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26123. (org-back-to-heading invisible-OK)
  26124. (let ((first t)
  26125. (level (funcall outline-level)))
  26126. (while (and (not (eobp))
  26127. (or first (> (funcall outline-level) level)))
  26128. (setq first nil)
  26129. (outline-next-heading))
  26130. (unless to-heading
  26131. (if (memq (preceding-char) '(?\n ?\^M))
  26132. (progn
  26133. ;; Go to end of line before heading
  26134. (forward-char -1)
  26135. (if (memq (preceding-char) '(?\n ?\^M))
  26136. ;; leave blank line before heading
  26137. (forward-char -1))))))
  26138. (point))
  26139. (defun org-show-subtree ()
  26140. "Show everything after this heading at deeper levels."
  26141. (outline-flag-region
  26142. (point)
  26143. (save-excursion
  26144. (outline-end-of-subtree) (outline-next-heading) (point))
  26145. nil))
  26146. (defun org-show-entry ()
  26147. "Show the body directly following this heading.
  26148. Show the heading too, if it is currently invisible."
  26149. (interactive)
  26150. (save-excursion
  26151. (condition-case nil
  26152. (progn
  26153. (org-back-to-heading t)
  26154. (outline-flag-region
  26155. (max (point-min) (1- (point)))
  26156. (save-excursion
  26157. (re-search-forward
  26158. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26159. (or (match-beginning 1) (point-max)))
  26160. nil))
  26161. (error nil))))
  26162. (defun org-make-options-regexp (kwds)
  26163. "Make a regular expression for keyword lines."
  26164. (concat
  26165. "^"
  26166. "#?[ \t]*\\+\\("
  26167. (mapconcat 'regexp-quote kwds "\\|")
  26168. "\\):[ \t]*"
  26169. "\\(.+\\)"))
  26170. ;; Make isearch reveal the necessary context
  26171. (defun org-isearch-end ()
  26172. "Reveal context after isearch exits."
  26173. (when isearch-success ; only if search was successful
  26174. (if (featurep 'xemacs)
  26175. ;; Under XEmacs, the hook is run in the correct place,
  26176. ;; we directly show the context.
  26177. (org-show-context 'isearch)
  26178. ;; In Emacs the hook runs *before* restoring the overlays.
  26179. ;; So we have to use a one-time post-command-hook to do this.
  26180. ;; (Emacs 22 has a special variable, see function `org-mode')
  26181. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26182. isearch-mode-end-hook-quit)
  26183. ;; Only when the isearch was not quitted.
  26184. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26185. 'append 'local)))))
  26186. (defun org-isearch-post-command ()
  26187. "Remove self from hook, and show context."
  26188. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26189. (org-show-context 'isearch))
  26190. ;;;; Integration with and fixes for other packages
  26191. ;;; Imenu support
  26192. (defvar org-imenu-markers nil
  26193. "All markers currently used by Imenu.")
  26194. (make-variable-buffer-local 'org-imenu-markers)
  26195. (defun org-imenu-new-marker (&optional pos)
  26196. "Return a new marker for use by Imenu, and remember the marker."
  26197. (let ((m (make-marker)))
  26198. (move-marker m (or pos (point)))
  26199. (push m org-imenu-markers)
  26200. m))
  26201. (defun org-imenu-get-tree ()
  26202. "Produce the index for Imenu."
  26203. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26204. (setq org-imenu-markers nil)
  26205. (let* ((n org-imenu-depth)
  26206. (re (concat "^" outline-regexp))
  26207. (subs (make-vector (1+ n) nil))
  26208. (last-level 0)
  26209. m tree level head)
  26210. (save-excursion
  26211. (save-restriction
  26212. (widen)
  26213. (goto-char (point-max))
  26214. (while (re-search-backward re nil t)
  26215. (setq level (org-reduced-level (funcall outline-level)))
  26216. (when (<= level n)
  26217. (looking-at org-complex-heading-regexp)
  26218. (setq head (org-match-string-no-properties 4)
  26219. m (org-imenu-new-marker))
  26220. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26221. (if (>= level last-level)
  26222. (push (cons head m) (aref subs level))
  26223. (push (cons head (aref subs (1+ level))) (aref subs level))
  26224. (loop for i from (1+ level) to n do (aset subs i nil)))
  26225. (setq last-level level)))))
  26226. (aref subs 1)))
  26227. (eval-after-load "imenu"
  26228. '(progn
  26229. (add-hook 'imenu-after-jump-hook
  26230. (lambda () (org-show-context 'org-goto)))))
  26231. ;; Speedbar support
  26232. (defun org-speedbar-set-agenda-restriction ()
  26233. "Restrict future agenda commands to the location at point in speedbar.
  26234. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26235. (interactive)
  26236. (let (p m tp np dir txt w)
  26237. (cond
  26238. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26239. 'org-imenu t))
  26240. (setq m (get-text-property p 'org-imenu-marker))
  26241. (save-excursion
  26242. (save-restriction
  26243. (set-buffer (marker-buffer m))
  26244. (goto-char m)
  26245. (org-agenda-set-restriction-lock 'subtree))))
  26246. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26247. 'speedbar-function 'speedbar-find-file))
  26248. (setq tp (previous-single-property-change
  26249. (1+ p) 'speedbar-function)
  26250. np (next-single-property-change
  26251. tp 'speedbar-function)
  26252. dir (speedbar-line-directory)
  26253. txt (buffer-substring-no-properties (or tp (point-min))
  26254. (or np (point-max))))
  26255. (save-excursion
  26256. (save-restriction
  26257. (set-buffer (find-file-noselect
  26258. (let ((default-directory dir))
  26259. (expand-file-name txt))))
  26260. (unless (org-mode-p)
  26261. (error "Cannot restrict to non-Org-mode file"))
  26262. (org-agenda-set-restriction-lock 'file))))
  26263. (t (error "Don't know how to restrict Org-mode's agenda")))
  26264. (org-move-overlay org-speedbar-restriction-lock-overlay
  26265. (point-at-bol) (point-at-eol))
  26266. (setq current-prefix-arg nil)
  26267. (org-agenda-maybe-redo)))
  26268. (eval-after-load "speedbar"
  26269. '(progn
  26270. (speedbar-add-supported-extension ".org")
  26271. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26272. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26273. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26274. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26275. (add-hook 'speedbar-visiting-tag-hook
  26276. (lambda () (org-show-context 'org-goto)))))
  26277. ;;; Fixes and Hacks
  26278. ;; Make flyspell not check words in links, to not mess up our keymap
  26279. (defun org-mode-flyspell-verify ()
  26280. "Don't let flyspell put overlays at active buttons."
  26281. (not (get-text-property (point) 'keymap)))
  26282. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26283. (eval-after-load "bookmark"
  26284. '(if (boundp 'bookmark-after-jump-hook)
  26285. ;; We can use the hook
  26286. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26287. ;; Hook not available, use advice
  26288. (defadvice bookmark-jump (after org-make-visible activate)
  26289. "Make the position visible."
  26290. (org-bookmark-jump-unhide))))
  26291. (defun org-bookmark-jump-unhide ()
  26292. "Unhide the current position, to show the bookmark location."
  26293. (and (org-mode-p)
  26294. (or (org-invisible-p)
  26295. (save-excursion (goto-char (max (point-min) (1- (point))))
  26296. (org-invisible-p)))
  26297. (org-show-context 'bookmark-jump)))
  26298. ;; Fix a bug in htmlize where there are text properties (face nil)
  26299. (eval-after-load "htmlize"
  26300. '(progn
  26301. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26302. "Make sure there are no nil faces"
  26303. (setq ad-return-value (delq nil ad-return-value)))))
  26304. ;; Make session.el ignore our circular variable
  26305. (eval-after-load "session"
  26306. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26307. ;;;; Experimental code
  26308. (defun org-closed-in-range ()
  26309. "Sparse tree of items closed in a certain time range.
  26310. Still experimental, may disappear in the future."
  26311. (interactive)
  26312. ;; Get the time interval from the user.
  26313. (let* ((time1 (time-to-seconds
  26314. (org-read-date nil 'to-time nil "Starting date: ")))
  26315. (time2 (time-to-seconds
  26316. (org-read-date nil 'to-time nil "End date:")))
  26317. ;; callback function
  26318. (callback (lambda ()
  26319. (let ((time
  26320. (time-to-seconds
  26321. (apply 'encode-time
  26322. (org-parse-time-string
  26323. (match-string 1))))))
  26324. ;; check if time in interval
  26325. (and (>= time time1) (<= time time2))))))
  26326. ;; make tree, check each match with the callback
  26327. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26328. ;;;; Finish up
  26329. (provide 'org)
  26330. (run-hooks 'org-load-hook)
  26331. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26332. ;;; org.el ends here