org.el 925 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183
  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 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.10a
  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. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defconst org-version "5.10a"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. (defmacro org-unmodified (&rest body)
  92. "Execute body without changing buffer-modified-p."
  93. `(set-buffer-modified-p
  94. (prog1 (buffer-modified-p) ,@body)))
  95. (defmacro org-re (s)
  96. "Replace posix classes in regular expression."
  97. (if (featurep 'xemacs)
  98. (let ((ss s))
  99. (save-match-data
  100. (while (string-match "\\[:alnum:\\]" ss)
  101. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  102. (while (string-match "\\[:alpha:\\]" ss)
  103. (setq ss (replace-match "a-zA-Z" t t ss)))
  104. ss))
  105. s))
  106. (defmacro org-preserve-lc (&rest body)
  107. `(let ((_line (org-current-line))
  108. (_col (current-column)))
  109. (unwind-protect
  110. (progn ,@body)
  111. (goto-line _line)
  112. (move-to-column _col))))
  113. ;;; The custom variables
  114. (defgroup org nil
  115. "Outline-based notes management and organizer."
  116. :tag "Org"
  117. :group 'outlines
  118. :group 'hypermedia
  119. :group 'calendar)
  120. (defgroup org-startup nil
  121. "Options concerning startup of Org-mode."
  122. :tag "Org Startup"
  123. :group 'org)
  124. (defcustom org-startup-folded t
  125. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  126. This can also be configured on a per-file basis by adding one of
  127. the following lines anywhere in the buffer:
  128. #+STARTUP: fold
  129. #+STARTUP: nofold
  130. #+STARTUP: content"
  131. :group 'org-startup
  132. :type '(choice
  133. (const :tag "nofold: show all" nil)
  134. (const :tag "fold: overview" t)
  135. (const :tag "content: all headlines" content)))
  136. (defcustom org-startup-truncated t
  137. "Non-nil means, entering Org-mode will set `truncate-lines'.
  138. This is useful since some lines containing links can be very long and
  139. uninteresting. Also tables look terrible when wrapped."
  140. :group 'org-startup
  141. :type 'boolean)
  142. (defcustom org-startup-align-all-tables nil
  143. "Non-nil means, align all tables when visiting a file.
  144. This is useful when the column width in tables is forced with <N> cookies
  145. in table fields. Such tables will look correct only after the first re-align.
  146. This can also be configured on a per-file basis by adding one of
  147. the following lines anywhere in the buffer:
  148. #+STARTUP: align
  149. #+STARTUP: noalign"
  150. :group 'org-startup
  151. :type 'boolean)
  152. (defcustom org-insert-mode-line-in-empty-file nil
  153. "Non-nil means insert the first line setting Org-mode in empty files.
  154. When the function `org-mode' is called interactively in an empty file, this
  155. normally means that the file name does not automatically trigger Org-mode.
  156. To ensure that the file will always be in Org-mode in the future, a
  157. line enforcing Org-mode will be inserted into the buffer, if this option
  158. has been set."
  159. :group 'org-startup
  160. :type 'boolean)
  161. (defcustom org-replace-disputed-keys nil
  162. "Non-nil means use alternative key bindings for some keys.
  163. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  164. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  165. If you want to use Org-mode together with one of these other modes,
  166. or more generally if you would like to move some Org-mode commands to
  167. other keys, set this variable and configure the keys with the variable
  168. `org-disputed-keys'.
  169. This option is only relevant at load-time of Org-mode, and must be set
  170. *before* org.el is loaded. Changing it requires a restart of Emacs to
  171. become effective."
  172. :group 'org-startup
  173. :type 'boolean)
  174. (if (fboundp 'defvaralias)
  175. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  176. (defcustom org-disputed-keys
  177. '(([(shift up)] . [(meta p)])
  178. ([(shift down)] . [(meta n)])
  179. ([(shift left)] . [(meta -)])
  180. ([(shift right)] . [(meta +)])
  181. ([(control shift right)] . [(meta shift +)])
  182. ([(control shift left)] . [(meta shift -)]))
  183. "Keys for which Org-mode and other modes compete.
  184. This is an alist, cars are the default keys, second element specifies
  185. the alternative to use when `org-replace-disputed-keys' is t.
  186. Keys can be specified in any syntax supported by `define-key'.
  187. The value of this option takes effect only at Org-mode's startup,
  188. therefore you'll have to restart Emacs to apply it after changing."
  189. :group 'org-startup
  190. :type 'alist)
  191. (defun org-key (key)
  192. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  193. Or return the original if not disputed."
  194. (if org-replace-disputed-keys
  195. (let* ((nkey (key-description key))
  196. (x (org-find-if (lambda (x)
  197. (equal (key-description (car x)) nkey))
  198. org-disputed-keys)))
  199. (if x (cdr x) key))
  200. key))
  201. (defun org-find-if (predicate seq)
  202. (catch 'exit
  203. (while seq
  204. (if (funcall predicate (car seq))
  205. (throw 'exit (car seq))
  206. (pop seq)))))
  207. (defun org-defkey (keymap key def)
  208. "Define a key, possibly translated, as returned by `org-key'."
  209. (define-key keymap (org-key key) def))
  210. (defcustom org-ellipsis nil
  211. "The ellipsis to use in the Org-mode outline.
  212. When nil, just use the standard three dots. When a string, use that instead,
  213. When a face, use the standart 3 dots, but with the specified face.
  214. The change affects only Org-mode (which will then use its own display table).
  215. Changing this requires executing `M-x org-mode' in a buffer to become
  216. effective."
  217. :group 'org-startup
  218. :type '(choice (const :tag "Default" nil)
  219. (face :tag "Face" :value org-warning)
  220. (string :tag "String" :value "...#")))
  221. (defvar org-display-table nil
  222. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  223. (defgroup org-keywords nil
  224. "Keywords in Org-mode."
  225. :tag "Org Keywords"
  226. :group 'org)
  227. (defcustom org-deadline-string "DEADLINE:"
  228. "String to mark deadline entries.
  229. A deadline is this string, followed by a time stamp. Should be a word,
  230. terminated by a colon. You can insert a schedule keyword and
  231. a timestamp with \\[org-deadline].
  232. Changes become only effective after restarting Emacs."
  233. :group 'org-keywords
  234. :type 'string)
  235. (defcustom org-scheduled-string "SCHEDULED:"
  236. "String to mark scheduled TODO entries.
  237. A schedule is this string, followed by a time stamp. Should be a word,
  238. terminated by a colon. You can insert a schedule keyword and
  239. a timestamp with \\[org-schedule].
  240. Changes become only effective after restarting Emacs."
  241. :group 'org-keywords
  242. :type 'string)
  243. (defcustom org-closed-string "CLOSED:"
  244. "String used as the prefix for timestamps logging closing a TODO entry."
  245. :group 'org-keywords
  246. :type 'string)
  247. (defcustom org-clock-string "CLOCK:"
  248. "String used as prefix for timestamps clocking work hours on an item."
  249. :group 'org-keywords
  250. :type 'string)
  251. (defcustom org-comment-string "COMMENT"
  252. "Entries starting with this keyword will never be exported.
  253. An entry can be toggled between COMMENT and normal with
  254. \\[org-toggle-comment].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-quote-string "QUOTE"
  259. "Entries starting with this keyword will be exported in fixed-width font.
  260. Quoting applies only to the text in the entry following the headline, and does
  261. not extend beyond the next headline, even if that is lower level.
  262. An entry can be toggled between QUOTE and normal with
  263. \\[org-toggle-fixed-width-section]."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defconst org-repeat-re
  267. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  268. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  269. "Regular expression for specifying repeated events.
  270. After a match, group 1 contains the repeat expression.")
  271. (defgroup org-structure nil
  272. "Options concerning the general structure of Org-mode files."
  273. :tag "Org Structure"
  274. :group 'org)
  275. (defgroup org-reveal-location nil
  276. "Options about how to make context of a location visible."
  277. :tag "Org Reveal Location"
  278. :group 'org-structure)
  279. (defcustom org-show-hierarchy-above '((default . t))
  280. "Non-nil means, show full hierarchy when revealing a location.
  281. Org-mode often shows locations in an org-mode file which might have
  282. been invisible before. When this is set, the hierarchy of headings
  283. above the exposed location is shown.
  284. Turning this off for example for sparse trees makes them very compact.
  285. Instead of t, this can also be an alist specifying this option for different
  286. contexts. Valid contexts are
  287. agenda when exposing an entry from the agenda
  288. org-goto when using the command `org-goto' on key C-c C-j
  289. occur-tree when using the command `org-occur' on key C-c /
  290. tags-tree when constructing a sparse tree based on tags matches
  291. link-search when exposing search matches associated with a link
  292. mark-goto when exposing the jump goal of a mark
  293. bookmark-jump when exposing a bookmark location
  294. isearch when exiting from an incremental search
  295. default default for all contexts not set explicitly"
  296. :group 'org-reveal-location
  297. :type '(choice
  298. (const :tag "Always" t)
  299. (const :tag "Never" nil)
  300. (repeat :greedy t :tag "Individual contexts"
  301. (cons
  302. (choice :tag "Context"
  303. (const agenda)
  304. (const org-goto)
  305. (const occur-tree)
  306. (const tags-tree)
  307. (const link-search)
  308. (const mark-goto)
  309. (const bookmark-jump)
  310. (const isearch)
  311. (const default))
  312. (boolean)))))
  313. (defcustom org-show-following-heading '((default . nil))
  314. "Non-nil means, show following heading when revealing a location.
  315. Org-mode often shows locations in an org-mode file which might have
  316. been invisible before. When this is set, the heading following the
  317. match is shown.
  318. Turning this off for example for sparse trees makes them very compact,
  319. but makes it harder to edit the location of the match. In such a case,
  320. use the command \\[org-reveal] to show more context.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. See `org-show-hierarchy-above' for valid contexts."
  323. :group 'org-reveal-location
  324. :type '(choice
  325. (const :tag "Always" t)
  326. (const :tag "Never" nil)
  327. (repeat :greedy t :tag "Individual contexts"
  328. (cons
  329. (choice :tag "Context"
  330. (const agenda)
  331. (const org-goto)
  332. (const occur-tree)
  333. (const tags-tree)
  334. (const link-search)
  335. (const mark-goto)
  336. (const bookmark-jump)
  337. (const isearch)
  338. (const default))
  339. (boolean)))))
  340. (defcustom org-show-siblings '((default . nil) (isearch t))
  341. "Non-nil means, show all sibling heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the sibling of the current entry
  344. heading are all made visible. If `org-show-hierarchy-above' is t,
  345. the same happens on each level of the hierarchy above the current entry.
  346. By default this is on for the isearch context, off for all other contexts.
  347. Turning this off for example for sparse trees makes them very compact,
  348. but makes it harder to edit the location of the match. In such a case,
  349. use the command \\[org-reveal] to show more context.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. See `org-show-hierarchy-above' for valid contexts."
  352. :group 'org-reveal-location
  353. :type '(choice
  354. (const :tag "Always" t)
  355. (const :tag "Never" nil)
  356. (repeat :greedy t :tag "Individual contexts"
  357. (cons
  358. (choice :tag "Context"
  359. (const agenda)
  360. (const org-goto)
  361. (const occur-tree)
  362. (const tags-tree)
  363. (const link-search)
  364. (const mark-goto)
  365. (const bookmark-jump)
  366. (const isearch)
  367. (const default))
  368. (boolean)))))
  369. (defgroup org-cycle nil
  370. "Options concerning visibility cycling in Org-mode."
  371. :tag "Org Cycle"
  372. :group 'org-structure)
  373. (defcustom org-drawers '("PROPERTIES")
  374. "Names of drawers. Drawers are not opened by cycling on the headline above.
  375. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  376. this:
  377. :DRAWERNAME:
  378. .....
  379. :END:
  380. The drawer \"PROPERTIES\" is special for capturing properties through
  381. the property API."
  382. :group 'org-structure
  383. :type '(repeat (string :tag "Drawer Name")))
  384. (defcustom org-cycle-global-at-bob nil
  385. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  386. This makes it possible to do global cycling without having to use S-TAB or
  387. C-u TAB. For this special case to work, the first line of the buffer
  388. must not be a headline - it may be empty ot some other text. When used in
  389. this way, `org-cycle-hook' is disables temporarily, to make sure the
  390. cursor stays at the beginning of the buffer.
  391. When this option is nil, don't do anything special at the beginning
  392. of the buffer."
  393. :group 'org-cycle
  394. :type 'boolean)
  395. (defcustom org-cycle-emulate-tab t
  396. "Where should `org-cycle' emulate TAB.
  397. nil Never
  398. white Only in completely white lines
  399. whitestart Only at the beginning of lines, before the first non-white char.
  400. t Everywhere except in headlines
  401. exc-hl-bol Everywhere except at the start of a headline
  402. If TAB is used in a place where it does not emulate TAB, the current subtree
  403. visibility is cycled."
  404. :group 'org-cycle
  405. :type '(choice (const :tag "Never" nil)
  406. (const :tag "Only in completely white lines" white)
  407. (const :tag "Before first char in a line" whitestart)
  408. (const :tag "Everywhere except in headlines" t)
  409. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  410. ))
  411. (defcustom org-cycle-separator-lines 2
  412. "Number of empty lines needed to keep an empty line between collapsed trees.
  413. If you leave an empty line between the end of a subtree and the following
  414. headline, this empty line is hidden when the subtree is folded.
  415. Org-mode will leave (exactly) one empty line visible if the number of
  416. empty lines is equal or larger to the number given in this variable.
  417. So the default 2 means, at least 2 empty lines after the end of a subtree
  418. are needed to produce free space between a collapsed subtree and the
  419. following headline.
  420. Special case: when 0, never leave empty lines in collapsed view."
  421. :group 'org-cycle
  422. :type 'integer)
  423. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  424. org-cycle-hide-drawers
  425. org-cycle-show-empty-lines
  426. org-optimize-window-after-visibility-change)
  427. "Hook that is run after `org-cycle' has changed the buffer visibility.
  428. The function(s) in this hook must accept a single argument which indicates
  429. the new state that was set by the most recent `org-cycle' command. The
  430. argument is a symbol. After a global state change, it can have the values
  431. `overview', `content', or `all'. After a local state change, it can have
  432. the values `folded', `children', or `subtree'."
  433. :group 'org-cycle
  434. :type 'hook)
  435. (defgroup org-edit-structure nil
  436. "Options concerning structure editing in Org-mode."
  437. :tag "Org Edit Structure"
  438. :group 'org-structure)
  439. (defcustom org-special-ctrl-a/e nil
  440. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  441. When t, `C-a' will bring back the cursor to the beginning of the
  442. headline text, i.e. after the stars and after a possible TODO keyword.
  443. In an item, this will be the position after the bullet.
  444. When the cursor is already at that position, another `C-a' will bring
  445. it to the beginning of the line.
  446. `C-e' will jump to the end of the headline, ignoring the presence of tags
  447. in the headline. A second `C-e' will then jump to the true end of the
  448. line, after any tags.
  449. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  450. and only a directly following, identical keypress will bring the cursor
  451. to the special positions."
  452. :group 'org-edit-structure
  453. :type '(choice
  454. (const :tag "off" nil)
  455. (const :tag "after bullet first" t)
  456. (const :tag "border first" reversed)))
  457. (if (fboundp 'defvaralias)
  458. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  459. (defcustom org-odd-levels-only nil
  460. "Non-nil means, skip even levels and only use odd levels for the outline.
  461. This has the effect that two stars are being added/taken away in
  462. promotion/demotion commands. It also influences how levels are
  463. handled by the exporters.
  464. Changing it requires restart of `font-lock-mode' to become effective
  465. for fontification also in regions already fontified.
  466. You may also set this on a per-file basis by adding one of the following
  467. lines to the buffer:
  468. #+STARTUP: odd
  469. #+STARTUP: oddeven"
  470. :group 'org-edit-structure
  471. :group 'org-font-lock
  472. :type 'boolean)
  473. (defcustom org-adapt-indentation t
  474. "Non-nil means, adapt indentation when promoting and demoting.
  475. When this is set and the *entire* text in an entry is indented, the
  476. indentation is increased by one space in a demotion command, and
  477. decreased by one in a promotion command. If any line in the entry
  478. body starts at column 0, indentation is not changed at all."
  479. :group 'org-edit-structure
  480. :type 'boolean)
  481. (defcustom org-blank-before-new-entry '((heading . nil)
  482. (plain-list-item . nil))
  483. "Should `org-insert-heading' leave a blank line before new heading/item?
  484. The value is an alist, with `heading' and `plain-list-item' as car,
  485. and a boolean flag as cdr."
  486. :group 'org-edit-structure
  487. :type '(list
  488. (cons (const heading) (boolean))
  489. (cons (const plain-list-item) (boolean))))
  490. (defcustom org-insert-heading-hook nil
  491. "Hook being run after inserting a new heading."
  492. :group 'org-edit-structure
  493. :type 'boolean)
  494. (defcustom org-enable-fixed-width-editor t
  495. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  496. This currently only means, they are never auto-wrapped.
  497. When nil, such lines will be treated like ordinary lines.
  498. See also the QUOTE keyword."
  499. :group 'org-edit-structure
  500. :type 'boolean)
  501. (defgroup org-sparse-trees nil
  502. "Options concerning sparse trees in Org-mode."
  503. :tag "Org Sparse Trees"
  504. :group 'org-structure)
  505. (defcustom org-highlight-sparse-tree-matches t
  506. "Non-nil means, highlight all matches that define a sparse tree.
  507. The highlights will automatically disappear the next time the buffer is
  508. changed by an edit command."
  509. :group 'org-sparse-trees
  510. :type 'boolean)
  511. (defcustom org-remove-highlights-with-change t
  512. "Non-nil means, any change to the buffer will remove temporary highlights.
  513. Such highlights are created by `org-occur' and `org-clock-display'.
  514. When nil, `C-c C-c needs to be used to get rid of the highlights.
  515. The highlights created by `org-preview-latex-fragment' always need
  516. `C-c C-c' to be removed."
  517. :group 'org-sparse-trees
  518. :group 'org-time
  519. :type 'boolean)
  520. (defcustom org-occur-hook '(org-first-headline-recenter)
  521. "Hook that is run after `org-occur' has constructed a sparse tree.
  522. This can be used to recenter the window to show as much of the structure
  523. as possible."
  524. :group 'org-sparse-trees
  525. :type 'hook)
  526. (defgroup org-plain-lists nil
  527. "Options concerning plain lists in Org-mode."
  528. :tag "Org Plain lists"
  529. :group 'org-structure)
  530. (defcustom org-cycle-include-plain-lists nil
  531. "Non-nil means, include plain lists into visibility cycling.
  532. This means that during cycling, plain list items will *temporarily* be
  533. interpreted as outline headlines with a level given by 1000+i where i is the
  534. indentation of the bullet. In all other operations, plain list items are
  535. not seen as headlines. For example, you cannot assign a TODO keyword to
  536. such an item."
  537. :group 'org-plain-lists
  538. :type 'boolean)
  539. (defcustom org-plain-list-ordered-item-terminator t
  540. "The character that makes a line with leading number an ordered list item.
  541. Valid values are ?. and ?\). To get both terminators, use t. While
  542. ?. may look nicer, it creates the danger that a line with leading
  543. number may be incorrectly interpreted as an item. ?\) therefore is
  544. the safe choice."
  545. :group 'org-plain-lists
  546. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  547. (const :tag "paren like in \"2)\"" ?\))
  548. (const :tab "both" t)))
  549. (defcustom org-auto-renumber-ordered-lists t
  550. "Non-nil means, automatically renumber ordered plain lists.
  551. Renumbering happens when the sequence have been changed with
  552. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  553. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  554. :group 'org-plain-lists
  555. :type 'boolean)
  556. (defcustom org-provide-checkbox-statistics t
  557. "Non-nil means, update checkbox statistics after insert and toggle.
  558. When this is set, checkbox statistics is updated each time you either insert
  559. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  560. with \\[org-ctrl-c-ctrl-c\\]."
  561. :group 'org-plain-lists
  562. :type 'boolean)
  563. (defgroup org-archive nil
  564. "Options concerning archiving in Org-mode."
  565. :tag "Org Archive"
  566. :group 'org-structure)
  567. (defcustom org-archive-tag "ARCHIVE"
  568. "The tag that marks a subtree as archived.
  569. An archived subtree does not open during visibility cycling, and does
  570. not contribute to the agenda listings."
  571. :group 'org-archive
  572. :group 'org-keywords
  573. :type 'string)
  574. (defcustom org-agenda-skip-archived-trees t
  575. "Non-nil means, the agenda will skip any items located in archived trees.
  576. An archived tree is a tree marked with the tag ARCHIVE."
  577. :group 'org-archive
  578. :group 'org-agenda-skip
  579. :type 'boolean)
  580. (defcustom org-cycle-open-archived-trees nil
  581. "Non-nil means, `org-cycle' will open archived trees.
  582. An archived tree is a tree marked with the tag ARCHIVE.
  583. When nil, archived trees will stay folded. You can still open them with
  584. normal outline commands like `show-all', but not with the cycling commands."
  585. :group 'org-archive
  586. :group 'org-cycle
  587. :type 'boolean)
  588. (defcustom org-sparse-tree-open-archived-trees nil
  589. "Non-nil means sparse tree construction shows matches in archived trees.
  590. When nil, matches in these trees are highlighted, but the trees are kept in
  591. collapsed state."
  592. :group 'org-archive
  593. :group 'org-sparse-trees
  594. :type 'boolean)
  595. (defcustom org-archive-location "%s_archive::"
  596. "The location where subtrees should be archived.
  597. This string consists of two parts, separated by a double-colon.
  598. The first part is a file name - when omitted, archiving happens in the same
  599. file. %s will be replaced by the current file name (without directory part).
  600. Archiving to a different file is useful to keep archived entries from
  601. contributing to the Org-mode Agenda.
  602. The part after the double colon is a headline. The archived entries will be
  603. filed under that headline. When omitted, the subtrees are simply filed away
  604. at the end of the file, as top-level entries.
  605. Here are a few examples:
  606. \"%s_archive::\"
  607. If the current file is Projects.org, archive in file
  608. Projects.org_archive, as top-level trees. This is the default.
  609. \"::* Archived Tasks\"
  610. Archive in the current file, under the top-level headline
  611. \"* Archived Tasks\".
  612. \"~/org/archive.org::\"
  613. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  614. \"basement::** Finished Tasks\"
  615. Archive in file ./basement (relative path), as level 3 trees
  616. below the level 2 heading \"** Finished Tasks\".
  617. You may set this option on a per-file basis by adding to the buffer a
  618. line like
  619. #+ARCHIVE: basement::** Finished Tasks"
  620. :group 'org-archive
  621. :type 'string)
  622. (defcustom org-archive-mark-done t
  623. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  624. This can be a string to set the keyword to use. When t, Org-mode will
  625. use the first keyword in its list that means done."
  626. :group 'org-archive
  627. :type '(choice
  628. (const :tag "No" nil)
  629. (const :tag "Yes" t)
  630. (string :tag "Use this keyword")))
  631. (defcustom org-archive-stamp-time t
  632. "Non-nil means, add a time stamp to entries moved to an archive file."
  633. :group 'org-archive
  634. :type 'boolean)
  635. (defcustom org-archive-save-context-info '(time file category todo itags)
  636. "Parts of context info that should be stored as properties when archiving.
  637. When a subtree is moved to an archive file, it looses information given by
  638. context, like inherited tags, the category, and possibly also the TODO
  639. state (depending on the variable `org-archive-mark-done').
  640. This variable can be a list of any of the following symbols:
  641. time The time of archiving.
  642. file The file where the entry originates.
  643. itags The local tags, in the headline of the subtree.
  644. ltags The tags the subtree inherits from further up the hierarchy.
  645. todo The pre-archive TODO state.
  646. category The category, taken from file name or #+CATEGORY lines.
  647. For each symbol present in the list, a property will be created in
  648. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  649. information."
  650. :group 'org-archive
  651. :type '(set
  652. (const :tag "File" file)
  653. (const :tag "Category" category)
  654. (const :tag "TODO state" todo)
  655. (const :tag "TODO state" priority)
  656. (const :tag "Inherited tags" itags)
  657. (const :tag "Local tags" ltags)))
  658. (defgroup org-table nil
  659. "Options concerning tables in Org-mode."
  660. :tag "Org Table"
  661. :group 'org)
  662. (defcustom org-enable-table-editor 'optimized
  663. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  664. When nil, such lines will be treated like ordinary lines.
  665. When equal to the symbol `optimized', the table editor will be optimized to
  666. do the following:
  667. - Automatic overwrite mode in front of whitespace in table fields.
  668. This makes the structure of the table stay in tact as long as the edited
  669. field does not exceed the column width.
  670. - Minimize the number of realigns. Normally, the table is aligned each time
  671. TAB or RET are pressed to move to another field. With optimization this
  672. happens only if changes to a field might have changed the column width.
  673. Optimization requires replacing the functions `self-insert-command',
  674. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  675. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  676. very good at guessing when a re-align will be necessary, but you can always
  677. force one with \\[org-ctrl-c-ctrl-c].
  678. If you would like to use the optimized version in Org-mode, but the
  679. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  680. This variable can be used to turn on and off the table editor during a session,
  681. but in order to toggle optimization, a restart is required.
  682. See also the variable `org-table-auto-blank-field'."
  683. :group 'org-table
  684. :type '(choice
  685. (const :tag "off" nil)
  686. (const :tag "on" t)
  687. (const :tag "on, optimized" optimized)))
  688. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  689. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  690. In the optimized version, the table editor takes over all simple keys that
  691. normally just insert a character. In tables, the characters are inserted
  692. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  693. for empty fields). Outside tables, the correct binding of the keys is
  694. restored.
  695. The default for this option is t if the optimized version is also used in
  696. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  697. this variable requires a restart of Emacs to become effective."
  698. :group 'org-table
  699. :type 'boolean)
  700. (defcustom orgtbl-radio-table-templates
  701. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  702. % END RECEIVE ORGTBL %n
  703. \\begin{comment}
  704. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  705. | | |
  706. \\end{comment}\n")
  707. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  708. @c END RECEIVE ORGTBL %n
  709. @ignore
  710. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  711. | | |
  712. @end ignore\n")
  713. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  714. <!-- END RECEIVE ORGTBL %n -->
  715. <!--
  716. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  717. | | |
  718. -->\n"))
  719. "Templates for radio tables in different major modes.
  720. All occurrences of %n in a template will be replaced with the name of the
  721. table, obtained by prompting the user."
  722. :group 'org-table
  723. :type '(repeat
  724. (list (symbol :tag "Major mode")
  725. (string :tag "Format"))))
  726. (defgroup org-table-settings nil
  727. "Settings for tables in Org-mode."
  728. :tag "Org Table Settings"
  729. :group 'org-table)
  730. (defcustom org-table-default-size "5x2"
  731. "The default size for newly created tables, Columns x Rows."
  732. :group 'org-table-settings
  733. :type 'string)
  734. (defcustom org-table-number-regexp
  735. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  736. "Regular expression for recognizing numbers in table columns.
  737. If a table column contains mostly numbers, it will be aligned to the
  738. right. If not, it will be aligned to the left.
  739. The default value of this option is a regular expression which allows
  740. anything which looks remotely like a number as used in scientific
  741. context. For example, all of the following will be considered a
  742. number:
  743. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  744. Other options offered by the customize interface are more restrictive."
  745. :group 'org-table-settings
  746. :type '(choice
  747. (const :tag "Positive Integers"
  748. "^[0-9]+$")
  749. (const :tag "Integers"
  750. "^[-+]?[0-9]+$")
  751. (const :tag "Floating Point Numbers"
  752. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  753. (const :tag "Floating Point Number or Integer"
  754. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  755. (const :tag "Exponential, Floating point, Integer"
  756. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  757. (const :tag "Very General Number-Like, including hex"
  758. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  759. (string :tag "Regexp:")))
  760. (defcustom org-table-number-fraction 0.5
  761. "Fraction of numbers in a column required to make the column align right.
  762. In a column all non-white fields are considered. If at least this
  763. fraction of fields is matched by `org-table-number-fraction',
  764. alignment to the right border applies."
  765. :group 'org-table-settings
  766. :type 'number)
  767. (defgroup org-table-editing nil
  768. "Bahavior of tables during editing in Org-mode."
  769. :tag "Org Table Editing"
  770. :group 'org-table)
  771. (defcustom org-table-automatic-realign t
  772. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  773. When nil, aligning is only done with \\[org-table-align], or after column
  774. removal/insertion."
  775. :group 'org-table-editing
  776. :type 'boolean)
  777. (defcustom org-table-auto-blank-field t
  778. "Non-nil means, automatically blank table field when starting to type into it.
  779. This only happens when typing immediately after a field motion
  780. command (TAB, S-TAB or RET).
  781. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  782. :group 'org-table-editing
  783. :type 'boolean)
  784. (defcustom org-table-tab-jumps-over-hlines t
  785. "Non-nil means, tab in the last column of a table with jump over a hline.
  786. If a horizontal separator line is following the current line,
  787. `org-table-next-field' can either create a new row before that line, or jump
  788. over the line. When this option is nil, a new line will be created before
  789. this line."
  790. :group 'org-table-editing
  791. :type 'boolean)
  792. (defcustom org-table-tab-recognizes-table.el t
  793. "Non-nil means, TAB will automatically notice a table.el table.
  794. When it sees such a table, it moves point into it and - if necessary -
  795. calls `table-recognize-table'."
  796. :group 'org-table-editing
  797. :type 'boolean)
  798. (defgroup org-table-calculation nil
  799. "Options concerning tables in Org-mode."
  800. :tag "Org Table Calculation"
  801. :group 'org-table)
  802. (defcustom org-table-use-standard-references t
  803. "Should org-mode work with table refrences like B3 instead of @3$2?
  804. Possible values are:
  805. nil never use them
  806. from accept as input, do not present for editing
  807. t: accept as input and present for editing"
  808. :group 'org-table-calculation
  809. :type '(choice
  810. (const :tag "Never, don't even check unser input for them" nil)
  811. (const :tag "Always, both as user input, and when editing" t)
  812. (const :tag "Convert user input, don't offer during editing" 'from)))
  813. (defcustom org-table-copy-increment t
  814. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  815. :group 'org-table-calculation
  816. :type 'boolean)
  817. (defcustom org-calc-default-modes
  818. '(calc-internal-prec 12
  819. calc-float-format (float 5)
  820. calc-angle-mode deg
  821. calc-prefer-frac nil
  822. calc-symbolic-mode nil
  823. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  824. calc-display-working-message t
  825. )
  826. "List with Calc mode settings for use in calc-eval for table formulas.
  827. The list must contain alternating symbols (Calc modes variables and values).
  828. Don't remove any of the default settings, just change the values. Org-mode
  829. relies on the variables to be present in the list."
  830. :group 'org-table-calculation
  831. :type 'plist)
  832. (defcustom org-table-formula-evaluate-inline t
  833. "Non-nil means, TAB and RET evaluate a formula in current table field.
  834. If the current field starts with an equal sign, it is assumed to be a formula
  835. which should be evaluated as described in the manual and in the documentation
  836. string of the command `org-table-eval-formula'. This feature requires the
  837. Emacs calc package.
  838. When this variable is nil, formula calculation is only available through
  839. the command \\[org-table-eval-formula]."
  840. :group 'org-table-calculation
  841. :type 'boolean)
  842. (defcustom org-table-formula-use-constants t
  843. "Non-nil means, interpret constants in formulas in tables.
  844. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  845. by the value given in `org-table-formula-constants', or by a value obtained
  846. from the `constants.el' package."
  847. :group 'org-table-calculation
  848. :type 'boolean)
  849. (defcustom org-table-formula-constants nil
  850. "Alist with constant names and values, for use in table formulas.
  851. The car of each element is a name of a constant, without the `$' before it.
  852. The cdr is the value as a string. For example, if you'd like to use the
  853. speed of light in a formula, you would configure
  854. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  855. and then use it in an equation like `$1*$c'.
  856. Constants can also be defined on a per-file basis using a line like
  857. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  858. :group 'org-table-calculation
  859. :type '(repeat
  860. (cons (string :tag "name")
  861. (string :tag "value"))))
  862. (defvar org-table-formula-constants-local nil
  863. "Local version of `org-table-formula-constants'.")
  864. (make-variable-buffer-local 'org-table-formula-constants-local)
  865. (defcustom org-table-allow-automatic-line-recalculation t
  866. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  867. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  868. :group 'org-table-calculation
  869. :type 'boolean)
  870. (defgroup org-link nil
  871. "Options concerning links in Org-mode."
  872. :tag "Org Link"
  873. :group 'org)
  874. (defvar org-link-abbrev-alist-local nil
  875. "Buffer-local version of `org-link-abbrev-alist', which see.
  876. The value of this is taken from the #+LINK lines.")
  877. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  878. (defcustom org-link-abbrev-alist nil
  879. "Alist of link abbreviations.
  880. The car of each element is a string, to be replaced at the start of a link.
  881. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  882. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  883. [[linkkey:tag][description]]
  884. If REPLACE is a string, the tag will simply be appended to create the link.
  885. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  886. also be a function that will be called with the tag as the only argument to
  887. create the link. See the manual for examples."
  888. :group 'org-link
  889. :type 'alist)
  890. (defcustom org-descriptive-links t
  891. "Non-nil means, hide link part and only show description of bracket links.
  892. Bracket links are like [[link][descritpion]]. This variable sets the initial
  893. state in new org-mode buffers. The setting can then be toggled on a
  894. per-buffer basis from the Org->Hyperlinks menu."
  895. :group 'org-link
  896. :type 'boolean)
  897. (defcustom org-link-file-path-type 'adaptive
  898. "How the path name in file links should be stored.
  899. Valid values are:
  900. relative relative to the current directory, i.e. the directory of the file
  901. into which the link is being inserted.
  902. absolute absolute path, if possible with ~ for home directory.
  903. noabbrev absolute path, no abbreviation of home directory.
  904. adaptive Use relative path for files in the current directory and sub-
  905. directories of it. For other files, use an absolute path."
  906. :group 'org-link
  907. :type '(choice
  908. (const relative)
  909. (const absolute)
  910. (const noabbrev)
  911. (const adaptive)))
  912. (defcustom org-activate-links '(bracket angle plain radio tag date)
  913. "Types of links that should be activated in Org-mode files.
  914. This is a list of symbols, each leading to the activation of a certain link
  915. type. In principle, it does not hurt to turn on most link types - there may
  916. be a small gain when turning off unused link types. The types are:
  917. bracket The recommended [[link][description]] or [[link]] links with hiding.
  918. angular Links in angular brackes that may contain whitespace like
  919. <bbdb:Carsten Dominik>.
  920. plain Plain links in normal text, no whitespace, like http://google.com.
  921. radio Text that is matched by a radio target, see manual for details.
  922. tag Tag settings in a headline (link to tag search).
  923. date Time stamps (link to calendar).
  924. Changing this variable requires a restart of Emacs to become effective."
  925. :group 'org-link
  926. :type '(set (const :tag "Double bracket links (new style)" bracket)
  927. (const :tag "Angular bracket links (old style)" angular)
  928. (const :tag "plain text links" plain)
  929. (const :tag "Radio target matches" radio)
  930. (const :tag "Tags" tag)
  931. (const :tag "Tags" target)
  932. (const :tag "Timestamps" date)))
  933. (defgroup org-link-store nil
  934. "Options concerning storing links in Org-mode"
  935. :tag "Org Store Link"
  936. :group 'org-link)
  937. (defcustom org-email-link-description-format "Email %c: %.30s"
  938. "Format of the description part of a link to an email or usenet message.
  939. The following %-excapes will be replaced by corresponding information:
  940. %F full \"From\" field
  941. %f name, taken from \"From\" field, address if no name
  942. %T full \"To\" field
  943. %t first name in \"To\" field, address if no name
  944. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  945. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  946. %s subject
  947. %m message-id.
  948. You may use normal field width specification between the % and the letter.
  949. This is for example useful to limit the length of the subject.
  950. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  951. :group 'org-link-store
  952. :type 'string)
  953. (defcustom org-from-is-user-regexp
  954. (let (r1 r2)
  955. (when (and user-mail-address (not (string= user-mail-address "")))
  956. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  957. (when (and user-full-name (not (string= user-full-name "")))
  958. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  959. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  960. "Regexp mached against the \"From:\" header of an email or usenet message.
  961. It should match if the message is from the user him/herself."
  962. :group 'org-link-store
  963. :type 'regexp)
  964. (defcustom org-context-in-file-links t
  965. "Non-nil means, file links from `org-store-link' contain context.
  966. A search string will be added to the file name with :: as separator and
  967. used to find the context when the link is activated by the command
  968. `org-open-at-point'.
  969. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  970. negates this setting for the duration of the command."
  971. :group 'org-link-store
  972. :type 'boolean)
  973. (defcustom org-keep-stored-link-after-insertion nil
  974. "Non-nil means, keep link in list for entire session.
  975. The command `org-store-link' adds a link pointing to the current
  976. location to an internal list. These links accumulate during a session.
  977. The command `org-insert-link' can be used to insert links into any
  978. Org-mode file (offering completion for all stored links). When this
  979. option is nil, every link which has been inserted once using \\[org-insert-link]
  980. will be removed from the list, to make completing the unused links
  981. more efficient."
  982. :group 'org-link-store
  983. :type 'boolean)
  984. (defcustom org-usenet-links-prefer-google nil
  985. "Non-nil means, `org-store-link' will create web links to Google groups.
  986. When nil, Gnus will be used for such links.
  987. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  988. negates this setting for the duration of the command."
  989. :group 'org-link-store
  990. :type 'boolean)
  991. (defgroup org-link-follow nil
  992. "Options concerning following links in Org-mode"
  993. :tag "Org Follow Link"
  994. :group 'org-link)
  995. (defcustom org-tab-follows-link nil
  996. "Non-nil means, on links TAB will follow the link.
  997. Needs to be set before org.el is loaded."
  998. :group 'org-link-follow
  999. :type 'boolean)
  1000. (defcustom org-return-follows-link nil
  1001. "Non-nil means, on links RET will follow the link.
  1002. Needs to be set before org.el is loaded."
  1003. :group 'org-link-follow
  1004. :type 'boolean)
  1005. (defcustom org-mouse-1-follows-link t
  1006. "Non-nil means, mouse-1 on a link will follow the link.
  1007. A longer mouse click will still set point. Does not wortk on XEmacs.
  1008. Needs to be set before org.el is loaded."
  1009. :group 'org-link-follow
  1010. :type 'boolean)
  1011. (defcustom org-mark-ring-length 4
  1012. "Number of different positions to be recorded in the ring
  1013. Changing this requires a restart of Emacs to work correctly."
  1014. :group 'org-link-follow
  1015. :type 'interger)
  1016. (defcustom org-link-frame-setup
  1017. '((vm . vm-visit-folder-other-frame)
  1018. (gnus . gnus-other-frame)
  1019. (file . find-file-other-window))
  1020. "Setup the frame configuration for following links.
  1021. When following a link with Emacs, it may often be useful to display
  1022. this link in another window or frame. This variable can be used to
  1023. set this up for the different types of links.
  1024. For VM, use any of
  1025. `vm-visit-folder'
  1026. `vm-visit-folder-other-frame'
  1027. For Gnus, use any of
  1028. `gnus'
  1029. `gnus-other-frame'
  1030. For FILE, use any of
  1031. `find-file'
  1032. `find-file-other-window'
  1033. `find-file-other-frame'
  1034. For the calendar, use the variable `calendar-setup'.
  1035. For BBDB, it is currently only possible to display the matches in
  1036. another window."
  1037. :group 'org-link-follow
  1038. :type '(list
  1039. (cons (const vm)
  1040. (choice
  1041. (const vm-visit-folder)
  1042. (const vm-visit-folder-other-window)
  1043. (const vm-visit-folder-other-frame)))
  1044. (cons (const gnus)
  1045. (choice
  1046. (const gnus)
  1047. (const gnus-other-frame)))
  1048. (cons (const file)
  1049. (choice
  1050. (const find-file)
  1051. (const find-file-other-window)
  1052. (const find-file-other-frame)))))
  1053. (defcustom org-display-internal-link-with-indirect-buffer nil
  1054. "Non-nil means, use indirect buffer to display infile links.
  1055. Activating internal links (from one location in a file to another location
  1056. in the same file) normally just jumps to the location. When the link is
  1057. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1058. another window. When this option is set, the other window actually displays
  1059. an indirect buffer clone of the current buffer, to avoid any visibility
  1060. changes to the current buffer."
  1061. :group 'org-link-follow
  1062. :type 'boolean)
  1063. (defcustom org-open-non-existing-files nil
  1064. "Non-nil means, `org-open-file' will open non-existing files.
  1065. When nil, an error will be generated."
  1066. :group 'org-link-follow
  1067. :type 'boolean)
  1068. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1069. "Function and arguments to call for following mailto links.
  1070. This is a list with the first element being a lisp function, and the
  1071. remaining elements being arguments to the function. In string arguments,
  1072. %a will be replaced by the address, and %s will be replaced by the subject
  1073. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1074. :group 'org-link-follow
  1075. :type '(choice
  1076. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1077. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1078. (const :tag "message-mail" (message-mail "%a" "%s"))
  1079. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1080. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1081. "Non-nil means, ask for confirmation before executing shell links.
  1082. Shell links can be dangerous, just thing about a link
  1083. [[shell:rm -rf ~/*][Google Search]]
  1084. This link would show up in your Org-mode document as \"Google Search\"
  1085. but really it would remove your entire home directory.
  1086. Therefore I *definitely* advise against setting this variable to nil.
  1087. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1088. rather than having to type \"yes\"."
  1089. :group 'org-link-follow
  1090. :type '(choice
  1091. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1092. (const :tag "with y-or-n (faster)" y-or-n-p)
  1093. (const :tag "no confirmation (dangerous)" nil)))
  1094. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1095. "Non-nil means, ask for confirmation before executing elisp links.
  1096. Elisp links can be dangerous, just think about a link
  1097. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1098. This link would show up in your Org-mode document as \"Google Search\"
  1099. but really it would remove your entire home directory.
  1100. Therefore I *definitely* advise against setting this variable to nil.
  1101. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1102. rather than having to type \"yes\"."
  1103. :group 'org-link-follow
  1104. :type '(choice
  1105. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1106. (const :tag "with y-or-n (faster)" y-or-n-p)
  1107. (const :tag "no confirmation (dangerous)" nil)))
  1108. (defconst org-file-apps-defaults-gnu
  1109. '((remote . emacs)
  1110. (t . mailcap))
  1111. "Default file applications on a UNIX or GNU/Linux system.
  1112. See `org-file-apps'.")
  1113. (defconst org-file-apps-defaults-macosx
  1114. '((remote . emacs)
  1115. (t . "open %s")
  1116. ("ps" . "gv %s")
  1117. ("ps.gz" . "gv %s")
  1118. ("eps" . "gv %s")
  1119. ("eps.gz" . "gv %s")
  1120. ("dvi" . "xdvi %s")
  1121. ("fig" . "xfig %s"))
  1122. "Default file applications on a MacOS X system.
  1123. The system \"open\" is known as a default, but we use X11 applications
  1124. for some files for which the OS does not have a good default.
  1125. See `org-file-apps'.")
  1126. (defconst org-file-apps-defaults-windowsnt
  1127. (list
  1128. '(remote . emacs)
  1129. (cons t
  1130. (list (if (featurep 'xemacs)
  1131. 'mswindows-shell-execute
  1132. 'w32-shell-execute)
  1133. "open" 'file)))
  1134. "Default file applications on a Windows NT system.
  1135. The system \"open\" is used for most files.
  1136. See `org-file-apps'.")
  1137. (defcustom org-file-apps
  1138. '(
  1139. ("txt" . emacs)
  1140. ("tex" . emacs)
  1141. ("ltx" . emacs)
  1142. ("org" . emacs)
  1143. ("el" . emacs)
  1144. ("bib" . emacs)
  1145. )
  1146. "External applications for opening `file:path' items in a document.
  1147. Org-mode uses system defaults for different file types, but
  1148. you can use this variable to set the application for a given file
  1149. extension. The entries in this list are cons cells where the car identifies
  1150. files and the cdr the corresponding command. Possible values for the
  1151. file identifier are
  1152. \"ext\" A string identifying an extension
  1153. `directory' Matches a directory
  1154. `remote' Matches a remote file, accessible through tramp or efs.
  1155. Remote files most likely should be visited through Emacs
  1156. because external applications cannot handle such paths.
  1157. t Default for all remaining files
  1158. Possible values for the command are:
  1159. `emacs' The file will be visited by the current Emacs process.
  1160. `default' Use the default application for this file type.
  1161. string A command to be executed by a shell; %s will be replaced
  1162. by the path to the file.
  1163. sexp A Lisp form which will be evaluated. The file path will
  1164. be available in the Lisp variable `file'.
  1165. For more examples, see the system specific constants
  1166. `org-file-apps-defaults-macosx'
  1167. `org-file-apps-defaults-windowsnt'
  1168. `org-file-apps-defaults-gnu'."
  1169. :group 'org-link-follow
  1170. :type '(repeat
  1171. (cons (choice :value ""
  1172. (string :tag "Extension")
  1173. (const :tag "Default for unrecognized files" t)
  1174. (const :tag "Remote file" remote)
  1175. (const :tag "Links to a directory" directory))
  1176. (choice :value ""
  1177. (const :tag "Visit with Emacs" emacs)
  1178. (const :tag "Use system default" default)
  1179. (string :tag "Command")
  1180. (sexp :tag "Lisp form")))))
  1181. (defcustom org-mhe-search-all-folders nil
  1182. "Non-nil means, that the search for the mh-message will be extended to
  1183. all folders if the message cannot be found in the folder given in the link.
  1184. Searching all folders is very efficient with one of the search engines
  1185. supported by MH-E, but will be slow with pick."
  1186. :group 'org-link-follow
  1187. :type 'boolean)
  1188. (defgroup org-remember nil
  1189. "Options concerning interaction with remember.el."
  1190. :tag "Org Remember"
  1191. :group 'org)
  1192. (defcustom org-directory "~/org"
  1193. "Directory with org files.
  1194. This directory will be used as default to prompt for org files.
  1195. Used by the hooks for remember.el."
  1196. :group 'org-remember
  1197. :type 'directory)
  1198. (defcustom org-default-notes-file "~/.notes"
  1199. "Default target for storing notes.
  1200. Used by the hooks for remember.el. This can be a string, or nil to mean
  1201. the value of `remember-data-file'.
  1202. You can set this on a per-template basis with the variable
  1203. `org-remember-templates'."
  1204. :group 'org-remember
  1205. :type '(choice
  1206. (const :tag "Default from remember-data-file" nil)
  1207. file))
  1208. (defcustom org-remember-store-without-prompt t
  1209. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1210. In this case, you need `C-u C-c C-c' to get the prompts for
  1211. note file and headline.
  1212. When this variable is nil, `C-c C-c' give you the prompts, and
  1213. `C-u C-c C-c' trigger the fasttrack."
  1214. :group 'org-remember
  1215. :type 'boolean)
  1216. (defcustom org-remember-default-headline ""
  1217. "The headline that should be the default location in the notes file.
  1218. When filing remember notes, the cursor will start at that position.
  1219. You can set this on a per-template basis with the variable
  1220. `org-remember-templates'."
  1221. :group 'org-remember
  1222. :type 'string)
  1223. (defcustom org-remember-templates nil
  1224. "Templates for the creation of remember buffers.
  1225. When nil, just let remember make the buffer.
  1226. When not nil, this is a list of 4-element lists. In each entry, the first
  1227. element is a character, a unique key to select this template.
  1228. The second element is the template. The third element is optional and can
  1229. specify a destination file for remember items created with this template.
  1230. The default file is given by `org-default-notes-file'. An optional forth
  1231. element can specify the headline in that file that should be offered
  1232. first when the user is asked to file the entry. The default headline is
  1233. given in the variable `org-remember-default-headline'.
  1234. The template specifies the structure of the remember buffer. It should have
  1235. a first line starting with a star, to act as the org-mode headline.
  1236. Furthermore, the following %-escapes will be replaced with content:
  1237. %^{prompt} prompt the user for a string and replace this sequence with it.
  1238. %t time stamp, date only
  1239. %T time stamp with date and time
  1240. %u, %U like the above, but inactive time stamps
  1241. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1242. You may define a prompt like %^{Please specify birthday}t
  1243. %n user name (taken from `user-full-name')
  1244. %a annotation, normally the link created with org-store-link
  1245. %i initial content, the region when remember is called with C-u.
  1246. If %i is indented, the entire inserted text will be indented
  1247. as well.
  1248. %? After completing the template, position cursor here.
  1249. Apart from these general escapes, you can access information specific to the
  1250. link type that is created. For example, calling `remember' in emails or gnus
  1251. will record the author and the subject of the message, which you can access
  1252. with %:author and %:subject, respectively. Here is a complete list of what
  1253. is recorded for each link type.
  1254. Link type | Available information
  1255. -------------------+------------------------------------------------------
  1256. bbdb | %:type %:name %:company
  1257. vm, wl, mh, rmail | %:type %:subject %:message-id
  1258. | %:from %:fromname %:fromaddress
  1259. | %:to %:toname %:toaddress
  1260. | %:fromto (either \"to NAME\" or \"from NAME\")
  1261. gnus | %:group, for messages also all email fields
  1262. w3, w3m | %:type %:url
  1263. info | %:type %:file %:node
  1264. calendar | %:type %:date"
  1265. :group 'org-remember
  1266. :get (lambda (var) ; Make sure all entries have 4 elements
  1267. (mapcar (lambda (x)
  1268. (cond ((= (length x) 3) (append x '("")))
  1269. ((= (length x) 2) (append x '("" "")))
  1270. (t x)))
  1271. (default-value var)))
  1272. :type '(repeat
  1273. :tag "enabled"
  1274. (list :value (?a "\n" nil nil)
  1275. (character :tag "Selection Key")
  1276. (string :tag "Template")
  1277. (file :tag "Destination file (optional)")
  1278. (string :tag "Destination headline (optional)"))))
  1279. (defcustom org-reverse-note-order nil
  1280. "Non-nil means, store new notes at the beginning of a file or entry.
  1281. When nil, new notes will be filed to the end of a file or entry."
  1282. :group 'org-remember
  1283. :type '(choice
  1284. (const :tag "Reverse always" t)
  1285. (const :tag "Reverse never" nil)
  1286. (repeat :tag "By file name regexp"
  1287. (cons regexp boolean))))
  1288. (defgroup org-todo nil
  1289. "Options concerning TODO items in Org-mode."
  1290. :tag "Org TODO"
  1291. :group 'org)
  1292. (defgroup org-progress nil
  1293. "Options concerning Progress logging in Org-mode."
  1294. :tag "Org Progress"
  1295. :group 'org-time)
  1296. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1297. "List of TODO entry keyword sequences and their interpretation.
  1298. \\<org-mode-map>This is a list of sequences.
  1299. Each sequence starts with a symbol, either `sequence' or `type',
  1300. indicating if the keywords should be interpreted as a sequence of
  1301. action steps, or as different types of TODO items. The first
  1302. keywords are states requiring action - these states will select a headline
  1303. for inclusion into the global TODO list Org-mode produces. If one of
  1304. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1305. signify that no further action is necessary. If \"|\" is not found,
  1306. the last keyword is treated as the only DONE state of the sequence.
  1307. The command \\[org-todo] cycles an entry through these states, and one
  1308. additional state where no keyword is present. For details about this
  1309. cycling, see the manual.
  1310. TODO keywords and interpretation can also be set on a per-file basis with
  1311. the special #+SEQ_TODO and #+TYP_TODO lines.
  1312. For backward compatibility, this variable may also be just a list
  1313. of keywords - in this case the interptetation (sequence or type) will be
  1314. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1315. :group 'org-todo
  1316. :group 'org-keywords
  1317. :type '(choice
  1318. (repeat :tag "Old syntax, just keywords"
  1319. (string :tag "Keyword"))
  1320. (repeat :tag "New syntax"
  1321. (cons
  1322. (choice
  1323. :tag "Interpretation"
  1324. (const :tag "Sequence (cycling hits every state)" sequence)
  1325. (const :tag "Type (cycling directly to DONE)" type))
  1326. (repeat
  1327. (string :tag "Keyword"))))))
  1328. (defvar org-todo-keywords-1 nil)
  1329. (make-variable-buffer-local 'org-todo-keywords-1)
  1330. (defvar org-todo-keywords-for-agenda nil)
  1331. (defvar org-done-keywords-for-agenda nil)
  1332. (defvar org-not-done-keywords nil)
  1333. (make-variable-buffer-local 'org-not-done-keywords)
  1334. (defvar org-done-keywords nil)
  1335. (make-variable-buffer-local 'org-done-keywords)
  1336. (defvar org-todo-heads nil)
  1337. (make-variable-buffer-local 'org-todo-heads)
  1338. (defvar org-todo-sets nil)
  1339. (make-variable-buffer-local 'org-todo-sets)
  1340. (defvar org-todo-log-states nil)
  1341. (make-variable-buffer-local 'org-todo-log-states)
  1342. (defvar org-todo-kwd-alist nil)
  1343. (make-variable-buffer-local 'org-todo-kwd-alist)
  1344. (defvar org-todo-key-alist nil)
  1345. (make-variable-buffer-local 'org-todo-key-alist)
  1346. (defvar org-todo-key-trigger nil)
  1347. (make-variable-buffer-local 'org-todo-key-trigger)
  1348. (defcustom org-todo-interpretation 'sequence
  1349. "Controls how TODO keywords are interpreted.
  1350. This variable is in principle obsolete and is only used for
  1351. backward compatibility, if the interpretation of todo keywords is
  1352. not given already in `org-todo-keywords'. See that variable for
  1353. more information."
  1354. :group 'org-todo
  1355. :group 'org-keywords
  1356. :type '(choice (const sequence)
  1357. (const type)))
  1358. (defcustom org-use-fast-todo-selection 'prefix
  1359. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1360. This variable describes if and under what circumstances the cycling
  1361. mechanism for TODO keywords will be replaced by a single-key, direct
  1362. selection scheme.
  1363. When nil, fast selection is never used.
  1364. When the symbol `prefix', it will be used when `org-todo' is called with
  1365. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1366. in an agenda buffer.
  1367. When t, fast selection is used by default. In this case, the prefix
  1368. argument forces cycling instead.
  1369. In all cases, the special interface is only used if access keys have actually
  1370. been assigned by the user, i.e. if keywords in the configuration are followed
  1371. by a letter in parenthesis, like TODO(t)."
  1372. :group 'org-todo
  1373. :type '(choice
  1374. (const :tag "Never" nil)
  1375. (const :tag "By default" t)
  1376. (const :tag "Only with C-u C-c C-t" prefix)))
  1377. (defcustom org-after-todo-state-change-hook nil
  1378. "Hook which is run after the state of a TODO item was changed.
  1379. The new state (a string with a TODO keyword, or nil) is available in the
  1380. Lisp variable `state'."
  1381. :group 'org-todo
  1382. :type 'hook)
  1383. (defcustom org-log-done nil
  1384. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1385. When the state of an entry is changed from nothing or a DONE state to
  1386. a not-done TODO state, remove a previous closing date.
  1387. This can also be a list of symbols indicating under which conditions
  1388. the time stamp recording the action should be annotated with a short note.
  1389. Valid members of this list are
  1390. done Offer to record a note when marking entries done
  1391. state Offer to record a note whenever changing the TODO state
  1392. of an item. This is only relevant if TODO keywords are
  1393. interpreted as sequence, see variable `org-todo-interpretation'.
  1394. When `state' is set, this includes tracking `done'.
  1395. clock-out Offer to record a note when clocking out of an item.
  1396. A separate window will then pop up and allow you to type a note.
  1397. After finishing with C-c C-c, the note will be added directly after the
  1398. timestamp, as a plain list item. See also the variable
  1399. `org-log-note-headings'.
  1400. Logging can also be configured on a per-file basis by adding one of
  1401. the following lines anywhere in the buffer:
  1402. #+STARTUP: logdone
  1403. #+STARTUP: nologging
  1404. #+STARTUP: lognotedone
  1405. #+STARTUP: lognotestate
  1406. #+STARTUP: lognoteclock-out"
  1407. :group 'org-todo
  1408. :group 'org-progress
  1409. :type '(choice
  1410. (const :tag "off" nil)
  1411. (const :tag "on" t)
  1412. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1413. (const :tag "when item is marked DONE" done)
  1414. (const :tag "when TODO state changes" state)
  1415. (const :tag "when clocking out" clock-out))))
  1416. (defcustom org-log-done-with-time t
  1417. "Non-nil means, the CLOSED time stamp will contain date and time.
  1418. When nil, only the date will be recorded."
  1419. :group 'org-progress
  1420. :type 'boolean)
  1421. (defcustom org-log-note-headings
  1422. '((done . "CLOSING NOTE %t")
  1423. (state . "State %-12s %t")
  1424. (clock-out . ""))
  1425. "Headings for notes added when clocking out or closing TODO items.
  1426. The value is an alist, with the car being a symbol indicating the note
  1427. context, and the cdr is the heading to be used. The heading may also be the
  1428. empty string.
  1429. %t in the heading will be replaced by a time stamp.
  1430. %s will be replaced by the new TODO state, in double quotes.
  1431. %u will be replaced by the user name.
  1432. %U will be replaced by the full user name."
  1433. :group 'org-todo
  1434. :group 'org-progress
  1435. :type '(list :greedy t
  1436. (cons (const :tag "Heading when closing an item" done) string)
  1437. (cons (const :tag
  1438. "Heading when changing todo state (todo sequence only)"
  1439. state) string)
  1440. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1441. (defcustom org-log-states-order-reversed t
  1442. "Non-nil means, the latest state change note will be directly after heading.
  1443. When nil, the notes will be orderer according to time."
  1444. :group 'org-todo
  1445. :group 'org-progress
  1446. :type 'boolean)
  1447. (defcustom org-log-repeat t
  1448. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1449. When nil, no note will be taken."
  1450. :group 'org-todo
  1451. :group 'org-progress
  1452. :type 'boolean)
  1453. (defcustom org-clock-out-when-done t
  1454. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1455. Nil means, clock will keep running until stopped explicitly with
  1456. `C-c C-x C-o', or until the clock is started in a different item."
  1457. :group 'org-progress
  1458. :type 'boolean)
  1459. (defgroup org-priorities nil
  1460. "Priorities in Org-mode."
  1461. :tag "Org Priorities"
  1462. :group 'org-todo)
  1463. (defcustom org-highest-priority ?A
  1464. "The highest priority of TODO items. A character like ?A, ?B etc.
  1465. Must have a smaller ASCII number than `org-lowest-priority'."
  1466. :group 'org-priorities
  1467. :type 'character)
  1468. (defcustom org-lowest-priority ?C
  1469. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1470. Must have a larger ASCII number than `org-highest-priority'."
  1471. :group 'org-priorities
  1472. :type 'character)
  1473. (defcustom org-default-priority ?B
  1474. "The default priority of TODO items.
  1475. This is the priority an item get if no explicit priority is given."
  1476. :group 'org-priorities
  1477. :type 'character)
  1478. (defcustom org-priority-start-cycle-with-default t
  1479. "Non-nil means, start with default priority when starting to cycle.
  1480. When this is nil, the first step in the cycle will be (depending on the
  1481. command used) one higher or lower that the default priority."
  1482. :group 'org-priorities
  1483. :type 'boolean)
  1484. (defgroup org-time nil
  1485. "Options concerning time stamps and deadlines in Org-mode."
  1486. :tag "Org Time"
  1487. :group 'org)
  1488. (defcustom org-insert-labeled-timestamps-at-point nil
  1489. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1490. When nil, these labeled time stamps are forces into the second line of an
  1491. entry, just after the headline. When scheduling from the global TODO list,
  1492. the time stamp will always be forced into the second line."
  1493. :group 'org-time
  1494. :type 'boolean)
  1495. (defcustom org-insert-labeled-timestamps-before-properties-drawer t
  1496. "Non-nil means, always insert planning info before property drawer.
  1497. When this is nil and there is a property drawer *directly* after
  1498. the headline, move the planning info into the drawer. If the property
  1499. drawer separated from the headline by at least one line, this variable
  1500. has no effect."
  1501. :group 'org-time
  1502. :type 'boolean)
  1503. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1504. "Formats for `format-time-string' which are used for time stamps.
  1505. It is not recommended to change this constant.")
  1506. (defcustom org-time-stamp-rounding-minutes 0
  1507. "Number of minutes to round time stamps to upon insertion.
  1508. When zero, insert the time unmodified. Useful rounding numbers
  1509. should be factors of 60, so for example 5, 10, 15.
  1510. When this is not zero, you can still force an exact time-stamp by using
  1511. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1512. :group 'org-time
  1513. :type 'integer)
  1514. (defcustom org-display-custom-times nil
  1515. "Non-nil means, overlay custom formats over all time stamps.
  1516. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1517. To turn this on on a per-file basis, insert anywhere in the file:
  1518. #+STARTUP: customtime"
  1519. :group 'org-time
  1520. :set 'set-default
  1521. :type 'sexp)
  1522. (make-variable-buffer-local 'org-display-custom-times)
  1523. (defcustom org-time-stamp-custom-formats
  1524. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1525. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1526. These are overlayed over the default ISO format if the variable
  1527. `org-display-custom-times' is set. Time like %H:%M should be at the
  1528. end of the second format."
  1529. :group 'org-time
  1530. :type 'sexp)
  1531. (defun org-time-stamp-format (&optional long inactive)
  1532. "Get the right format for a time string."
  1533. (let ((f (if long (cdr org-time-stamp-formats)
  1534. (car org-time-stamp-formats))))
  1535. (if inactive
  1536. (concat "[" (substring f 1 -1) "]")
  1537. f)))
  1538. (defcustom org-deadline-warning-days 14
  1539. "No. of days before expiration during which a deadline becomes active.
  1540. This variable governs the display in sparse trees and in the agenda.
  1541. When negative, it means use this number (the absolute value of it)
  1542. even if a deadline has a different individual lead time specified."
  1543. :group 'org-time
  1544. :type 'number)
  1545. (defcustom org-popup-calendar-for-date-prompt t
  1546. "Non-nil means, pop up a calendar when prompting for a date.
  1547. In the calendar, the date can be selected with mouse-1. However, the
  1548. minibuffer will also be active, and you can simply enter the date as well.
  1549. When nil, only the minibuffer will be available."
  1550. :group 'org-time
  1551. :type 'boolean)
  1552. (defcustom org-edit-timestamp-down-means-later nil
  1553. "Non-nil means, S-down will increase the time in a time stamp.
  1554. When nil, S-up will increase."
  1555. :group 'org-time
  1556. :type 'boolean)
  1557. (defcustom org-calendar-follow-timestamp-change t
  1558. "Non-nil means, make the calendar window follow timestamp changes.
  1559. When a timestamp is modified and the calendar window is visible, it will be
  1560. moved to the new date."
  1561. :group 'org-time
  1562. :type 'boolean)
  1563. (defgroup org-tags nil
  1564. "Options concerning tags in Org-mode."
  1565. :tag "Org Tags"
  1566. :group 'org)
  1567. (defcustom org-tag-alist nil
  1568. "List of tags allowed in Org-mode files.
  1569. When this list is nil, Org-mode will base TAG input on what is already in the
  1570. buffer.
  1571. The value of this variable is an alist, the car of each entry must be a
  1572. keyword as a string, the cdr may be a character that is used to select
  1573. that tag through the fast-tag-selection interface.
  1574. See the manual for details."
  1575. :group 'org-tags
  1576. :type '(repeat
  1577. (choice
  1578. (cons (string :tag "Tag name")
  1579. (character :tag "Access char"))
  1580. (const :tag "Start radio group" (:startgroup))
  1581. (const :tag "End radio group" (:endgroup)))))
  1582. (defcustom org-use-fast-tag-selection 'auto
  1583. "Non-nil means, use fast tag selection scheme.
  1584. This is a special interface to select and deselect tags with single keys.
  1585. When nil, fast selection is never used.
  1586. When the symbol `auto', fast selection is used if and only if selection
  1587. characters for tags have been configured, either through the variable
  1588. `org-tag-alist' or through a #+TAGS line in the buffer.
  1589. When t, fast selection is always used and selection keys are assigned
  1590. automatically if necessary."
  1591. :group 'org-tags
  1592. :type '(choice
  1593. (const :tag "Always" t)
  1594. (const :tag "Never" nil)
  1595. (const :tag "When selection characters are configured" 'auto)))
  1596. (defcustom org-fast-tag-selection-single-key nil
  1597. "Non-nil means, fast tag selection exits after first change.
  1598. When nil, you have to press RET to exit it.
  1599. During fast tag selection, you can toggle this flag with `C-c'.
  1600. This variable can also have the value `expert'. In this case, the window
  1601. displaying the tags menu is not even shown, until you press C-c again."
  1602. :group 'org-tags
  1603. :type '(choice
  1604. (const :tag "No" nil)
  1605. (const :tag "Yes" t)
  1606. (const :tag "Expert" expert)))
  1607. (defvar org-fast-tag-selection-include-todo nil
  1608. "Non-nil means, fast tags selection interface will also offer TODO states.
  1609. This is an undocumented feature, you should not rely on it.")
  1610. (defcustom org-tags-column 48
  1611. "The column to which tags should be indented in a headline.
  1612. If this number is positive, it specifies the column. If it is negative,
  1613. it means that the tags should be flushright to that column. For example,
  1614. -79 works well for a normal 80 character screen."
  1615. :group 'org-tags
  1616. :type 'integer)
  1617. (defcustom org-auto-align-tags t
  1618. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1619. These operations change the length of a headline and therefore shift
  1620. the tags around. With this options turned on, after each such operation
  1621. the tags are again aligned to `org-tags-column'."
  1622. :group 'org-tags
  1623. :type 'boolean)
  1624. (defcustom org-use-tag-inheritance t
  1625. "Non-nil means, tags in levels apply also for sublevels.
  1626. When nil, only the tags directly given in a specific line apply there.
  1627. If you turn off this option, you very likely want to turn on the
  1628. companion option `org-tags-match-list-sublevels'."
  1629. :group 'org-tags
  1630. :type 'boolean)
  1631. (defcustom org-tags-match-list-sublevels nil
  1632. "Non-nil means list also sublevels of headlines matching tag search.
  1633. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1634. the sublevels of a headline matching a tag search often also match
  1635. the same search. Listing all of them can create very long lists.
  1636. Setting this variable to nil causes subtrees of a match to be skipped.
  1637. This option is off by default, because inheritance in on. If you turn
  1638. inheritance off, you very likely want to turn this option on.
  1639. As a special case, if the tag search is restricted to TODO items, the
  1640. value of this variable is ignored and sublevels are always checked, to
  1641. make sure all corresponding TODO items find their way into the list."
  1642. :group 'org-tags
  1643. :type 'boolean)
  1644. (defvar org-tags-history nil
  1645. "History of minibuffer reads for tags.")
  1646. (defvar org-last-tags-completion-table nil
  1647. "The last used completion table for tags.")
  1648. (defvar org-after-tags-change-hook nil
  1649. "Hook that is run after the tags in a line have changed.")
  1650. (defgroup org-properties nil
  1651. "Options concerning properties in Org-mode."
  1652. :tag "Org Properties"
  1653. :group 'org)
  1654. (defcustom org-property-format "%-10s %s"
  1655. "How property key/value pairs should be formatted by `indent-line'.
  1656. When `indent-line' hits a property definition, it will format the line
  1657. according to this format, mainly to make sure that the values are
  1658. lined-up with respect to each other."
  1659. :group 'org-properties
  1660. :type 'string)
  1661. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1662. "The default column format, if no other format has been defined.
  1663. This variable can be set on the per-file basis by inserting a line
  1664. #+COLUMNS: %25ITEM ....."
  1665. :group 'org-properties
  1666. :type 'string)
  1667. (defcustom org-global-properties nil
  1668. "List of property/value pairs that can be inherited by any entry.
  1669. You can set buffer-local values for this by adding lines like
  1670. #+PROPERTY: NAME VALUE"
  1671. :group 'org-properties
  1672. :type '(repeat
  1673. (cons (string :tag "Property")
  1674. (string :tag "Value"))))
  1675. (defvar org-local-properties nil
  1676. "List of property/value pairs that can be inherited by any entry.
  1677. Valid for the current buffer.
  1678. This variable is populated from #+PROPERTY lines.")
  1679. (defgroup org-agenda nil
  1680. "Options concerning agenda views in Org-mode."
  1681. :tag "Org Agenda"
  1682. :group 'org)
  1683. (defvar org-category nil
  1684. "Variable used by org files to set a category for agenda display.
  1685. Such files should use a file variable to set it, for example
  1686. # -*- mode: org; org-category: \"ELisp\"
  1687. or contain a special line
  1688. #+CATEGORY: ELisp
  1689. If the file does not specify a category, then file's base name
  1690. is used instead.")
  1691. (make-variable-buffer-local 'org-category)
  1692. (defcustom org-agenda-files nil
  1693. "The files to be used for agenda display.
  1694. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1695. \\[org-remove-file]. You can also use customize to edit the list.
  1696. If the value of the variable is not a list but a single file name, then
  1697. the list of agenda files is actually stored and maintained in that file, one
  1698. agenda file per line."
  1699. :group 'org-agenda
  1700. :type '(choice
  1701. (repeat :tag "List of files" file)
  1702. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1703. (defcustom org-agenda-skip-unavailable-files nil
  1704. "t means to just skip non-reachable files in `org-agenda-files'.
  1705. Nil means to remove them, after a query, from the list."
  1706. :group 'org-agenda
  1707. :type 'boolean)
  1708. (defcustom org-agenda-confirm-kill 1
  1709. "When set, remote killing from the agenda buffer needs confirmation.
  1710. When t, a confirmation is always needed. When a number N, confirmation is
  1711. only needed when the text to be killed contains more than N non-white lines."
  1712. :group 'org-agenda
  1713. :type '(choice
  1714. (const :tag "Never" nil)
  1715. (const :tag "Always" t)
  1716. (number :tag "When more than N lines")))
  1717. (defcustom org-calendar-to-agenda-key [?c]
  1718. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1719. The command `org-calendar-goto-agenda' will be bound to this key. The
  1720. default is the character `c' because then `c' can be used to switch back and
  1721. forth between agenda and calendar."
  1722. :group 'org-agenda
  1723. :type 'sexp)
  1724. (defgroup org-agenda-export nil
  1725. "Options concerning exporting agenda views in Org-mode."
  1726. :tag "Org Agenda Export"
  1727. :group 'org-agenda)
  1728. (defcustom org-agenda-with-colors t
  1729. "Non-nil means, use colors in agenda views."
  1730. :group 'org-agenda-export
  1731. :type 'boolean)
  1732. (defcustom org-agenda-exporter-settings nil
  1733. "Alist of variable/value pairs that should be active during agenda export.
  1734. This is a good place to set uptions for ps-print and for htmlize."
  1735. :group 'org-agenda-export
  1736. :type '(repeat
  1737. (list
  1738. (variable)
  1739. (sexp :tag "Value"))))
  1740. (defcustom org-agenda-export-html-style ""
  1741. "The style specification for exported HTML Agenda files.
  1742. If this variable contains a string, it will replace the default <style>
  1743. section as produced by `htmlize'.
  1744. Since there are different ways of setting style information, this variable
  1745. needs to contain the full HTML structure to provide a style, including the
  1746. surrounding HTML tags. The style specifications should include definitions
  1747. the fonts used by the agenda, here is an example:
  1748. <style type=\"text/css\">
  1749. p { font-weight: normal; color: gray; }
  1750. .org-agenda-structure {
  1751. font-size: 110%;
  1752. color: #003399;
  1753. font-weight: 600;
  1754. }
  1755. .org-todo {
  1756. color: #cc6666;Week-agenda:
  1757. font-weight: bold;
  1758. }
  1759. .org-done {
  1760. color: #339933;
  1761. }
  1762. .title { text-align: center; }
  1763. .todo, .deadline { color: red; }
  1764. .done { color: green; }
  1765. </style>
  1766. or, if you want to keep the style in a file,
  1767. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1768. As the value of this option simply gets inserted into the HTML <head> header,
  1769. you can \"misuse\" it to also add other text to the header. However,
  1770. <style>...</style> is required, if not present the variable will be ignored."
  1771. :group 'org-agenda-export
  1772. :group 'org-export-html
  1773. :type 'string)
  1774. (defgroup org-agenda-custom-commands nil
  1775. "Options concerning agenda views in Org-mode."
  1776. :tag "Org Agenda Custom Commands"
  1777. :group 'org-agenda)
  1778. (defcustom org-agenda-custom-commands nil
  1779. "Custom commands for the agenda.
  1780. These commands will be offered on the splash screen displayed by the
  1781. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1782. (key type match options files)
  1783. key The key (a single char as a string) to be associated with the command.
  1784. type The command type, any of the following symbols:
  1785. todo Entries with a specific TODO keyword, in all agenda files.
  1786. tags Tags match in all agenda files.
  1787. tags-todo Tags match in all agenda files, TODO entries only.
  1788. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1789. tags-tree Sparse tree with all tags matches in *current* file.
  1790. occur-tree Occur sparse tree for *current* file.
  1791. match What to search for:
  1792. - a single keyword for TODO keyword searches
  1793. - a tags match expression for tags searches
  1794. - a regular expression for occur searches
  1795. options A list of option settings, similar to that in a let form, so like
  1796. this: ((opt1 val1) (opt2 val2) ...)
  1797. files A list of files file to write the produced agenda buffer to
  1798. with the command `org-store-agenda-views'.
  1799. If a file name ends in \".html\", an HTML version of the buffer
  1800. is written out. If it ends in \".ps\", a postscript version is
  1801. produced. Otherwide, only the plain text is written to the file.
  1802. You can also define a set of commands, to create a composite agenda buffer.
  1803. In this case, an entry looks like this:
  1804. (key desc (cmd1 cmd2 ...) general-options file)
  1805. where
  1806. desc A description string to be displayed in the dispatcher menu.
  1807. cmd An agenda command, similar to the above. However, tree commands
  1808. are no allowed, but instead you can get agenda and global todo list.
  1809. So valid commands for a set are:
  1810. (agenda)
  1811. (alltodo)
  1812. (stuck)
  1813. (todo \"match\" options files)
  1814. (tags \"match\" options files)
  1815. (tags-todo \"match\" options files)
  1816. Each command can carry a list of options, and another set of options can be
  1817. given for the whole set of commands. Individual command options take
  1818. precedence over the general options."
  1819. :group 'org-agenda-custom-commands
  1820. :type '(repeat
  1821. (choice :value ("a" tags "" nil)
  1822. (list :tag "Single command"
  1823. (string :tag "Key")
  1824. (choice
  1825. (const :tag "Agenda" agenda)
  1826. (const :tag "TODO list" alltodo)
  1827. (const :tag "Stuck projects" stuck)
  1828. (const :tag "Tags search (all agenda files)" tags)
  1829. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1830. (const :tag "TODO keyword search (all agenda files)" todo)
  1831. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1832. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1833. (const :tag "Occur tree (current buffer)" occur-tree)
  1834. (symbol :tag "Other, user-defined function"))
  1835. (string :tag "Match")
  1836. (repeat :tag "Local options"
  1837. (list (variable :tag "Option") (sexp :tag "Value")))
  1838. (option (repeat :tag "Export" (file :tag "Export to"))))
  1839. (list :tag "Command series, all agenda files"
  1840. (string :tag "Key")
  1841. (string :tag "Description")
  1842. (repeat
  1843. (choice
  1844. (const :tag "Agenda" (agenda))
  1845. (const :tag "TODO list" (alltodo))
  1846. (const :tag "Stuck projects" (stuck))
  1847. (list :tag "Tags search"
  1848. (const :format "" tags)
  1849. (string :tag "Match")
  1850. (repeat :tag "Local options"
  1851. (list (variable :tag "Option")
  1852. (sexp :tag "Value"))))
  1853. (list :tag "Tags search, TODO entries only"
  1854. (const :format "" tags-todo)
  1855. (string :tag "Match")
  1856. (repeat :tag "Local options"
  1857. (list (variable :tag "Option")
  1858. (sexp :tag "Value"))))
  1859. (list :tag "TODO keyword search"
  1860. (const :format "" todo)
  1861. (string :tag "Match")
  1862. (repeat :tag "Local options"
  1863. (list (variable :tag "Option")
  1864. (sexp :tag "Value"))))
  1865. (list :tag "Other, user-defined function"
  1866. (symbol :tag "function")
  1867. (string :tag "Match")
  1868. (repeat :tag "Local options"
  1869. (list (variable :tag "Option")
  1870. (sexp :tag "Value"))))))
  1871. (repeat :tag "General options"
  1872. (list (variable :tag "Option")
  1873. (sexp :tag "Value")))
  1874. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1875. (defcustom org-stuck-projects
  1876. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1877. "How to identify stuck projects.
  1878. This is a list of four items:
  1879. 1. A tags/todo matcher string that is used to identify a project.
  1880. The entire tree below a headline matched by this is considered one project.
  1881. 2. A list of TODO keywords identifying non-stuck projects.
  1882. If the project subtree contains any headline with one of these todo
  1883. keywords, the project is considered to be not stuck. If you specify
  1884. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1885. 3. A list of tags identifying non-stuck projects.
  1886. If the project subtree contains any headline with one of these tags,
  1887. the project is considered to be not stuck. If you specify \"*\" as
  1888. a tag, any tag will mark the project unstuck.
  1889. 4. An arbitrary regular expression matching non-stuck projects.
  1890. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1891. or `C-c a #' to produce the list."
  1892. :group 'org-agenda-custom-commands
  1893. :type '(list
  1894. (string :tag "Tags/TODO match to identify a project")
  1895. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1896. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1897. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1898. (defgroup org-agenda-skip nil
  1899. "Options concerning skipping parts of agenda files."
  1900. :tag "Org Agenda Skip"
  1901. :group 'org-agenda)
  1902. (defcustom org-agenda-todo-list-sublevels t
  1903. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1904. When nil, the sublevels of a TODO entry are not checked, resulting in
  1905. potentially much shorter TODO lists."
  1906. :group 'org-agenda-skip
  1907. :group 'org-todo
  1908. :type 'boolean)
  1909. (defcustom org-agenda-todo-ignore-scheduled nil
  1910. "Non-nil means, don't show scheduled entries in the global todo list.
  1911. The idea behind this is that by scheduling it, you have already taken care
  1912. of this item."
  1913. :group 'org-agenda-skip
  1914. :group 'org-todo
  1915. :type 'boolean)
  1916. (defcustom org-agenda-todo-ignore-deadlines nil
  1917. "Non-nil means, don't show near deadline entries in the global todo list.
  1918. Near means closer than `org-deadline-warning-days' days.
  1919. The idea behind this is that such items will appear in the agenda anyway."
  1920. :group 'org-agenda-skip
  1921. :group 'org-todo
  1922. :type 'boolean)
  1923. (defcustom org-agenda-skip-scheduled-if-done nil
  1924. "Non-nil means don't show scheduled items in agenda when they are done.
  1925. This is relevant for the daily/weekly agenda, not for the TODO list. And
  1926. it applied only to the actualy date of the scheduling. Warnings about
  1927. an item with a past scheduling dates are always turned off when the item
  1928. is DONE."
  1929. :group 'org-agenda-skip
  1930. :type 'boolean)
  1931. (defcustom org-agenda-skip-deadline-if-done nil
  1932. "Non-nil means don't show deadines when the corresponding item is done.
  1933. When nil, the deadline is still shown and should give you a happy feeling.
  1934. This is relevant for the daily/weekly agenda. And it applied only to the
  1935. actualy date of the deadline. Warnings about approching and past-due
  1936. deadlines are always turned off when the item is DONE."
  1937. :group 'org-agenda-skip
  1938. :type 'boolean)
  1939. (defcustom org-timeline-show-empty-dates 3
  1940. "Non-nil means, `org-timeline' also shows dates without an entry.
  1941. When nil, only the days which actually have entries are shown.
  1942. When t, all days between the first and the last date are shown.
  1943. When an integer, show also empty dates, but if there is a gap of more than
  1944. N days, just insert a special line indicating the size of the gap."
  1945. :group 'org-agenda-skip
  1946. :type '(choice
  1947. (const :tag "None" nil)
  1948. (const :tag "All" t)
  1949. (number :tag "at most")))
  1950. (defgroup org-agenda-startup nil
  1951. "Options concerning initial settings in the Agenda in Org Mode."
  1952. :tag "Org Agenda Startup"
  1953. :group 'org-agenda)
  1954. (defcustom org-finalize-agenda-hook nil
  1955. "Hook run just before displaying an agenda buffer."
  1956. :group 'org-agenda-startup
  1957. :type 'hook)
  1958. (defcustom org-agenda-mouse-1-follows-link nil
  1959. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1960. A longer mouse click will still set point. Does not wortk on XEmacs.
  1961. Needs to be set before org.el is loaded."
  1962. :group 'org-agenda-startup
  1963. :type 'boolean)
  1964. (defcustom org-agenda-start-with-follow-mode nil
  1965. "The initial value of follow-mode in a newly created agenda window."
  1966. :group 'org-agenda-startup
  1967. :type 'boolean)
  1968. (defgroup org-agenda-windows nil
  1969. "Options concerning the windows used by the Agenda in Org Mode."
  1970. :tag "Org Agenda Windows"
  1971. :group 'org-agenda)
  1972. (defcustom org-agenda-window-setup 'reorganize-frame
  1973. "How the agenda buffer should be displayed.
  1974. Possible values for this option are:
  1975. current-window Show agenda in the current window, keeping all other windows.
  1976. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1977. other-window Use `switch-to-buffer-other-window' to display agenda.
  1978. reorganize-frame Show only two windows on the current frame, the current
  1979. window and the agenda.
  1980. See also the variable `org-agenda-restore-windows-after-quit'."
  1981. :group 'org-agenda-windows
  1982. :type '(choice
  1983. (const current-window)
  1984. (const other-frame)
  1985. (const other-window)
  1986. (const reorganize-frame)))
  1987. (defcustom org-agenda-restore-windows-after-quit nil
  1988. "Non-nil means, restore window configuration open exiting agenda.
  1989. Before the window configuration is changed for displaying the agenda,
  1990. the current status is recorded. When the agenda is exited with
  1991. `q' or `x' and this option is set, the old state is restored. If
  1992. `org-agenda-window-setup' is `other-frame', the value of this
  1993. option will be ignored.."
  1994. :group 'org-agenda-windows
  1995. :type 'boolean)
  1996. (defcustom org-indirect-buffer-display 'other-window
  1997. "How should indirect tree buffers be displayed?
  1998. This applies to indirect buffers created with the commands
  1999. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2000. Valid values are:
  2001. current-window Display in the current window
  2002. other-window Just display in another window.
  2003. dedicated-frame Create one new frame, and re-use it each time.
  2004. new-frame Make a new frame each time."
  2005. :group 'org-structure
  2006. :group 'org-agenda-windows
  2007. :type '(choice
  2008. (const :tag "In current window" current-window)
  2009. (const :tag "In current frame, other window" other-window)
  2010. (const :tag "Each time a new frame" new-frame)
  2011. (const :tag "One dedicated frame" dedicated-frame)))
  2012. (defgroup org-agenda-daily/weekly nil
  2013. "Options concerning the daily/weekly agenda."
  2014. :tag "Org Agenda Daily/Weekly"
  2015. :group 'org-agenda)
  2016. (defcustom org-agenda-ndays 7
  2017. "Number of days to include in overview display.
  2018. Should be 1 or 7."
  2019. :group 'org-agenda-daily/weekly
  2020. :type 'number)
  2021. (defcustom org-agenda-start-on-weekday 1
  2022. "Non-nil means, start the overview always on the specified weekday.
  2023. 0 denotes Sunday, 1 denotes Monday etc.
  2024. When nil, always start on the current day."
  2025. :group 'org-agenda-daily/weekly
  2026. :type '(choice (const :tag "Today" nil)
  2027. (number :tag "Weekday No.")))
  2028. (defcustom org-agenda-show-all-dates t
  2029. "Non-nil means, `org-agenda' shows every day in the selected range.
  2030. When nil, only the days which actually have entries are shown."
  2031. :group 'org-agenda-daily/weekly
  2032. :type 'boolean)
  2033. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2034. "Format string for displaying dates in the agenda.
  2035. Used by the daily/weekly agenda and by the timeline. This should be
  2036. a format string understood by `format-time-string', or a function returning
  2037. the formatted date as a string. The function must take a single argument,
  2038. a calendar-style date list like (month day year)."
  2039. :group 'org-agenda-daily/weekly
  2040. :type '(choice
  2041. (string :tag "Format string")
  2042. (function :tag "Function")))
  2043. (defun org-agenda-format-date-aligned (date)
  2044. "Format a date string for display in the daily/weekly agenda, or timeline.
  2045. This function makes sure that dates are aligned for easy reading."
  2046. (format "%-9s %2d %s %4d"
  2047. (calendar-day-name date)
  2048. (extract-calendar-day date)
  2049. (calendar-month-name (extract-calendar-month date))
  2050. (extract-calendar-year date)))
  2051. (defcustom org-agenda-include-diary nil
  2052. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2053. :group 'org-agenda-daily/weekly
  2054. :type 'boolean)
  2055. (defcustom org-agenda-include-all-todo nil
  2056. "Set means weekly/daily agenda will always contain all TODO entries.
  2057. The TODO entries will be listed at the top of the agenda, before
  2058. the entries for specific days."
  2059. :group 'org-agenda-daily/weekly
  2060. :type 'boolean)
  2061. (defcustom org-agenda-repeating-timestamp-show-all t
  2062. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2063. When nil, only one occurence is shown, either today or the
  2064. nearest into the future."
  2065. :group 'org-agenda-daily/weekly
  2066. :type 'boolean)
  2067. (defgroup org-agenda-time-grid nil
  2068. "Options concerning the time grid in the Org-mode Agenda."
  2069. :tag "Org Agenda Time Grid"
  2070. :group 'org-agenda)
  2071. (defcustom org-agenda-use-time-grid t
  2072. "Non-nil means, show a time grid in the agenda schedule.
  2073. A time grid is a set of lines for specific times (like every two hours between
  2074. 8:00 and 20:00). The items scheduled for a day at specific times are
  2075. sorted in between these lines.
  2076. For details about when the grid will be shown, and what it will look like, see
  2077. the variable `org-agenda-time-grid'."
  2078. :group 'org-agenda-time-grid
  2079. :type 'boolean)
  2080. (defcustom org-agenda-time-grid
  2081. '((daily today require-timed)
  2082. "----------------"
  2083. (800 1000 1200 1400 1600 1800 2000))
  2084. "The settings for time grid for agenda display.
  2085. This is a list of three items. The first item is again a list. It contains
  2086. symbols specifying conditions when the grid should be displayed:
  2087. daily if the agenda shows a single day
  2088. weekly if the agenda shows an entire week
  2089. today show grid on current date, independent of daily/weekly display
  2090. require-timed show grid only if at least one item has a time specification
  2091. The second item is a string which will be places behing the grid time.
  2092. The third item is a list of integers, indicating the times that should have
  2093. a grid line."
  2094. :group 'org-agenda-time-grid
  2095. :type
  2096. '(list
  2097. (set :greedy t :tag "Grid Display Options"
  2098. (const :tag "Show grid in single day agenda display" daily)
  2099. (const :tag "Show grid in weekly agenda display" weekly)
  2100. (const :tag "Always show grid for today" today)
  2101. (const :tag "Show grid only if any timed entries are present"
  2102. require-timed)
  2103. (const :tag "Skip grid times already present in an entry"
  2104. remove-match))
  2105. (string :tag "Grid String")
  2106. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2107. (defgroup org-agenda-sorting nil
  2108. "Options concerning sorting in the Org-mode Agenda."
  2109. :tag "Org Agenda Sorting"
  2110. :group 'org-agenda)
  2111. (let ((sorting-choice
  2112. '(choice
  2113. (const time-up) (const time-down)
  2114. (const category-keep) (const category-up) (const category-down)
  2115. (const tag-down) (const tag-up)
  2116. (const priority-up) (const priority-down))))
  2117. (defcustom org-agenda-sorting-strategy
  2118. '((agenda time-up category-keep priority-down)
  2119. (todo category-keep priority-down)
  2120. (tags category-keep priority-down))
  2121. "Sorting structure for the agenda items of a single day.
  2122. This is a list of symbols which will be used in sequence to determine
  2123. if an entry should be listed before another entry. The following
  2124. symbols are recognized:
  2125. time-up Put entries with time-of-day indications first, early first
  2126. time-down Put entries with time-of-day indications first, late first
  2127. category-keep Keep the default order of categories, corresponding to the
  2128. sequence in `org-agenda-files'.
  2129. category-up Sort alphabetically by category, A-Z.
  2130. category-down Sort alphabetically by category, Z-A.
  2131. tag-up Sort alphabetically by last tag, A-Z.
  2132. tag-down Sort alphabetically by last tag, Z-A.
  2133. priority-up Sort numerically by priority, high priority last.
  2134. priority-down Sort numerically by priority, high priority first.
  2135. The different possibilities will be tried in sequence, and testing stops
  2136. if one comparison returns a \"not-equal\". For example, the default
  2137. '(time-up category-keep priority-down)
  2138. means: Pull out all entries having a specified time of day and sort them,
  2139. in order to make a time schedule for the current day the first thing in the
  2140. agenda listing for the day. Of the entries without a time indication, keep
  2141. the grouped in categories, don't sort the categories, but keep them in
  2142. the sequence given in `org-agenda-files'. Within each category sort by
  2143. priority.
  2144. Leaving out `category-keep' would mean that items will be sorted across
  2145. categories by priority."
  2146. :group 'org-agenda-sorting
  2147. :type `(choice
  2148. (repeat :tag "General" ,sorting-choice)
  2149. (list :tag "Individually"
  2150. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2151. (repeat ,sorting-choice))
  2152. (cons (const :tag "Strategy for TODO lists" todo)
  2153. (repeat ,sorting-choice))
  2154. (cons (const :tag "Strategy for Tags matches" tags)
  2155. (repeat ,sorting-choice))))))
  2156. (defcustom org-sort-agenda-notime-is-late t
  2157. "Non-nil means, items without time are considered late.
  2158. This is only relevant for sorting. When t, items which have no explicit
  2159. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2160. do have a time. When nil, the default time is before 0:00. You can use this
  2161. option to decide if the schedule for today should come before or after timeless
  2162. agenda entries."
  2163. :group 'org-agenda-sorting
  2164. :type 'boolean)
  2165. (defgroup org-agenda-prefix nil
  2166. "Options concerning the entry prefix in the Org-mode agenda display."
  2167. :tag "Org Agenda Prefix"
  2168. :group 'org-agenda)
  2169. (defcustom org-agenda-prefix-format
  2170. '((agenda . " %-12:c%?-12t% s")
  2171. (timeline . " % s")
  2172. (todo . " %-12:c")
  2173. (tags . " %-12:c"))
  2174. "Format specifications for the prefix of items in the agenda views.
  2175. An alist with four entries, for the different agenda types. The keys to the
  2176. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2177. are format strings.
  2178. This format works similar to a printf format, with the following meaning:
  2179. %c the category of the item, \"Diary\" for entries from the diary, or
  2180. as given by the CATEGORY keyword or derived from the file name.
  2181. %T the *last* tag of the item. Last because inherited tags come
  2182. first in the list.
  2183. %t the time-of-day specification if one applies to the entry, in the
  2184. format HH:MM
  2185. %s Scheduling/Deadline information, a short string
  2186. All specifiers work basically like the standard `%s' of printf, but may
  2187. contain two additional characters: A question mark just after the `%' and
  2188. a whitespace/punctuation character just before the final letter.
  2189. If the first character after `%' is a question mark, the entire field
  2190. will only be included if the corresponding value applies to the
  2191. current entry. This is useful for fields which should have fixed
  2192. width when present, but zero width when absent. For example,
  2193. \"%?-12t\" will result in a 12 character time field if a time of the
  2194. day is specified, but will completely disappear in entries which do
  2195. not contain a time.
  2196. If there is punctuation or whitespace character just before the final
  2197. format letter, this character will be appended to the field value if
  2198. the value is not empty. For example, the format \"%-12:c\" leads to
  2199. \"Diary: \" if the category is \"Diary\". If the category were be
  2200. empty, no additional colon would be interted.
  2201. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2202. - Indent the line with two space characters
  2203. - Give the category in a 12 chars wide field, padded with whitespace on
  2204. the right (because of `-'). Append a colon if there is a category
  2205. (because of `:').
  2206. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2207. time, don't put in an empty field, just skip it (because of '?').
  2208. - Finally, put the scheduling information and append a whitespace.
  2209. As another example, if you don't want the time-of-day of entries in
  2210. the prefix, you could use:
  2211. (setq org-agenda-prefix-format \" %-11:c% s\")
  2212. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2213. `org-agenda-remove-tags'."
  2214. :type '(choice
  2215. (string :tag "General format")
  2216. (list :greedy t :tag "View dependent"
  2217. (cons (const agenda) (string :tag "Format"))
  2218. (cons (const timeline) (string :tag "Format"))
  2219. (cons (const todo) (string :tag "Format"))
  2220. (cons (const tags) (string :tag "Format"))))
  2221. :group 'org-agenda-prefix)
  2222. (defvar org-prefix-format-compiled nil
  2223. "The compiled version of the most recently used prefix format.
  2224. See the variable `org-agenda-prefix-format'.")
  2225. (defcustom org-agenda-remove-times-when-in-prefix t
  2226. "Non-nil means, remove duplicate time specifications in agenda items.
  2227. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2228. time-of-day specification in a headline or diary entry is extracted and
  2229. placed into the prefix. If this option is non-nil, the original specification
  2230. \(a timestamp or -range, or just a plain time(range) specification like
  2231. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2232. cluttered.
  2233. The option can be t or nil. It may also be the symbol `beg', indicating
  2234. that the time should only be removed what it is located at the beginning of
  2235. the headline/diary entry."
  2236. :group 'org-agenda-prefix
  2237. :type '(choice
  2238. (const :tag "Always" t)
  2239. (const :tag "Never" nil)
  2240. (const :tag "When at beginning of entry" beg)))
  2241. (defcustom org-agenda-default-appointment-duration nil
  2242. "Default duration for appointments that only have a starting time.
  2243. When nil, no duration is specified in such cases.
  2244. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2245. :group 'org-agenda-prefix
  2246. :type '(choice
  2247. (integer :tag "Minutes")
  2248. (const :tag "No default duration")))
  2249. (defcustom org-agenda-remove-tags nil
  2250. "Non-nil means, remove the tags from the headline copy in the agenda.
  2251. When this is the symbol `prefix', only remove tags when
  2252. `org-agenda-prefix-format' contains a `%T' specifier."
  2253. :group 'org-agenda-prefix
  2254. :type '(choice
  2255. (const :tag "Always" t)
  2256. (const :tag "Never" nil)
  2257. (const :tag "When prefix format contains %T" prefix)))
  2258. (if (fboundp 'defvaralias)
  2259. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2260. 'org-agenda-remove-tags))
  2261. (defcustom org-agenda-align-tags-to-column 65
  2262. "Shift tags in agenda items to this column."
  2263. :group 'org-agenda-prefix
  2264. :type 'integer)
  2265. (defgroup org-latex nil
  2266. "Options for embedding LaTeX code into Org-mode"
  2267. :tag "Org LaTeX"
  2268. :group 'org)
  2269. (defcustom org-format-latex-options
  2270. '(:foreground default :background default :scale 1.0
  2271. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2272. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2273. "Options for creating images from LaTeX fragments.
  2274. This is a property list with the following properties:
  2275. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2276. `default' means use the forground of the default face.
  2277. :background the background color, or \"Transparent\".
  2278. `default' means use the background of the default face.
  2279. :scale a scaling factor for the size of the images
  2280. :html-foreground, :html-background, :html-scale
  2281. The same numbers for HTML export.
  2282. :matchers a list indicating which matchers should be used to
  2283. find LaTeX fragments. Valid members of this list are:
  2284. \"begin\" find environments
  2285. \"$\" find math expressions surrounded by $...$
  2286. \"$$\" find math expressions surrounded by $$....$$
  2287. \"\\(\" find math expressions surrounded by \\(...\\)
  2288. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2289. :group 'org-export-latex
  2290. :type 'plist)
  2291. (defcustom org-format-latex-header "\\documentclass{article}
  2292. \\usepackage{fullpage} % do not remove
  2293. \\usepackage{amssymb}
  2294. \\usepackage[usenames]{color}
  2295. \\usepackage{amsmath}
  2296. \\usepackage{latexsym}
  2297. \\usepackage[mathscr]{eucal}
  2298. \\pagestyle{empty} % do not remove"
  2299. "The document header used for processing LaTeX fragments."
  2300. :group 'org-export-latex
  2301. :type 'string)
  2302. (defgroup org-export nil
  2303. "Options for exporting org-listings."
  2304. :tag "Org Export"
  2305. :group 'org)
  2306. (defgroup org-export-general nil
  2307. "General options for exporting Org-mode files."
  2308. :tag "Org Export General"
  2309. :group 'org-export)
  2310. (defcustom org-export-publishing-directory "."
  2311. "Path to the location where exported files should be located.
  2312. This path may be relative to the directory where the Org-mode file lives.
  2313. The default is to put them into the same directory as the Org-mode file.
  2314. The variable may also be an alist with export types `:html', `:ascii',
  2315. `:ical', `:LaTeX', or `:xoxo' and the corresponding directories.
  2316. If a directory path is relative, it is interpreted relative to the
  2317. directory where the exported Org-mode files lives."
  2318. :group 'org-export-general
  2319. :type '(choice
  2320. (directory)
  2321. (repeat
  2322. (cons
  2323. (choice :tag "Type"
  2324. (const :html) (const :LaTeX)
  2325. (const :ascii) (const :ical) (const :xoxo))
  2326. (directory)))))
  2327. (defcustom org-export-language-setup
  2328. '(("en" "Author" "Date" "Table of Contents")
  2329. ("cs" "Autor" "Datum" "Obsah")
  2330. ("da" "Ophavsmand" "Dato" "Indhold")
  2331. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2332. ("es" "Autor" "Fecha" "\xcdndice")
  2333. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2334. ("it" "Autore" "Data" "Indice")
  2335. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2336. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2337. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2338. "Terms used in export text, translated to different languages.
  2339. Use the variable `org-export-default-language' to set the language,
  2340. or use the +OPTION lines for a per-file setting."
  2341. :group 'org-export-general
  2342. :type '(repeat
  2343. (list
  2344. (string :tag "HTML language tag")
  2345. (string :tag "Author")
  2346. (string :tag "Date")
  2347. (string :tag "Table of Contents"))))
  2348. (defcustom org-export-default-language "en"
  2349. "The default language of HTML export, as a string.
  2350. This should have an association in `org-export-language-setup'."
  2351. :group 'org-export-general
  2352. :type 'string)
  2353. (defcustom org-export-skip-text-before-1st-heading t
  2354. "Non-nil means, skip all text before the first headline when exporting.
  2355. When nil, that text is exported as well."
  2356. :group 'org-export-general
  2357. :type 'boolean)
  2358. (defcustom org-export-headline-levels 3
  2359. "The last level which is still exported as a headline.
  2360. Inferior levels will produce itemize lists when exported.
  2361. Note that a numeric prefix argument to an exporter function overrides
  2362. this setting.
  2363. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2364. :group 'org-export-general
  2365. :type 'number)
  2366. (defcustom org-export-with-section-numbers t
  2367. "Non-nil means, add section numbers to headlines when exporting.
  2368. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2369. :group 'org-export-general
  2370. :type 'boolean)
  2371. (defcustom org-export-with-toc t
  2372. "Non-nil means, create a table of contents in exported files.
  2373. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2374. When an integer, include levels up to N in the toc, this may then be
  2375. different from `org-export-headline-levels', but it will not be allowed
  2376. to be larger than the number of headline levels.
  2377. When nil, no table of contents is made.
  2378. Headlines which contain any TODO items will be marked with \"(*)\" in
  2379. ASCII export, and with red color in HTML output, if the option
  2380. `org-export-mark-todo-in-toc' is set.
  2381. In HTML output, the TOC will be clickable.
  2382. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2383. or \"toc:3\"."
  2384. :group 'org-export-general
  2385. :type '(choice
  2386. (const :tag "No Table of Contents" nil)
  2387. (const :tag "Full Table of Contents" t)
  2388. (integer :tag "TOC to level")))
  2389. (defcustom org-export-mark-todo-in-toc nil
  2390. "Non-nil means, mark TOC lines that contain any open TODO items."
  2391. :group 'org-export-general
  2392. :type 'boolean)
  2393. (defcustom org-export-preserve-breaks nil
  2394. "Non-nil means, preserve all line breaks when exporting.
  2395. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2396. export, line breaks will always be preserved, regardless of this variable.
  2397. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2398. :group 'org-export-general
  2399. :type 'boolean)
  2400. (defcustom org-export-with-archived-trees 'headline
  2401. "Whether subtrees with the ARCHIVE tag should be exported.
  2402. This can have three different values
  2403. nil Do not export, pretend this tree is not present
  2404. t Do export the entire tree
  2405. headline Only export the headline, but skip the tree below it."
  2406. :group 'org-export-general
  2407. :group 'org-archive
  2408. :type '(choice
  2409. (const :tag "not at all" nil)
  2410. (const :tag "headline only" 'headline)
  2411. (const :tag "entirely" t)))
  2412. (defcustom org-export-author-info t
  2413. "Non-nil means, insert author name and email into the exported file.
  2414. This option can also be set with the +OPTIONS line,
  2415. e.g. \"author-info:nil\"."
  2416. :group 'org-export-general
  2417. :type 'boolean)
  2418. (defcustom org-export-time-stamp-file t
  2419. "Non-nil means, insert a time stamp into the exported file.
  2420. The time stamp shows when the file was created.
  2421. This option can also be set with the +OPTIONS line,
  2422. e.g. \"timestamp:nil\"."
  2423. :group 'org-export-general
  2424. :type 'boolean)
  2425. (defcustom org-export-with-timestamps t
  2426. "If nil, do not export time stamps and associated keywords."
  2427. :group 'org-export-general
  2428. :type 'boolean)
  2429. (defcustom org-export-remove-timestamps-from-toc t
  2430. "If nil, remove timestamps from the table of contents entries."
  2431. :group 'org-export-general
  2432. :type 'boolean)
  2433. (defcustom org-export-with-tags 'not-in-toc
  2434. "If nil, do not export tags, just remove them from headlines.
  2435. If this is the symbol `not-in-toc', tags will be removed from table of
  2436. contents entries, but still be shown in the headlines of the document."
  2437. :group 'org-export-general
  2438. :type '(choice
  2439. (const :tag "Off" nil)
  2440. (const :tag "Not in TOC" not-in-toc)
  2441. (const :tag "On" t)))
  2442. (defcustom org-export-with-property-drawer nil
  2443. "Non-nil means, export property drawers.
  2444. When nil, these drawers are removed before export.
  2445. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2446. :group 'org-export-general
  2447. :type 'boolean)
  2448. (defgroup org-export-translation nil
  2449. "Options for translating special ascii sequences for the export backends."
  2450. :tag "Org Export Translation"
  2451. :group 'org-export)
  2452. (defcustom org-export-with-emphasize t
  2453. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2454. If the export target supports emphasizing text, the word will be
  2455. typeset in bold, italic, or underlined, respectively. Works only for
  2456. single words, but you can say: I *really* *mean* *this*.
  2457. Not all export backends support this.
  2458. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2459. :group 'org-export-translation
  2460. :type 'boolean)
  2461. (defcustom org-export-with-footnotes t
  2462. "If nil, export [1] as a footnote marker.
  2463. Lines starting with [1] will be formatted as footnotes.
  2464. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2465. :group 'org-export-translation
  2466. :type 'boolean)
  2467. (defcustom org-export-with-sub-superscripts t
  2468. "Non-nil means, interpret \"_\" and \"^\" for export.
  2469. When this option is turned on, you can use TeX-like syntax for sub- and
  2470. superscripts. Several characters after \"_\" or \"^\" will be
  2471. considered as a single item - so grouping with {} is normally not
  2472. needed. For example, the following things will be parsed as single
  2473. sub- or superscripts.
  2474. 10^24 or 10^tau several digits will be considered 1 item.
  2475. 10^-12 or 10^-tau a leading sign with digits or a word
  2476. x^2-y^3 will be read as x^2 - y^3, because items are
  2477. terminated by almost any nonword/nondigit char.
  2478. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2479. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2480. sub/superscript. If you set this variable to the symbol `{}',
  2481. the braces are *required* in order to trigger interpretations as
  2482. sub/superscript. This can be helpful in documents that need \"_\"
  2483. frequently in plain text.
  2484. Not all export backends support this, but HTML does.
  2485. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2486. :group 'org-export-translation
  2487. :type '(choice
  2488. (const :tag "Always interpret" t)
  2489. (const :tag "Only with braces" {})
  2490. (const :tag "Never interpret" nil)))
  2491. (defcustom org-export-with-TeX-macros t
  2492. "Non-nil means, interpret simple TeX-like macros when exporting.
  2493. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2494. No only real TeX macros will work here, but the standard HTML entities
  2495. for math can be used as macro names as well. For a list of supported
  2496. names in HTML export, see the constant `org-html-entities'.
  2497. Not all export backends support this.
  2498. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2499. :group 'org-export-translation
  2500. :group 'org-export-latex
  2501. :type 'boolean)
  2502. (defcustom org-export-with-LaTeX-fragments nil
  2503. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2504. When set, the exporter will find LaTeX environments if the \\begin line is
  2505. the first non-white thing on a line. It will also find the math delimiters
  2506. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2507. display math.
  2508. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2509. :group 'org-export-translation
  2510. :group 'org-export-latex
  2511. :type 'boolean)
  2512. (defcustom org-export-with-fixed-width t
  2513. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2514. This can be used to have pre-formatted text, fragments of code etc. For
  2515. example:
  2516. : ;; Some Lisp examples
  2517. : (while (defc cnt)
  2518. : (ding))
  2519. will be looking just like this in also HTML. See also the QUOTE keyword.
  2520. Not all export backends support this.
  2521. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2522. :group 'org-export-translation
  2523. :type 'boolean)
  2524. (defcustom org-match-sexp-depth 3
  2525. "Number of stacked braces for sub/superscript matching.
  2526. This has to be set before loading org.el to be effective."
  2527. :group 'org-export-translation
  2528. :type 'integer)
  2529. (defgroup org-export-tables nil
  2530. "Options for exporting tables in Org-mode."
  2531. :tag "Org Export Tables"
  2532. :group 'org-export)
  2533. (defcustom org-export-with-tables t
  2534. "If non-nil, lines starting with \"|\" define a table.
  2535. For example:
  2536. | Name | Address | Birthday |
  2537. |-------------+----------+-----------|
  2538. | Arthur Dent | England | 29.2.2100 |
  2539. Not all export backends support this.
  2540. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2541. :group 'org-export-tables
  2542. :type 'boolean)
  2543. (defcustom org-export-highlight-first-table-line t
  2544. "Non-nil means, highlight the first table line.
  2545. In HTML export, this means use <th> instead of <td>.
  2546. In tables created with table.el, this applies to the first table line.
  2547. In Org-mode tables, all lines before the first horizontal separator
  2548. line will be formatted with <th> tags."
  2549. :group 'org-export-tables
  2550. :type 'boolean)
  2551. (defcustom org-export-table-remove-special-lines t
  2552. "Remove special lines and marking characters in calculating tables.
  2553. This removes the special marking character column from tables that are set
  2554. up for spreadsheet calculations. It also removes the entire lines
  2555. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2556. the values of constants may be useful to have."
  2557. :group 'org-export-tables
  2558. :type 'boolean)
  2559. (defcustom org-export-prefer-native-exporter-for-tables nil
  2560. "Non-nil means, always export tables created with table.el natively.
  2561. Natively means, use the HTML code generator in table.el.
  2562. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2563. the table does not use row- or column-spanning). This has the
  2564. advantage, that the automatic HTML conversions for math symbols and
  2565. sub/superscripts can be applied. Org-mode's HTML generator is also
  2566. much faster."
  2567. :group 'org-export-tables
  2568. :type 'boolean)
  2569. (defgroup org-export-ascii nil
  2570. "Options specific for ASCII export of Org-mode files."
  2571. :tag "Org Export ASCII"
  2572. :group 'org-export)
  2573. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2574. "Characters for underlining headings in ASCII export.
  2575. In the given sequence, these characters will be used for level 1, 2, ..."
  2576. :group 'org-export-ascii
  2577. :type '(repeat character))
  2578. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2579. "Bullet characters for headlines converted to lists in ASCII export.
  2580. The first character is is used for the first lest level generated in this
  2581. way, and so on. If there are more levels than characters given here,
  2582. the list will be repeated.
  2583. Note that plain lists will keep the same bullets as the have in the
  2584. Org-mode file."
  2585. :group 'org-export-ascii
  2586. :type '(repeat character))
  2587. (defgroup org-export-xml nil
  2588. "Options specific for XML export of Org-mode files."
  2589. :tag "Org Export XML"
  2590. :group 'org-export)
  2591. (defgroup org-export-html nil
  2592. "Options specific for HTML export of Org-mode files."
  2593. :tag "Org Export HTML"
  2594. :group 'org-export)
  2595. (defcustom org-export-html-coding-system nil
  2596. ""
  2597. :group 'org-export-html
  2598. :type 'coding-system)
  2599. (defcustom org-export-html-extension "html"
  2600. "The extension for exported HTML files."
  2601. :group 'org-export-html
  2602. :type 'string)
  2603. (defcustom org-export-html-style
  2604. "<style type=\"text/css\">
  2605. html {
  2606. font-family: Times, serif;
  2607. font-size: 12pt;
  2608. }
  2609. .title { text-align: center; }
  2610. .todo { color: red; }
  2611. .done { color: green; }
  2612. .timestamp { color: grey }
  2613. .timestamp-kwd { color: CadetBlue }
  2614. .tag { background-color:lightblue; font-weight:normal }
  2615. .target { background-color: lavender; }
  2616. pre {
  2617. border: 1pt solid #AEBDCC;
  2618. background-color: #F3F5F7;
  2619. padding: 5pt;
  2620. font-family: courier, monospace;
  2621. }
  2622. table { border-collapse: collapse; }
  2623. td, th {
  2624. vertical-align: top;
  2625. <!--border: 1pt solid #ADB9CC;-->
  2626. }
  2627. </style>"
  2628. "The default style specification for exported HTML files.
  2629. Since there are different ways of setting style information, this variable
  2630. needs to contain the full HTML structure to provide a style, including the
  2631. surrounding HTML tags. The style specifications should include definitions
  2632. for new classes todo, done, title, and deadline. For example, legal values
  2633. would be:
  2634. <style type=\"text/css\">
  2635. p { font-weight: normal; color: gray; }
  2636. h1 { color: black; }
  2637. .title { text-align: center; }
  2638. .todo, .deadline { color: red; }
  2639. .done { color: green; }
  2640. </style>
  2641. or, if you want to keep the style in a file,
  2642. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2643. As the value of this option simply gets inserted into the HTML <head> header,
  2644. you can \"misuse\" it to add arbitrary text to the header."
  2645. :group 'org-export-html
  2646. :type 'string)
  2647. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2648. "Format for typesetting the document title in HTML export."
  2649. :group 'org-export-html
  2650. :type 'string)
  2651. (defcustom org-export-html-toplevel-hlevel 2
  2652. "The <H> level for level 1 headings in HTML export."
  2653. :group 'org-export-html
  2654. :type 'string)
  2655. (defcustom org-export-html-link-org-files-as-html t
  2656. "Non-nil means, make file links to `file.org' point to `file.html'.
  2657. When org-mode is exporting an org-mode file to HTML, links to
  2658. non-html files are directly put into a href tag in HTML.
  2659. However, links to other Org-mode files (recognized by the
  2660. extension `.org.) should become links to the corresponding html
  2661. file, assuming that the linked org-mode file will also be
  2662. converted to HTML.
  2663. When nil, the links still point to the plain `.org' file."
  2664. :group 'org-export-html
  2665. :type 'boolean)
  2666. (defcustom org-export-html-inline-images 'maybe
  2667. "Non-nil means, inline images into exported HTML pages.
  2668. This is done using an <img> tag. When nil, an anchor with href is used to
  2669. link to the image. If this option is `maybe', then images in links with
  2670. an empty description will be inlined, while images with a description will
  2671. be linked only."
  2672. :group 'org-export-html
  2673. :type '(choice (const :tag "Never" nil)
  2674. (const :tag "Always" t)
  2675. (const :tag "When there is no description" maybe)))
  2676. ;; FIXME: rename
  2677. (defcustom org-export-html-expand t
  2678. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2679. When nil, these tags will be exported as plain text and therefore
  2680. not be interpreted by a browser.
  2681. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2682. :group 'org-export-html
  2683. :type 'boolean)
  2684. (defcustom org-export-html-table-tag
  2685. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2686. "The HTML tag that is used to start a table.
  2687. This must be a <table> tag, but you may change the options like
  2688. borders and spacing."
  2689. :group 'org-export-html
  2690. :type 'string)
  2691. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2692. "The opening tag for table header fields.
  2693. This is customizable so that alignment options can be specified."
  2694. :group 'org-export-tables
  2695. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2696. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2697. "The opening tag for table data fields.
  2698. This is customizable so that alignment options can be specified."
  2699. :group 'org-export-tables
  2700. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2701. (defcustom org-export-html-with-timestamp nil
  2702. "If non-nil, write `org-export-html-html-helper-timestamp'
  2703. into the exported HTML text. Otherwise, the buffer will just be saved
  2704. to a file."
  2705. :group 'org-export-html
  2706. :type 'boolean)
  2707. (defcustom org-export-html-html-helper-timestamp
  2708. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2709. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2710. :group 'org-export-html
  2711. :type 'string)
  2712. (defgroup org-export-icalendar nil
  2713. "Options specific for iCalendar export of Org-mode files."
  2714. :tag "Org Export iCalendar"
  2715. :group 'org-export)
  2716. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2717. "The file name for the iCalendar file covering all agenda files.
  2718. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2719. The file name should be absolute."
  2720. :group 'org-export-icalendar
  2721. :type 'file)
  2722. (defcustom org-icalendar-include-todo nil
  2723. "Non-nil means, export to iCalendar files should also cover TODO items."
  2724. :group 'org-export-icalendar
  2725. :type '(choice
  2726. (const :tag "None" nil)
  2727. (const :tag "Unfinished" t)
  2728. (const :tag "All" all)))
  2729. (defcustom org-icalendar-include-sexps t
  2730. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2731. These are entries like in the diary, but directly in an Org-mode file."
  2732. :group 'org-export-icalendar
  2733. :type 'boolean)
  2734. (defcustom org-icalendar-include-body 100
  2735. "Amount of text below headline to be included in iCalendar export.
  2736. This is a number of characters that should maximally be included.
  2737. Properties, scheduling and clocking lines will always be removed.
  2738. The text will be inserted into the DESCRIPTION field."
  2739. :group 'org-export-icalendar
  2740. :type '(choice
  2741. (const :tag "Nothing" nil)
  2742. (const :tag "Everything" t)
  2743. (integer :tag "Max characters")))
  2744. (defcustom org-icalendar-combined-name "OrgMode"
  2745. "Calendar name for the combined iCalendar representing all agenda files."
  2746. :group 'org-export-icalendar
  2747. :type 'string)
  2748. (defgroup org-font-lock nil
  2749. "Font-lock settings for highlighting in Org-mode."
  2750. :tag "Org Font Lock"
  2751. :group 'org)
  2752. (defcustom org-level-color-stars-only nil
  2753. "Non-nil means fontify only the stars in each headline.
  2754. When nil, the entire headline is fontified.
  2755. Changing it requires restart of `font-lock-mode' to become effective
  2756. also in regions already fontified."
  2757. :group 'org-font-lock
  2758. :type 'boolean)
  2759. (defcustom org-hide-leading-stars nil
  2760. "Non-nil means, hide the first N-1 stars in a headline.
  2761. This works by using the face `org-hide' for these stars. This
  2762. face is white for a light background, and black for a dark
  2763. background. You may have to customize the face `org-hide' to
  2764. make this work.
  2765. Changing it requires restart of `font-lock-mode' to become effective
  2766. also in regions already fontified.
  2767. You may also set this on a per-file basis by adding one of the following
  2768. lines to the buffer:
  2769. #+STARTUP: hidestars
  2770. #+STARTUP: showstars"
  2771. :group 'org-font-lock
  2772. :type 'boolean)
  2773. (defcustom org-fontify-done-headline nil
  2774. "Non-nil means, change the face of a headline if it is marked DONE.
  2775. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2776. When this is non-nil, the headline after the keyword is set to the
  2777. `org-headline-done' as an additional indication."
  2778. :group 'org-font-lock
  2779. :type 'boolean)
  2780. (defcustom org-fontify-emphasized-text t
  2781. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2782. Changing this variable requires a restart of Emacs to take effect."
  2783. :group 'org-font-lock
  2784. :type 'boolean)
  2785. (defvar org-emph-re nil
  2786. "Regular expression for matching emphasis.")
  2787. (defvar org-emphasis-regexp-components) ; defined just below
  2788. (defvar org-emphasis-alist) ; defined just below
  2789. (defun org-set-emph-re (var val)
  2790. "Set variable and compute the emphasis regular expression."
  2791. (set var val)
  2792. (when (and (boundp 'org-emphasis-alist)
  2793. (boundp 'org-emphasis-regexp-components)
  2794. org-emphasis-alist org-emphasis-regexp-components)
  2795. (let* ((e org-emphasis-regexp-components)
  2796. (pre (car e))
  2797. (post (nth 1 e))
  2798. (border (nth 2 e))
  2799. (body (nth 3 e))
  2800. (nl (nth 4 e))
  2801. (stacked (nth 5 e))
  2802. (body1 (concat body "*?"))
  2803. (markers (mapconcat 'car org-emphasis-alist "")))
  2804. ;; make sure special characters appear at the right position in the class
  2805. (if (string-match "\\^" markers)
  2806. (setq markers (concat (replace-match "" t t markers) "^")))
  2807. (if (string-match "-" markers)
  2808. (setq markers (concat (replace-match "" t t markers) "-")))
  2809. (if (> nl 0)
  2810. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2811. (int-to-string nl) "\\}")))
  2812. ;; Make the regexp
  2813. (setq org-emph-re
  2814. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2815. "\\("
  2816. "\\([" markers "]\\)"
  2817. "\\("
  2818. "[^" border (if (and nil stacked) markers) "]"
  2819. body1
  2820. "[^" border (if (and nil stacked) markers) "]"
  2821. "\\)"
  2822. "\\3\\)"
  2823. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2824. (defcustom org-emphasis-regexp-components
  2825. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2826. "Components used to build the reqular expression for emphasis.
  2827. This is a list with 6 entries. Terminology: In an emphasis string
  2828. like \" *strong word* \", we call the initial space PREMATCH, the final
  2829. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2830. and \"trong wor\" is the body. The different components in this variable
  2831. specify what is allowed/forbidden in each part:
  2832. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2833. post Chars allowed as postmatch. End of line will be allowed too.
  2834. border The chars *forbidden* as border characters.
  2835. body-regexp A regexp like \".\" to match a body character. Don't use
  2836. non-shy groups here, and don't allow newline here.
  2837. newline The maximum number of newlines allowed in an emphasis exp.
  2838. stacked Non-nil means, allow stacked styles. This works only in HTML
  2839. export. When this is set, all marker characters (as given in
  2840. `org-emphasis-alist') will be allowed as pre/post, aiding
  2841. inside-out matching.
  2842. Use customize to modify this, or restart Emacs after changing it."
  2843. :group 'org-font-lock
  2844. :set 'org-set-emph-re
  2845. :type '(list
  2846. (sexp :tag "Allowed chars in pre ")
  2847. (sexp :tag "Allowed chars in post ")
  2848. (sexp :tag "Forbidden chars in border ")
  2849. (sexp :tag "Regexp for body ")
  2850. (integer :tag "number of newlines allowed")
  2851. (boolean :tag "Stacking allowed ")))
  2852. (defcustom org-emphasis-alist
  2853. '(("*" bold "<b>" "</b>")
  2854. ("/" italic "<i>" "</i>")
  2855. ("_" underline "<u>" "</u>")
  2856. ("=" org-code "<code>" "</code>")
  2857. ("+" (:strike-through t) "<del>" "</del>")
  2858. )
  2859. "Special syntax for emphasized text.
  2860. Text starting and ending with a special character will be emphasized, for
  2861. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2862. characters, the face to be used by font-lock for highlighting in Org-mode
  2863. Emacs buffers, and the HTML tags to be used for this.
  2864. Use customize to modify this, or restart Emacs after changing it."
  2865. :group 'org-font-lock
  2866. :set 'org-set-emph-re
  2867. :type '(repeat
  2868. (list
  2869. (string :tag "Marker character")
  2870. (choice
  2871. (face :tag "Font-lock-face")
  2872. (plist :tag "Face property list"))
  2873. (string :tag "HTML start tag")
  2874. (string :tag "HTML end tag"))))
  2875. ;;; The faces
  2876. (defgroup org-faces nil
  2877. "Faces in Org-mode."
  2878. :tag "Org Faces"
  2879. :group 'org-font-lock)
  2880. ;; FIXME: convert that into a macro? Not critical, because this
  2881. ;; is only executed a few times at load time.
  2882. (defun org-compatible-face (inherits specs)
  2883. "Make a compatible face specification.
  2884. If INHERITS is an existing face and if the Emacs version supports it,
  2885. just inherit the face. If not, use SPECS to define the face.
  2886. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2887. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2888. to the top of the list. The `min-colors' attribute will be removed from
  2889. any other entries, and any resulting duplicates will be removed entirely."
  2890. (cond
  2891. ((and inherits (facep inherits)
  2892. (not (featurep 'xemacs)) (> emacs-major-version 22))
  2893. ;; In Emacs 23, we use inheritance where possible.
  2894. ;; We only do this in Emacs 23, because only there the outline
  2895. ;; faces have been changed to the original org-mode-level-faces.
  2896. (list (list t :inherit inherits)))
  2897. ((or (featurep 'xemacs) (< emacs-major-version 22))
  2898. ;; These do not understand the `min-colors' attribute.
  2899. (let (r e a)
  2900. (while (setq e (pop specs))
  2901. (cond
  2902. ((memq (car e) '(t default)) (push e r))
  2903. ((setq a (member '(min-colors 8) (car e)))
  2904. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2905. (cdr e)))))
  2906. ((setq a (assq 'min-colors (car e)))
  2907. (setq e (cons (delq a (car e)) (cdr e)))
  2908. (or (assoc (car e) r) (push e r)))
  2909. (t (or (assoc (car e) r) (push e r)))))
  2910. (nreverse r)))
  2911. (t specs)))
  2912. (defface org-hide
  2913. '((((background light)) (:foreground "white"))
  2914. (((background dark)) (:foreground "black")))
  2915. "Face used to hide leading stars in headlines.
  2916. The forground color of this face should be equal to the background
  2917. color of the frame."
  2918. :group 'org-faces)
  2919. (defface org-level-1 ;; font-lock-function-name-face
  2920. (org-compatible-face
  2921. 'outline-1
  2922. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2923. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2924. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2925. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2926. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2927. (t (:bold t))))
  2928. "Face used for level 1 headlines."
  2929. :group 'org-faces)
  2930. (defface org-level-2 ;; font-lock-variable-name-face
  2931. (org-compatible-face
  2932. 'outline-2
  2933. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2934. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2935. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2936. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2937. (t (:bold t))))
  2938. "Face used for level 2 headlines."
  2939. :group 'org-faces)
  2940. (defface org-level-3 ;; font-lock-keyword-face
  2941. (org-compatible-face
  2942. 'outline-3
  2943. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2944. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2945. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2946. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2947. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2948. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2949. (t (:bold t))))
  2950. "Face used for level 3 headlines."
  2951. :group 'org-faces)
  2952. (defface org-level-4 ;; font-lock-comment-face
  2953. (org-compatible-face
  2954. 'outline-4
  2955. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2956. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2957. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2958. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2959. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2960. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2961. (t (:bold t))))
  2962. "Face used for level 4 headlines."
  2963. :group 'org-faces)
  2964. (defface org-level-5 ;; font-lock-type-face
  2965. (org-compatible-face
  2966. 'outline-5
  2967. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2968. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2969. (((class color) (min-colors 8)) (:foreground "green"))))
  2970. "Face used for level 5 headlines."
  2971. :group 'org-faces)
  2972. (defface org-level-6 ;; font-lock-constant-face
  2973. (org-compatible-face
  2974. 'outline-6
  2975. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2976. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2977. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2978. "Face used for level 6 headlines."
  2979. :group 'org-faces)
  2980. (defface org-level-7 ;; font-lock-builtin-face
  2981. (org-compatible-face
  2982. 'outline-7
  2983. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2984. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2985. (((class color) (min-colors 8)) (:foreground "blue"))))
  2986. "Face used for level 7 headlines."
  2987. :group 'org-faces)
  2988. (defface org-level-8 ;; font-lock-string-face
  2989. (org-compatible-face
  2990. 'outline-8
  2991. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2992. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2993. (((class color) (min-colors 8)) (:foreground "green"))))
  2994. "Face used for level 8 headlines."
  2995. :group 'org-faces)
  2996. (defface org-special-keyword ;; font-lock-string-face
  2997. (org-compatible-face
  2998. nil
  2999. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3000. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3001. (t (:italic t))))
  3002. "Face used for special keywords."
  3003. :group 'org-faces)
  3004. (defface org-drawer ;; font-lock-function-name-face
  3005. (org-compatible-face
  3006. nil
  3007. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3008. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3009. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3010. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3011. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3012. (t (:bold t))))
  3013. "Face used for drawers."
  3014. :group 'org-faces)
  3015. (defface org-property-value nil
  3016. "Face used for the value of a property."
  3017. :group 'org-faces)
  3018. (defface org-column
  3019. (org-compatible-face
  3020. nil
  3021. '((((class color) (min-colors 16) (background light))
  3022. (:background "grey90"))
  3023. (((class color) (min-colors 16) (background dark))
  3024. (:background "grey30"))
  3025. (((class color) (min-colors 8))
  3026. (:background "cyan" :foreground "black"))
  3027. (t (:inverse-video t))))
  3028. "Face for column display of entry properties."
  3029. :group 'org-faces)
  3030. (when (fboundp 'set-face-attribute)
  3031. ;; Make sure that a fixed-width face is used when we have a column table.
  3032. (set-face-attribute 'org-column nil
  3033. :height (face-attribute 'default :height)
  3034. :family (face-attribute 'default :family)))
  3035. (defface org-warning
  3036. (org-compatible-face
  3037. 'font-lock-warning-face
  3038. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3039. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3040. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3041. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3042. (t (:bold t))))
  3043. "Face for deadlines and TODO keywords."
  3044. :group 'org-faces)
  3045. (defface org-archived ; similar to shadow
  3046. (org-compatible-face
  3047. 'shadow
  3048. '((((class color grayscale) (min-colors 88) (background light))
  3049. (:foreground "grey50"))
  3050. (((class color grayscale) (min-colors 88) (background dark))
  3051. (:foreground "grey70"))
  3052. (((class color) (min-colors 8) (background light))
  3053. (:foreground "green"))
  3054. (((class color) (min-colors 8) (background dark))
  3055. (:foreground "yellow"))))
  3056. "Face for headline with the ARCHIVE tag."
  3057. :group 'org-faces)
  3058. (defface org-link
  3059. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3060. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3061. (t (:underline t)))
  3062. "Face for links."
  3063. :group 'org-faces)
  3064. (defface org-target
  3065. '((((class color) (background light)) (:underline t))
  3066. (((class color) (background dark)) (:underline t))
  3067. (t (:underline t)))
  3068. "Face for links."
  3069. :group 'org-faces)
  3070. (defface org-date
  3071. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3072. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3073. (t (:underline t)))
  3074. "Face for links."
  3075. :group 'org-faces)
  3076. (defface org-sexp-date
  3077. '((((class color) (background light)) (:foreground "Purple"))
  3078. (((class color) (background dark)) (:foreground "Cyan"))
  3079. (t (:underline t)))
  3080. "Face for links."
  3081. :group 'org-faces)
  3082. (defface org-tag
  3083. '((t (:bold t)))
  3084. "Face for tags."
  3085. :group 'org-faces)
  3086. (defface org-todo ; font-lock-warning-face
  3087. (org-compatible-face
  3088. nil
  3089. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3090. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3091. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3092. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3093. (t (:inverse-video t :bold t))))
  3094. "Face for TODO keywords."
  3095. :group 'org-faces)
  3096. (defface org-done ;; font-lock-type-face
  3097. (org-compatible-face
  3098. nil
  3099. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3100. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3101. (((class color) (min-colors 8)) (:foreground "green"))
  3102. (t (:bold t))))
  3103. "Face used for todo keywords that indicate DONE items."
  3104. :group 'org-faces)
  3105. (defface org-headline-done ;; font-lock-string-face
  3106. (org-compatible-face
  3107. nil
  3108. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3109. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3110. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3111. "Face used to indicate that a headline is DONE.
  3112. This face is only used if `org-fontify-done-headline' is set. If applies
  3113. to the part of the headline after the DONE keyword."
  3114. :group 'org-faces)
  3115. (defcustom org-todo-keyword-faces nil
  3116. "Faces for specific TODO keywords.
  3117. This is a list of cons cells, with TODO keywords in the car
  3118. and faces in the cdr. The face can be a symbol, or a property
  3119. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3120. :group 'org-faces
  3121. :group 'org-todo
  3122. :type '(repeat
  3123. (cons
  3124. (string :tag "keyword")
  3125. (sexp :tag "face"))))
  3126. (defface org-table ;; font-lock-function-name-face
  3127. (org-compatible-face
  3128. nil
  3129. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3130. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3131. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3132. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3133. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3134. (((class color) (min-colors 8) (background dark)))))
  3135. "Face used for tables."
  3136. :group 'org-faces)
  3137. (defface org-formula
  3138. (org-compatible-face
  3139. nil
  3140. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3141. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3142. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3143. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3144. (t (:bold t :italic t))))
  3145. "Face for formulas."
  3146. :group 'org-faces)
  3147. (defface org-code
  3148. (org-compatible-face
  3149. nil
  3150. '((((class color grayscale) (min-colors 88) (background light))
  3151. (:foreground "grey50"))
  3152. (((class color grayscale) (min-colors 88) (background dark))
  3153. (:foreground "grey70"))
  3154. (((class color) (min-colors 8) (background light))
  3155. (:foreground "green"))
  3156. (((class color) (min-colors 8) (background dark))
  3157. (:foreground "yellow"))))
  3158. "Face for fixed-with text like code snippets."
  3159. :group 'org-faces
  3160. :version "22.1")
  3161. (defface org-agenda-structure ;; font-lock-function-name-face
  3162. (org-compatible-face
  3163. nil
  3164. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3165. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3166. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3167. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3168. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3169. (t (:bold t))))
  3170. "Face used in agenda for captions and dates."
  3171. :group 'org-faces)
  3172. (defface org-scheduled-today
  3173. (org-compatible-face
  3174. nil
  3175. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3176. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3177. (((class color) (min-colors 8)) (:foreground "green"))
  3178. (t (:bold t :italic t))))
  3179. "Face for items scheduled for a certain day."
  3180. :group 'org-faces)
  3181. (defface org-scheduled-previously
  3182. (org-compatible-face
  3183. nil
  3184. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3185. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3186. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3187. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3188. (t (:bold t))))
  3189. "Face for items scheduled previously, and not yet done."
  3190. :group 'org-faces)
  3191. (defface org-upcoming-deadline
  3192. (org-compatible-face
  3193. nil
  3194. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3195. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3196. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3197. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3198. (t (:bold t))))
  3199. "Face for items scheduled previously, and not yet done."
  3200. :group 'org-faces)
  3201. (defcustom org-agenda-deadline-faces
  3202. '((1.0 . org-warning)
  3203. (0.5 . org-upcoming-deadline)
  3204. (0.0 . default))
  3205. "Faces for showing deadlines in the agenda.
  3206. This is a list of cons cells. The cdr of each cess is a face to be used,
  3207. and it can also just be a like like '(:foreground \"yellow\").
  3208. Each car is a fraction of the head-warning time that must have passed for
  3209. this the face in the cdr to be used for display. The numbers must be
  3210. given in descending order. The head-warning time is normally taken
  3211. from `org-deadline-warning-days', but can also be specified in the deadline
  3212. timestamp itself, like this:
  3213. DEADLINE: <2007-08-13 Mon -8d>
  3214. You may use d for days, w for weeks, m for months and y for years. Months
  3215. and years will only be treated in an approximate fashion (30.4 days for a
  3216. month and 365.24 days for a year)."
  3217. :group 'org-faces
  3218. :group 'org-agenda-daily/weekly
  3219. :type '(repeat
  3220. (cons
  3221. (number :tag "Fraction of head-warning time passed")
  3222. (sexp :tag "Face"))))
  3223. (defface org-time-grid ;; font-lock-variable-name-face
  3224. (org-compatible-face
  3225. nil
  3226. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3227. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3228. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3229. "Face used for time grids."
  3230. :group 'org-faces)
  3231. (defconst org-level-faces
  3232. '(org-level-1 org-level-2 org-level-3 org-level-4
  3233. org-level-5 org-level-6 org-level-7 org-level-8
  3234. ))
  3235. (defcustom org-n-level-faces (length org-level-faces)
  3236. "The number different faces to be used for headlines.
  3237. Org-mode defines 8 different headline faces, so this can be at most 8.
  3238. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3239. :type 'number
  3240. :group 'org-faces)
  3241. ;;; Variables for pre-computed regular expressions, all buffer local
  3242. (defvar org-drawer-regexp nil
  3243. "Matches first line of a hidden block.")
  3244. (make-variable-buffer-local 'org-drawer-regexp)
  3245. (defvar org-todo-regexp nil
  3246. "Matches any of the TODO state keywords.")
  3247. (make-variable-buffer-local 'org-todo-regexp)
  3248. (defvar org-not-done-regexp nil
  3249. "Matches any of the TODO state keywords except the last one.")
  3250. (make-variable-buffer-local 'org-not-done-regexp)
  3251. (defvar org-todo-line-regexp nil
  3252. "Matches a headline and puts TODO state into group 2 if present.")
  3253. (make-variable-buffer-local 'org-todo-line-regexp)
  3254. (defvar org-todo-line-tags-regexp nil
  3255. "Matches a headline and puts TODO state into group 2 if present.
  3256. Also put tags into group 4 if tags are present.")
  3257. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3258. (defvar org-nl-done-regexp nil
  3259. "Matches newline followed by a headline with the DONE keyword.")
  3260. (make-variable-buffer-local 'org-nl-done-regexp)
  3261. (defvar org-looking-at-done-regexp nil
  3262. "Matches the DONE keyword a point.")
  3263. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3264. (defvar org-ds-keyword-length 12
  3265. "Maximum length of the Deadline and SCHEDULED keywords.")
  3266. (make-variable-buffer-local 'org-ds-keyword-length)
  3267. (defvar org-deadline-regexp nil
  3268. "Matches the DEADLINE keyword.")
  3269. (make-variable-buffer-local 'org-deadline-regexp)
  3270. (defvar org-deadline-time-regexp nil
  3271. "Matches the DEADLINE keyword together with a time stamp.")
  3272. (make-variable-buffer-local 'org-deadline-time-regexp)
  3273. (defvar org-deadline-line-regexp nil
  3274. "Matches the DEADLINE keyword and the rest of the line.")
  3275. (make-variable-buffer-local 'org-deadline-line-regexp)
  3276. (defvar org-scheduled-regexp nil
  3277. "Matches the SCHEDULED keyword.")
  3278. (make-variable-buffer-local 'org-scheduled-regexp)
  3279. (defvar org-scheduled-time-regexp nil
  3280. "Matches the SCHEDULED keyword together with a time stamp.")
  3281. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3282. (defvar org-closed-time-regexp nil
  3283. "Matches the CLOSED keyword together with a time stamp.")
  3284. (make-variable-buffer-local 'org-closed-time-regexp)
  3285. (defvar org-keyword-time-regexp nil
  3286. "Matches any of the 4 keywords, together with the time stamp.")
  3287. (make-variable-buffer-local 'org-keyword-time-regexp)
  3288. (defvar org-keyword-time-not-clock-regexp nil
  3289. "Matches any of the 3 keywords, together with the time stamp.")
  3290. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3291. (defvar org-maybe-keyword-time-regexp nil
  3292. "Matches a timestamp, possibly preceeded by a keyword.")
  3293. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3294. (defvar org-planning-or-clock-line-re nil
  3295. "Matches a line with planning or clock info.")
  3296. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3297. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3298. rear-nonsticky t mouse-map t fontified t)
  3299. "Properties to remove when a string without properties is wanted.")
  3300. (defsubst org-match-string-no-properties (num &optional string)
  3301. (if (featurep 'xemacs)
  3302. (let ((s (match-string num string)))
  3303. (remove-text-properties 0 (length s) org-rm-props s)
  3304. s)
  3305. (match-string-no-properties num string)))
  3306. (defsubst org-no-properties (s)
  3307. (if (fboundp 'set-text-properties)
  3308. (set-text-properties 0 (length s) nil s)
  3309. (remove-text-properties 0 (length s) org-rm-props s))
  3310. s)
  3311. (defsubst org-get-alist-option (option key)
  3312. (cond ((eq key t) t)
  3313. ((eq option t) t)
  3314. ((assoc key option) (cdr (assoc key option)))
  3315. (t (cdr (assq 'default option)))))
  3316. (defsubst org-inhibit-invisibility ()
  3317. "Modified `buffer-invisibility-spec' for Emacs 21.
  3318. Some ops with invisible text do not work correctly on Emacs 21. For these
  3319. we turn off invisibility temporarily. Use this in a `let' form."
  3320. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3321. (defsubst org-set-local (var value)
  3322. "Make VAR local in current buffer and set it to VALUE."
  3323. (set (make-variable-buffer-local var) value))
  3324. (defsubst org-mode-p ()
  3325. "Check if the current buffer is in Org-mode."
  3326. (eq major-mode 'org-mode))
  3327. (defsubst org-last (list)
  3328. "Return the last element of LIST."
  3329. (car (last list)))
  3330. (defun org-let (list &rest body)
  3331. (eval (cons 'let (cons list body))))
  3332. (put 'org-let 'lisp-indent-function 1)
  3333. (defun org-let2 (list1 list2 &rest body)
  3334. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3335. (put 'org-let2 'lisp-indent-function 2)
  3336. (defconst org-startup-options
  3337. '(("fold" org-startup-folded t)
  3338. ("overview" org-startup-folded t)
  3339. ("nofold" org-startup-folded nil)
  3340. ("showall" org-startup-folded nil)
  3341. ("content" org-startup-folded content)
  3342. ("hidestars" org-hide-leading-stars t)
  3343. ("showstars" org-hide-leading-stars nil)
  3344. ("odd" org-odd-levels-only t)
  3345. ("oddeven" org-odd-levels-only nil)
  3346. ("align" org-startup-align-all-tables t)
  3347. ("noalign" org-startup-align-all-tables nil)
  3348. ("customtime" org-display-custom-times t)
  3349. ("logging" org-log-done t)
  3350. ("logdone" org-log-done t)
  3351. ("nologging" org-log-done nil)
  3352. ("lognotedone" org-log-done done push)
  3353. ("lognotestate" org-log-done state push)
  3354. ("lognoteclock-out" org-log-done clock-out push)
  3355. ("logrepeat" org-log-repeat t)
  3356. ("nologrepeat" org-log-repeat nil)
  3357. ("constcgs" constants-unit-system cgs)
  3358. ("constSI" constants-unit-system SI))
  3359. "Variable associated with STARTUP options for org-mode.
  3360. Each element is a list of three items: The startup options as written
  3361. in the #+STARTUP line, the corresponding variable, and the value to
  3362. set this variable to if the option is found. An optional forth element PUSH
  3363. means to push this value onto the list in the variable.")
  3364. (defun org-set-regexps-and-options ()
  3365. "Precompute regular expressions for current buffer."
  3366. (when (org-mode-p)
  3367. (org-set-local 'org-todo-kwd-alist nil)
  3368. (org-set-local 'org-todo-key-alist nil)
  3369. (org-set-local 'org-todo-key-trigger nil)
  3370. (org-set-local 'org-todo-keywords-1 nil)
  3371. (org-set-local 'org-done-keywords nil)
  3372. (org-set-local 'org-todo-heads nil)
  3373. (org-set-local 'org-todo-sets nil)
  3374. (org-set-local 'org-todo-log-states nil)
  3375. (let ((re (org-make-options-regexp
  3376. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3377. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3378. "CONSTANTS" "PROPERTY")))
  3379. (splitre "[ \t]+")
  3380. kwds kws0 kwsa key value cat arch tags const links hw dws
  3381. tail sep kws1 prio props
  3382. ex log note)
  3383. (save-excursion
  3384. (save-restriction
  3385. (widen)
  3386. (goto-char (point-min))
  3387. (while (re-search-forward re nil t)
  3388. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3389. (cond
  3390. ((equal key "CATEGORY")
  3391. (if (string-match "[ \t]+$" value)
  3392. (setq value (replace-match "" t t value)))
  3393. (setq cat (intern value)))
  3394. ((member key '("SEQ_TODO" "TODO"))
  3395. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3396. ((equal key "TYP_TODO")
  3397. (push (cons 'type (org-split-string value splitre)) kwds))
  3398. ((equal key "TAGS")
  3399. (setq tags (append tags (org-split-string value splitre))))
  3400. ((equal key "COLUMNS")
  3401. (org-set-local 'org-columns-default-format value))
  3402. ((equal key "LINK")
  3403. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3404. (push (cons (match-string 1 value)
  3405. (org-trim (match-string 2 value)))
  3406. links)))
  3407. ((equal key "PRIORITIES")
  3408. (setq prio (org-split-string value " +")))
  3409. ((equal key "PROPERTY")
  3410. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3411. (push (cons (match-string 1 value) (match-string 2 value))
  3412. props)))
  3413. ((equal key "CONSTANTS")
  3414. (setq const (append const (org-split-string value splitre))))
  3415. ((equal key "STARTUP")
  3416. (let ((opts (org-split-string value splitre))
  3417. l var val)
  3418. (while (setq l (pop opts))
  3419. (when (setq l (assoc l org-startup-options))
  3420. (setq var (nth 1 l) val (nth 2 l))
  3421. (if (not (nth 3 l))
  3422. (set (make-local-variable var) val)
  3423. (if (not (listp (symbol-value var)))
  3424. (set (make-local-variable var) nil))
  3425. (set (make-local-variable var) (symbol-value var))
  3426. (add-to-list var val))))))
  3427. ((equal key "ARCHIVE")
  3428. (string-match " *$" value)
  3429. (setq arch (replace-match "" t t value))
  3430. (remove-text-properties 0 (length arch)
  3431. '(face t fontified t) arch)))
  3432. )))
  3433. (and cat (org-set-local 'org-category cat))
  3434. (when prio
  3435. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3436. (setq prio (mapcar 'string-to-char prio))
  3437. (org-set-local 'org-highest-priority (nth 0 prio))
  3438. (org-set-local 'org-lowest-priority (nth 1 prio))
  3439. (org-set-local 'org-default-priority (nth 2 prio)))
  3440. (and props (org-set-local 'org-local-properties (nreverse props)))
  3441. (and arch (org-set-local 'org-archive-location arch))
  3442. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3443. ;; Process the TODO keywords
  3444. (unless kwds
  3445. ;; Use the global values as if they had been given locally.
  3446. (setq kwds (default-value 'org-todo-keywords))
  3447. (if (stringp (car kwds))
  3448. (setq kwds (list (cons org-todo-interpretation
  3449. (default-value 'org-todo-keywords)))))
  3450. (setq kwds (reverse kwds)))
  3451. (setq kwds (nreverse kwds))
  3452. (let (inter kws kw)
  3453. (while (setq kws (pop kwds))
  3454. (setq inter (pop kws) sep (member "|" kws)
  3455. kws0 (delete "|" (copy-sequence kws))
  3456. kwsa nil
  3457. kws1 (mapcar
  3458. (lambda (x)
  3459. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3460. (progn
  3461. (setq kw (match-string 1 x)
  3462. ex (and (match-end 2) (match-string 2 x))
  3463. log (and ex (string-match "@" ex))
  3464. key (and ex (substring ex 0 1)))
  3465. (if (equal key "@") (setq key nil))
  3466. (push (cons kw (and key (string-to-char key))) kwsa)
  3467. (and log (push kw org-todo-log-states))
  3468. kw)
  3469. (error "Invalid TODO keyword %s" x)))
  3470. kws0)
  3471. kwsa (if kwsa (append '((:startgroup))
  3472. (nreverse kwsa)
  3473. '((:endgroup))))
  3474. hw (car kws1)
  3475. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3476. tail (list inter hw (car dws) (org-last dws)))
  3477. (add-to-list 'org-todo-heads hw 'append)
  3478. (push kws1 org-todo-sets)
  3479. (setq org-done-keywords (append org-done-keywords dws nil))
  3480. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3481. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3482. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3483. (setq org-todo-sets (nreverse org-todo-sets)
  3484. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3485. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3486. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3487. ;; Process the constants
  3488. (when const
  3489. (let (e cst)
  3490. (while (setq e (pop const))
  3491. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3492. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3493. (setq org-table-formula-constants-local cst)))
  3494. ;; Process the tags.
  3495. (when tags
  3496. (let (e tgs)
  3497. (while (setq e (pop tags))
  3498. (cond
  3499. ((equal e "{") (push '(:startgroup) tgs))
  3500. ((equal e "}") (push '(:endgroup) tgs))
  3501. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3502. (push (cons (match-string 1 e)
  3503. (string-to-char (match-string 2 e)))
  3504. tgs))
  3505. (t (push (list e) tgs))))
  3506. (org-set-local 'org-tag-alist nil)
  3507. (while (setq e (pop tgs))
  3508. (or (and (stringp (car e))
  3509. (assoc (car e) org-tag-alist))
  3510. (push e org-tag-alist))))))
  3511. ;; Compute the regular expressions and other local variables
  3512. (if (not org-done-keywords)
  3513. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3514. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3515. (length org-scheduled-string)))
  3516. org-drawer-regexp
  3517. (concat "^[ \t]*:\\("
  3518. (mapconcat 'regexp-quote org-drawers "\\|")
  3519. "\\):[ \t]*$")
  3520. org-not-done-keywords
  3521. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3522. org-todo-regexp
  3523. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3524. "\\|") "\\)\\>")
  3525. org-not-done-regexp
  3526. (concat "\\<\\("
  3527. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3528. "\\)\\>")
  3529. org-todo-line-regexp
  3530. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3531. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3532. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3533. org-nl-done-regexp
  3534. (concat "\n\\*+[ \t]+"
  3535. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3536. "\\)" "\\>")
  3537. org-todo-line-tags-regexp
  3538. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3539. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3540. (org-re
  3541. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3542. org-looking-at-done-regexp
  3543. (concat "^" "\\(?:"
  3544. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3545. "\\>")
  3546. org-deadline-regexp (concat "\\<" org-deadline-string)
  3547. org-deadline-time-regexp
  3548. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3549. org-deadline-line-regexp
  3550. (concat "\\<\\(" org-deadline-string "\\).*")
  3551. org-scheduled-regexp
  3552. (concat "\\<" org-scheduled-string)
  3553. org-scheduled-time-regexp
  3554. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3555. org-closed-time-regexp
  3556. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3557. org-keyword-time-regexp
  3558. (concat "\\<\\(" org-scheduled-string
  3559. "\\|" org-deadline-string
  3560. "\\|" org-closed-string
  3561. "\\|" org-clock-string "\\)"
  3562. " *[[<]\\([^]>]+\\)[]>]")
  3563. org-keyword-time-not-clock-regexp
  3564. (concat "\\<\\(" org-scheduled-string
  3565. "\\|" org-deadline-string
  3566. "\\|" org-closed-string
  3567. "\\)"
  3568. " *[[<]\\([^]>]+\\)[]>]")
  3569. org-maybe-keyword-time-regexp
  3570. (concat "\\(\\<\\(" org-scheduled-string
  3571. "\\|" org-deadline-string
  3572. "\\|" org-closed-string
  3573. "\\|" org-clock-string "\\)\\)?"
  3574. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3575. org-planning-or-clock-line-re
  3576. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3577. "\\|" org-deadline-string
  3578. "\\|" org-closed-string "\\|" org-clock-string
  3579. "\\)\\>\\)")
  3580. )
  3581. (org-set-font-lock-defaults)))
  3582. (defun org-remove-keyword-keys (list)
  3583. (mapcar (lambda (x)
  3584. (if (string-match "(..?)$" x)
  3585. (substring x 0 (match-beginning 0))
  3586. x))
  3587. list))
  3588. ;;; Some variables ujsed in various places
  3589. (defvar org-window-configuration nil
  3590. "Used in various places to store a window configuration.")
  3591. (defvar org-finish-function nil
  3592. "Function to be called when `C-c C-c' is used.
  3593. This is for getting out of special buffers like remember.")
  3594. ;;; Foreign variables, to inform the compiler
  3595. ;; XEmacs only
  3596. (defvar outline-mode-menu-heading)
  3597. (defvar outline-mode-menu-show)
  3598. (defvar outline-mode-menu-hide)
  3599. (defvar zmacs-regions) ; XEmacs regions
  3600. ;; Emacs only
  3601. (defvar mark-active)
  3602. ;; Packages that org-mode interacts with
  3603. (defvar calc-embedded-close-formula)
  3604. (defvar calc-embedded-open-formula)
  3605. (defvar font-lock-unfontify-region-function)
  3606. (defvar org-goto-start-pos)
  3607. (defvar vm-message-pointer)
  3608. (defvar vm-folder-directory)
  3609. (defvar wl-summary-buffer-elmo-folder)
  3610. (defvar wl-summary-buffer-folder-name)
  3611. (defvar gnus-other-frame-object)
  3612. (defvar gnus-group-name)
  3613. (defvar gnus-article-current)
  3614. (defvar w3m-current-url)
  3615. (defvar w3m-current-title)
  3616. (defvar mh-progs)
  3617. (defvar mh-current-folder)
  3618. (defvar mh-show-folder-buffer)
  3619. (defvar mh-index-folder)
  3620. (defvar mh-searcher)
  3621. (defvar calendar-mode-map)
  3622. (defvar Info-current-file)
  3623. (defvar Info-current-node)
  3624. (defvar texmathp-why)
  3625. (defvar remember-save-after-remembering)
  3626. (defvar remember-data-file)
  3627. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3628. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3629. (defvar org-latex-regexps)
  3630. (defvar constants-unit-system)
  3631. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3632. ;; FIXME: Occasionally check by commenting these, to make sure
  3633. ;; no other functions uses these, forgetting to let-bind them.
  3634. (defvar entry)
  3635. (defvar state)
  3636. (defvar last-state)
  3637. (defvar date)
  3638. (defvar description)
  3639. ;; Defined somewhere in this file, but used before definition.
  3640. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3641. (defvar org-agenda-buffer-name)
  3642. (defvar org-agenda-undo-list)
  3643. (defvar org-agenda-pending-undo-list)
  3644. (defvar org-agenda-overriding-header)
  3645. (defvar orgtbl-mode)
  3646. (defvar org-html-entities)
  3647. (defvar org-struct-menu)
  3648. (defvar org-org-menu)
  3649. (defvar org-tbl-menu)
  3650. (defvar org-agenda-keymap)
  3651. ;;;; Emacs/XEmacs compatibility
  3652. ;; Overlay compatibility functions
  3653. (defun org-make-overlay (beg end &optional buffer)
  3654. (if (featurep 'xemacs)
  3655. (make-extent beg end buffer)
  3656. (make-overlay beg end buffer)))
  3657. (defun org-delete-overlay (ovl)
  3658. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3659. (defun org-detach-overlay (ovl)
  3660. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3661. (defun org-move-overlay (ovl beg end &optional buffer)
  3662. (if (featurep 'xemacs)
  3663. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3664. (move-overlay ovl beg end buffer)))
  3665. (defun org-overlay-put (ovl prop value)
  3666. (if (featurep 'xemacs)
  3667. (set-extent-property ovl prop value)
  3668. (overlay-put ovl prop value)))
  3669. (defun org-overlay-display (ovl text &optional face evap)
  3670. "Make overlay OVL display TEXT with face FACE."
  3671. (if (featurep 'xemacs)
  3672. (let ((gl (make-glyph text)))
  3673. (and face (set-glyph-face gl face))
  3674. (set-extent-property ovl 'invisible t)
  3675. (set-extent-property ovl 'end-glyph gl))
  3676. (overlay-put ovl 'display text)
  3677. (if face (overlay-put ovl 'face face))
  3678. (if evap (overlay-put ovl 'evaporate t))))
  3679. (defun org-overlay-before-string (ovl text &optional face evap)
  3680. "Make overlay OVL display TEXT with face FACE."
  3681. (if (featurep 'xemacs)
  3682. (let ((gl (make-glyph text)))
  3683. (and face (set-glyph-face gl face))
  3684. (set-extent-property ovl 'begin-glyph gl))
  3685. (if face (org-add-props text nil 'face face))
  3686. (overlay-put ovl 'before-string text)
  3687. (if evap (overlay-put ovl 'evaporate t))))
  3688. (defun org-overlay-get (ovl prop)
  3689. (if (featurep 'xemacs)
  3690. (extent-property ovl prop)
  3691. (overlay-get ovl prop)))
  3692. (defun org-overlays-at (pos)
  3693. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3694. ;; FIXME: this is currently not used
  3695. (defun org-overlays-in (&optional start end)
  3696. (if (featurep 'xemacs)
  3697. (extent-list nil start end)
  3698. (overlays-in start end)))
  3699. (defun org-overlay-start (o)
  3700. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3701. (defun org-overlay-end (o)
  3702. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3703. ;; FIXME: this is currently not used
  3704. (defun org-find-overlays (prop &optional pos delete)
  3705. "Find all overlays specifying PROP at POS or point.
  3706. If DELETE is non-nil, delete all those overlays."
  3707. (let ((overlays (org-overlays-at (or pos (point))))
  3708. ov found)
  3709. (while (setq ov (pop overlays))
  3710. (if (org-overlay-get ov prop)
  3711. (if delete (org-delete-overlay ov) (push ov found))))
  3712. found))
  3713. ;; Region compatibility
  3714. (defun org-add-hook (hook function &optional append local)
  3715. "Add-hook, compatible with both Emacsen."
  3716. (if (and local (featurep 'xemacs))
  3717. (add-local-hook hook function append)
  3718. (add-hook hook function append local)))
  3719. (defvar org-ignore-region nil
  3720. "To temporarily disable the active region.")
  3721. (defun org-region-active-p ()
  3722. "Is `transient-mark-mode' on and the region active?
  3723. Works on both Emacs and XEmacs."
  3724. (if org-ignore-region
  3725. nil
  3726. (if (featurep 'xemacs)
  3727. (and zmacs-regions (region-active-p))
  3728. (and transient-mark-mode mark-active))))
  3729. ;; Invisibility compatibility
  3730. (defun org-add-to-invisibility-spec (arg)
  3731. "Add elements to `buffer-invisibility-spec'.
  3732. See documentation for `buffer-invisibility-spec' for the kind of elements
  3733. that can be added."
  3734. (cond
  3735. ((fboundp 'add-to-invisibility-spec)
  3736. (add-to-invisibility-spec arg))
  3737. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3738. (setq buffer-invisibility-spec (list arg)))
  3739. (t
  3740. (setq buffer-invisibility-spec
  3741. (cons arg buffer-invisibility-spec)))))
  3742. (defun org-remove-from-invisibility-spec (arg)
  3743. "Remove elements from `buffer-invisibility-spec'."
  3744. (if (fboundp 'remove-from-invisibility-spec)
  3745. (remove-from-invisibility-spec arg)
  3746. (if (consp buffer-invisibility-spec)
  3747. (setq buffer-invisibility-spec
  3748. (delete arg buffer-invisibility-spec)))))
  3749. ;; FIXME: this is currently not used
  3750. (defun org-in-invisibility-spec-p (arg)
  3751. "Is ARG a member of `buffer-invisibility-spec'?"
  3752. (if (consp buffer-invisibility-spec)
  3753. (member arg buffer-invisibility-spec)
  3754. nil))
  3755. ;;;; Define the Org-mode
  3756. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3757. (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."))
  3758. ;; We use a before-change function to check if a table might need
  3759. ;; an update.
  3760. (defvar org-table-may-need-update t
  3761. "Indicates that a table might need an update.
  3762. This variable is set by `org-before-change-function'.
  3763. `org-table-align' sets it back to nil.")
  3764. (defvar org-mode-map)
  3765. (defvar org-mode-hook nil)
  3766. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3767. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3768. (defvar org-table-buffer-is-an nil)
  3769. (defconst org-outline-regexp "\\*+ ")
  3770. ;;;###autoload
  3771. (define-derived-mode org-mode outline-mode "Org"
  3772. "Outline-based notes management and organizer, alias
  3773. \"Carsten's outline-mode for keeping track of everything.\"
  3774. Org-mode develops organizational tasks around a NOTES file which
  3775. contains information about projects as plain text. Org-mode is
  3776. implemented on top of outline-mode, which is ideal to keep the content
  3777. of large files well structured. It supports ToDo items, deadlines and
  3778. time stamps, which magically appear in the diary listing of the Emacs
  3779. calendar. Tables are easily created with a built-in table editor.
  3780. Plain text URL-like links connect to websites, emails (VM), Usenet
  3781. messages (Gnus), BBDB entries, and any files related to the project.
  3782. For printing and sharing of notes, an Org-mode file (or a part of it)
  3783. can be exported as a structured ASCII or HTML file.
  3784. The following commands are available:
  3785. \\{org-mode-map}"
  3786. ;; Get rid of Outline menus, they are not needed
  3787. ;; Need to do this here because define-derived-mode sets up
  3788. ;; the keymap so late. Still, it is a waste to call this each time
  3789. ;; we switch another buffer into org-mode.
  3790. (if (featurep 'xemacs)
  3791. (when (boundp 'outline-mode-menu-heading)
  3792. ;; Assume this is Greg's port, it used easymenu
  3793. (easy-menu-remove outline-mode-menu-heading)
  3794. (easy-menu-remove outline-mode-menu-show)
  3795. (easy-menu-remove outline-mode-menu-hide))
  3796. (define-key org-mode-map [menu-bar headings] 'undefined)
  3797. (define-key org-mode-map [menu-bar hide] 'undefined)
  3798. (define-key org-mode-map [menu-bar show] 'undefined))
  3799. (easy-menu-add org-org-menu)
  3800. (easy-menu-add org-tbl-menu)
  3801. (org-install-agenda-files-menu)
  3802. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3803. (org-add-to-invisibility-spec '(org-cwidth))
  3804. (when (featurep 'xemacs)
  3805. (org-set-local 'line-move-ignore-invisible t))
  3806. (org-set-local 'outline-regexp org-outline-regexp)
  3807. (org-set-local 'outline-level 'org-outline-level)
  3808. (when (and org-ellipsis
  3809. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3810. (fboundp 'make-glyph-code))
  3811. (unless org-display-table
  3812. (setq org-display-table (make-display-table)))
  3813. (set-display-table-slot
  3814. org-display-table 4
  3815. (vconcat (mapcar
  3816. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3817. org-ellipsis)))
  3818. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3819. (setq buffer-display-table org-display-table))
  3820. (org-set-regexps-and-options)
  3821. ;; Calc embedded
  3822. (org-set-local 'calc-embedded-open-mode "# ")
  3823. (modify-syntax-entry ?# "<")
  3824. (modify-syntax-entry ?@ "w")
  3825. (if org-startup-truncated (setq truncate-lines t))
  3826. (org-set-local 'font-lock-unfontify-region-function
  3827. 'org-unfontify-region)
  3828. ;; Activate before-change-function
  3829. (org-set-local 'org-table-may-need-update t)
  3830. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3831. 'local)
  3832. ;; Check for running clock before killing a buffer
  3833. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3834. ;; Paragraphs and auto-filling
  3835. (org-set-autofill-regexps)
  3836. (setq indent-line-function 'org-indent-line-function)
  3837. (org-update-radio-target-regexp)
  3838. ;; Comment characters
  3839. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3840. (org-set-local 'comment-padding " ")
  3841. ;; Make isearch reveal context
  3842. (if (or (featurep 'xemacs)
  3843. (not (boundp 'outline-isearch-open-invisible-function)))
  3844. ;; Emacs 21 and XEmacs make use of the hook
  3845. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3846. ;; Emacs 22 deals with this through a special variable
  3847. (org-set-local 'outline-isearch-open-invisible-function
  3848. (lambda (&rest ignore) (org-show-context 'isearch))))
  3849. ;; If empty file that did not turn on org-mode automatically, make it to.
  3850. (if (and org-insert-mode-line-in-empty-file
  3851. (interactive-p)
  3852. (= (point-min) (point-max)))
  3853. (insert "# -*- mode: org -*-\n\n"))
  3854. (unless org-inhibit-startup
  3855. (when org-startup-align-all-tables
  3856. (let ((bmp (buffer-modified-p)))
  3857. (org-table-map-tables 'org-table-align)
  3858. (set-buffer-modified-p bmp)))
  3859. (org-cycle-hide-drawers 'all)
  3860. (cond
  3861. ((eq org-startup-folded t)
  3862. (org-cycle '(4)))
  3863. ((eq org-startup-folded 'content)
  3864. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3865. (org-cycle '(4)) (org-cycle '(4)))))))
  3866. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3867. (defsubst org-call-with-arg (command arg)
  3868. "Call COMMAND interactively, but pretend prefix are was ARG."
  3869. (let ((current-prefix-arg arg)) (call-interactively command)))
  3870. (defsubst org-current-line (&optional pos)
  3871. (save-excursion
  3872. (and pos (goto-char pos))
  3873. ;; works also in narrowed buffer, because we start at 1, not point-min
  3874. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3875. (defun org-current-time ()
  3876. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3877. (if (> org-time-stamp-rounding-minutes 0)
  3878. (let ((r org-time-stamp-rounding-minutes)
  3879. (time (decode-time)))
  3880. (apply 'encode-time
  3881. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3882. (nthcdr 2 time))))
  3883. (current-time)))
  3884. (defun org-add-props (string plist &rest props)
  3885. "Add text properties to entire string, from beginning to end.
  3886. PLIST may be a list of properties, PROPS are individual properties and values
  3887. that will be added to PLIST. Returns the string that was modified."
  3888. (add-text-properties
  3889. 0 (length string) (if props (append plist props) plist) string)
  3890. string)
  3891. (put 'org-add-props 'lisp-indent-function 2)
  3892. ;;;; Font-Lock stuff, including the activators
  3893. (defvar org-mouse-map (make-sparse-keymap))
  3894. (org-defkey org-mouse-map
  3895. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3896. (org-defkey org-mouse-map
  3897. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3898. (when org-mouse-1-follows-link
  3899. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3900. (when org-tab-follows-link
  3901. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3902. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3903. (when org-return-follows-link
  3904. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3905. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3906. (require 'font-lock)
  3907. (defconst org-non-link-chars "]\t\n\r<>")
  3908. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3909. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3910. (defvar org-link-re-with-space nil
  3911. "Matches a link with spaces, optional angular brackets around it.")
  3912. (defvar org-link-re-with-space2 nil
  3913. "Matches a link with spaces, optional angular brackets around it.")
  3914. (defvar org-angle-link-re nil
  3915. "Matches link with angular brackets, spaces are allowed.")
  3916. (defvar org-plain-link-re nil
  3917. "Matches plain link, without spaces.")
  3918. (defvar org-bracket-link-regexp nil
  3919. "Matches a link in double brackets.")
  3920. (defvar org-bracket-link-analytic-regexp nil
  3921. "Regular expression used to analyze links.
  3922. Here is what the match groups contain after a match:
  3923. 1: http:
  3924. 2: http
  3925. 3: path
  3926. 4: [desc]
  3927. 5: desc")
  3928. (defvar org-any-link-re nil
  3929. "Regular expression matching any link.")
  3930. (defun org-make-link-regexps ()
  3931. "Update the link regular expressions.
  3932. This should be called after the variable `org-link-types' has changed."
  3933. (setq org-link-re-with-space
  3934. (concat
  3935. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3936. "\\([^" org-non-link-chars " ]"
  3937. "[^" org-non-link-chars "]*"
  3938. "[^" org-non-link-chars " ]\\)>?")
  3939. org-link-re-with-space2
  3940. (concat
  3941. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3942. "\\([^" org-non-link-chars " ]"
  3943. "[^]\t\n\r]*"
  3944. "[^" org-non-link-chars " ]\\)>?")
  3945. org-angle-link-re
  3946. (concat
  3947. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3948. "\\([^" org-non-link-chars " ]"
  3949. "[^" org-non-link-chars "]*"
  3950. "\\)>")
  3951. org-plain-link-re
  3952. (concat
  3953. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3954. "\\([^]\t\n\r<>,;() ]+\\)")
  3955. org-bracket-link-regexp
  3956. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3957. org-bracket-link-analytic-regexp
  3958. (concat
  3959. "\\[\\["
  3960. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3961. "\\([^]]+\\)"
  3962. "\\]"
  3963. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3964. "\\]")
  3965. org-any-link-re
  3966. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3967. org-angle-link-re "\\)\\|\\("
  3968. org-plain-link-re "\\)")))
  3969. (org-make-link-regexps)
  3970. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3971. "Regular expression for fast time stamp matching.")
  3972. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3973. "Regular expression for fast time stamp matching.")
  3974. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3975. "Regular expression matching time strings for analysis.
  3976. This one does not require the space after the date.")
  3977. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3978. "Regular expression matching time strings for analysis.")
  3979. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3980. "Regular expression matching time stamps, with groups.")
  3981. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3982. "Regular expression matching time stamps (also [..]), with groups.")
  3983. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3984. "Regular expression matching a time stamp range.")
  3985. (defconst org-tr-regexp-both
  3986. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3987. "Regular expression matching a time stamp range.")
  3988. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3989. org-ts-regexp "\\)?")
  3990. "Regular expression matching a time stamp or time stamp range.")
  3991. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3992. org-ts-regexp-both "\\)?")
  3993. "Regular expression matching a time stamp or time stamp range.
  3994. The time stamps may be either active or inactive.")
  3995. (defvar org-emph-face nil)
  3996. (defun org-do-emphasis-faces (limit)
  3997. "Run through the buffer and add overlays to links."
  3998. (let (rtn)
  3999. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4000. (if (not (= (char-after (match-beginning 3))
  4001. (char-after (match-beginning 4))))
  4002. (progn
  4003. (setq rtn t)
  4004. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4005. 'face
  4006. (nth 1 (assoc (match-string 3)
  4007. org-emphasis-alist)))
  4008. (add-text-properties (match-beginning 2) (match-end 2)
  4009. '(font-lock-multiline t))
  4010. (backward-char 1))))
  4011. rtn))
  4012. (defun org-emphasize (&optional char)
  4013. "Insert or change an emphasis, i.e. a font like bold or italic.
  4014. If there is an active region, change that region to a new emphasis.
  4015. If there is no region, just insert the marker characters and position
  4016. the cursor between them.
  4017. CHAR should be either the marker character, or the first character of the
  4018. HTML tag associated with that emphasis. If CHAR is a space, the means
  4019. to remove the emphasis of the selected region.
  4020. If char is not given (for example in an interactive call) it
  4021. will be prompted for."
  4022. (interactive)
  4023. (let ((eal org-emphasis-alist) e det
  4024. (erc org-emphasis-regexp-components)
  4025. (prompt "")
  4026. (string "") beg end move tag c s)
  4027. (if (org-region-active-p)
  4028. (setq beg (region-beginning) end (region-end)
  4029. string (buffer-substring beg end))
  4030. (setq move t))
  4031. (while (setq e (pop eal))
  4032. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4033. c (aref tag 0))
  4034. (push (cons c (string-to-char (car e))) det)
  4035. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4036. (substring tag 1)))))
  4037. (unless char
  4038. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4039. (setq char (read-char-exclusive)))
  4040. (setq char (or (cdr (assoc char det)) char))
  4041. (if (equal char ?\ )
  4042. (setq s "" move nil)
  4043. (unless (assoc (char-to-string char) org-emphasis-alist)
  4044. (error "No such emphasis marker: \"%c\"" char))
  4045. (setq s (char-to-string char)))
  4046. (while (and (> (length string) 1)
  4047. (equal (substring string 0 1) (substring string -1))
  4048. (assoc (substring string 0 1) org-emphasis-alist))
  4049. (setq string (substring string 1 -1)))
  4050. (setq string (concat s string s))
  4051. (if beg (delete-region beg end))
  4052. (unless (or (bolp)
  4053. (string-match (concat "[" (nth 0 erc) "\n]")
  4054. (char-to-string (char-before (point)))))
  4055. (insert " "))
  4056. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4057. (char-to-string (char-after (point))))
  4058. (insert " ") (backward-char 1))
  4059. (insert string)
  4060. (and move (backward-char 1))))
  4061. (defun org-activate-plain-links (limit)
  4062. "Run through the buffer and add overlays to links."
  4063. (catch 'exit
  4064. (let (f)
  4065. (while (re-search-forward org-plain-link-re limit t)
  4066. (setq f (get-text-property (match-beginning 0) 'face))
  4067. (if (or (eq f 'org-tag)
  4068. (and (listp f) (memq 'org-tag f)))
  4069. nil
  4070. (add-text-properties (match-beginning 0) (match-end 0)
  4071. (list 'mouse-face 'highlight
  4072. 'rear-nonsticky t
  4073. 'keymap org-mouse-map
  4074. ))
  4075. (throw 'exit t))))))
  4076. (defun org-activate-angle-links (limit)
  4077. "Run through the buffer and add overlays to links."
  4078. (if (re-search-forward org-angle-link-re limit t)
  4079. (progn
  4080. (add-text-properties (match-beginning 0) (match-end 0)
  4081. (list 'mouse-face 'highlight
  4082. 'rear-nonsticky t
  4083. 'keymap org-mouse-map
  4084. ))
  4085. t)))
  4086. (defmacro org-maybe-intangible (props)
  4087. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4088. In emacs 21, invisible text is not avoided by the command loop, so the
  4089. intangible property is needed to make sure point skips this text.
  4090. In Emacs 22, this is not necessary. The intangible text property has
  4091. led to problems with flyspell. These problems are fixed in flyspell.el,
  4092. but we still avoid setting the property in Emacs 22 and later.
  4093. We use a macro so that the test can happen at compilation time."
  4094. (if (< emacs-major-version 22)
  4095. `(append '(intangible t) ,props)
  4096. props))
  4097. (defun org-activate-bracket-links (limit)
  4098. "Run through the buffer and add overlays to bracketed links."
  4099. (if (re-search-forward org-bracket-link-regexp limit t)
  4100. (let* ((help (concat "LINK: "
  4101. (org-match-string-no-properties 1)))
  4102. ;; FIXME: above we should remove the escapes.
  4103. ;; but that requires another match, protecting match data,
  4104. ;; a lot of overhead for font-lock.
  4105. (ip (org-maybe-intangible
  4106. (list 'invisible 'org-link 'rear-nonsticky t
  4107. 'keymap org-mouse-map 'mouse-face 'highlight
  4108. 'help-echo help)))
  4109. (vp (list 'rear-nonsticky t
  4110. 'keymap org-mouse-map 'mouse-face 'highlight
  4111. 'help-echo help)))
  4112. ;; We need to remove the invisible property here. Table narrowing
  4113. ;; may have made some of this invisible.
  4114. (remove-text-properties (match-beginning 0) (match-end 0)
  4115. '(invisible nil))
  4116. (if (match-end 3)
  4117. (progn
  4118. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4119. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4120. (add-text-properties (match-end 3) (match-end 0) ip))
  4121. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4122. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4123. (add-text-properties (match-end 1) (match-end 0) ip))
  4124. t)))
  4125. (defun org-activate-dates (limit)
  4126. "Run through the buffer and add overlays to dates."
  4127. (if (re-search-forward org-tsr-regexp-both limit t)
  4128. (progn
  4129. (add-text-properties (match-beginning 0) (match-end 0)
  4130. (list 'mouse-face 'highlight
  4131. 'rear-nonsticky t
  4132. 'keymap org-mouse-map))
  4133. (when org-display-custom-times
  4134. (if (match-end 3)
  4135. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4136. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4137. t)))
  4138. (defvar org-target-link-regexp nil
  4139. "Regular expression matching radio targets in plain text.")
  4140. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4141. "Regular expression matching a link target.")
  4142. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4143. "Regular expression matching a radio target.")
  4144. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4145. "Regular expression matching any target.")
  4146. (defun org-activate-target-links (limit)
  4147. "Run through the buffer and add overlays to target matches."
  4148. (when org-target-link-regexp
  4149. (let ((case-fold-search t))
  4150. (if (re-search-forward org-target-link-regexp limit t)
  4151. (progn
  4152. (add-text-properties (match-beginning 0) (match-end 0)
  4153. (list 'mouse-face 'highlight
  4154. 'rear-nonsticky t
  4155. 'keymap org-mouse-map
  4156. 'help-echo "Radio target link"
  4157. 'org-linked-text t))
  4158. t)))))
  4159. (defun org-update-radio-target-regexp ()
  4160. "Find all radio targets in this file and update the regular expression."
  4161. (interactive)
  4162. (when (memq 'radio org-activate-links)
  4163. (setq org-target-link-regexp
  4164. (org-make-target-link-regexp (org-all-targets 'radio)))
  4165. (org-restart-font-lock)))
  4166. (defun org-hide-wide-columns (limit)
  4167. (let (s e)
  4168. (setq s (text-property-any (point) (or limit (point-max))
  4169. 'org-cwidth t))
  4170. (when s
  4171. (setq e (next-single-property-change s 'org-cwidth))
  4172. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4173. (goto-char e)
  4174. t)))
  4175. (defun org-restart-font-lock ()
  4176. "Restart font-lock-mode, to force refontification."
  4177. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4178. ;; FIXME: Could font-lock-fontify-buffer be enough???
  4179. (font-lock-mode -1)
  4180. (font-lock-mode 1)))
  4181. (defun org-all-targets (&optional radio)
  4182. "Return a list of all targets in this file.
  4183. With optional argument RADIO, only find radio targets."
  4184. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4185. rtn)
  4186. (save-excursion
  4187. (goto-char (point-min))
  4188. (while (re-search-forward re nil t)
  4189. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4190. rtn)))
  4191. (defun org-make-target-link-regexp (targets)
  4192. "Make regular expression matching all strings in TARGETS.
  4193. The regular expression finds the targets also if there is a line break
  4194. between words."
  4195. (and targets
  4196. (concat
  4197. "\\<\\("
  4198. (mapconcat
  4199. (lambda (x)
  4200. (while (string-match " +" x)
  4201. (setq x (replace-match "\\s-+" t t x)))
  4202. x)
  4203. targets
  4204. "\\|")
  4205. "\\)\\>")))
  4206. (defun org-activate-tags (limit)
  4207. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4208. (progn
  4209. (add-text-properties (match-beginning 1) (match-end 1)
  4210. (list 'mouse-face 'highlight
  4211. 'rear-nonsticky t
  4212. 'keymap org-mouse-map))
  4213. t)))
  4214. (defun org-outline-level ()
  4215. (save-excursion
  4216. (looking-at outline-regexp)
  4217. (if (match-beginning 1)
  4218. (+ (org-get-string-indentation (match-string 1)) 1000)
  4219. (1- (- (match-end 0) (match-beginning 0))))))
  4220. (defvar org-font-lock-keywords nil)
  4221. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4222. "Regular expression matching a property line.")
  4223. (defun org-set-font-lock-defaults ()
  4224. (let* ((em org-fontify-emphasized-text)
  4225. (lk org-activate-links)
  4226. (org-font-lock-extra-keywords
  4227. (list
  4228. ;; Headlines
  4229. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4230. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4231. ;; Table lines
  4232. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4233. (1 'org-table))
  4234. ;; Links
  4235. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4236. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4237. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4238. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4239. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4240. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4241. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4242. '(org-hide-wide-columns (0 nil append))
  4243. ;; TODO lines
  4244. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4245. '(1 (org-get-todo-face 1) t))
  4246. ;; DONE
  4247. (if org-fontify-done-headline
  4248. (list (concat "^[*]+ +\\<\\("
  4249. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4250. "\\)\\(.*\\)")
  4251. '(2 'org-headline-done t))
  4252. nil)
  4253. ;; Priorities
  4254. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4255. ;; Special keywords
  4256. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4257. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4258. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4259. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4260. ;; Emphasis
  4261. (if em
  4262. (if (featurep 'xemacs)
  4263. '(org-do-emphasis-faces (0 nil append))
  4264. '(org-do-emphasis-faces)))
  4265. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  4266. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4267. 2 'bold prepend)
  4268. (if org-provide-checkbox-statistics
  4269. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4270. (0 (org-get-checkbox-statistics-face) t)))
  4271. ;; COMMENT
  4272. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4273. "\\|" org-quote-string "\\)\\>")
  4274. '(1 'org-special-keyword t))
  4275. '("^#.*" (0 'font-lock-comment-face t))
  4276. ;; Code
  4277. '("^[ \t]*\\(:.*\\)" (1 'org-code t))
  4278. ;; Table internals
  4279. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4280. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4281. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4282. ;; Drawers
  4283. (list org-drawer-regexp '(0 'org-special-keyword t))
  4284. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4285. ;; Properties
  4286. (list org-property-re
  4287. '(1 'org-special-keyword t)
  4288. '(3 'org-property-value t))
  4289. (if org-format-transports-properties-p
  4290. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4291. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4292. )))
  4293. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4294. ;; Now set the full font-lock-keywords
  4295. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4296. (org-set-local 'font-lock-defaults
  4297. '(org-font-lock-keywords t nil nil backward-paragraph))
  4298. (kill-local-variable 'font-lock-keywords) nil))
  4299. (defvar org-m nil)
  4300. (defvar org-l nil)
  4301. (defvar org-f nil)
  4302. (defun org-get-level-face (n)
  4303. "Get the right face for match N in font-lock matching of healdines."
  4304. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4305. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4306. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4307. (cond
  4308. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4309. ((eq n 2) org-f)
  4310. (t (if org-level-color-stars-only nil org-f))))
  4311. (defun org-get-todo-face (kwd)
  4312. "Get the right face for a TODO keyword KWD.
  4313. If KWD is a number, get the corresponding match group."
  4314. (if (numberp kwd) (setq kwd (match-string kwd)))
  4315. (or (cdr (assoc kwd org-todo-keyword-faces))
  4316. (and (member kwd org-done-keywords) 'org-done)
  4317. 'org-todo))
  4318. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4319. "Remove fontification and activation overlays from links."
  4320. (font-lock-default-unfontify-region beg end)
  4321. (let* ((buffer-undo-list t)
  4322. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4323. (inhibit-modification-hooks t)
  4324. deactivate-mark buffer-file-name buffer-file-truename)
  4325. (remove-text-properties beg end
  4326. '(mouse-face t keymap t org-linked-text t
  4327. rear-nonsticky t
  4328. invisible t intangible t))))
  4329. ;;;; Visibility cycling, including org-goto and indirect buffer
  4330. ;;; Cycling
  4331. (defvar org-cycle-global-status nil)
  4332. (make-variable-buffer-local 'org-cycle-global-status)
  4333. (defvar org-cycle-subtree-status nil)
  4334. (make-variable-buffer-local 'org-cycle-subtree-status)
  4335. ;;;###autoload
  4336. (defun org-cycle (&optional arg)
  4337. "Visibility cycling for Org-mode.
  4338. - When this function is called with a prefix argument, rotate the entire
  4339. buffer through 3 states (global cycling)
  4340. 1. OVERVIEW: Show only top-level headlines.
  4341. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4342. 3. SHOW ALL: Show everything.
  4343. - When point is at the beginning of a headline, rotate the subtree started
  4344. by this line through 3 different states (local cycling)
  4345. 1. FOLDED: Only the main headline is shown.
  4346. 2. CHILDREN: The main headline and the direct children are shown.
  4347. From this state, you can move to one of the children
  4348. and zoom in further.
  4349. 3. SUBTREE: Show the entire subtree, including body text.
  4350. - When there is a numeric prefix, go up to a heading with level ARG, do
  4351. a `show-subtree' and return to the previous cursor position. If ARG
  4352. is negative, go up that many levels.
  4353. - When point is not at the beginning of a headline, execute
  4354. `indent-relative', like TAB normally does. See the option
  4355. `org-cycle-emulate-tab' for details.
  4356. - Special case: if point is at the beginning of the buffer and there is
  4357. no headline in line 1, this function will act as if called with prefix arg.
  4358. But only if also the variable `org-cycle-global-at-bob' is t."
  4359. (interactive "P")
  4360. (let* ((outline-regexp
  4361. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4362. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4363. outline-regexp))
  4364. (bob-special (and org-cycle-global-at-bob (bobp)
  4365. (not (looking-at outline-regexp))))
  4366. (org-cycle-hook
  4367. (if bob-special
  4368. (delq 'org-optimize-window-after-visibility-change
  4369. (copy-sequence org-cycle-hook))
  4370. org-cycle-hook))
  4371. (pos (point)))
  4372. (if (or bob-special (equal arg '(4)))
  4373. ;; special case: use global cycling
  4374. (setq arg t))
  4375. (cond
  4376. ((org-at-table-p 'any)
  4377. ;; Enter the table or move to the next field in the table
  4378. (or (org-table-recognize-table.el)
  4379. (progn
  4380. (if arg (org-table-edit-field t)
  4381. (org-table-justify-field-maybe)
  4382. (call-interactively 'org-table-next-field)))))
  4383. ((eq arg t) ;; Global cycling
  4384. (cond
  4385. ((and (eq last-command this-command)
  4386. (eq org-cycle-global-status 'overview))
  4387. ;; We just created the overview - now do table of contents
  4388. ;; This can be slow in very large buffers, so indicate action
  4389. (message "CONTENTS...")
  4390. (org-content)
  4391. (message "CONTENTS...done")
  4392. (setq org-cycle-global-status 'contents)
  4393. (run-hook-with-args 'org-cycle-hook 'contents))
  4394. ((and (eq last-command this-command)
  4395. (eq org-cycle-global-status 'contents))
  4396. ;; We just showed the table of contents - now show everything
  4397. (show-all)
  4398. (message "SHOW ALL")
  4399. (setq org-cycle-global-status 'all)
  4400. (run-hook-with-args 'org-cycle-hook 'all))
  4401. (t
  4402. ;; Default action: go to overview
  4403. (org-overview)
  4404. (message "OVERVIEW")
  4405. (setq org-cycle-global-status 'overview)
  4406. (run-hook-with-args 'org-cycle-hook 'overview))))
  4407. ((and org-drawers org-drawer-regexp
  4408. (save-excursion
  4409. (beginning-of-line 1)
  4410. (looking-at org-drawer-regexp)))
  4411. ;; Toggle block visibility
  4412. (org-flag-drawer
  4413. (not (get-char-property (match-end 0) 'invisible))))
  4414. ((integerp arg)
  4415. ;; Show-subtree, ARG levels up from here.
  4416. (save-excursion
  4417. (org-back-to-heading)
  4418. (outline-up-heading (if (< arg 0) (- arg)
  4419. (- (funcall outline-level) arg)))
  4420. (org-show-subtree)))
  4421. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4422. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4423. ;; At a heading: rotate between three different views
  4424. (org-back-to-heading)
  4425. (let ((goal-column 0) eoh eol eos)
  4426. ;; First, some boundaries
  4427. (save-excursion
  4428. (org-back-to-heading)
  4429. (save-excursion
  4430. (beginning-of-line 2)
  4431. (while (and (not (eobp)) ;; this is like `next-line'
  4432. (get-char-property (1- (point)) 'invisible))
  4433. (beginning-of-line 2)) (setq eol (point)))
  4434. (outline-end-of-heading) (setq eoh (point))
  4435. (org-end-of-subtree t)
  4436. (unless (eobp)
  4437. (skip-chars-forward " \t\n")
  4438. (beginning-of-line 1) ; in case this is an item
  4439. )
  4440. (setq eos (1- (point))))
  4441. ;; Find out what to do next and set `this-command'
  4442. (cond
  4443. ((= eos eoh)
  4444. ;; Nothing is hidden behind this heading
  4445. (message "EMPTY ENTRY")
  4446. (setq org-cycle-subtree-status nil)
  4447. (save-excursion
  4448. (goto-char eos)
  4449. (outline-next-heading)
  4450. (if (org-invisible-p) (org-flag-heading nil))))
  4451. ((or (>= eol eos)
  4452. (not (string-match "\\S-" (buffer-substring eol eos))))
  4453. ;; Entire subtree is hidden in one line: open it
  4454. (org-show-entry)
  4455. (show-children)
  4456. (message "CHILDREN")
  4457. (save-excursion
  4458. (goto-char eos)
  4459. (outline-next-heading)
  4460. (if (org-invisible-p) (org-flag-heading nil)))
  4461. (setq org-cycle-subtree-status 'children)
  4462. (run-hook-with-args 'org-cycle-hook 'children))
  4463. ((and (eq last-command this-command)
  4464. (eq org-cycle-subtree-status 'children))
  4465. ;; We just showed the children, now show everything.
  4466. (org-show-subtree)
  4467. (message "SUBTREE")
  4468. (setq org-cycle-subtree-status 'subtree)
  4469. (run-hook-with-args 'org-cycle-hook 'subtree))
  4470. (t
  4471. ;; Default action: hide the subtree.
  4472. (hide-subtree)
  4473. (message "FOLDED")
  4474. (setq org-cycle-subtree-status 'folded)
  4475. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4476. ;; TAB emulation
  4477. (buffer-read-only (org-back-to-heading))
  4478. ((org-try-cdlatex-tab))
  4479. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4480. (or (not (bolp))
  4481. (not (looking-at outline-regexp))))
  4482. (call-interactively (global-key-binding "\t")))
  4483. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4484. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4485. (or (and (eq org-cycle-emulate-tab 'white)
  4486. (= (match-end 0) (point-at-eol)))
  4487. (and (eq org-cycle-emulate-tab 'whitestart)
  4488. (>= (match-end 0) pos))))
  4489. t
  4490. (eq org-cycle-emulate-tab t))
  4491. (if (and (looking-at "[ \n\r\t]")
  4492. (string-match "^[ \t]*$" (buffer-substring
  4493. (point-at-bol) (point))))
  4494. (progn
  4495. (beginning-of-line 1)
  4496. (and (looking-at "[ \t]+") (replace-match ""))))
  4497. (call-interactively (global-key-binding "\t")))
  4498. (t (save-excursion
  4499. (org-back-to-heading)
  4500. (org-cycle))))))
  4501. ;;;###autoload
  4502. (defun org-global-cycle (&optional arg)
  4503. "Cycle the global visibility. For details see `org-cycle'."
  4504. (interactive "P")
  4505. (let ((org-cycle-include-plain-lists
  4506. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4507. (if (integerp arg)
  4508. (progn
  4509. (show-all)
  4510. (hide-sublevels arg)
  4511. (setq org-cycle-global-status 'contents))
  4512. (org-cycle '(4)))))
  4513. (defun org-overview ()
  4514. "Switch to overview mode, shoing only top-level headlines.
  4515. Really, this shows all headlines with level equal or greater than the level
  4516. of the first headline in the buffer. This is important, because if the
  4517. first headline is not level one, then (hide-sublevels 1) gives confusing
  4518. results."
  4519. (interactive)
  4520. (let ((level (save-excursion
  4521. (goto-char (point-min))
  4522. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4523. (progn
  4524. (goto-char (match-beginning 0))
  4525. (funcall outline-level))))))
  4526. (and level (hide-sublevels level))))
  4527. (defun org-content (&optional arg)
  4528. "Show all headlines in the buffer, like a table of contents.
  4529. With numerical argument N, show content up to level N."
  4530. (interactive "P")
  4531. (save-excursion
  4532. ;; Visit all headings and show their offspring
  4533. (and (integerp arg) (org-overview))
  4534. (goto-char (point-max))
  4535. (catch 'exit
  4536. (while (and (progn (condition-case nil
  4537. (outline-previous-visible-heading 1)
  4538. (error (goto-char (point-min))))
  4539. t)
  4540. (looking-at outline-regexp))
  4541. (if (integerp arg)
  4542. (show-children (1- arg))
  4543. (show-branches))
  4544. (if (bobp) (throw 'exit nil))))))
  4545. (defun org-optimize-window-after-visibility-change (state)
  4546. "Adjust the window after a change in outline visibility.
  4547. This function is the default value of the hook `org-cycle-hook'."
  4548. (when (get-buffer-window (current-buffer))
  4549. (cond
  4550. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4551. ; ((eq state 'overview) (org-beginning-of-line))
  4552. ((eq state 'content) nil)
  4553. ((eq state 'all) nil)
  4554. ((eq state 'folded) nil)
  4555. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4556. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4557. (defun org-cycle-show-empty-lines (state)
  4558. "Show empty lines above all visible headlines.
  4559. The region to be covered depends on STATE when called through
  4560. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4561. entire buffer covered. Note that an empty line is only shown if there
  4562. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4563. (when (> org-cycle-separator-lines 0)
  4564. (save-excursion
  4565. (let* ((n org-cycle-separator-lines)
  4566. (re (cond
  4567. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4568. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4569. (t (let ((ns (number-to-string (- n 2))))
  4570. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4571. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4572. beg end)
  4573. (cond
  4574. ((memq state '(overview contents t))
  4575. (setq beg (point-min) end (point-max)))
  4576. ((memq state '(children folded))
  4577. (setq beg (point) end (progn (org-end-of-subtree t t)
  4578. (beginning-of-line 2)
  4579. (point)))))
  4580. (when beg
  4581. (goto-char beg)
  4582. (while (re-search-forward re end t)
  4583. (if (not (get-char-property (match-end 1) 'invisible))
  4584. (outline-flag-region
  4585. (match-beginning 1) (match-end 1) nil)))))))
  4586. ;; Never hide empty lines at the end of the file.
  4587. (save-excursion
  4588. (goto-char (point-max))
  4589. (outline-previous-heading)
  4590. (outline-end-of-heading)
  4591. (if (and (looking-at "[ \t\n]+")
  4592. (= (match-end 0) (point-max)))
  4593. (outline-flag-region (point) (match-end 0) nil))))
  4594. (defun org-subtree-end-visible-p ()
  4595. "Is the end of the current subtree visible?"
  4596. (pos-visible-in-window-p
  4597. (save-excursion (org-end-of-subtree t) (point))))
  4598. (defun org-first-headline-recenter (&optional N)
  4599. "Move cursor to the first headline and recenter the headline.
  4600. Optional argument N means, put the headline into the Nth line of the window."
  4601. (goto-char (point-min))
  4602. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4603. (beginning-of-line)
  4604. (recenter (prefix-numeric-value N))))
  4605. ;;; Org-goto
  4606. (defvar org-goto-window-configuration nil)
  4607. (defvar org-goto-marker nil)
  4608. (defvar org-goto-map
  4609. (let ((map (make-sparse-keymap)))
  4610. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4611. (while (setq cmd (pop cmds))
  4612. (substitute-key-definition cmd cmd map global-map)))
  4613. (org-defkey map "\C-m" 'org-goto-ret)
  4614. (org-defkey map [(left)] 'org-goto-left)
  4615. (org-defkey map [(right)] 'org-goto-right)
  4616. (org-defkey map [(?q)] 'org-goto-quit)
  4617. (org-defkey map [(control ?g)] 'org-goto-quit)
  4618. (org-defkey map "\C-i" 'org-cycle)
  4619. (org-defkey map [(tab)] 'org-cycle)
  4620. (org-defkey map [(down)] 'outline-next-visible-heading)
  4621. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4622. (org-defkey map "n" 'outline-next-visible-heading)
  4623. (org-defkey map "p" 'outline-previous-visible-heading)
  4624. (org-defkey map "f" 'outline-forward-same-level)
  4625. (org-defkey map "b" 'outline-backward-same-level)
  4626. (org-defkey map "u" 'outline-up-heading)
  4627. (org-defkey map "/" 'org-occur)
  4628. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4629. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4630. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4631. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4632. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4633. ;; FIXME: Could we use suppress-keymap?
  4634. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4635. (while l (org-defkey map (int-to-string (pop l)) 'digit-argument)))
  4636. map))
  4637. (defconst org-goto-help
  4638. "Browse copy of buffer to find location or copy text.
  4639. RET=jump to location [Q]uit and return to previous location
  4640. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  4641. )
  4642. (defun org-goto ()
  4643. "Look up a different location in the current file, keeping current visibility.
  4644. When you want look-up or go to a different location in a document, the
  4645. fastest way is often to fold the entire buffer and then dive into the tree.
  4646. This method has the disadvantage, that the previous location will be folded,
  4647. which may not be what you want.
  4648. This command works around this by showing a copy of the current buffer
  4649. in an indirect buffer, in overview mode. You can dive into the tree in
  4650. that copy, use org-occur and incremental search to find a location.
  4651. When pressing RET or `Q', the command returns to the original buffer in
  4652. which the visibility is still unchanged. After RET is will also jump to
  4653. the location selected in the indirect buffer and expose the
  4654. the headline hierarchy above."
  4655. (interactive)
  4656. (let* ((org-goto-start-pos (point))
  4657. (selected-point
  4658. (car (org-get-location (current-buffer) org-goto-help))))
  4659. (if selected-point
  4660. (progn
  4661. (org-mark-ring-push org-goto-start-pos)
  4662. (goto-char selected-point)
  4663. (if (or (org-invisible-p) (org-invisible-p2))
  4664. (org-show-context 'org-goto)))
  4665. (message "Quit"))))
  4666. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4667. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4668. (defun org-get-location (buf help)
  4669. "Let the user select a location in the Org-mode buffer BUF.
  4670. This function uses a recursive edit. It returns the selected position
  4671. or nil."
  4672. (let (org-goto-selected-point org-goto-exit-command)
  4673. (save-excursion
  4674. (save-window-excursion
  4675. (delete-other-windows)
  4676. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4677. (switch-to-buffer
  4678. (condition-case nil
  4679. (make-indirect-buffer (current-buffer) "*org-goto*")
  4680. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4681. (with-output-to-temp-buffer "*Help*"
  4682. (princ help))
  4683. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4684. (setq buffer-read-only nil)
  4685. (let ((org-startup-truncated t)
  4686. (org-startup-folded nil)
  4687. (org-startup-align-all-tables nil))
  4688. (org-mode)
  4689. (org-overview))
  4690. (setq buffer-read-only t)
  4691. (if (and (boundp 'org-goto-start-pos)
  4692. (integer-or-marker-p org-goto-start-pos))
  4693. (let ((org-show-hierarchy-above t)
  4694. (org-show-siblings t)
  4695. (org-show-following-heading t))
  4696. (goto-char org-goto-start-pos)
  4697. (and (org-invisible-p) (org-show-context)))
  4698. (goto-char (point-min)))
  4699. (org-beginning-of-line)
  4700. (message "Select location and press RET")
  4701. ;; now we make sure that during selection, ony very few keys work
  4702. ;; and that it is impossible to switch to another window.
  4703. ; (let ((gm (current-global-map))
  4704. ; (overriding-local-map org-goto-map))
  4705. ; (unwind-protect
  4706. ; (progn
  4707. ; (use-global-map org-goto-map)
  4708. ; (recursive-edit))
  4709. ; (use-global-map gm)))
  4710. (use-local-map org-goto-map)
  4711. (recursive-edit)
  4712. ))
  4713. (kill-buffer "*org-goto*")
  4714. (cons org-goto-selected-point org-goto-exit-command)))
  4715. (defun org-goto-ret (&optional arg)
  4716. "Finish `org-goto' by going to the new location."
  4717. (interactive "P")
  4718. (setq org-goto-selected-point (point)
  4719. org-goto-exit-command 'return)
  4720. (throw 'exit nil))
  4721. (defun org-goto-left ()
  4722. "Finish `org-goto' by going to the new location."
  4723. (interactive)
  4724. (if (org-on-heading-p)
  4725. (progn
  4726. (beginning-of-line 1)
  4727. (setq org-goto-selected-point (point)
  4728. org-goto-exit-command 'left)
  4729. (throw 'exit nil))
  4730. (error "Not on a heading")))
  4731. (defun org-goto-right ()
  4732. "Finish `org-goto' by going to the new location."
  4733. (interactive)
  4734. (if (org-on-heading-p)
  4735. (progn
  4736. (setq org-goto-selected-point (point)
  4737. org-goto-exit-command 'right)
  4738. (throw 'exit nil))
  4739. (error "Not on a heading")))
  4740. (defun org-goto-quit ()
  4741. "Finish `org-goto' without cursor motion."
  4742. (interactive)
  4743. (setq org-goto-selected-point nil)
  4744. (setq org-goto-exit-command 'quit)
  4745. (throw 'exit nil))
  4746. ;;; Indirect buffer display of subtrees
  4747. (defvar org-indirect-dedicated-frame nil
  4748. "This is the frame being used for indirect tree display.")
  4749. (defvar org-last-indirect-buffer nil)
  4750. (defun org-tree-to-indirect-buffer (&optional arg)
  4751. "Create indirect buffer and narrow it to current subtree.
  4752. With numerical prefix ARG, go up to this level and then take that tree.
  4753. If ARG is negative, go up that many levels.
  4754. Normally this command removes the indirect buffer previously made
  4755. with this command. However, when called with a C-u prefix, the last buffer
  4756. is kept so that you can work with several indirect buffers at the same time.
  4757. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4758. requests that a new frame be made for the new buffer, so that the dedicated
  4759. frame is not changed."
  4760. (interactive "P")
  4761. (let ((cbuf (current-buffer))
  4762. (cwin (selected-window))
  4763. (pos (point))
  4764. beg end level heading ibuf)
  4765. (save-excursion
  4766. (org-back-to-heading t)
  4767. (when (numberp arg)
  4768. (setq level (org-outline-level))
  4769. (if (< arg 0) (setq arg (+ level arg)))
  4770. (while (> (setq level (org-outline-level)) arg)
  4771. (outline-up-heading 1 t)))
  4772. (setq beg (point)
  4773. heading (org-get-heading))
  4774. (org-end-of-subtree t) (setq end (point)))
  4775. (if (and (not arg)
  4776. (buffer-live-p org-last-indirect-buffer))
  4777. (kill-buffer org-last-indirect-buffer))
  4778. (setq ibuf (org-get-indirect-buffer cbuf)
  4779. org-last-indirect-buffer ibuf)
  4780. (cond
  4781. ((or (eq org-indirect-buffer-display 'new-frame)
  4782. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4783. (select-frame (make-frame))
  4784. (delete-other-windows)
  4785. (switch-to-buffer ibuf)
  4786. (org-set-frame-title heading))
  4787. ((eq org-indirect-buffer-display 'dedicated-frame)
  4788. (raise-frame
  4789. (select-frame (or (and org-indirect-dedicated-frame
  4790. (frame-live-p org-indirect-dedicated-frame)
  4791. org-indirect-dedicated-frame)
  4792. (setq org-indirect-dedicated-frame (make-frame)))))
  4793. (delete-other-windows)
  4794. (switch-to-buffer ibuf)
  4795. (org-set-frame-title (concat "Indirect: " heading)))
  4796. ((eq org-indirect-buffer-display 'current-window)
  4797. (switch-to-buffer ibuf))
  4798. ((eq org-indirect-buffer-display 'other-window)
  4799. (pop-to-buffer ibuf))
  4800. (t (error "Invalid value.")))
  4801. (if (featurep 'xemacs)
  4802. (save-excursion (org-mode) (turn-on-font-lock)))
  4803. (narrow-to-region beg end)
  4804. (show-all)
  4805. (goto-char pos)
  4806. (and (window-live-p cwin) (select-window cwin))))
  4807. (defun org-get-indirect-buffer (&optional buffer)
  4808. (setq buffer (or buffer (current-buffer)))
  4809. (let ((n 1) (base (buffer-name buffer)) bname)
  4810. (while (buffer-live-p
  4811. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4812. (setq n (1+ n)))
  4813. (condition-case nil
  4814. (make-indirect-buffer buffer bname 'clone)
  4815. (error (make-indirect-buffer buffer bname)))))
  4816. (defun org-set-frame-title (title)
  4817. "Set the title of the current frame to the string TITLE."
  4818. ;; FIXME: how to name a single frame in XEmacs???
  4819. (unless (featurep 'xemacs)
  4820. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4821. ;;;; Structure editing
  4822. ;;; Inserting headlines
  4823. (defun org-insert-heading (&optional force-heading)
  4824. "Insert a new heading or item with same depth at point.
  4825. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4826. If point is at the beginning of a headline, insert a sibling before the
  4827. current headline. If point is in the middle of a headline, split the headline
  4828. at that position and make the rest of the headline part of the sibling below
  4829. the current headline."
  4830. (interactive "P")
  4831. (if (= (buffer-size) 0)
  4832. (insert "\n* ")
  4833. (when (or force-heading (not (org-insert-item)))
  4834. (let* ((head (save-excursion
  4835. (condition-case nil
  4836. (progn
  4837. (org-back-to-heading)
  4838. (match-string 0))
  4839. (error "*"))))
  4840. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4841. pos)
  4842. (cond
  4843. ((and (org-on-heading-p) (bolp)
  4844. (or (bobp)
  4845. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4846. (open-line (if blank 2 1)))
  4847. ((and (bolp)
  4848. (or (bobp)
  4849. (save-excursion
  4850. (backward-char 1) (not (org-invisible-p)))))
  4851. nil)
  4852. (t (newline (if blank 2 1))))
  4853. (insert head) (just-one-space)
  4854. (setq pos (point))
  4855. (end-of-line 1)
  4856. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4857. (run-hooks 'org-insert-heading-hook)))))
  4858. (defun org-insert-heading-after-current ()
  4859. "Insert a new heading with same level as current, after current subtree."
  4860. (interactive)
  4861. (org-back-to-heading)
  4862. (org-insert-heading)
  4863. (org-move-subtree-down)
  4864. (end-of-line 1))
  4865. (defun org-insert-todo-heading (arg)
  4866. "Insert a new heading with the same level and TODO state as current heading.
  4867. If the heading has no TODO state, or if the state is DONE, use the first
  4868. state (TODO by default). Also with prefix arg, force first state."
  4869. (interactive "P")
  4870. (when (not (org-insert-item 'checkbox))
  4871. (org-insert-heading)
  4872. (save-excursion
  4873. (org-back-to-heading)
  4874. (outline-previous-heading)
  4875. (looking-at org-todo-line-regexp))
  4876. (if (or arg
  4877. (not (match-beginning 2))
  4878. (member (match-string 2) org-done-keywords))
  4879. (insert (car org-todo-keywords-1) " ")
  4880. (insert (match-string 2) " "))))
  4881. (defun org-insert-subheading (arg)
  4882. "Insert a new subheading and demote it.
  4883. Works for outline headings and for plain lists alike."
  4884. (interactive "P")
  4885. (org-insert-heading arg)
  4886. (cond
  4887. ((org-on-heading-p) (org-do-demote))
  4888. ((org-at-item-p) (org-indent-item 1))))
  4889. (defun org-insert-todo-subheading (arg)
  4890. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4891. Works for outline headings and for plain lists alike."
  4892. (interactive "P")
  4893. (org-insert-todo-heading arg)
  4894. (cond
  4895. ((org-on-heading-p) (org-do-demote))
  4896. ((org-at-item-p) (org-indent-item 1))))
  4897. ;;; Promotion and Demotion
  4898. (defun org-promote-subtree ()
  4899. "Promote the entire subtree.
  4900. See also `org-promote'."
  4901. (interactive)
  4902. (save-excursion
  4903. (org-map-tree 'org-promote))
  4904. (org-fix-position-after-promote))
  4905. (defun org-demote-subtree ()
  4906. "Demote the entire subtree. See `org-demote'.
  4907. See also `org-promote'."
  4908. (interactive)
  4909. (save-excursion
  4910. (org-map-tree 'org-demote))
  4911. (org-fix-position-after-promote))
  4912. (defun org-do-promote ()
  4913. "Promote the current heading higher up the tree.
  4914. If the region is active in `transient-mark-mode', promote all headings
  4915. in the region."
  4916. (interactive)
  4917. (save-excursion
  4918. (if (org-region-active-p)
  4919. (org-map-region 'org-promote (region-beginning) (region-end))
  4920. (org-promote)))
  4921. (org-fix-position-after-promote))
  4922. (defun org-do-demote ()
  4923. "Demote the current heading lower down the tree.
  4924. If the region is active in `transient-mark-mode', demote all headings
  4925. in the region."
  4926. (interactive)
  4927. (save-excursion
  4928. (if (org-region-active-p)
  4929. (org-map-region 'org-demote (region-beginning) (region-end))
  4930. (org-demote)))
  4931. (org-fix-position-after-promote))
  4932. (defun org-fix-position-after-promote ()
  4933. "Make sure that after pro/demotion cursor position is right."
  4934. (let ((pos (point)))
  4935. (when (save-excursion
  4936. (beginning-of-line 1)
  4937. (looking-at org-todo-line-regexp)
  4938. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4939. (cond ((eobp) (insert " "))
  4940. ((eolp) (insert " "))
  4941. ((equal (char-after) ?\ ) (forward-char 1))))))
  4942. (defun org-reduced-level (l)
  4943. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4944. (defun org-get-legal-level (level &optional change)
  4945. "Rectify a level change under the influence of `org-odd-levels-only'
  4946. LEVEL is a current level, CHANGE is by how much the level should be
  4947. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4948. even level numbers will become the next higher odd number."
  4949. (if org-odd-levels-only
  4950. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4951. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4952. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4953. (max 1 (+ level change))))
  4954. (defun org-promote ()
  4955. "Promote the current heading higher up the tree.
  4956. If the region is active in `transient-mark-mode', promote all headings
  4957. in the region."
  4958. (org-back-to-heading t)
  4959. (let* ((level (save-match-data (funcall outline-level)))
  4960. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  4961. (diff (abs (- level (length up-head) -1))))
  4962. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4963. (replace-match up-head nil t)
  4964. ;; Fixup tag positioning
  4965. (and org-auto-align-tags (org-set-tags nil t))
  4966. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4967. (defun org-demote ()
  4968. "Demote the current heading lower down the tree.
  4969. If the region is active in `transient-mark-mode', demote all headings
  4970. in the region."
  4971. (org-back-to-heading t)
  4972. (let* ((level (save-match-data (funcall outline-level)))
  4973. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  4974. (diff (abs (- level (length down-head) -1))))
  4975. (replace-match down-head nil t)
  4976. ;; Fixup tag positioning
  4977. (and org-auto-align-tags (org-set-tags nil t))
  4978. (if org-adapt-indentation (org-fixup-indentation diff))))
  4979. (defun org-map-tree (fun)
  4980. "Call FUN for every heading underneath the current one."
  4981. (org-back-to-heading)
  4982. (let ((level (funcall outline-level)))
  4983. (save-excursion
  4984. (funcall fun)
  4985. (while (and (progn
  4986. (outline-next-heading)
  4987. (> (funcall outline-level) level))
  4988. (not (eobp)))
  4989. (funcall fun)))))
  4990. (defun org-map-region (fun beg end)
  4991. "Call FUN for every heading between BEG and END."
  4992. (let ((org-ignore-region t))
  4993. (save-excursion
  4994. (setq end (copy-marker end))
  4995. (goto-char beg)
  4996. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4997. (< (point) end))
  4998. (funcall fun))
  4999. (while (and (progn
  5000. (outline-next-heading)
  5001. (< (point) end))
  5002. (not (eobp)))
  5003. (funcall fun)))))
  5004. (defun org-fixup-indentation (diff)
  5005. "Change the indentation in the current entry by DIFF
  5006. However, if any line in the current entry has no indentation, or if it
  5007. would end up with no indentation after the change, nothing at all is done."
  5008. (save-excursion
  5009. (let ((end (save-excursion (outline-next-heading)
  5010. (point-marker)))
  5011. (prohibit (if (> diff 0)
  5012. "^\\S-"
  5013. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5014. col)
  5015. (unless (save-excursion (end-of-line 1)
  5016. (re-search-forward prohibit end t))
  5017. (while (re-search-forward "^[ \t]+" end t)
  5018. (goto-char (match-end 0))
  5019. (setq col (current-column))
  5020. (if (< diff 0) (replace-match ""))
  5021. (indent-to (+ diff col))))
  5022. (move-marker end nil))))
  5023. (defun org-convert-to-odd-levels ()
  5024. "Convert an org-mode file with all levels allowed to one with odd levels.
  5025. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5026. level 5 etc."
  5027. (interactive)
  5028. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5029. (let ((org-odd-levels-only nil) n)
  5030. (save-excursion
  5031. (goto-char (point-min))
  5032. (while (re-search-forward "^\\*\\*+ " nil t)
  5033. (setq n (- (length (match-string 0)) 2))
  5034. (while (>= (setq n (1- n)) 0)
  5035. (org-demote))
  5036. (end-of-line 1))))))
  5037. (defun org-convert-to-oddeven-levels ()
  5038. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5039. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5040. section with an even level, conversion would destroy the structure of the file. An error
  5041. is signaled in this case."
  5042. (interactive)
  5043. (goto-char (point-min))
  5044. ;; First check if there are no even levels
  5045. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5046. (org-show-context t)
  5047. (error "Not all levels are odd in this file. Conversion not possible."))
  5048. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5049. (let ((org-odd-levels-only nil) n)
  5050. (save-excursion
  5051. (goto-char (point-min))
  5052. (while (re-search-forward "^\\*\\*+ " nil t)
  5053. (setq n (/ (length (1- (match-string 0))) 2))
  5054. (while (>= (setq n (1- n)) 0)
  5055. (org-promote))
  5056. (end-of-line 1))))))
  5057. (defun org-tr-level (n)
  5058. "Make N odd if required."
  5059. (if org-odd-levels-only (1+ (/ n 2)) n))
  5060. ;;; Vertical tree motion, cutting and pasting of subtrees
  5061. (defun org-move-subtree-up (&optional arg)
  5062. "Move the current subtree up past ARG headlines of the same level."
  5063. (interactive "p")
  5064. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5065. (defun org-move-subtree-down (&optional arg)
  5066. "Move the current subtree down past ARG headlines of the same level."
  5067. (interactive "p")
  5068. (setq arg (prefix-numeric-value arg))
  5069. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5070. 'outline-get-last-sibling))
  5071. (ins-point (make-marker))
  5072. (cnt (abs arg))
  5073. beg end txt folded)
  5074. ;; Select the tree
  5075. (org-back-to-heading)
  5076. (setq beg (point))
  5077. (save-match-data
  5078. (save-excursion (outline-end-of-heading)
  5079. (setq folded (org-invisible-p)))
  5080. (outline-end-of-subtree))
  5081. (outline-next-heading)
  5082. (setq end (point))
  5083. ;; Find insertion point, with error handling
  5084. (goto-char beg)
  5085. (while (> cnt 0)
  5086. (or (and (funcall movfunc) (looking-at outline-regexp))
  5087. (progn (goto-char beg)
  5088. (error "Cannot move past superior level or buffer limit")))
  5089. (setq cnt (1- cnt)))
  5090. (if (> arg 0)
  5091. ;; Moving forward - still need to move over subtree
  5092. (progn (outline-end-of-subtree)
  5093. (outline-next-heading)
  5094. (if (not (or (looking-at (concat "^" outline-regexp))
  5095. (bolp)))
  5096. (newline))))
  5097. (move-marker ins-point (point))
  5098. (setq txt (buffer-substring beg end))
  5099. (delete-region beg end)
  5100. (insert txt)
  5101. (or (bolp) (insert "\n"))
  5102. (goto-char ins-point)
  5103. (if folded (hide-subtree))
  5104. (move-marker ins-point nil)))
  5105. (defvar org-subtree-clip ""
  5106. "Clipboard for cut and paste of subtrees.
  5107. This is actually only a copy of the kill, because we use the normal kill
  5108. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5109. (defvar org-subtree-clip-folded nil
  5110. "Was the last copied subtree folded?
  5111. This is used to fold the tree back after pasting.")
  5112. (defun org-cut-subtree ()
  5113. "Cut the current subtree into the clipboard.
  5114. This is a short-hand for marking the subtree and then cutting it."
  5115. (interactive)
  5116. (org-copy-subtree 'cut))
  5117. (defun org-copy-subtree (&optional cut)
  5118. "Cut the current subtree into the clipboard.
  5119. This is a short-hand for marking the subtree and then copying it.
  5120. If CUT is non-nil, actually cut the subtree."
  5121. (interactive)
  5122. (let (beg end folded)
  5123. (if (interactive-p)
  5124. (org-back-to-heading nil) ; take what looks like a subtree
  5125. (org-back-to-heading t)) ; take what is really there
  5126. (setq beg (point))
  5127. (save-match-data
  5128. (save-excursion (outline-end-of-heading)
  5129. (setq folded (org-invisible-p)))
  5130. (outline-end-of-subtree))
  5131. (if (equal (char-after) ?\n) (forward-char 1))
  5132. (setq end (point))
  5133. (goto-char beg)
  5134. (when (> end beg)
  5135. (setq org-subtree-clip-folded folded)
  5136. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5137. (setq org-subtree-clip (current-kill 0))
  5138. (message "%s: Subtree with %d characters"
  5139. (if cut "Cut" "Copied")
  5140. (length org-subtree-clip)))))
  5141. (defun org-paste-subtree (&optional level tree)
  5142. "Paste the clipboard as a subtree, with modification of headline level.
  5143. The entire subtree is promoted or demoted in order to match a new headline
  5144. level. By default, the new level is derived from the visible headings
  5145. before and after the insertion point, and taken to be the inferior headline
  5146. level of the two. So if the previous visible heading is level 3 and the
  5147. next is level 4 (or vice versa), level 4 will be used for insertion.
  5148. This makes sure that the subtree remains an independent subtree and does
  5149. not swallow low level entries.
  5150. You can also force a different level, either by using a numeric prefix
  5151. argument, or by inserting the heading marker by hand. For example, if the
  5152. cursor is after \"*****\", then the tree will be shifted to level 5.
  5153. If you want to insert the tree as is, just use \\[yank].
  5154. If optional TREE is given, use this text instead of the kill ring."
  5155. (interactive "P")
  5156. (unless (org-kill-is-subtree-p tree)
  5157. (error
  5158. (substitute-command-keys
  5159. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5160. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5161. (^re (concat "^\\(" outline-regexp "\\)"))
  5162. (re (concat "\\(" outline-regexp "\\)"))
  5163. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  5164. (old-level (if (string-match ^re txt)
  5165. (- (match-end 0) (match-beginning 0) 1)
  5166. -1))
  5167. (force-level (cond (level (prefix-numeric-value level))
  5168. ((string-match
  5169. ^re_ (buffer-substring (point-at-bol) (point)))
  5170. (- (match-end 0) (match-beginning 0)))
  5171. (t nil)))
  5172. (previous-level (save-excursion
  5173. (condition-case nil
  5174. (progn
  5175. (outline-previous-visible-heading 1)
  5176. (if (looking-at re)
  5177. (- (match-end 0) (match-beginning 0))
  5178. 1))
  5179. (error 1))))
  5180. (next-level (save-excursion
  5181. (condition-case nil
  5182. (progn
  5183. (outline-next-visible-heading 1)
  5184. (if (looking-at re)
  5185. (- (match-end 0) (match-beginning 0))
  5186. 1))
  5187. (error 1))))
  5188. (new-level (or force-level (max previous-level next-level)))
  5189. (shift (if (or (= old-level -1)
  5190. (= new-level -1)
  5191. (= old-level new-level))
  5192. 0
  5193. (- new-level old-level)))
  5194. (shift1 shift)
  5195. (delta (if (> shift 0) -1 1))
  5196. (func (if (> shift 0) 'org-demote 'org-promote))
  5197. (org-odd-levels-only nil)
  5198. beg end)
  5199. ;; Remove the forced level indicator
  5200. (if force-level
  5201. (delete-region (point-at-bol) (point)))
  5202. ;; Paste
  5203. (beginning-of-line 1)
  5204. (setq beg (point))
  5205. (insert txt)
  5206. (unless (string-match "\n[ \t]*\\'" txt) (insert "\n"))
  5207. (setq end (point))
  5208. (goto-char beg)
  5209. ;; Shift if necessary
  5210. (unless (= shift 0)
  5211. (save-restriction
  5212. (narrow-to-region beg end)
  5213. (while (not (= shift 0))
  5214. (org-map-region func (point-min) (point-max))
  5215. (setq shift (+ delta shift)))
  5216. (goto-char (point-min))))
  5217. (when (interactive-p)
  5218. (message "Clipboard pasted as level %d subtree" new-level))
  5219. (if (and kill-ring
  5220. (eq org-subtree-clip (current-kill 0))
  5221. org-subtree-clip-folded)
  5222. ;; The tree was folded before it was killed/copied
  5223. (hide-subtree))))
  5224. (defun org-kill-is-subtree-p (&optional txt)
  5225. "Check if the current kill is an outline subtree, or a set of trees.
  5226. Returns nil if kill does not start with a headline, or if the first
  5227. headline level is not the largest headline level in the tree.
  5228. So this will actually accept several entries of equal levels as well,
  5229. which is OK for `org-paste-subtree'.
  5230. If optional TXT is given, check this string instead of the current kill."
  5231. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5232. (start-level (and kill
  5233. (string-match (concat "\\`" org-outline-regexp) kill)
  5234. (- (match-end 0) (match-beginning 0) 1)))
  5235. (re (concat "^" org-outline-regexp))
  5236. (start 1))
  5237. (if (not start-level)
  5238. (progn
  5239. nil) ;; does not even start with a heading
  5240. (catch 'exit
  5241. (while (setq start (string-match re kill (1+ start)))
  5242. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5243. (throw 'exit nil)))
  5244. t))))
  5245. (defun org-narrow-to-subtree ()
  5246. "Narrow buffer to the current subtree."
  5247. (interactive)
  5248. (save-excursion
  5249. (narrow-to-region
  5250. (progn (org-back-to-heading) (point))
  5251. (progn (org-end-of-subtree t t) (point)))))
  5252. ;;; Outline Sorting
  5253. (defun org-sort (with-case)
  5254. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  5255. (interactive "P")
  5256. (if (org-at-table-p)
  5257. (org-call-with-arg 'org-table-sort-lines with-case)
  5258. (org-call-with-arg 'org-sort-entries with-case)))
  5259. (defun org-sort-entries (&optional with-case sorting-type)
  5260. "Sort entries on a certain level of an outline tree.
  5261. If there is an active region, the entries in the region are sorted.
  5262. Else, if the cursor is before the first entry, sort the top-level items.
  5263. Else, the children of the entry at point are sorted.
  5264. Sorting can be alphabetically, numerically, and by date/time as given by
  5265. the first time stamp in the entry. The command prompts for the sorting
  5266. type unless it has been given to the function through the SORTING-TYPE
  5267. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  5268. Comparing entries ignores case by default. However, with an optional argument
  5269. WITH-CASE, the sorting considers case as well. With two prefix arguments
  5270. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  5271. (interactive "P")
  5272. (let ((unique (equal with-case '(16)))
  5273. start beg end entries stars re re2 p nentries (nremoved 0)
  5274. last txt what)
  5275. ;; Find beginning and end of region to sort
  5276. (cond
  5277. ((org-region-active-p)
  5278. ;; we will sort the region
  5279. (setq end (region-end)
  5280. what "region")
  5281. (goto-char (region-beginning))
  5282. (if (not (org-on-heading-p)) (outline-next-heading))
  5283. (setq start (point)))
  5284. ((or (org-on-heading-p)
  5285. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5286. ;; we will sort the children of the current headline
  5287. (org-back-to-heading)
  5288. (setq start (point) end (org-end-of-subtree) what "children")
  5289. (goto-char start)
  5290. (show-subtree)
  5291. (outline-next-heading))
  5292. (t
  5293. ;; we will sort the top-level entries in this file
  5294. (goto-char (point-min))
  5295. (or (org-on-heading-p) (outline-next-heading))
  5296. (setq start (point) end (point-max) what "top-level")
  5297. (goto-char start)
  5298. (show-all)))
  5299. (setq beg (point))
  5300. (if (>= (point) end) (error "Nothing to sort"))
  5301. (looking-at "\\(\\*+\\)")
  5302. (setq stars (match-string 1)
  5303. re (concat "^" (regexp-quote stars) " +")
  5304. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5305. txt (buffer-substring beg end))
  5306. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5307. (if (and (not (equal stars "*")) (string-match re2 txt))
  5308. (error "Region to sort contains a level above the first entry"))
  5309. ;; Make a list that can be sorted.
  5310. ;; The car is the string for comparison, the cdr is the subtree
  5311. (message "Sorting entries...")
  5312. (setq entries
  5313. (mapcar
  5314. (lambda (x)
  5315. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  5316. (cons (match-string 0 x) x))
  5317. (org-split-string txt re)))
  5318. ;; Sort the list
  5319. (save-excursion
  5320. (goto-char start)
  5321. (setq entries (org-do-sort entries what with-case sorting-type)))
  5322. ;; Delete the old stuff
  5323. (goto-char beg)
  5324. (kill-region beg end)
  5325. (setq nentries (length entries))
  5326. ;; Insert the sorted entries, and remove duplicates if this is required
  5327. (while (setq p (pop entries))
  5328. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  5329. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  5330. (insert stars " " (cdr p))))
  5331. (goto-char start)
  5332. (message "Sorting entries...done (%d entries%s)"
  5333. nentries
  5334. (if unique (format ", %d duplicates removed" nremoved) ""))))
  5335. (defvar org-priority-regexp) ; defined later in the file
  5336. (defun org-do-sort (table what &optional with-case sorting-type)
  5337. "Sort TABLE of WHAT according to SORTING-TYPE.
  5338. The user will be prompted for the SORTING-TYPE if the call to this
  5339. function does not specify it. WHAT is only for the prompt, to indicate
  5340. what is being sorted. The sorting key will be extracted from
  5341. the car of the elements of the table.
  5342. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5343. (unless sorting-type
  5344. (message
  5345. "Sort %s: [a]lphabetic. [n]umeric. [t]ime [p]riority. A/N/T/P means reversed:"
  5346. what)
  5347. (setq sorting-type (read-char-exclusive)))
  5348. (let ((dcst (downcase sorting-type))
  5349. extractfun comparefun)
  5350. ;; Define the appropriate functions
  5351. (cond
  5352. ((= dcst ?n)
  5353. (setq extractfun 'string-to-number
  5354. comparefun (if (= dcst sorting-type) '< '>)))
  5355. ((= dcst ?a)
  5356. (setq extractfun (if with-case 'identity 'downcase)
  5357. comparefun (if (= dcst sorting-type)
  5358. 'string<
  5359. (lambda (a b) (and (not (string< a b))
  5360. (not (string= a b)))))))
  5361. ((= dcst ?t)
  5362. (setq extractfun
  5363. (lambda (x)
  5364. (if (string-match org-ts-regexp x)
  5365. (time-to-seconds
  5366. (org-time-string-to-time (match-string 0 x)))
  5367. 0))
  5368. comparefun (if (= dcst sorting-type) '< '>)))
  5369. ((= dcst ?p)
  5370. (setq extractfun
  5371. (lambda (x)
  5372. (if (string-match org-priority-regexp x)
  5373. (string-to-char (match-string 2 x))
  5374. org-default-priority))
  5375. comparefun (if (= dcst sorting-type) '< '>)))
  5376. (t (error "Invalid sorting type `%c'" sorting-type)))
  5377. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5378. table)
  5379. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5380. ;;;; Plain list items, including checkboxes
  5381. ;;; Plain list items
  5382. (defun org-at-item-p ()
  5383. "Is point in a line starting a hand-formatted item?"
  5384. (let ((llt org-plain-list-ordered-item-terminator))
  5385. (save-excursion
  5386. (goto-char (point-at-bol))
  5387. (looking-at
  5388. (cond
  5389. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5390. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5391. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5392. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5393. (defun org-in-item-p ()
  5394. "It the cursor inside a plain list item.
  5395. Does not have to be the first line."
  5396. (save-excursion
  5397. (condition-case nil
  5398. (progn
  5399. (org-beginning-of-item)
  5400. (org-at-item-p)
  5401. t)
  5402. (error nil))))
  5403. (defun org-insert-item (&optional checkbox)
  5404. "Insert a new item at the current level.
  5405. Return t when things worked, nil when we are not in an item."
  5406. (when (save-excursion
  5407. (condition-case nil
  5408. (progn
  5409. (org-beginning-of-item)
  5410. (org-at-item-p)
  5411. (if (org-invisible-p) (error "Invisible item"))
  5412. t)
  5413. (error nil)))
  5414. (let* ((bul (match-string 0))
  5415. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5416. (match-end 0)))
  5417. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5418. pos)
  5419. (cond
  5420. ((and (org-at-item-p) (<= (point) eow))
  5421. ;; before the bullet
  5422. (beginning-of-line 1)
  5423. (open-line (if blank 2 1)))
  5424. ((<= (point) eow)
  5425. (beginning-of-line 1))
  5426. (t (newline (if blank 2 1))))
  5427. (insert bul (if checkbox "[ ]" ""))
  5428. (just-one-space)
  5429. (setq pos (point))
  5430. (end-of-line 1)
  5431. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5432. (org-maybe-renumber-ordered-list)
  5433. (and checkbox (org-update-checkbox-count-maybe))
  5434. t))
  5435. ;;; Checkboxes
  5436. (defun org-at-item-checkbox-p ()
  5437. "Is point at a line starting a plain-list item with a checklet?"
  5438. (and (org-at-item-p)
  5439. (save-excursion
  5440. (goto-char (match-end 0))
  5441. (skip-chars-forward " \t")
  5442. (looking-at "\\[[- X]\\]"))))
  5443. (defun org-toggle-checkbox (&optional arg)
  5444. "Toggle the checkbox in the current line."
  5445. (interactive "P")
  5446. (catch 'exit
  5447. (let (beg end status (firstnew 'unknown))
  5448. (cond
  5449. ((org-region-active-p)
  5450. (setq beg (region-beginning) end (region-end)))
  5451. ((org-on-heading-p)
  5452. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5453. ((org-at-item-checkbox-p)
  5454. (save-excursion
  5455. (replace-match
  5456. (cond (arg "[-]")
  5457. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5458. (t "[ ]"))
  5459. t t))
  5460. (throw 'exit t))
  5461. (t (error "Not at a checkbox or heading, and no active region")))
  5462. (save-excursion
  5463. (goto-char beg)
  5464. (while (< (point) end)
  5465. (when (org-at-item-checkbox-p)
  5466. (setq status (equal (match-string 0) "[X]"))
  5467. (when (eq firstnew 'unknown)
  5468. (setq firstnew (not status)))
  5469. (replace-match
  5470. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5471. (beginning-of-line 2)))))
  5472. (org-update-checkbox-count-maybe))
  5473. (defun org-update-checkbox-count-maybe ()
  5474. "Update checkbox statistics unless turned off by user."
  5475. (when org-provide-checkbox-statistics
  5476. (org-update-checkbox-count)))
  5477. (defun org-update-checkbox-count (&optional all)
  5478. "Update the checkbox statistics in the current section.
  5479. This will find all statistic cookies like [57%] and [6/12] and update them
  5480. with the current numbers. With optional prefix argument ALL, do this for
  5481. the whole buffer."
  5482. (interactive "P")
  5483. (save-excursion
  5484. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5485. (beg (condition-case nil
  5486. (progn (outline-back-to-heading) (point))
  5487. (error (point-min))))
  5488. (end (move-marker (make-marker)
  5489. (progn (outline-next-heading) (point))))
  5490. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5491. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5492. b1 e1 f1 c-on c-off lim (cstat 0))
  5493. (when all
  5494. (goto-char (point-min))
  5495. (outline-next-heading)
  5496. (setq beg (point) end (point-max)))
  5497. (goto-char beg)
  5498. (while (re-search-forward re end t)
  5499. (setq cstat (1+ cstat)
  5500. b1 (match-beginning 0)
  5501. e1 (match-end 0)
  5502. f1 (match-beginning 1)
  5503. lim (cond
  5504. ((org-on-heading-p) (outline-next-heading) (point))
  5505. ((org-at-item-p) (org-end-of-item) (point))
  5506. (t nil))
  5507. c-on 0 c-off 0)
  5508. (goto-char e1)
  5509. (when lim
  5510. (while (re-search-forward re-box lim t)
  5511. (if (member (match-string 2) '("[ ]" "[-]"))
  5512. (setq c-off (1+ c-off))
  5513. (setq c-on (1+ c-on))))
  5514. ; (delete-region b1 e1)
  5515. (goto-char b1)
  5516. (insert (if f1
  5517. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5518. (format "[%d/%d]" c-on (+ c-on c-off))))
  5519. (and (looking-at "\\[.*?\\]")
  5520. (replace-match ""))))
  5521. (when (interactive-p)
  5522. (message "Checkbox satistics updated %s (%d places)"
  5523. (if all "in entire file" "in current outline entry") cstat)))))
  5524. (defun org-get-checkbox-statistics-face ()
  5525. "Select the face for checkbox statistics.
  5526. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5527. it will be `org-todo'."
  5528. (if (match-end 1)
  5529. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5530. (if (and (> (match-end 2) (match-beginning 2))
  5531. (equal (match-string 2) (match-string 3)))
  5532. 'org-done
  5533. 'org-todo)))
  5534. (defun org-get-indentation (&optional line)
  5535. "Get the indentation of the current line, interpreting tabs.
  5536. When LINE is given, assume it represents a line and compute its indentation."
  5537. (if line
  5538. (if (string-match "^ *" (org-remove-tabs line))
  5539. (match-end 0))
  5540. (save-excursion
  5541. (beginning-of-line 1)
  5542. (skip-chars-forward " \t")
  5543. (current-column))))
  5544. (defun org-remove-tabs (s &optional width)
  5545. "Replace tabulators in S with spaces.
  5546. Assumes that s is a single line, starting in column 0."
  5547. (setq width (or width tab-width))
  5548. (while (string-match "\t" s)
  5549. (setq s (replace-match
  5550. (make-string
  5551. (- (* width (/ (+ (match-beginning 0) width) width))
  5552. (match-beginning 0)) ?\ )
  5553. t t s)))
  5554. s)
  5555. (defun org-fix-indentation (line ind)
  5556. "Fix indentation in LINE.
  5557. IND is a cons cell with target and minimum indentation.
  5558. If the current indenation in LINE is smaller than the minimum,
  5559. leave it alone. If it is larger than ind, set it to the target."
  5560. (let* ((l (org-remove-tabs line))
  5561. (i (org-get-indentation l))
  5562. (i1 (car ind)) (i2 (cdr ind)))
  5563. (if (>= i i2) (setq l (substring line i2)))
  5564. (if (> i1 0)
  5565. (concat (make-string i1 ?\ ) l)
  5566. l)))
  5567. (defcustom org-empty-line-terminates-plain-lists nil
  5568. "Non-nil means, an empty line ends all plain list levels.
  5569. When nil, empty lines are part of the preceeding item."
  5570. :group 'org-plain-lists
  5571. :type 'boolean)
  5572. (defun org-beginning-of-item ()
  5573. "Go to the beginning of the current hand-formatted item.
  5574. If the cursor is not in an item, throw an error."
  5575. (interactive)
  5576. (let ((pos (point))
  5577. (limit (save-excursion
  5578. (condition-case nil
  5579. (progn
  5580. (org-back-to-heading)
  5581. (beginning-of-line 2) (point))
  5582. (error (point-min)))))
  5583. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5584. ind ind1)
  5585. (if (org-at-item-p)
  5586. (beginning-of-line 1)
  5587. (beginning-of-line 1)
  5588. (skip-chars-forward " \t")
  5589. (setq ind (current-column))
  5590. (if (catch 'exit
  5591. (while t
  5592. (beginning-of-line 0)
  5593. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5594. (if (looking-at "[ \t]*$")
  5595. (setq ind1 ind-empty)
  5596. (skip-chars-forward " \t")
  5597. (setq ind1 (current-column)))
  5598. (if (< ind1 ind)
  5599. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5600. nil
  5601. (goto-char pos)
  5602. (error "Not in an item")))))
  5603. (defun org-end-of-item ()
  5604. "Go to the end of the current hand-formatted item.
  5605. If the cursor is not in an item, throw an error."
  5606. (interactive)
  5607. (let* ((pos (point))
  5608. ind1
  5609. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5610. (limit (save-excursion (outline-next-heading) (point)))
  5611. (ind (save-excursion
  5612. (org-beginning-of-item)
  5613. (skip-chars-forward " \t")
  5614. (current-column)))
  5615. (end (catch 'exit
  5616. (while t
  5617. (beginning-of-line 2)
  5618. (if (eobp) (throw 'exit (point)))
  5619. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5620. (if (looking-at "[ \t]*$")
  5621. (setq ind1 ind-empty)
  5622. (skip-chars-forward " \t")
  5623. (setq ind1 (current-column)))
  5624. (if (<= ind1 ind)
  5625. (throw 'exit (point-at-bol)))))))
  5626. (if end
  5627. (goto-char end)
  5628. (goto-char pos)
  5629. (error "Not in an item"))))
  5630. (defun org-next-item ()
  5631. "Move to the beginning of the next item in the current plain list.
  5632. Error if not at a plain list, or if this is the last item in the list."
  5633. (interactive)
  5634. (let (ind ind1 (pos (point)))
  5635. (org-beginning-of-item)
  5636. (setq ind (org-get-indentation))
  5637. (org-end-of-item)
  5638. (setq ind1 (org-get-indentation))
  5639. (unless (and (org-at-item-p) (= ind ind1))
  5640. (goto-char pos)
  5641. (error "On last item"))))
  5642. (defun org-previous-item ()
  5643. "Move to the beginning of the previous item in the current plain list.
  5644. Error if not at a plain list, or if this is the first item in the list."
  5645. (interactive)
  5646. (let (beg ind ind1 (pos (point)))
  5647. (org-beginning-of-item)
  5648. (setq beg (point))
  5649. (setq ind (org-get-indentation))
  5650. (goto-char beg)
  5651. (catch 'exit
  5652. (while t
  5653. (beginning-of-line 0)
  5654. (if (looking-at "[ \t]*$")
  5655. nil
  5656. (if (<= (setq ind1 (org-get-indentation)) ind)
  5657. (throw 'exit t)))))
  5658. (condition-case nil
  5659. (if (or (not (org-at-item-p))
  5660. (< ind1 (1- ind)))
  5661. (error "")
  5662. (org-beginning-of-item))
  5663. (error (goto-char pos)
  5664. (error "On first item")))))
  5665. (defun org-move-item-down ()
  5666. "Move the plain list item at point down, i.e. swap with following item.
  5667. Subitems (items with larger indentation) are considered part of the item,
  5668. so this really moves item trees."
  5669. (interactive)
  5670. (let (beg end ind ind1 (pos (point)) txt)
  5671. (org-beginning-of-item)
  5672. (setq beg (point))
  5673. (setq ind (org-get-indentation))
  5674. (org-end-of-item)
  5675. (setq end (point))
  5676. (setq ind1 (org-get-indentation))
  5677. (if (and (org-at-item-p) (= ind ind1))
  5678. (progn
  5679. (org-end-of-item)
  5680. (setq txt (buffer-substring beg end))
  5681. (save-excursion
  5682. (delete-region beg end))
  5683. (setq pos (point))
  5684. (insert txt)
  5685. (goto-char pos)
  5686. (org-maybe-renumber-ordered-list))
  5687. (goto-char pos)
  5688. (error "Cannot move this item further down"))))
  5689. (defun org-move-item-up (arg)
  5690. "Move the plain list item at point up, i.e. swap with previous item.
  5691. Subitems (items with larger indentation) are considered part of the item,
  5692. so this really moves item trees."
  5693. (interactive "p")
  5694. (let (beg end ind ind1 (pos (point)) txt)
  5695. (org-beginning-of-item)
  5696. (setq beg (point))
  5697. (setq ind (org-get-indentation))
  5698. (org-end-of-item)
  5699. (setq end (point))
  5700. (goto-char beg)
  5701. (catch 'exit
  5702. (while t
  5703. (beginning-of-line 0)
  5704. (if (looking-at "[ \t]*$")
  5705. (if org-empty-line-terminates-plain-lists
  5706. (progn
  5707. (goto-char pos)
  5708. (error "Cannot move this item further up"))
  5709. nil)
  5710. (if (<= (setq ind1 (org-get-indentation)) ind)
  5711. (throw 'exit t)))))
  5712. (condition-case nil
  5713. (org-beginning-of-item)
  5714. (error (goto-char beg)
  5715. (error "Cannot move this item further up")))
  5716. (setq ind1 (org-get-indentation))
  5717. (if (and (org-at-item-p) (= ind ind1))
  5718. (progn
  5719. (setq txt (buffer-substring beg end))
  5720. (save-excursion
  5721. (delete-region beg end))
  5722. (setq pos (point))
  5723. (insert txt)
  5724. (goto-char pos)
  5725. (org-maybe-renumber-ordered-list))
  5726. (goto-char pos)
  5727. (error "Cannot move this item further up"))))
  5728. (defun org-maybe-renumber-ordered-list ()
  5729. "Renumber the ordered list at point if setup allows it.
  5730. This tests the user option `org-auto-renumber-ordered-lists' before
  5731. doing the renumbering."
  5732. (interactive)
  5733. (when (and org-auto-renumber-ordered-lists
  5734. (org-at-item-p))
  5735. (if (match-beginning 3)
  5736. (org-renumber-ordered-list 1)
  5737. (org-fix-bullet-type 1))))
  5738. (defun org-maybe-renumber-ordered-list-safe ()
  5739. (condition-case nil
  5740. (save-excursion
  5741. (org-maybe-renumber-ordered-list))
  5742. (error nil)))
  5743. (defun org-cycle-list-bullet (&optional which)
  5744. "Cycle through the different itemize/enumerate bullets.
  5745. This cycle the entire list level through the sequence:
  5746. `-' -> `+' -> `*' -> `1.' -> `1)'
  5747. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5748. 0 meand `-', 1 means `+' etc."
  5749. (interactive "P")
  5750. (org-preserve-lc
  5751. (org-beginning-of-item-list)
  5752. (org-at-item-p)
  5753. (beginning-of-line 1)
  5754. (let ((current (match-string 0)) new)
  5755. (setq new (cond
  5756. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5757. ((string-match "-" current) "+")
  5758. ((string-match "\\+" current)
  5759. (if (looking-at "\\S-") "1." "*"))
  5760. ((string-match "\\*" current) "1.")
  5761. ((string-match "\\." current) "1)")
  5762. ((string-match ")" current) "-")
  5763. (t (error "This should not happen"))))
  5764. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5765. (org-fix-bullet-type 1)
  5766. (org-maybe-renumber-ordered-list))))
  5767. (defun org-get-string-indentation (s)
  5768. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5769. (let ((n -1) (i 0) (w tab-width) c)
  5770. (catch 'exit
  5771. (while (< (setq n (1+ n)) (length s))
  5772. (setq c (aref s n))
  5773. (cond ((= c ?\ ) (setq i (1+ i)))
  5774. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5775. (t (throw 'exit t)))))
  5776. i))
  5777. (defun org-renumber-ordered-list (arg)
  5778. "Renumber an ordered plain list.
  5779. Cursor needs to be in the first line of an item, the line that starts
  5780. with something like \"1.\" or \"2)\"."
  5781. (interactive "p")
  5782. (unless (and (org-at-item-p)
  5783. (match-beginning 3))
  5784. (error "This is not an ordered list"))
  5785. (let ((line (org-current-line))
  5786. (col (current-column))
  5787. (ind (org-get-string-indentation
  5788. (buffer-substring (point-at-bol) (match-beginning 3))))
  5789. ;; (term (substring (match-string 3) -1))
  5790. ind1 (n (1- arg))
  5791. fmt)
  5792. ;; find where this list begins
  5793. (org-beginning-of-item-list)
  5794. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5795. (setq fmt (concat "%d" (match-string 1)))
  5796. (beginning-of-line 0)
  5797. ;; walk forward and replace these numbers
  5798. (catch 'exit
  5799. (while t
  5800. (catch 'next
  5801. (beginning-of-line 2)
  5802. (if (eobp) (throw 'exit nil))
  5803. (if (looking-at "[ \t]*$") (throw 'next nil))
  5804. (skip-chars-forward " \t") (setq ind1 (current-column))
  5805. (if (> ind1 ind) (throw 'next t))
  5806. (if (< ind1 ind) (throw 'exit t))
  5807. (if (not (org-at-item-p)) (throw 'exit nil))
  5808. (delete-region (match-beginning 2) (match-end 2))
  5809. (goto-char (match-beginning 2))
  5810. (insert (format fmt (setq n (1+ n)))))))
  5811. (goto-line line)
  5812. (move-to-column col)))
  5813. ;; FIXME: use ARG to select bullet type.
  5814. (defun org-fix-bullet-type (arg)
  5815. "Make sure all items in this list have the same bullet."
  5816. (interactive "p")
  5817. (unless (org-at-item-p) (error "This is not a list"))
  5818. (let ((line (org-current-line))
  5819. (col (current-column))
  5820. (ind (current-indentation))
  5821. ind1 bullet)
  5822. ;; find where this list begins
  5823. (org-beginning-of-item-list)
  5824. (beginning-of-line 1)
  5825. ;; find out what the bullet type is
  5826. (looking-at "[ \t]*\\(\\S-+\\)")
  5827. (setq bullet (match-string 1))
  5828. ;; walk forward and replace these numbers
  5829. (beginning-of-line 0)
  5830. (catch 'exit
  5831. (while t
  5832. (catch 'next
  5833. (beginning-of-line 2)
  5834. (if (eobp) (throw 'exit nil))
  5835. (if (looking-at "[ \t]*$") (throw 'next nil))
  5836. (skip-chars-forward " \t") (setq ind1 (current-column))
  5837. (if (> ind1 ind) (throw 'next t))
  5838. (if (< ind1 ind) (throw 'exit t))
  5839. (if (not (org-at-item-p)) (throw 'exit nil))
  5840. (skip-chars-forward " \t")
  5841. (looking-at "\\S-+")
  5842. (replace-match bullet))))
  5843. (goto-line line)
  5844. (move-to-column col)
  5845. (if (string-match "[0-9]" bullet)
  5846. (org-renumber-ordered-list 1))))
  5847. (defun org-beginning-of-item-list ()
  5848. "Go to the beginning of the current item list.
  5849. I.e. to the first item in this list."
  5850. (interactive)
  5851. (org-beginning-of-item)
  5852. (let ((pos (point-at-bol))
  5853. (ind (org-get-indentation))
  5854. ind1)
  5855. ;; find where this list begins
  5856. (catch 'exit
  5857. (while t
  5858. (catch 'next
  5859. (beginning-of-line 0)
  5860. (if (looking-at "[ \t]*$")
  5861. (throw (if (bobp) 'exit 'next) t))
  5862. (skip-chars-forward " \t") (setq ind1 (current-column))
  5863. (if (or (< ind1 ind)
  5864. (and (= ind1 ind)
  5865. (not (org-at-item-p)))
  5866. (bobp))
  5867. (throw 'exit t)
  5868. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5869. (goto-char pos)))
  5870. (defvar org-last-indent-begin-marker (make-marker))
  5871. (defvar org-last-indent-end-marker (make-marker))
  5872. (defun org-outdent-item (arg)
  5873. "Outdent a local list item."
  5874. (interactive "p")
  5875. (org-indent-item (- arg)))
  5876. (defun org-indent-item (arg)
  5877. "Indent a local list item."
  5878. (interactive "p")
  5879. (unless (org-at-item-p)
  5880. (error "Not on an item"))
  5881. (save-excursion
  5882. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5883. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5884. (setq beg org-last-indent-begin-marker
  5885. end org-last-indent-end-marker)
  5886. (org-beginning-of-item)
  5887. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5888. (org-end-of-item)
  5889. (setq end (move-marker org-last-indent-end-marker (point))))
  5890. (goto-char beg)
  5891. (setq tmp (org-item-indent-positions)
  5892. ind (car tmp)
  5893. ind-down (nth 2 tmp)
  5894. ind-up (nth 1 tmp)
  5895. delta (if (> arg 0)
  5896. (if ind-down (- ind-down ind) 2)
  5897. (if ind-up (- ind-up ind) -2)))
  5898. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5899. (while (< (point) end)
  5900. (beginning-of-line 1)
  5901. (skip-chars-forward " \t") (setq ind1 (current-column))
  5902. (delete-region (point-at-bol) (point))
  5903. (or (eolp) (indent-to-column (+ ind1 delta)))
  5904. (beginning-of-line 2))))
  5905. (org-fix-bullet-type 1)
  5906. (org-maybe-renumber-ordered-list-safe)
  5907. (save-excursion
  5908. (beginning-of-line 0)
  5909. (condition-case nil (org-beginning-of-item) (error nil))
  5910. (org-maybe-renumber-ordered-list-safe)))
  5911. (defun org-item-indent-positions ()
  5912. "Assumes cursor in item line. FIXME"
  5913. (let* ((bolpos (point-at-bol))
  5914. (ind (org-get-indentation))
  5915. ind-down ind-up pos)
  5916. (save-excursion
  5917. (org-beginning-of-item-list)
  5918. (skip-chars-backward "\n\r \t")
  5919. (when (org-in-item-p)
  5920. (org-beginning-of-item)
  5921. (setq ind-up (org-get-indentation))))
  5922. (setq pos (point))
  5923. (save-excursion
  5924. (cond
  5925. ((and (condition-case nil (progn (org-previous-item) t)
  5926. (error nil))
  5927. (or (forward-char 1) t)
  5928. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5929. (setq ind-down (org-get-indentation)))
  5930. ((and (goto-char pos)
  5931. (org-at-item-p))
  5932. (goto-char (match-end 0))
  5933. (skip-chars-forward " \t")
  5934. (setq ind-down (current-column)))))
  5935. (list ind ind-up ind-down)))
  5936. ;;; The orgstruct minor mode
  5937. ;; Define a minor mode which can be used in other modes in order to
  5938. ;; integrate the org-mode structure editing commands.
  5939. ;; This is really a hack, because the org-mode structure commands use
  5940. ;; keys which normally belong to the major mode. Here is how it
  5941. ;; works: The minor mode defines all the keys necessary to operate the
  5942. ;; structure commands, but wraps the commands into a function which
  5943. ;; tests if the cursor is currently at a headline or a plain list
  5944. ;; item. If that is the case, the structure command is used,
  5945. ;; temporarily setting many Org-mode variables like regular
  5946. ;; expressions for filling etc. However, when any of those keys is
  5947. ;; used at a different location, function uses `key-binding' to look
  5948. ;; up if the key has an associated command in another currently active
  5949. ;; keymap (minor modes, major mode, global), and executes that
  5950. ;; command. There might be problems if any of the keys is otherwise
  5951. ;; used as a prefix key.
  5952. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5953. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5954. ;; addresses this by checking explicitly for both bindings.
  5955. (defvar orgstruct-mode-map (make-sparse-keymap)
  5956. "Keymap for the minor `orgstruct-mode'.")
  5957. (defvar org-local-vars nil
  5958. "List of local variables, for use by `orgstruct-mode'")
  5959. ;;;###autoload
  5960. (define-minor-mode orgstruct-mode
  5961. "Toggle the minor more `orgstruct-mode'.
  5962. This mode is for using Org-mode structure commands in other modes.
  5963. The following key behave as if Org-mode was active, if the cursor
  5964. is on a headline, or on a plain list item (both in the definition
  5965. of Org-mode).
  5966. M-up Move entry/item up
  5967. M-down Move entry/item down
  5968. M-left Promote
  5969. M-right Demote
  5970. M-S-up Move entry/item up
  5971. M-S-down Move entry/item down
  5972. M-S-left Promote subtree
  5973. M-S-right Demote subtree
  5974. M-q Fill paragraph and items like in Org-mode
  5975. C-c ^ Sort entries
  5976. C-c - Cycle list bullet
  5977. TAB Cycle item visibility
  5978. M-RET Insert new heading/item
  5979. S-M-RET Insert new TODO heading / Chekbox item
  5980. C-c C-c Set tags / toggle checkbox"
  5981. nil " OrgStruct" nil
  5982. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5983. ;;;###autoload
  5984. (defun turn-on-orgstruct ()
  5985. "Unconditionally turn on `orgstruct-mode'."
  5986. (orgstruct-mode 1))
  5987. ;;;###autoload
  5988. (defun turn-on-orgstruct++ ()
  5989. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5990. In addition to setting orgstruct-mode, this also exports all indentation and
  5991. autofilling variables from org-mode into the buffer. Note that turning
  5992. off orgstruct-mode will *not* remove these additonal settings."
  5993. (orgstruct-mode 1)
  5994. (let (var val)
  5995. (mapc
  5996. (lambda (x)
  5997. (when (string-match
  5998. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5999. (symbol-name (car x)))
  6000. (setq var (car x) val (nth 1 x))
  6001. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6002. org-local-vars)))
  6003. (defun orgstruct-error ()
  6004. "Error when there is no default binding for a structure key."
  6005. (interactive)
  6006. (error "This key is has no function outside structure elements"))
  6007. (defun orgstruct-setup ()
  6008. "Setup orgstruct keymaps."
  6009. (let ((nfunc 0)
  6010. (bindings
  6011. (list
  6012. '([(meta up)] org-metaup)
  6013. '([(meta down)] org-metadown)
  6014. '([(meta left)] org-metaleft)
  6015. '([(meta right)] org-metaright)
  6016. '([(meta shift up)] org-shiftmetaup)
  6017. '([(meta shift down)] org-shiftmetadown)
  6018. '([(meta shift left)] org-shiftmetaleft)
  6019. '([(meta shift right)] org-shiftmetaright)
  6020. '([(shift up)] org-shiftup)
  6021. '([(shift down)] org-shiftdown)
  6022. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6023. '("\M-q" fill-paragraph)
  6024. '("\C-c^" org-sort)
  6025. '("\C-c-" org-cycle-list-bullet)))
  6026. elt key fun cmd)
  6027. (while (setq elt (pop bindings))
  6028. (setq nfunc (1+ nfunc))
  6029. (setq key (org-key (car elt))
  6030. fun (nth 1 elt)
  6031. cmd (orgstruct-make-binding fun nfunc key))
  6032. (org-defkey orgstruct-mode-map key cmd))
  6033. ;; Special treatment needed for TAB and RET
  6034. (org-defkey orgstruct-mode-map [(tab)]
  6035. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6036. (org-defkey orgstruct-mode-map "\C-i"
  6037. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6038. (org-defkey orgstruct-mode-map "\M-\C-m"
  6039. (orgstruct-make-binding 'org-insert-heading 105
  6040. "\M-\C-m" [(meta return)]))
  6041. (org-defkey orgstruct-mode-map [(meta return)]
  6042. (orgstruct-make-binding 'org-insert-heading 106
  6043. [(meta return)] "\M-\C-m"))
  6044. (org-defkey orgstruct-mode-map [(shift meta return)]
  6045. (orgstruct-make-binding 'org-insert-todo-heading 107
  6046. [(meta return)] "\M-\C-m"))
  6047. (unless org-local-vars
  6048. (setq org-local-vars (org-get-local-variables)))
  6049. t))
  6050. (defun orgstruct-make-binding (fun n &rest keys)
  6051. "Create a function for binding in the structure minor mode.
  6052. FUN is the command to call inside a table. N is used to create a unique
  6053. command name. KEYS are keys that should be checked in for a command
  6054. to execute outside of tables."
  6055. (eval
  6056. (list 'defun
  6057. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6058. '(arg)
  6059. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6060. "Outside of structure, run the binding of `"
  6061. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6062. "'.")
  6063. '(interactive "p")
  6064. (list 'if
  6065. '(org-context-p 'headline 'item)
  6066. (list 'org-run-like-in-org-mode (list 'quote fun))
  6067. (list 'let '(orgstruct-mode)
  6068. (list 'call-interactively
  6069. (append '(or)
  6070. (mapcar (lambda (k)
  6071. (list 'key-binding k))
  6072. keys)
  6073. '('orgstruct-error))))))))
  6074. (defun org-context-p (&rest contexts)
  6075. "FIXME:"
  6076. (let ((pos (point)))
  6077. (goto-char (point-at-bol))
  6078. (prog1 (or (and (memq 'table contexts)
  6079. (looking-at "[ \t]*|"))
  6080. (and (memq 'headline contexts)
  6081. (looking-at "\\*+"))
  6082. (and (memq 'item contexts)
  6083. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6084. (goto-char pos))))
  6085. (defun org-get-local-variables ()
  6086. "Return a list of all local variables in an org-mode buffer."
  6087. (let (varlist)
  6088. (with-current-buffer (get-buffer-create "*Org tmp*")
  6089. (erase-buffer)
  6090. (org-mode)
  6091. (setq varlist (buffer-local-variables)))
  6092. (kill-buffer "*Org tmp*")
  6093. (delq nil
  6094. (mapcar
  6095. (lambda (x)
  6096. (setq x
  6097. (if (symbolp x)
  6098. (list x)
  6099. (list (car x) (list 'quote (cdr x)))))
  6100. (if (string-match
  6101. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6102. (symbol-name (car x)))
  6103. x nil))
  6104. varlist))))
  6105. ;;;###autoload
  6106. (defun org-run-like-in-org-mode (cmd)
  6107. (unless org-local-vars
  6108. (setq org-local-vars (org-get-local-variables)))
  6109. (eval (list 'let org-local-vars
  6110. (list 'call-interactively (list 'quote cmd)))))
  6111. ;;;; Archiving
  6112. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6113. (defun org-archive-subtree (&optional find-done)
  6114. "Move the current subtree to the archive.
  6115. The archive can be a certain top-level heading in the current file, or in
  6116. a different file. The tree will be moved to that location, the subtree
  6117. heading be marked DONE, and the current time will be added.
  6118. When called with prefix argument FIND-DONE, find whole trees without any
  6119. open TODO items and archive them (after getting confirmation from the user).
  6120. If the cursor is not at a headline when this comand is called, try all level
  6121. 1 trees. If the cursor is on a headline, only try the direct children of
  6122. this heading."
  6123. (interactive "P")
  6124. (if find-done
  6125. (org-archive-all-done)
  6126. ;; Save all relevant TODO keyword-relatex variables
  6127. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6128. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6129. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6130. (tr-org-done-keywords org-done-keywords)
  6131. (tr-org-todo-regexp org-todo-regexp)
  6132. (tr-org-todo-line-regexp org-todo-line-regexp)
  6133. (tr-org-odd-levels-only org-odd-levels-only)
  6134. (this-buffer (current-buffer))
  6135. (org-archive-location org-archive-location)
  6136. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6137. (file (abbreviate-file-name (buffer-file-name)))
  6138. (time (format-time-string
  6139. (substring (cdr org-time-stamp-formats) 1 -1)
  6140. (current-time)))
  6141. afile heading buffer level newfile-p
  6142. category todo priority ltags itags prop)
  6143. ;; Try to find a local archive location
  6144. (save-excursion
  6145. (save-restriction
  6146. (widen)
  6147. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6148. (if (and prop (string-match "\\S-" prop))
  6149. (setq org-archive-location prop)
  6150. (if (or (re-search-backward re nil t)
  6151. (re-search-forward re nil t))
  6152. (setq org-archive-location (match-string 1))))))
  6153. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6154. (progn
  6155. (setq afile (format (match-string 1 org-archive-location)
  6156. (file-name-nondirectory buffer-file-name))
  6157. heading (match-string 2 org-archive-location)))
  6158. (error "Invalid `org-archive-location'"))
  6159. (if (> (length afile) 0)
  6160. (setq newfile-p (not (file-exists-p afile))
  6161. buffer (find-file-noselect afile))
  6162. (setq buffer (current-buffer)))
  6163. (unless buffer
  6164. (error "Cannot access file \"%s\"" afile))
  6165. (if (and (> (length heading) 0)
  6166. (string-match "^\\*+" heading))
  6167. (setq level (match-end 0))
  6168. (setq heading nil level 0))
  6169. (save-excursion
  6170. (org-back-to-heading t)
  6171. ;; Get context information that will be lost by moving the tree
  6172. (org-refresh-category-properties)
  6173. (setq category (org-get-category)
  6174. todo (and (looking-at org-todo-line-regexp)
  6175. (match-string 2))
  6176. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6177. ltags (org-get-tags)
  6178. itags (org-delete-all ltags (org-get-tags-at)))
  6179. (setq ltags (mapconcat 'identity ltags " ")
  6180. itags (mapconcat 'identity itags " "))
  6181. ;; We first only copy, in case something goes wrong
  6182. ;; we need to protect this-command, to avoid kill-region sets it,
  6183. ;; which would lead to duplication of subtrees
  6184. (let (this-command) (org-copy-subtree))
  6185. (set-buffer buffer)
  6186. ;; Enforce org-mode for the archive buffer
  6187. (if (not (org-mode-p))
  6188. ;; Force the mode for future visits.
  6189. (let ((org-insert-mode-line-in-empty-file t)
  6190. (org-inhibit-startup t))
  6191. (call-interactively 'org-mode)))
  6192. (when newfile-p
  6193. (goto-char (point-max))
  6194. (insert (format "\nArchived entries from file %s\n\n"
  6195. (buffer-file-name this-buffer))))
  6196. ;; Force the TODO keywords of the original buffer
  6197. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6198. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6199. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6200. (org-done-keywords tr-org-done-keywords)
  6201. (org-todo-regexp tr-org-todo-regexp)
  6202. (org-todo-line-regexp tr-org-todo-line-regexp)
  6203. (org-odd-levels-only
  6204. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6205. org-odd-levels-only
  6206. tr-org-odd-levels-only)))
  6207. (goto-char (point-min))
  6208. (if heading
  6209. (progn
  6210. (if (re-search-forward
  6211. (concat "^" (regexp-quote heading)
  6212. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6213. nil t)
  6214. (goto-char (match-end 0))
  6215. ;; Heading not found, just insert it at the end
  6216. (goto-char (point-max))
  6217. (or (bolp) (insert "\n"))
  6218. (insert "\n" heading "\n")
  6219. (end-of-line 0))
  6220. ;; Make the subtree visible
  6221. (show-subtree)
  6222. (org-end-of-subtree t)
  6223. (skip-chars-backward " \t\r\n")
  6224. (and (looking-at "[ \t\r\n]*")
  6225. (replace-match "\n\n")))
  6226. ;; No specific heading, just go to end of file.
  6227. (goto-char (point-max)) (insert "\n"))
  6228. ;; Paste
  6229. (org-paste-subtree (org-get-legal-level level 1))
  6230. ;; Mark the entry as done
  6231. (when (and org-archive-mark-done
  6232. (looking-at org-todo-line-regexp)
  6233. (or (not (match-end 2))
  6234. (not (member (match-string 2) org-done-keywords))))
  6235. (let (org-log-done)
  6236. (org-todo
  6237. (car (or (member org-archive-mark-done org-done-keywords)
  6238. org-done-keywords)))))
  6239. ;; Add the context info
  6240. (when org-archive-save-context-info
  6241. (let ((l org-archive-save-context-info) e n v)
  6242. (while (setq e (pop l))
  6243. (when (and (setq v (symbol-value e))
  6244. (stringp v) (string-match "\\S-" v))
  6245. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6246. (org-entry-put (point) n v)))))
  6247. ;; Save the buffer, if it is not the same buffer.
  6248. (if (not (eq this-buffer buffer)) (save-buffer))))
  6249. ;; Here we are back in the original buffer. Everything seems to have
  6250. ;; worked. So now cut the tree and finish up.
  6251. (let (this-command) (org-cut-subtree))
  6252. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6253. (message "Subtree archived %s"
  6254. (if (eq this-buffer buffer)
  6255. (concat "under heading: " heading)
  6256. (concat "in file: " (abbreviate-file-name afile)))))))
  6257. (defun org-refresh-category-properties ()
  6258. "Refresh category text properties in teh buffer."
  6259. (let ((def-cat (cond
  6260. ((null org-category)
  6261. (if buffer-file-name
  6262. (file-name-sans-extension
  6263. (file-name-nondirectory buffer-file-name))
  6264. "???"))
  6265. ((symbolp org-category) (symbol-name org-category))
  6266. (t org-category)))
  6267. beg end cat pos optionp)
  6268. (org-unmodified
  6269. (save-excursion
  6270. (save-restriction
  6271. (widen)
  6272. (goto-char (point-min))
  6273. (put-text-property (point) (point-max) 'org-category def-cat)
  6274. (while (re-search-forward
  6275. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6276. (setq pos (match-end 0)
  6277. optionp (equal (char-after (match-beginning 0)) ?#)
  6278. cat (org-trim (match-string 2)))
  6279. (if optionp
  6280. (setq beg (point-at-bol) end (point-max))
  6281. (org-back-to-heading t)
  6282. (setq beg (point) end (org-end-of-subtree t t)))
  6283. (put-text-property beg end 'org-category cat)
  6284. (goto-char pos)))))))
  6285. (defun org-archive-all-done (&optional tag)
  6286. "Archive sublevels of the current tree without open TODO items.
  6287. If the cursor is not on a headline, try all level 1 trees. If
  6288. it is on a headline, try all direct children.
  6289. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6290. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6291. (rea (concat ".*:" org-archive-tag ":"))
  6292. (begm (make-marker))
  6293. (endm (make-marker))
  6294. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6295. "Move subtree to archive (no open TODO items)? "))
  6296. beg end (cntarch 0))
  6297. (if (org-on-heading-p)
  6298. (progn
  6299. (setq re1 (concat "^" (regexp-quote
  6300. (make-string
  6301. (1+ (- (match-end 0) (match-beginning 0)))
  6302. ?*))
  6303. " "))
  6304. (move-marker begm (point))
  6305. (move-marker endm (org-end-of-subtree t)))
  6306. (setq re1 "^* ")
  6307. (move-marker begm (point-min))
  6308. (move-marker endm (point-max)))
  6309. (save-excursion
  6310. (goto-char begm)
  6311. (while (re-search-forward re1 endm t)
  6312. (setq beg (match-beginning 0)
  6313. end (save-excursion (org-end-of-subtree t) (point)))
  6314. (goto-char beg)
  6315. (if (re-search-forward re end t)
  6316. (goto-char end)
  6317. (goto-char beg)
  6318. (if (and (or (not tag) (not (looking-at rea)))
  6319. (y-or-n-p question))
  6320. (progn
  6321. (if tag
  6322. (org-toggle-tag org-archive-tag 'on)
  6323. (org-archive-subtree))
  6324. (setq cntarch (1+ cntarch)))
  6325. (goto-char end)))))
  6326. (message "%d trees archived" cntarch)))
  6327. (defun org-cycle-hide-drawers (state)
  6328. "Re-hide all drawers after a visibility state change."
  6329. (when (and (org-mode-p)
  6330. (not (memq state '(overview folded))))
  6331. (save-excursion
  6332. (let* ((globalp (memq state '(contents all)))
  6333. (beg (if globalp (point-min) (point)))
  6334. (end (if globalp (point-max) (org-end-of-subtree t))))
  6335. (goto-char beg)
  6336. (while (re-search-forward org-drawer-regexp end t)
  6337. (org-flag-drawer t))))))
  6338. (defun org-flag-drawer (flag)
  6339. (save-excursion
  6340. (beginning-of-line 1)
  6341. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6342. (let ((b (match-end 0)))
  6343. (if (re-search-forward
  6344. "^[ \t]*:END:"
  6345. (save-excursion (outline-next-heading) (point)) t)
  6346. (outline-flag-region b (point-at-eol) flag)
  6347. (error ":END: line missing"))))))
  6348. (defun org-cycle-hide-archived-subtrees (state)
  6349. "Re-hide all archived subtrees after a visibility state change."
  6350. (when (and (not org-cycle-open-archived-trees)
  6351. (not (memq state '(overview folded))))
  6352. (save-excursion
  6353. (let* ((globalp (memq state '(contents all)))
  6354. (beg (if globalp (point-min) (point)))
  6355. (end (if globalp (point-max) (org-end-of-subtree t))))
  6356. (org-hide-archived-subtrees beg end)
  6357. (goto-char beg)
  6358. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6359. (message (substitute-command-keys
  6360. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6361. (defun org-force-cycle-archived ()
  6362. "Cycle subtree even if it is archived."
  6363. (interactive)
  6364. (setq this-command 'org-cycle)
  6365. (let ((org-cycle-open-archived-trees t))
  6366. (call-interactively 'org-cycle)))
  6367. (defun org-hide-archived-subtrees (beg end)
  6368. "Re-hide all archived subtrees after a visibility state change."
  6369. (save-excursion
  6370. (let* ((re (concat ":" org-archive-tag ":")))
  6371. (goto-char beg)
  6372. (while (re-search-forward re end t)
  6373. (and (org-on-heading-p) (hide-subtree))
  6374. (org-end-of-subtree t)))))
  6375. (defun org-toggle-tag (tag &optional onoff)
  6376. "Toggle the tag TAG for the current line.
  6377. If ONOFF is `on' or `off', don't toggle but set to this state."
  6378. (unless (org-on-heading-p t) (error "Not on headling"))
  6379. (let (res current)
  6380. (save-excursion
  6381. (beginning-of-line)
  6382. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6383. (point-at-eol) t)
  6384. (progn
  6385. (setq current (match-string 1))
  6386. (replace-match ""))
  6387. (setq current ""))
  6388. (setq current (nreverse (org-split-string current ":")))
  6389. (cond
  6390. ((eq onoff 'on)
  6391. (setq res t)
  6392. (or (member tag current) (push tag current)))
  6393. ((eq onoff 'off)
  6394. (or (not (member tag current)) (setq current (delete tag current))))
  6395. (t (if (member tag current)
  6396. (setq current (delete tag current))
  6397. (setq res t)
  6398. (push tag current))))
  6399. (end-of-line 1)
  6400. (when current
  6401. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  6402. (org-set-tags nil t)
  6403. res)
  6404. (run-hooks 'org-after-tags-change-hook)))
  6405. (defun org-toggle-archive-tag (&optional arg)
  6406. "Toggle the archive tag for the current headline.
  6407. With prefix ARG, check all children of current headline and offer tagging
  6408. the children that do not contain any open TODO items."
  6409. (interactive "P")
  6410. (if arg
  6411. (org-archive-all-done 'tag)
  6412. (let (set)
  6413. (save-excursion
  6414. (org-back-to-heading t)
  6415. (setq set (org-toggle-tag org-archive-tag))
  6416. (when set (hide-subtree)))
  6417. (and set (beginning-of-line 1))
  6418. (message "Subtree %s" (if set "archived" "unarchived")))))
  6419. ;;;; Tables
  6420. ;;; The table editor
  6421. ;; Watch out: Here we are talking about two different kind of tables.
  6422. ;; Most of the code is for the tables created with the Org-mode table editor.
  6423. ;; Sometimes, we talk about tables created and edited with the table.el
  6424. ;; Emacs package. We call the former org-type tables, and the latter
  6425. ;; table.el-type tables.
  6426. (defun org-before-change-function (beg end)
  6427. "Every change indicates that a table might need an update."
  6428. (setq org-table-may-need-update t))
  6429. (defconst org-table-line-regexp "^[ \t]*|"
  6430. "Detects an org-type table line.")
  6431. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6432. "Detects an org-type table line.")
  6433. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6434. "Detects a table line marked for automatic recalculation.")
  6435. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6436. "Detects a table line marked for automatic recalculation.")
  6437. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6438. "Detects a table line marked for automatic recalculation.")
  6439. (defconst org-table-hline-regexp "^[ \t]*|-"
  6440. "Detects an org-type table hline.")
  6441. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6442. "Detects a table-type table hline.")
  6443. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6444. "Detects an org-type or table-type table.")
  6445. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6446. "Searching from within a table (any type) this finds the first line
  6447. outside the table.")
  6448. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6449. "Searching from within a table (any type) this finds the first line
  6450. outside the table.")
  6451. (defvar org-table-last-highlighted-reference nil)
  6452. (defvar org-table-formula-history nil)
  6453. (defvar org-table-column-names nil
  6454. "Alist with column names, derived from the `!' line.")
  6455. (defvar org-table-column-name-regexp nil
  6456. "Regular expression matching the current column names.")
  6457. (defvar org-table-local-parameters nil
  6458. "Alist with parameter names, derived from the `$' line.")
  6459. (defvar org-table-named-field-locations nil
  6460. "Alist with locations of named fields.")
  6461. (defvar org-table-current-line-types nil
  6462. "Table row types, non-nil only for the duration of a comand.")
  6463. (defvar org-table-current-begin-line nil
  6464. "Table begin line, non-nil only for the duration of a comand.")
  6465. (defvar org-table-current-begin-pos nil
  6466. "Table begin position, non-nil only for the duration of a comand.")
  6467. (defvar org-table-dlines nil
  6468. "Vector of data line line numbers in the current table.")
  6469. (defvar org-table-hlines nil
  6470. "Vector of hline line numbers in the current table.")
  6471. (defconst org-table-range-regexp
  6472. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6473. ;; 1 2 3 4 5
  6474. "Regular expression for matching ranges in formulas.")
  6475. (defconst org-table-range-regexp2
  6476. (concat
  6477. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6478. "\\.\\."
  6479. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6480. "Match a range for reference display.")
  6481. (defconst org-table-translate-regexp
  6482. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6483. "Match a reference that needs translation, for reference display.")
  6484. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6485. (defun org-table-create-with-table.el ()
  6486. "Use the table.el package to insert a new table.
  6487. If there is already a table at point, convert between Org-mode tables
  6488. and table.el tables."
  6489. (interactive)
  6490. (require 'table)
  6491. (cond
  6492. ((org-at-table.el-p)
  6493. (if (y-or-n-p "Convert table to Org-mode table? ")
  6494. (org-table-convert)))
  6495. ((org-at-table-p)
  6496. (if (y-or-n-p "Convert table to table.el table? ")
  6497. (org-table-convert)))
  6498. (t (call-interactively 'table-insert))))
  6499. (defun org-table-create-or-convert-from-region (arg)
  6500. "Convert region to table, or create an empty table.
  6501. If there is an active region, convert it to a table, using the function
  6502. `org-table-convert-region'. See the documentation of that function
  6503. to learn how the prefix argument is interpreted to determine the field
  6504. separator.
  6505. If there is no such region, create an empty table with `org-table-create'."
  6506. (interactive "P")
  6507. (if (org-region-active-p)
  6508. (org-table-convert-region (region-beginning) (region-end) arg)
  6509. (org-table-create arg)))
  6510. (defun org-table-create (&optional size)
  6511. "Query for a size and insert a table skeleton.
  6512. SIZE is a string Columns x Rows like for example \"3x2\"."
  6513. (interactive "P")
  6514. (unless size
  6515. (setq size (read-string
  6516. (concat "Table size Columns x Rows [e.g. "
  6517. org-table-default-size "]: ")
  6518. "" nil org-table-default-size)))
  6519. (let* ((pos (point))
  6520. (indent (make-string (current-column) ?\ ))
  6521. (split (org-split-string size " *x *"))
  6522. (rows (string-to-number (nth 1 split)))
  6523. (columns (string-to-number (car split)))
  6524. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6525. "\n")))
  6526. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6527. (point-at-bol) (point)))
  6528. (beginning-of-line 1)
  6529. (newline))
  6530. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6531. (dotimes (i rows) (insert line))
  6532. (goto-char pos)
  6533. (if (> rows 1)
  6534. ;; Insert a hline after the first row.
  6535. (progn
  6536. (end-of-line 1)
  6537. (insert "\n|-")
  6538. (goto-char pos)))
  6539. (org-table-align)))
  6540. (defun org-table-convert-region (beg0 end0 &optional separator)
  6541. "Convert region to a table.
  6542. The region goes from BEG0 to END0, but these borders will be moved
  6543. slightly, to make sure a beginning of line in the first line is included.
  6544. SEPARATOR specifies the field separator in the lines. It can have the
  6545. following values:
  6546. '(4) Use the comma as a field separator
  6547. '(16) Use a TAB as field separator
  6548. integer When a number, use that many spaces as field separator
  6549. nil When nil, the command tries to be smart and figure out the
  6550. separator in the following way:
  6551. - when each line contains a TAB, assume TAB-separated material
  6552. - when each line contains a comme, assume CSV material
  6553. - else, assume one or more SPACE charcters as separator."
  6554. (interactive "rP")
  6555. (let* ((beg (min beg0 end0))
  6556. (end (max beg0 end0))
  6557. sep-re re)
  6558. (goto-char beg)
  6559. (beginning-of-line 1)
  6560. (setq beg (move-marker (make-marker) (point)))
  6561. (goto-char end)
  6562. (if (bolp) (backward-char 1) (end-of-line 1))
  6563. (setq end (move-marker (make-marker) (point)))
  6564. ;; Get the right field separator
  6565. (unless separator
  6566. (goto-char beg)
  6567. (setq separator
  6568. (cond
  6569. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  6570. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  6571. (t 1))))
  6572. (setq re (cond
  6573. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  6574. ((equal separator '(16)) "^\\|\t")
  6575. ((integerp separator)
  6576. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  6577. (t (error "This should not happen"))))
  6578. (goto-char beg)
  6579. (while (re-search-forward re end t)
  6580. (replace-match "| " t t))
  6581. (goto-char beg)
  6582. (insert " ")
  6583. (org-table-align)))
  6584. (defun org-table-import (file arg)
  6585. "Import FILE as a table.
  6586. The file is assumed to be tab-separated. Such files can be produced by most
  6587. spreadsheet and database applications. If no tabs (at least one per line)
  6588. are found, lines will be split on whitespace into fields."
  6589. (interactive "f\nP")
  6590. (or (bolp) (newline))
  6591. (let ((beg (point))
  6592. (pm (point-max)))
  6593. (insert-file-contents file)
  6594. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6595. (defun org-table-export ()
  6596. "Export table as a tab-separated file.
  6597. Such a file can be imported into a spreadsheet program like Excel."
  6598. (interactive)
  6599. (let* ((beg (org-table-begin))
  6600. (end (org-table-end))
  6601. (table (buffer-substring beg end))
  6602. (file (read-file-name "Export table to: "))
  6603. buf)
  6604. (unless (or (not (file-exists-p file))
  6605. (y-or-n-p (format "Overwrite file %s? " file)))
  6606. (error "Abort"))
  6607. (with-current-buffer (find-file-noselect file)
  6608. (setq buf (current-buffer))
  6609. (erase-buffer)
  6610. (fundamental-mode)
  6611. (insert table)
  6612. (goto-char (point-min))
  6613. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6614. (replace-match "" t t)
  6615. (end-of-line 1))
  6616. (goto-char (point-min))
  6617. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6618. (replace-match "" t t)
  6619. (goto-char (min (1+ (point)) (point-max))))
  6620. (goto-char (point-min))
  6621. (while (re-search-forward "^-[-+]*$" nil t)
  6622. (replace-match "")
  6623. (if (looking-at "\n")
  6624. (delete-char 1)))
  6625. (goto-char (point-min))
  6626. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6627. (replace-match "\t" t t))
  6628. (save-buffer))
  6629. (kill-buffer buf)))
  6630. (defvar org-table-aligned-begin-marker (make-marker)
  6631. "Marker at the beginning of the table last aligned.
  6632. Used to check if cursor still is in that table, to minimize realignment.")
  6633. (defvar org-table-aligned-end-marker (make-marker)
  6634. "Marker at the end of the table last aligned.
  6635. Used to check if cursor still is in that table, to minimize realignment.")
  6636. (defvar org-table-last-alignment nil
  6637. "List of flags for flushright alignment, from the last re-alignment.
  6638. This is being used to correctly align a single field after TAB or RET.")
  6639. (defvar org-table-last-column-widths nil
  6640. "List of max width of fields in each column.
  6641. This is being used to correctly align a single field after TAB or RET.")
  6642. (defvar org-table-overlay-coordinates nil
  6643. "Overlay coordinates after each align of a table.")
  6644. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6645. (defvar org-last-recalc-line nil)
  6646. (defconst org-narrow-column-arrow "=>"
  6647. "Used as display property in narrowed table columns.")
  6648. (defun org-table-align ()
  6649. "Align the table at point by aligning all vertical bars."
  6650. (interactive)
  6651. (let* (
  6652. ;; Limits of table
  6653. (beg (org-table-begin))
  6654. (end (org-table-end))
  6655. ;; Current cursor position
  6656. (linepos (org-current-line))
  6657. (colpos (org-table-current-column))
  6658. (winstart (window-start))
  6659. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6660. lines (new "") lengths l typenums ty fields maxfields i
  6661. column
  6662. (indent "") cnt frac
  6663. rfmt hfmt
  6664. (spaces '(1 . 1))
  6665. (sp1 (car spaces))
  6666. (sp2 (cdr spaces))
  6667. (rfmt1 (concat
  6668. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6669. (hfmt1 (concat
  6670. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6671. emptystrings links dates narrow fmax f1 len c e)
  6672. (untabify beg end)
  6673. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6674. ;; Check if we have links or dates
  6675. (goto-char beg)
  6676. (setq links (re-search-forward org-bracket-link-regexp end t))
  6677. (goto-char beg)
  6678. (setq dates (and org-display-custom-times
  6679. (re-search-forward org-ts-regexp-both end t)))
  6680. ;; Make sure the link properties are right
  6681. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6682. ;; Make sure the date properties are right
  6683. (when dates (goto-char beg) (while (org-activate-dates end)))
  6684. ;; Check if we are narrowing any columns
  6685. (goto-char beg)
  6686. (setq narrow (and org-format-transports-properties-p
  6687. (re-search-forward "<[0-9]+>" end t)))
  6688. ;; Get the rows
  6689. (setq lines (org-split-string
  6690. (buffer-substring beg end) "\n"))
  6691. ;; Store the indentation of the first line
  6692. (if (string-match "^ *" (car lines))
  6693. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6694. ;; Mark the hlines by setting the corresponding element to nil
  6695. ;; At the same time, we remove trailing space.
  6696. (setq lines (mapcar (lambda (l)
  6697. (if (string-match "^ *|-" l)
  6698. nil
  6699. (if (string-match "[ \t]+$" l)
  6700. (substring l 0 (match-beginning 0))
  6701. l)))
  6702. lines))
  6703. ;; Get the data fields by splitting the lines.
  6704. (setq fields (mapcar
  6705. (lambda (l)
  6706. (org-split-string l " *| *"))
  6707. (delq nil (copy-sequence lines))))
  6708. ;; How many fields in the longest line?
  6709. (condition-case nil
  6710. (setq maxfields (apply 'max (mapcar 'length fields)))
  6711. (error
  6712. (kill-region beg end)
  6713. (org-table-create org-table-default-size)
  6714. (error "Empty table - created default table")))
  6715. ;; A list of empty strings to fill any short rows on output
  6716. (setq emptystrings (make-list maxfields ""))
  6717. ;; Check for special formatting.
  6718. (setq i -1)
  6719. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6720. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6721. ;; Check if there is an explicit width specified
  6722. (when narrow
  6723. (setq c column fmax nil)
  6724. (while c
  6725. (setq e (pop c))
  6726. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6727. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6728. ;; Find fields that are wider than fmax, and shorten them
  6729. (when fmax
  6730. (loop for xx in column do
  6731. (when (and (stringp xx)
  6732. (> (org-string-width xx) fmax))
  6733. (org-add-props xx nil
  6734. 'help-echo
  6735. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6736. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6737. (unless (> f1 1)
  6738. (error "Cannot narrow field starting with wide link \"%s\""
  6739. (match-string 0 xx)))
  6740. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6741. (add-text-properties (- f1 2) f1
  6742. (list 'display org-narrow-column-arrow)
  6743. xx)))))
  6744. ;; Get the maximum width for each column
  6745. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6746. ;; Get the fraction of numbers, to decide about alignment of the column
  6747. (setq cnt 0 frac 0.0)
  6748. (loop for x in column do
  6749. (if (equal x "")
  6750. nil
  6751. (setq frac ( / (+ (* frac cnt)
  6752. (if (string-match org-table-number-regexp x) 1 0))
  6753. (setq cnt (1+ cnt))))))
  6754. (push (>= frac org-table-number-fraction) typenums))
  6755. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6756. ;; Store the alignment of this table, for later editing of single fields
  6757. (setq org-table-last-alignment typenums
  6758. org-table-last-column-widths lengths)
  6759. ;; With invisible characters, `format' does not get the field width right
  6760. ;; So we need to make these fields wide by hand.
  6761. (when links
  6762. (loop for i from 0 upto (1- maxfields) do
  6763. (setq len (nth i lengths))
  6764. (loop for j from 0 upto (1- (length fields)) do
  6765. (setq c (nthcdr i (car (nthcdr j fields))))
  6766. (if (and (stringp (car c))
  6767. (string-match org-bracket-link-regexp (car c))
  6768. (< (org-string-width (car c)) len))
  6769. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6770. ;; Compute the formats needed for output of the table
  6771. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6772. (while (setq l (pop lengths))
  6773. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6774. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6775. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6776. (setq rfmt (concat rfmt "\n")
  6777. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6778. (setq new (mapconcat
  6779. (lambda (l)
  6780. (if l (apply 'format rfmt
  6781. (append (pop fields) emptystrings))
  6782. hfmt))
  6783. lines ""))
  6784. ;; Replace the old one
  6785. (delete-region beg end)
  6786. (move-marker end nil)
  6787. (move-marker org-table-aligned-begin-marker (point))
  6788. (insert new)
  6789. (move-marker org-table-aligned-end-marker (point))
  6790. (when (and orgtbl-mode (not (org-mode-p)))
  6791. (goto-char org-table-aligned-begin-marker)
  6792. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6793. ;; Try to move to the old location
  6794. (goto-line winstartline)
  6795. (setq winstart (point-at-bol))
  6796. (goto-line linepos)
  6797. (set-window-start (selected-window) winstart 'noforce)
  6798. (org-table-goto-column colpos)
  6799. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6800. (setq org-table-may-need-update nil)
  6801. ))
  6802. (defun org-string-width (s)
  6803. "Compute width of string, ignoring invisible characters.
  6804. This ignores character with invisibility property `org-link', and also
  6805. characters with property `org-cwidth', because these will become invisible
  6806. upon the next fontification round."
  6807. (let (b l)
  6808. (when (or (eq t buffer-invisibility-spec)
  6809. (assq 'org-link buffer-invisibility-spec))
  6810. (while (setq b (text-property-any 0 (length s)
  6811. 'invisible 'org-link s))
  6812. (setq s (concat (substring s 0 b)
  6813. (substring s (or (next-single-property-change
  6814. b 'invisible s) (length s)))))))
  6815. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6816. (setq s (concat (substring s 0 b)
  6817. (substring s (or (next-single-property-change
  6818. b 'org-cwidth s) (length s))))))
  6819. (setq l (string-width s) b -1)
  6820. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6821. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6822. l))
  6823. (defun org-table-begin (&optional table-type)
  6824. "Find the beginning of the table and return its position.
  6825. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6826. (save-excursion
  6827. (if (not (re-search-backward
  6828. (if table-type org-table-any-border-regexp
  6829. org-table-border-regexp)
  6830. nil t))
  6831. (progn (goto-char (point-min)) (point))
  6832. (goto-char (match-beginning 0))
  6833. (beginning-of-line 2)
  6834. (point))))
  6835. (defun org-table-end (&optional table-type)
  6836. "Find the end of the table and return its position.
  6837. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6838. (save-excursion
  6839. (if (not (re-search-forward
  6840. (if table-type org-table-any-border-regexp
  6841. org-table-border-regexp)
  6842. nil t))
  6843. (goto-char (point-max))
  6844. (goto-char (match-beginning 0)))
  6845. (point-marker)))
  6846. (defun org-table-justify-field-maybe (&optional new)
  6847. "Justify the current field, text to left, number to right.
  6848. Optional argument NEW may specify text to replace the current field content."
  6849. (cond
  6850. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6851. ((org-at-table-hline-p))
  6852. ((and (not new)
  6853. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6854. (current-buffer)))
  6855. (< (point) org-table-aligned-begin-marker)
  6856. (>= (point) org-table-aligned-end-marker)))
  6857. ;; This is not the same table, force a full re-align
  6858. (setq org-table-may-need-update t))
  6859. (t ;; realign the current field, based on previous full realign
  6860. (let* ((pos (point)) s
  6861. (col (org-table-current-column))
  6862. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6863. l f n o e)
  6864. (when (> col 0)
  6865. (skip-chars-backward "^|\n")
  6866. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6867. (progn
  6868. (setq s (match-string 1)
  6869. o (match-string 0)
  6870. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6871. e (not (= (match-beginning 2) (match-end 2))))
  6872. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6873. l (if e "|" (setq org-table-may-need-update t) ""))
  6874. n (format f s))
  6875. (if new
  6876. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6877. (setq n (format f new))
  6878. (setq n (concat new "|") org-table-may-need-update t)))
  6879. (or (equal n o)
  6880. (let (org-table-may-need-update)
  6881. (replace-match n t t))))
  6882. (setq org-table-may-need-update t))
  6883. (goto-char pos))))))
  6884. (defun org-table-next-field ()
  6885. "Go to the next field in the current table, creating new lines as needed.
  6886. Before doing so, re-align the table if necessary."
  6887. (interactive)
  6888. (org-table-maybe-eval-formula)
  6889. (org-table-maybe-recalculate-line)
  6890. (if (and org-table-automatic-realign
  6891. org-table-may-need-update)
  6892. (org-table-align))
  6893. (let ((end (org-table-end)))
  6894. (if (org-at-table-hline-p)
  6895. (end-of-line 1))
  6896. (condition-case nil
  6897. (progn
  6898. (re-search-forward "|" end)
  6899. (if (looking-at "[ \t]*$")
  6900. (re-search-forward "|" end))
  6901. (if (and (looking-at "-")
  6902. org-table-tab-jumps-over-hlines
  6903. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6904. (goto-char (match-beginning 1)))
  6905. (if (looking-at "-")
  6906. (progn
  6907. (beginning-of-line 0)
  6908. (org-table-insert-row 'below))
  6909. (if (looking-at " ") (forward-char 1))))
  6910. (error
  6911. (org-table-insert-row 'below)))))
  6912. (defun org-table-previous-field ()
  6913. "Go to the previous field in the table.
  6914. Before doing so, re-align the table if necessary."
  6915. (interactive)
  6916. (org-table-justify-field-maybe)
  6917. (org-table-maybe-recalculate-line)
  6918. (if (and org-table-automatic-realign
  6919. org-table-may-need-update)
  6920. (org-table-align))
  6921. (if (org-at-table-hline-p)
  6922. (end-of-line 1))
  6923. (re-search-backward "|" (org-table-begin))
  6924. (re-search-backward "|" (org-table-begin))
  6925. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6926. (re-search-backward "|" (org-table-begin)))
  6927. (if (looking-at "| ?")
  6928. (goto-char (match-end 0))))
  6929. (defun org-table-next-row ()
  6930. "Go to the next row (same column) in the current table.
  6931. Before doing so, re-align the table if necessary."
  6932. (interactive)
  6933. (org-table-maybe-eval-formula)
  6934. (org-table-maybe-recalculate-line)
  6935. (if (or (looking-at "[ \t]*$")
  6936. (save-excursion (skip-chars-backward " \t") (bolp)))
  6937. (newline)
  6938. (if (and org-table-automatic-realign
  6939. org-table-may-need-update)
  6940. (org-table-align))
  6941. (let ((col (org-table-current-column)))
  6942. (beginning-of-line 2)
  6943. (if (or (not (org-at-table-p))
  6944. (org-at-table-hline-p))
  6945. (progn
  6946. (beginning-of-line 0)
  6947. (org-table-insert-row 'below)))
  6948. (org-table-goto-column col)
  6949. (skip-chars-backward "^|\n\r")
  6950. (if (looking-at " ") (forward-char 1)))))
  6951. (defun org-table-copy-down (n)
  6952. "Copy a field down in the current column.
  6953. If the field at the cursor is empty, copy into it the content of the nearest
  6954. non-empty field above. With argument N, use the Nth non-empty field.
  6955. If the current field is not empty, it is copied down to the next row, and
  6956. the cursor is moved with it. Therefore, repeating this command causes the
  6957. column to be filled row-by-row.
  6958. If the variable `org-table-copy-increment' is non-nil and the field is an
  6959. integer or a timestamp, it will be incremented while copying. In the case of
  6960. a timestamp, if the cursor is on the year, change the year. If it is on the
  6961. month or the day, change that. Point will stay on the current date field
  6962. in order to easily repeat the interval."
  6963. (interactive "p")
  6964. (let* ((colpos (org-table-current-column))
  6965. (col (current-column))
  6966. (field (org-table-get-field))
  6967. (non-empty (string-match "[^ \t]" field))
  6968. (beg (org-table-begin))
  6969. txt)
  6970. (org-table-check-inside-data-field)
  6971. (if non-empty
  6972. (progn
  6973. (setq txt (org-trim field))
  6974. (org-table-next-row)
  6975. (org-table-blank-field))
  6976. (save-excursion
  6977. (setq txt
  6978. (catch 'exit
  6979. (while (progn (beginning-of-line 1)
  6980. (re-search-backward org-table-dataline-regexp
  6981. beg t))
  6982. (org-table-goto-column colpos t)
  6983. (if (and (looking-at
  6984. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6985. (= (setq n (1- n)) 0))
  6986. (throw 'exit (match-string 1))))))))
  6987. (if txt
  6988. (progn
  6989. (if (and org-table-copy-increment
  6990. (string-match "^[0-9]+$" txt))
  6991. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6992. (insert txt)
  6993. (move-to-column col)
  6994. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6995. (org-timestamp-up 1)
  6996. (org-table-maybe-recalculate-line))
  6997. (org-table-align)
  6998. (move-to-column col))
  6999. (error "No non-empty field found"))))
  7000. (defun org-table-check-inside-data-field ()
  7001. "Is point inside a table data field?
  7002. I.e. not on a hline or before the first or after the last column?
  7003. This actually throws an error, so it aborts the current command."
  7004. (if (or (not (org-at-table-p))
  7005. (= (org-table-current-column) 0)
  7006. (org-at-table-hline-p)
  7007. (looking-at "[ \t]*$"))
  7008. (error "Not in table data field")))
  7009. (defvar org-table-clip nil
  7010. "Clipboard for table regions.")
  7011. (defun org-table-blank-field ()
  7012. "Blank the current table field or active region."
  7013. (interactive)
  7014. (org-table-check-inside-data-field)
  7015. (if (and (interactive-p) (org-region-active-p))
  7016. (let (org-table-clip)
  7017. (org-table-cut-region (region-beginning) (region-end)))
  7018. (skip-chars-backward "^|")
  7019. (backward-char 1)
  7020. (if (looking-at "|[^|\n]+")
  7021. (let* ((pos (match-beginning 0))
  7022. (match (match-string 0))
  7023. (len (org-string-width match)))
  7024. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7025. (goto-char (+ 2 pos))
  7026. (substring match 1)))))
  7027. (defun org-table-get-field (&optional n replace)
  7028. "Return the value of the field in column N of current row.
  7029. N defaults to current field.
  7030. If REPLACE is a string, replace field with this value. The return value
  7031. is always the old value."
  7032. (and n (org-table-goto-column n))
  7033. (skip-chars-backward "^|\n")
  7034. (backward-char 1)
  7035. (if (looking-at "|[^|\r\n]*")
  7036. (let* ((pos (match-beginning 0))
  7037. (val (buffer-substring (1+ pos) (match-end 0))))
  7038. (if replace
  7039. (replace-match (concat "|" replace) t t))
  7040. (goto-char (min (point-at-eol) (+ 2 pos)))
  7041. val)
  7042. (forward-char 1) ""))
  7043. (defun org-table-field-info (arg)
  7044. "Show info about the current field, and highlight any reference at point."
  7045. (interactive "P")
  7046. (org-table-get-specials)
  7047. (save-excursion
  7048. (let* ((pos (point))
  7049. (col (org-table-current-column))
  7050. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7051. (name (car (rassoc (list (org-current-line) col)
  7052. org-table-named-field-locations)))
  7053. (eql (org-table-get-stored-formulas))
  7054. (dline (org-table-current-dline))
  7055. (ref (format "@%d$%d" dline col))
  7056. (ref1 (org-table-convert-refs-to-an ref))
  7057. (fequation (or (assoc name eql) (assoc ref eql)))
  7058. (cequation (assoc (int-to-string col) eql))
  7059. (eqn (or fequation cequation)))
  7060. (goto-char pos)
  7061. (condition-case nil
  7062. (org-table-show-reference 'local)
  7063. (error nil))
  7064. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7065. dline col
  7066. (if cname (concat " or $" cname) "")
  7067. dline col ref1
  7068. (if name (concat " or $" name) "")
  7069. ;; FIXME: formula info not correct if special table line
  7070. (if eqn
  7071. (concat ", formula: "
  7072. (org-table-formula-to-user
  7073. (concat
  7074. (if (string-match "^[$@]"(car eqn)) "" "$")
  7075. (car eqn) "=" (cdr eqn))))
  7076. "")))))
  7077. (defun org-table-current-column ()
  7078. "Find out which column we are in.
  7079. When called interactively, column is also displayed in echo area."
  7080. (interactive)
  7081. (if (interactive-p) (org-table-check-inside-data-field))
  7082. (save-excursion
  7083. (let ((cnt 0) (pos (point)))
  7084. (beginning-of-line 1)
  7085. (while (search-forward "|" pos t)
  7086. (setq cnt (1+ cnt)))
  7087. (if (interactive-p) (message "This is table column %d" cnt))
  7088. cnt)))
  7089. (defun org-table-current-dline ()
  7090. "Find out what table data line we are in.
  7091. Only datalins count for this."
  7092. (interactive)
  7093. (if (interactive-p) (org-table-check-inside-data-field))
  7094. (save-excursion
  7095. (let ((cnt 0) (pos (point)))
  7096. (goto-char (org-table-begin))
  7097. (while (<= (point) pos)
  7098. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7099. (beginning-of-line 2))
  7100. (if (interactive-p) (message "This is table line %d" cnt))
  7101. cnt)))
  7102. (defun org-table-goto-column (n &optional on-delim force)
  7103. "Move the cursor to the Nth column in the current table line.
  7104. With optional argument ON-DELIM, stop with point before the left delimiter
  7105. of the field.
  7106. If there are less than N fields, just go to after the last delimiter.
  7107. However, when FORCE is non-nil, create new columns if necessary."
  7108. (interactive "p")
  7109. (let ((pos (point-at-eol)))
  7110. (beginning-of-line 1)
  7111. (when (> n 0)
  7112. (while (and (> (setq n (1- n)) -1)
  7113. (or (search-forward "|" pos t)
  7114. (and force
  7115. (progn (end-of-line 1)
  7116. (skip-chars-backward "^|")
  7117. (insert " | "))))))
  7118. ; (backward-char 2) t)))))
  7119. (when (and force (not (looking-at ".*|")))
  7120. (save-excursion (end-of-line 1) (insert " | ")))
  7121. (if on-delim
  7122. (backward-char 1)
  7123. (if (looking-at " ") (forward-char 1))))))
  7124. (defun org-at-table-p (&optional table-type)
  7125. "Return t if the cursor is inside an org-type table.
  7126. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7127. (if org-enable-table-editor
  7128. (save-excursion
  7129. (beginning-of-line 1)
  7130. (looking-at (if table-type org-table-any-line-regexp
  7131. org-table-line-regexp)))
  7132. nil))
  7133. (defun org-at-table.el-p ()
  7134. "Return t if and only if we are at a table.el table."
  7135. (and (org-at-table-p 'any)
  7136. (save-excursion
  7137. (goto-char (org-table-begin 'any))
  7138. (looking-at org-table1-hline-regexp))))
  7139. (defun org-table-recognize-table.el ()
  7140. "If there is a table.el table nearby, recognize it and move into it."
  7141. (if org-table-tab-recognizes-table.el
  7142. (if (org-at-table.el-p)
  7143. (progn
  7144. (beginning-of-line 1)
  7145. (if (looking-at org-table-dataline-regexp)
  7146. nil
  7147. (if (looking-at org-table1-hline-regexp)
  7148. (progn
  7149. (beginning-of-line 2)
  7150. (if (looking-at org-table-any-border-regexp)
  7151. (beginning-of-line -1)))))
  7152. (if (re-search-forward "|" (org-table-end t) t)
  7153. (progn
  7154. (require 'table)
  7155. (if (table--at-cell-p (point))
  7156. t
  7157. (message "recognizing table.el table...")
  7158. (table-recognize-table)
  7159. (message "recognizing table.el table...done")))
  7160. (error "This should not happen..."))
  7161. t)
  7162. nil)
  7163. nil))
  7164. (defun org-at-table-hline-p ()
  7165. "Return t if the cursor is inside a hline in a table."
  7166. (if org-enable-table-editor
  7167. (save-excursion
  7168. (beginning-of-line 1)
  7169. (looking-at org-table-hline-regexp))
  7170. nil))
  7171. (defun org-table-insert-column ()
  7172. "Insert a new column into the table."
  7173. (interactive)
  7174. (if (not (org-at-table-p))
  7175. (error "Not at a table"))
  7176. (org-table-find-dataline)
  7177. (let* ((col (max 1 (org-table-current-column)))
  7178. (beg (org-table-begin))
  7179. (end (org-table-end))
  7180. ;; Current cursor position
  7181. (linepos (org-current-line))
  7182. (colpos col))
  7183. (goto-char beg)
  7184. (while (< (point) end)
  7185. (if (org-at-table-hline-p)
  7186. nil
  7187. (org-table-goto-column col t)
  7188. (insert "| "))
  7189. (beginning-of-line 2))
  7190. (move-marker end nil)
  7191. (goto-line linepos)
  7192. (org-table-goto-column colpos)
  7193. (org-table-align)
  7194. (org-table-fix-formulas "$" nil (1- col) 1)))
  7195. (defun org-table-find-dataline ()
  7196. "Find a dataline in the current table, which is needed for column commands."
  7197. (if (and (org-at-table-p)
  7198. (not (org-at-table-hline-p)))
  7199. t
  7200. (let ((col (current-column))
  7201. (end (org-table-end)))
  7202. (move-to-column col)
  7203. (while (and (< (point) end)
  7204. (or (not (= (current-column) col))
  7205. (org-at-table-hline-p)))
  7206. (beginning-of-line 2)
  7207. (move-to-column col))
  7208. (if (and (org-at-table-p)
  7209. (not (org-at-table-hline-p)))
  7210. t
  7211. (error
  7212. "Please position cursor in a data line for column operations")))))
  7213. (defun org-table-delete-column ()
  7214. "Delete a column from the table."
  7215. (interactive)
  7216. (if (not (org-at-table-p))
  7217. (error "Not at a table"))
  7218. (org-table-find-dataline)
  7219. (org-table-check-inside-data-field)
  7220. (let* ((col (org-table-current-column))
  7221. (beg (org-table-begin))
  7222. (end (org-table-end))
  7223. ;; Current cursor position
  7224. (linepos (org-current-line))
  7225. (colpos col))
  7226. (goto-char beg)
  7227. (while (< (point) end)
  7228. (if (org-at-table-hline-p)
  7229. nil
  7230. (org-table-goto-column col t)
  7231. (and (looking-at "|[^|\n]+|")
  7232. (replace-match "|")))
  7233. (beginning-of-line 2))
  7234. (move-marker end nil)
  7235. (goto-line linepos)
  7236. (org-table-goto-column colpos)
  7237. (org-table-align)
  7238. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7239. col -1 col)))
  7240. (defun org-table-move-column-right ()
  7241. "Move column to the right."
  7242. (interactive)
  7243. (org-table-move-column nil))
  7244. (defun org-table-move-column-left ()
  7245. "Move column to the left."
  7246. (interactive)
  7247. (org-table-move-column 'left))
  7248. (defun org-table-move-column (&optional left)
  7249. "Move the current column to the right. With arg LEFT, move to the left."
  7250. (interactive "P")
  7251. (if (not (org-at-table-p))
  7252. (error "Not at a table"))
  7253. (org-table-find-dataline)
  7254. (org-table-check-inside-data-field)
  7255. (let* ((col (org-table-current-column))
  7256. (col1 (if left (1- col) col))
  7257. (beg (org-table-begin))
  7258. (end (org-table-end))
  7259. ;; Current cursor position
  7260. (linepos (org-current-line))
  7261. (colpos (if left (1- col) (1+ col))))
  7262. (if (and left (= col 1))
  7263. (error "Cannot move column further left"))
  7264. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7265. (error "Cannot move column further right"))
  7266. (goto-char beg)
  7267. (while (< (point) end)
  7268. (if (org-at-table-hline-p)
  7269. nil
  7270. (org-table-goto-column col1 t)
  7271. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7272. (replace-match "|\\2|\\1|")))
  7273. (beginning-of-line 2))
  7274. (move-marker end nil)
  7275. (goto-line linepos)
  7276. (org-table-goto-column colpos)
  7277. (org-table-align)
  7278. (org-table-fix-formulas
  7279. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7280. (cons (number-to-string colpos) (number-to-string col))))))
  7281. (defun org-table-move-row-down ()
  7282. "Move table row down."
  7283. (interactive)
  7284. (org-table-move-row nil))
  7285. (defun org-table-move-row-up ()
  7286. "Move table row up."
  7287. (interactive)
  7288. (org-table-move-row 'up))
  7289. (defun org-table-move-row (&optional up)
  7290. "Move the current table line down. With arg UP, move it up."
  7291. (interactive "P")
  7292. (let* ((col (current-column))
  7293. (pos (point))
  7294. (hline1p (save-excursion (beginning-of-line 1)
  7295. (looking-at org-table-hline-regexp)))
  7296. (dline1 (org-table-current-dline))
  7297. (dline2 (+ dline1 (if up -1 1)))
  7298. (tonew (if up 0 2))
  7299. txt hline2p)
  7300. (beginning-of-line tonew)
  7301. (unless (org-at-table-p)
  7302. (goto-char pos)
  7303. (error "Cannot move row further"))
  7304. (setq hline2p (looking-at org-table-hline-regexp))
  7305. (goto-char pos)
  7306. (beginning-of-line 1)
  7307. (setq pos (point))
  7308. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7309. (delete-region (point) (1+ (point-at-eol)))
  7310. (beginning-of-line tonew)
  7311. (insert txt)
  7312. (beginning-of-line 0)
  7313. (move-to-column col)
  7314. (unless (or hline1p hline2p)
  7315. (org-table-fix-formulas
  7316. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7317. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7318. (defun org-table-insert-row (&optional arg)
  7319. "Insert a new row above the current line into the table.
  7320. With prefix ARG, insert below the current line."
  7321. (interactive "P")
  7322. (if (not (org-at-table-p))
  7323. (error "Not at a table"))
  7324. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7325. (new (org-table-clean-line line)))
  7326. ;; Fix the first field if necessary
  7327. (if (string-match "^[ \t]*| *[#$] *|" line)
  7328. (setq new (replace-match (match-string 0 line) t t new)))
  7329. (beginning-of-line (if arg 2 1))
  7330. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7331. (beginning-of-line 0)
  7332. (re-search-forward "| ?" (point-at-eol) t)
  7333. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7334. (org-table-align))
  7335. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7336. (defun org-table-insert-hline (&optional above)
  7337. "Insert a horizontal-line below the current line into the table.
  7338. With prefix ABOVE, insert above the current line."
  7339. (interactive "P")
  7340. (if (not (org-at-table-p))
  7341. (error "Not at a table"))
  7342. (let ((line (org-table-clean-line
  7343. (buffer-substring (point-at-bol) (point-at-eol))))
  7344. (col (current-column)))
  7345. (while (string-match "|\\( +\\)|" line)
  7346. (setq line (replace-match
  7347. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7348. ?-) "|") t t line)))
  7349. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7350. (beginning-of-line (if above 1 2))
  7351. (insert line "\n")
  7352. (beginning-of-line (if above 1 -1))
  7353. (move-to-column col)
  7354. (and org-table-overlay-coordinates (org-table-align))))
  7355. (defun org-table-hline-and-move (&optional same-column)
  7356. "Insert a hline and move to the row below that line."
  7357. (interactive "P")
  7358. (let ((col (org-table-current-column)))
  7359. (org-table-maybe-eval-formula)
  7360. (org-table-maybe-recalculate-line)
  7361. (org-table-insert-hline)
  7362. (end-of-line 2)
  7363. (if (looking-at "\n[ \t]*|-")
  7364. (progn (insert "\n|") (org-table-align))
  7365. (org-table-next-field))
  7366. (if same-column (org-table-goto-column col))))
  7367. (defun org-table-clean-line (s)
  7368. "Convert a table line S into a string with only \"|\" and space.
  7369. In particular, this does handle wide and invisible characters."
  7370. (if (string-match "^[ \t]*|-" s)
  7371. ;; It's a hline, just map the characters
  7372. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7373. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7374. (setq s (replace-match
  7375. (concat "|" (make-string (org-string-width (match-string 1 s))
  7376. ?\ ) "|")
  7377. t t s)))
  7378. s))
  7379. (defun org-table-kill-row ()
  7380. "Delete the current row or horizontal line from the table."
  7381. (interactive)
  7382. (if (not (org-at-table-p))
  7383. (error "Not at a table"))
  7384. (let ((col (current-column))
  7385. (dline (org-table-current-dline)))
  7386. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7387. (if (not (org-at-table-p)) (beginning-of-line 0))
  7388. (move-to-column col)
  7389. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7390. dline -1 dline)))
  7391. (defun org-table-sort-lines (with-case &optional sorting-type)
  7392. "Sort table lines according to the column at point.
  7393. The position of point indicates the column to be used for
  7394. sorting, and the range of lines is the range between the nearest
  7395. horizontal separator lines, or the entire table of no such lines
  7396. exist. If point is before the first column, you will be prompted
  7397. for the sorting column. If there is an active region, the mark
  7398. specifies the first line and the sorting column, while point
  7399. should be in the last line to be included into the sorting.
  7400. The command then prompts for the sorting type which can be
  7401. alphabetically, numerically, or by time (as given in a time stamp
  7402. in the field). Sorting in reverse order is also possible.
  7403. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7404. If SORTING-TYPE is specified when this function is called from a Lisp
  7405. program, no prompting will take place. SORTING-TYPE must be a character,
  7406. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7407. should be done in reverse order."
  7408. (interactive "P")
  7409. (let* ((thisline (org-current-line))
  7410. (thiscol (org-table-current-column))
  7411. beg end bcol ecol tend tbeg column lns pos)
  7412. (when (equal thiscol 0)
  7413. (if (interactive-p)
  7414. (setq thiscol
  7415. (string-to-number
  7416. (read-string "Use column N for sorting: ")))
  7417. (setq thiscol 1))
  7418. (org-table-goto-column thiscol))
  7419. (org-table-check-inside-data-field)
  7420. (if (org-region-active-p)
  7421. (progn
  7422. (setq beg (region-beginning) end (region-end))
  7423. (goto-char beg)
  7424. (setq column (org-table-current-column)
  7425. beg (point-at-bol))
  7426. (goto-char end)
  7427. (setq end (point-at-bol 2)))
  7428. (setq column (org-table-current-column)
  7429. pos (point)
  7430. tbeg (org-table-begin)
  7431. tend (org-table-end))
  7432. (if (re-search-backward org-table-hline-regexp tbeg t)
  7433. (setq beg (point-at-bol 2))
  7434. (goto-char tbeg)
  7435. (setq beg (point-at-bol 1)))
  7436. (goto-char pos)
  7437. (if (re-search-forward org-table-hline-regexp tend t)
  7438. (setq end (point-at-bol 1))
  7439. (goto-char tend)
  7440. (setq end (point-at-bol))))
  7441. (setq beg (move-marker (make-marker) beg)
  7442. end (move-marker (make-marker) end))
  7443. (untabify beg end)
  7444. (goto-char beg)
  7445. (org-table-goto-column column)
  7446. (skip-chars-backward "^|")
  7447. (setq bcol (current-column))
  7448. (org-table-goto-column (1+ column))
  7449. (skip-chars-backward "^|")
  7450. (setq ecol (1- (current-column)))
  7451. (org-table-goto-column column)
  7452. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7453. (org-split-string (buffer-substring beg end) "\n")))
  7454. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7455. (delete-region beg end)
  7456. (move-marker beg nil)
  7457. (move-marker end nil)
  7458. (insert (mapconcat 'cdr lns "\n") "\n")
  7459. (goto-line thisline)
  7460. (org-table-goto-column thiscol)
  7461. (message "%d lines sorted, based on column %d" (length lns) column)))
  7462. (defun org-table-cut-region (beg end)
  7463. "Copy region in table to the clipboard and blank all relevant fields."
  7464. (interactive "r")
  7465. (org-table-copy-region beg end 'cut))
  7466. (defun org-table-copy-region (beg end &optional cut)
  7467. "Copy rectangular region in table to clipboard.
  7468. A special clipboard is used which can only be accessed
  7469. with `org-table-paste-rectangle'."
  7470. (interactive "rP")
  7471. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7472. region cols
  7473. (rpl (if cut " " nil)))
  7474. (goto-char beg)
  7475. (org-table-check-inside-data-field)
  7476. (setq l01 (org-current-line)
  7477. c01 (org-table-current-column))
  7478. (goto-char end)
  7479. (org-table-check-inside-data-field)
  7480. (setq l02 (org-current-line)
  7481. c02 (org-table-current-column))
  7482. (setq l1 (min l01 l02) l2 (max l01 l02)
  7483. c1 (min c01 c02) c2 (max c01 c02))
  7484. (catch 'exit
  7485. (while t
  7486. (catch 'nextline
  7487. (if (> l1 l2) (throw 'exit t))
  7488. (goto-line l1)
  7489. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7490. (setq cols nil ic1 c1 ic2 c2)
  7491. (while (< ic1 (1+ ic2))
  7492. (push (org-table-get-field ic1 rpl) cols)
  7493. (setq ic1 (1+ ic1)))
  7494. (push (nreverse cols) region)
  7495. (setq l1 (1+ l1)))))
  7496. (setq org-table-clip (nreverse region))
  7497. (if cut (org-table-align))
  7498. org-table-clip))
  7499. (defun org-table-paste-rectangle ()
  7500. "Paste a rectangular region into a table.
  7501. The upper right corner ends up in the current field. All involved fields
  7502. will be overwritten. If the rectangle does not fit into the present table,
  7503. the table is enlarged as needed. The process ignores horizontal separator
  7504. lines."
  7505. (interactive)
  7506. (unless (and org-table-clip (listp org-table-clip))
  7507. (error "First cut/copy a region to paste!"))
  7508. (org-table-check-inside-data-field)
  7509. (let* ((clip org-table-clip)
  7510. (line (org-current-line))
  7511. (col (org-table-current-column))
  7512. (org-enable-table-editor t)
  7513. (org-table-automatic-realign nil)
  7514. c cols field)
  7515. (while (setq cols (pop clip))
  7516. (while (org-at-table-hline-p) (beginning-of-line 2))
  7517. (if (not (org-at-table-p))
  7518. (progn (end-of-line 0) (org-table-next-field)))
  7519. (setq c col)
  7520. (while (setq field (pop cols))
  7521. (org-table-goto-column c nil 'force)
  7522. (org-table-get-field nil field)
  7523. (setq c (1+ c)))
  7524. (beginning-of-line 2))
  7525. (goto-line line)
  7526. (org-table-goto-column col)
  7527. (org-table-align)))
  7528. (defun org-table-convert ()
  7529. "Convert from `org-mode' table to table.el and back.
  7530. Obviously, this only works within limits. When an Org-mode table is
  7531. converted to table.el, all horizontal separator lines get lost, because
  7532. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7533. A table.el table can be converted to an Org-mode table only if it does not
  7534. do row or column spanning. Multiline cells will become multiple cells.
  7535. Beware, Org-mode does not test if the table can be successfully converted - it
  7536. blindly applies a recipe that works for simple tables."
  7537. (interactive)
  7538. (require 'table)
  7539. (if (org-at-table.el-p)
  7540. ;; convert to Org-mode table
  7541. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7542. (end (move-marker (make-marker) (org-table-end t))))
  7543. (table-unrecognize-region beg end)
  7544. (goto-char beg)
  7545. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7546. (replace-match ""))
  7547. (goto-char beg))
  7548. (if (org-at-table-p)
  7549. ;; convert to table.el table
  7550. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7551. (end (move-marker (make-marker) (org-table-end))))
  7552. ;; first, get rid of all horizontal lines
  7553. (goto-char beg)
  7554. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7555. (replace-match ""))
  7556. ;; insert a hline before first
  7557. (goto-char beg)
  7558. (org-table-insert-hline 'above)
  7559. (beginning-of-line -1)
  7560. ;; insert a hline after each line
  7561. (while (progn (beginning-of-line 3) (< (point) end))
  7562. (org-table-insert-hline))
  7563. (goto-char beg)
  7564. (setq end (move-marker end (org-table-end)))
  7565. ;; replace "+" at beginning and ending of hlines
  7566. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7567. (replace-match "\\1+-"))
  7568. (goto-char beg)
  7569. (while (re-search-forward "-|[ \t]*$" end t)
  7570. (replace-match "-+"))
  7571. (goto-char beg)))))
  7572. (defun org-table-wrap-region (arg)
  7573. "Wrap several fields in a column like a paragraph.
  7574. This is useful if you'd like to spread the contents of a field over several
  7575. lines, in order to keep the table compact.
  7576. If there is an active region, and both point and mark are in the same column,
  7577. the text in the column is wrapped to minimum width for the given number of
  7578. lines. Generally, this makes the table more compact. A prefix ARG may be
  7579. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7580. formats the selected text to two lines. If the region was longer than two
  7581. lines, the remaining lines remain empty. A negative prefix argument reduces
  7582. the current number of lines by that amount. The wrapped text is pasted back
  7583. into the table. If you formatted it to more lines than it was before, fields
  7584. further down in the table get overwritten - so you might need to make space in
  7585. the table first.
  7586. If there is no region, the current field is split at the cursor position and
  7587. the text fragment to the right of the cursor is prepended to the field one
  7588. line down.
  7589. If there is no region, but you specify a prefix ARG, the current field gets
  7590. blank, and the content is appended to the field above."
  7591. (interactive "P")
  7592. (org-table-check-inside-data-field)
  7593. (if (org-region-active-p)
  7594. ;; There is a region: fill as a paragraph
  7595. (let* ((beg (region-beginning))
  7596. (cline (save-excursion (goto-char beg) (org-current-line)))
  7597. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7598. nlines)
  7599. (org-table-cut-region (region-beginning) (region-end))
  7600. (if (> (length (car org-table-clip)) 1)
  7601. (error "Region must be limited to single column"))
  7602. (setq nlines (if arg
  7603. (if (< arg 1)
  7604. (+ (length org-table-clip) arg)
  7605. arg)
  7606. (length org-table-clip)))
  7607. (setq org-table-clip
  7608. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7609. nil nlines)))
  7610. (goto-line cline)
  7611. (org-table-goto-column ccol)
  7612. (org-table-paste-rectangle))
  7613. ;; No region, split the current field at point
  7614. (if arg
  7615. ;; combine with field above
  7616. (let ((s (org-table-blank-field))
  7617. (col (org-table-current-column)))
  7618. (beginning-of-line 0)
  7619. (while (org-at-table-hline-p) (beginning-of-line 0))
  7620. (org-table-goto-column col)
  7621. (skip-chars-forward "^|")
  7622. (skip-chars-backward " ")
  7623. (insert " " (org-trim s))
  7624. (org-table-align))
  7625. ;; split field
  7626. (when (looking-at "\\([^|]+\\)+|")
  7627. (let ((s (match-string 1)))
  7628. (replace-match " |")
  7629. (goto-char (match-beginning 0))
  7630. (org-table-next-row)
  7631. (insert (org-trim s) " ")
  7632. (org-table-align))))))
  7633. (defvar org-field-marker nil)
  7634. (defun org-table-edit-field (arg)
  7635. "Edit table field in a different window.
  7636. This is mainly useful for fields that contain hidden parts.
  7637. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7638. it can be edited in place."
  7639. (interactive "P")
  7640. (if arg
  7641. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7642. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7643. (remove-text-properties b e '(org-cwidth t invisible t
  7644. display t intangible t))
  7645. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7646. (font-lock-fontify-block)))
  7647. (let ((pos (move-marker (make-marker) (point)))
  7648. (field (org-table-get-field))
  7649. (cw (current-window-configuration))
  7650. p)
  7651. (org-switch-to-buffer-other-window "*Org tmp*")
  7652. (erase-buffer)
  7653. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7654. (let ((org-inhibit-startup t)) (org-mode))
  7655. (goto-char (setq p (point-max)))
  7656. (insert (org-trim field))
  7657. (remove-text-properties p (point-max)
  7658. '(invisible t org-cwidth t display t
  7659. intangible t))
  7660. (goto-char p)
  7661. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7662. (org-set-local 'org-window-configuration cw)
  7663. (org-set-local 'org-field-marker pos)
  7664. (message "Edit and finish with C-c C-c"))))
  7665. (defun org-table-finish-edit-field ()
  7666. "Finish editing a table data field.
  7667. Remove all newline characters, insert the result into the table, realign
  7668. the table and kill the editing buffer."
  7669. (let ((pos org-field-marker)
  7670. (cw org-window-configuration)
  7671. (cb (current-buffer))
  7672. text)
  7673. (goto-char (point-min))
  7674. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7675. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7676. (replace-match " "))
  7677. (setq text (org-trim (buffer-string)))
  7678. (set-window-configuration cw)
  7679. (kill-buffer cb)
  7680. (select-window (get-buffer-window (marker-buffer pos)))
  7681. (goto-char pos)
  7682. (move-marker pos nil)
  7683. (org-table-check-inside-data-field)
  7684. (org-table-get-field nil text)
  7685. (org-table-align)
  7686. (message "New field value inserted")))
  7687. (defun org-trim (s)
  7688. "Remove whitespace at beginning and end of string."
  7689. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7690. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7691. s)
  7692. (defun org-wrap (string &optional width lines)
  7693. "Wrap string to either a number of lines, or a width in characters.
  7694. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7695. that costs. If there is a word longer than WIDTH, the text is actually
  7696. wrapped to the length of that word.
  7697. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7698. many lines, whatever width that takes.
  7699. The return value is a list of lines, without newlines at the end."
  7700. (let* ((words (org-split-string string "[ \t\n]+"))
  7701. (maxword (apply 'max (mapcar 'org-string-width words)))
  7702. w ll)
  7703. (cond (width
  7704. (org-do-wrap words (max maxword width)))
  7705. (lines
  7706. (setq w maxword)
  7707. (setq ll (org-do-wrap words maxword))
  7708. (if (<= (length ll) lines)
  7709. ll
  7710. (setq ll words)
  7711. (while (> (length ll) lines)
  7712. (setq w (1+ w))
  7713. (setq ll (org-do-wrap words w)))
  7714. ll))
  7715. (t (error "Cannot wrap this")))))
  7716. (defun org-do-wrap (words width)
  7717. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7718. (let (lines line)
  7719. (while words
  7720. (setq line (pop words))
  7721. (while (and words (< (+ (length line) (length (car words))) width))
  7722. (setq line (concat line " " (pop words))))
  7723. (setq lines (push line lines)))
  7724. (nreverse lines)))
  7725. (defun org-split-string (string &optional separators)
  7726. "Splits STRING into substrings at SEPARATORS.
  7727. No empty strings are returned if there are matches at the beginning
  7728. and end of string."
  7729. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7730. (start 0)
  7731. notfirst
  7732. (list nil))
  7733. (while (and (string-match rexp string
  7734. (if (and notfirst
  7735. (= start (match-beginning 0))
  7736. (< start (length string)))
  7737. (1+ start) start))
  7738. (< (match-beginning 0) (length string)))
  7739. (setq notfirst t)
  7740. (or (eq (match-beginning 0) 0)
  7741. (and (eq (match-beginning 0) (match-end 0))
  7742. (eq (match-beginning 0) start))
  7743. (setq list
  7744. (cons (substring string start (match-beginning 0))
  7745. list)))
  7746. (setq start (match-end 0)))
  7747. (or (eq start (length string))
  7748. (setq list
  7749. (cons (substring string start)
  7750. list)))
  7751. (nreverse list)))
  7752. (defun org-table-map-tables (function)
  7753. "Apply FUNCTION to the start of all tables in the buffer."
  7754. (save-excursion
  7755. (save-restriction
  7756. (widen)
  7757. (goto-char (point-min))
  7758. (while (re-search-forward org-table-any-line-regexp nil t)
  7759. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7760. (beginning-of-line 1)
  7761. (if (looking-at org-table-line-regexp)
  7762. (save-excursion (funcall function)))
  7763. (re-search-forward org-table-any-border-regexp nil 1))))
  7764. (message "Mapping tables: done"))
  7765. (defvar org-timecnt) ; dynamically scoped parameter
  7766. (defun org-table-sum (&optional beg end nlast)
  7767. "Sum numbers in region of current table column.
  7768. The result will be displayed in the echo area, and will be available
  7769. as kill to be inserted with \\[yank].
  7770. If there is an active region, it is interpreted as a rectangle and all
  7771. numbers in that rectangle will be summed. If there is no active
  7772. region and point is located in a table column, sum all numbers in that
  7773. column.
  7774. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7775. numbers are assumed to be times as well (in decimal hours) and the
  7776. numbers are added as such.
  7777. If NLAST is a number, only the NLAST fields will actually be summed."
  7778. (interactive)
  7779. (save-excursion
  7780. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7781. (cond
  7782. ((and beg end)) ; beg and end given explicitly
  7783. ((org-region-active-p)
  7784. (setq beg (region-beginning) end (region-end)))
  7785. (t
  7786. (setq col (org-table-current-column))
  7787. (goto-char (org-table-begin))
  7788. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7789. (error "No table data"))
  7790. (org-table-goto-column col)
  7791. (setq beg (point))
  7792. (goto-char (org-table-end))
  7793. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7794. (error "No table data"))
  7795. (org-table-goto-column col)
  7796. (setq end (point))))
  7797. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7798. (items1 (cond ((not nlast) items)
  7799. ((>= nlast (length items)) items)
  7800. (t (setq items (reverse items))
  7801. (setcdr (nthcdr (1- nlast) items) nil)
  7802. (nreverse items))))
  7803. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7804. items1)))
  7805. (res (apply '+ numbers))
  7806. (sres (if (= org-timecnt 0)
  7807. (format "%g" res)
  7808. (setq diff (* 3600 res)
  7809. h (floor (/ diff 3600)) diff (mod diff 3600)
  7810. m (floor (/ diff 60)) diff (mod diff 60)
  7811. s diff)
  7812. (format "%d:%02d:%02d" h m s))))
  7813. (kill-new sres)
  7814. (if (interactive-p)
  7815. (message "%s"
  7816. (substitute-command-keys
  7817. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7818. (length numbers) sres))))
  7819. sres))))
  7820. (defun org-table-get-number-for-summing (s)
  7821. (let (n)
  7822. (if (string-match "^ *|? *" s)
  7823. (setq s (replace-match "" nil nil s)))
  7824. (if (string-match " *|? *$" s)
  7825. (setq s (replace-match "" nil nil s)))
  7826. (setq n (string-to-number s))
  7827. (cond
  7828. ((and (string-match "0" s)
  7829. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7830. ((string-match "\\`[ \t]+\\'" s) nil)
  7831. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7832. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7833. (m (string-to-number (or (match-string 2 s) "0")))
  7834. (s (string-to-number (or (match-string 4 s) "0"))))
  7835. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7836. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7837. ((equal n 0) nil)
  7838. (t n))))
  7839. (defun org-table-current-field-formula (&optional key noerror)
  7840. "Return the formula active for the current field.
  7841. Assumes that specials are in place.
  7842. If KEY is given, return the key to this formula.
  7843. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7844. (let* ((name (car (rassoc (list (org-current-line)
  7845. (org-table-current-column))
  7846. org-table-named-field-locations)))
  7847. (col (org-table-current-column))
  7848. (scol (int-to-string col))
  7849. (ref (format "@%d$%d" (org-table-current-dline) col))
  7850. (stored-list (org-table-get-stored-formulas noerror))
  7851. (ass (or (assoc name stored-list)
  7852. (assoc ref stored-list)
  7853. (assoc scol stored-list))))
  7854. (if key
  7855. (car ass)
  7856. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7857. (cdr ass))))))
  7858. (defun org-table-get-formula (&optional equation named)
  7859. "Read a formula from the minibuffer, offer stored formula as default.
  7860. When NAMED is non-nil, look for a named equation."
  7861. (let* ((stored-list (org-table-get-stored-formulas))
  7862. (name (car (rassoc (list (org-current-line)
  7863. (org-table-current-column))
  7864. org-table-named-field-locations)))
  7865. (ref (format "@%d$%d" (org-table-current-dline)
  7866. (org-table-current-column)))
  7867. (refass (assoc ref stored-list))
  7868. (scol (if named
  7869. (if name name ref)
  7870. (int-to-string (org-table-current-column))))
  7871. (dummy (and (or name refass) (not named)
  7872. (not (y-or-n-p "Replace field formula with column formula? " ))
  7873. (error "Abort")))
  7874. (name (or name ref))
  7875. (org-table-may-need-update nil)
  7876. (stored (cdr (assoc scol stored-list)))
  7877. (eq (cond
  7878. ((and stored equation (string-match "^ *=? *$" equation))
  7879. stored)
  7880. ((stringp equation)
  7881. equation)
  7882. (t (org-table-formula-from-user
  7883. (read-string
  7884. (org-table-formula-to-user
  7885. (format "%s formula %s%s="
  7886. (if named "Field" "Column")
  7887. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7888. scol))
  7889. (if stored (org-table-formula-to-user stored) "")
  7890. 'org-table-formula-history
  7891. )))))
  7892. mustsave)
  7893. (when (not (string-match "\\S-" eq))
  7894. ;; remove formula
  7895. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7896. (org-table-store-formulas stored-list)
  7897. (error "Formula removed"))
  7898. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7899. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7900. (if (and name (not named))
  7901. ;; We set the column equation, delete the named one.
  7902. (setq stored-list (delq (assoc name stored-list) stored-list)
  7903. mustsave t))
  7904. (if stored
  7905. (setcdr (assoc scol stored-list) eq)
  7906. (setq stored-list (cons (cons scol eq) stored-list)))
  7907. (if (or mustsave (not (equal stored eq)))
  7908. (org-table-store-formulas stored-list))
  7909. eq))
  7910. (defun org-table-store-formulas (alist)
  7911. "Store the list of formulas below the current table."
  7912. (setq alist (sort alist 'org-table-formula-less-p))
  7913. (save-excursion
  7914. (goto-char (org-table-end))
  7915. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7916. (progn
  7917. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7918. (goto-char (match-beginning 2))
  7919. (delete-region (match-beginning 2) (match-end 0)))
  7920. (insert "#+TBLFM:"))
  7921. (insert " "
  7922. (mapconcat (lambda (x)
  7923. (concat
  7924. (if (equal (string-to-char (car x)) ?@) "" "$")
  7925. (car x) "=" (cdr x)))
  7926. alist "::")
  7927. "\n")))
  7928. (defsubst org-table-formula-make-cmp-string (a)
  7929. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7930. (concat
  7931. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7932. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7933. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7934. (defun org-table-formula-less-p (a b)
  7935. "Compare two formulas for sorting."
  7936. (let ((as (org-table-formula-make-cmp-string (car a)))
  7937. (bs (org-table-formula-make-cmp-string (car b))))
  7938. (and as bs (string< as bs))))
  7939. (defun org-table-get-stored-formulas (&optional noerror)
  7940. "Return an alist with the stored formulas directly after current table."
  7941. (interactive)
  7942. (let (scol eq eq-alist strings string seen)
  7943. (save-excursion
  7944. (goto-char (org-table-end))
  7945. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7946. (setq strings (org-split-string (match-string 2) " *:: *"))
  7947. (while (setq string (pop strings))
  7948. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7949. (setq scol (if (match-end 2)
  7950. (match-string 2 string)
  7951. (match-string 1 string))
  7952. eq (match-string 3 string)
  7953. eq-alist (cons (cons scol eq) eq-alist))
  7954. (if (member scol seen)
  7955. (if noerror
  7956. (progn
  7957. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7958. (ding)
  7959. (sit-for 2))
  7960. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7961. (push scol seen))))))
  7962. (nreverse eq-alist)))
  7963. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7964. "Modify the equations after the table structure has been edited.
  7965. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7966. For all numbers larger than LIMIT, shift them by DELTA."
  7967. (save-excursion
  7968. (goto-char (org-table-end))
  7969. (when (looking-at "#\\+TBLFM:")
  7970. (let ((re (concat key "\\([0-9]+\\)"))
  7971. (re2
  7972. (when remove
  7973. (if (equal key "$")
  7974. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7975. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7976. s n a)
  7977. (when remove
  7978. (while (re-search-forward re2 (point-at-eol) t)
  7979. (replace-match "")))
  7980. (while (re-search-forward re (point-at-eol) t)
  7981. (setq s (match-string 1) n (string-to-number s))
  7982. (cond
  7983. ((setq a (assoc s replace))
  7984. (replace-match (concat key (cdr a)) t t))
  7985. ((and limit (> n limit))
  7986. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7987. (defun org-table-get-specials ()
  7988. "Get the column names and local parameters for this table."
  7989. (save-excursion
  7990. (let ((beg (org-table-begin)) (end (org-table-end))
  7991. names name fields fields1 field cnt
  7992. c v l line col types dlines hlines)
  7993. (setq org-table-column-names nil
  7994. org-table-local-parameters nil
  7995. org-table-named-field-locations nil
  7996. org-table-current-begin-line nil
  7997. org-table-current-begin-pos nil
  7998. org-table-current-line-types nil)
  7999. (goto-char beg)
  8000. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8001. (setq names (org-split-string (match-string 1) " *| *")
  8002. cnt 1)
  8003. (while (setq name (pop names))
  8004. (setq cnt (1+ cnt))
  8005. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8006. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8007. (setq org-table-column-names (nreverse org-table-column-names))
  8008. (setq org-table-column-name-regexp
  8009. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8010. (goto-char beg)
  8011. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8012. (setq fields (org-split-string (match-string 1) " *| *"))
  8013. (while (setq field (pop fields))
  8014. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8015. (push (cons (match-string 1 field) (match-string 2 field))
  8016. org-table-local-parameters))))
  8017. (goto-char beg)
  8018. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8019. (setq c (match-string 1)
  8020. fields (org-split-string (match-string 2) " *| *"))
  8021. (save-excursion
  8022. (beginning-of-line (if (equal c "_") 2 0))
  8023. (setq line (org-current-line) col 1)
  8024. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8025. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8026. (while (and fields1 (setq field (pop fields)))
  8027. (setq v (pop fields1) col (1+ col))
  8028. (when (and (stringp field) (stringp v)
  8029. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8030. (push (cons field v) org-table-local-parameters)
  8031. (push (list field line col) org-table-named-field-locations))))
  8032. ;; Analyse the line types
  8033. (goto-char beg)
  8034. (setq org-table-current-begin-line (org-current-line)
  8035. org-table-current-begin-pos (point)
  8036. l org-table-current-begin-line)
  8037. (while (looking-at "[ \t]*|\\(-\\)?")
  8038. (push (if (match-end 1) 'hline 'dline) types)
  8039. (if (match-end 1) (push l hlines) (push l dlines))
  8040. (beginning-of-line 2)
  8041. (setq l (1+ l)))
  8042. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8043. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8044. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8045. (defun org-table-maybe-eval-formula ()
  8046. "Check if the current field starts with \"=\" or \":=\".
  8047. If yes, store the formula and apply it."
  8048. ;; We already know we are in a table. Get field will only return a formula
  8049. ;; when appropriate. It might return a separator line, but no problem.
  8050. (when org-table-formula-evaluate-inline
  8051. (let* ((field (org-trim (or (org-table-get-field) "")))
  8052. named eq)
  8053. (when (string-match "^:?=\\(.*\\)" field)
  8054. (setq named (equal (string-to-char field) ?:)
  8055. eq (match-string 1 field))
  8056. (if (or (fboundp 'calc-eval)
  8057. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8058. (org-table-eval-formula (if named '(4) nil)
  8059. (org-table-formula-from-user eq))
  8060. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8061. (defvar org-recalc-commands nil
  8062. "List of commands triggering the recalculation of a line.
  8063. Will be filled automatically during use.")
  8064. (defvar org-recalc-marks
  8065. '((" " . "Unmarked: no special line, no automatic recalculation")
  8066. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8067. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8068. ("!" . "Column name definition line. Reference in formula as $name.")
  8069. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8070. ("_" . "Names for values in row below this one.")
  8071. ("^" . "Names for values in row above this one.")))
  8072. (defun org-table-rotate-recalc-marks (&optional newchar)
  8073. "Rotate the recalculation mark in the first column.
  8074. If in any row, the first field is not consistent with a mark,
  8075. insert a new column for the markers.
  8076. When there is an active region, change all the lines in the region,
  8077. after prompting for the marking character.
  8078. After each change, a message will be displayed indicating the meaning
  8079. of the new mark."
  8080. (interactive)
  8081. (unless (org-at-table-p) (error "Not at a table"))
  8082. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8083. (beg (org-table-begin))
  8084. (end (org-table-end))
  8085. (l (org-current-line))
  8086. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8087. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8088. (have-col
  8089. (save-excursion
  8090. (goto-char beg)
  8091. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8092. (col (org-table-current-column))
  8093. (forcenew (car (assoc newchar org-recalc-marks)))
  8094. epos new)
  8095. (when l1
  8096. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8097. (setq newchar (char-to-string (read-char-exclusive))
  8098. forcenew (car (assoc newchar org-recalc-marks))))
  8099. (if (and newchar (not forcenew))
  8100. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8101. newchar))
  8102. (if l1 (goto-line l1))
  8103. (save-excursion
  8104. (beginning-of-line 1)
  8105. (unless (looking-at org-table-dataline-regexp)
  8106. (error "Not at a table data line")))
  8107. (unless have-col
  8108. (org-table-goto-column 1)
  8109. (org-table-insert-column)
  8110. (org-table-goto-column (1+ col)))
  8111. (setq epos (point-at-eol))
  8112. (save-excursion
  8113. (beginning-of-line 1)
  8114. (org-table-get-field
  8115. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8116. (concat " "
  8117. (setq new (or forcenew
  8118. (cadr (member (match-string 1) marks))))
  8119. " ")
  8120. " # ")))
  8121. (if (and l1 l2)
  8122. (progn
  8123. (goto-line l1)
  8124. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8125. (and (looking-at org-table-dataline-regexp)
  8126. (org-table-get-field 1 (concat " " new " "))))
  8127. (goto-line l1)))
  8128. (if (not (= epos (point-at-eol))) (org-table-align))
  8129. (goto-line l)
  8130. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  8131. (defun org-table-maybe-recalculate-line ()
  8132. "Recompute the current line if marked for it, and if we haven't just done it."
  8133. (interactive)
  8134. (and org-table-allow-automatic-line-recalculation
  8135. (not (and (memq last-command org-recalc-commands)
  8136. (equal org-last-recalc-line (org-current-line))))
  8137. (save-excursion (beginning-of-line 1)
  8138. (looking-at org-table-auto-recalculate-regexp))
  8139. (org-table-recalculate) t))
  8140. (defvar org-table-formula-debug nil
  8141. "Non-nil means, debug table formulas.
  8142. When nil, simply write \"#ERROR\" in corrupted fields.")
  8143. (make-variable-buffer-local 'org-table-formula-debug)
  8144. (defvar modes)
  8145. (defsubst org-set-calc-mode (var &optional value)
  8146. (if (stringp var)
  8147. (setq var (assoc var '(("D" calc-angle-mode deg)
  8148. ("R" calc-angle-mode rad)
  8149. ("F" calc-prefer-frac t)
  8150. ("S" calc-symbolic-mode t)))
  8151. value (nth 2 var) var (nth 1 var)))
  8152. (if (memq var modes)
  8153. (setcar (cdr (memq var modes)) value)
  8154. (cons var (cons value modes)))
  8155. modes)
  8156. (defun org-table-eval-formula (&optional arg equation
  8157. suppress-align suppress-const
  8158. suppress-store suppress-analysis)
  8159. "Replace the table field value at the cursor by the result of a calculation.
  8160. This function makes use of Dave Gillespie's Calc package, in my view the
  8161. most exciting program ever written for GNU Emacs. So you need to have Calc
  8162. installed in order to use this function.
  8163. In a table, this command replaces the value in the current field with the
  8164. result of a formula. It also installs the formula as the \"current\" column
  8165. formula, by storing it in a special line below the table. When called
  8166. with a `C-u' prefix, the current field must ba a named field, and the
  8167. formula is installed as valid in only this specific field.
  8168. When called with two `C-u' prefixes, insert the active equation
  8169. for the field back into the current field, so that it can be
  8170. edited there. This is useful in order to use \\[org-table-show-reference]
  8171. to check the referenced fields.
  8172. When called, the command first prompts for a formula, which is read in
  8173. the minibuffer. Previously entered formulas are available through the
  8174. history list, and the last used formula is offered as a default.
  8175. These stored formulas are adapted correctly when moving, inserting, or
  8176. deleting columns with the corresponding commands.
  8177. The formula can be any algebraic expression understood by the Calc package.
  8178. For details, see the Org-mode manual.
  8179. This function can also be called from Lisp programs and offers
  8180. additional arguments: EQUATION can be the formula to apply. If this
  8181. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8182. used to speed-up recursive calls by by-passing unnecessary aligns.
  8183. SUPPRESS-CONST suppresses the interpretation of constants in the
  8184. formula, assuming that this has been done already outside the function.
  8185. SUPPRESS-STORE means the formula should not be stored, either because
  8186. it is already stored, or because it is a modified equation that should
  8187. not overwrite the stored one."
  8188. (interactive "P")
  8189. (org-table-check-inside-data-field)
  8190. (or suppress-analysis (org-table-get-specials))
  8191. (if (equal arg '(16))
  8192. (let ((eq (org-table-current-field-formula)))
  8193. (or eq (error "No equation active for current field"))
  8194. (org-table-get-field nil eq)
  8195. (org-table-align)
  8196. (setq org-table-may-need-update t))
  8197. (let* (fields
  8198. (ndown (if (integerp arg) arg 1))
  8199. (org-table-automatic-realign nil)
  8200. (case-fold-search nil)
  8201. (down (> ndown 1))
  8202. (formula (if (and equation suppress-store)
  8203. equation
  8204. (org-table-get-formula equation (equal arg '(4)))))
  8205. (n0 (org-table-current-column))
  8206. (modes (copy-sequence org-calc-default-modes))
  8207. (numbers nil) ; was a variable, now fixed default
  8208. (keep-empty nil)
  8209. n form form0 bw fmt x ev orig c lispp literal)
  8210. ;; Parse the format string. Since we have a lot of modes, this is
  8211. ;; a lot of work. However, I think calc still uses most of the time.
  8212. (if (string-match ";" formula)
  8213. (let ((tmp (org-split-string formula ";")))
  8214. (setq formula (car tmp)
  8215. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8216. (nth 1 tmp)))
  8217. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8218. (setq c (string-to-char (match-string 1 fmt))
  8219. n (string-to-number (match-string 2 fmt)))
  8220. (if (= c ?p)
  8221. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8222. (setq modes (org-set-calc-mode
  8223. 'calc-float-format
  8224. (list (cdr (assoc c '((?n . float) (?f . fix)
  8225. (?s . sci) (?e . eng))))
  8226. n))))
  8227. (setq fmt (replace-match "" t t fmt)))
  8228. (if (string-match "[NT]" fmt)
  8229. (setq numbers (equal (match-string 0 fmt) "N")
  8230. fmt (replace-match "" t t fmt)))
  8231. (if (string-match "L" fmt)
  8232. (setq literal t
  8233. fmt (replace-match "" t t fmt)))
  8234. (if (string-match "E" fmt)
  8235. (setq keep-empty t
  8236. fmt (replace-match "" t t fmt)))
  8237. (while (string-match "[DRFS]" fmt)
  8238. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8239. (setq fmt (replace-match "" t t fmt)))
  8240. (unless (string-match "\\S-" fmt)
  8241. (setq fmt nil))))
  8242. (if (and (not suppress-const) org-table-formula-use-constants)
  8243. (setq formula (org-table-formula-substitute-names formula)))
  8244. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8245. (while (> ndown 0)
  8246. (setq fields (org-split-string
  8247. (org-no-properties
  8248. (buffer-substring (point-at-bol) (point-at-eol)))
  8249. " *| *"))
  8250. (if (eq numbers t)
  8251. (setq fields (mapcar
  8252. (lambda (x) (number-to-string (string-to-number x)))
  8253. fields)))
  8254. (setq ndown (1- ndown))
  8255. (setq form (copy-sequence formula)
  8256. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8257. (if (and lispp literal) (setq lispp 'literal))
  8258. ;; Check for old vertical references
  8259. (setq form (org-rewrite-old-row-references form))
  8260. ;; Insert complex ranges
  8261. (while (string-match org-table-range-regexp form)
  8262. (setq form
  8263. (replace-match
  8264. (save-match-data
  8265. (org-table-make-reference
  8266. (org-table-get-range (match-string 0 form) nil n0)
  8267. keep-empty numbers lispp))
  8268. t t form)))
  8269. ;; Insert simple ranges
  8270. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8271. (setq form
  8272. (replace-match
  8273. (save-match-data
  8274. (org-table-make-reference
  8275. (org-sublist
  8276. fields (string-to-number (match-string 1 form))
  8277. (string-to-number (match-string 2 form)))
  8278. keep-empty numbers lispp))
  8279. t t form)))
  8280. (setq form0 form)
  8281. ;; Insert the references to fields in same row
  8282. (while (string-match "\\$\\([0-9]+\\)" form)
  8283. (setq n (string-to-number (match-string 1 form))
  8284. x (nth (1- (if (= n 0) n0 n)) fields))
  8285. (unless x (error "Invalid field specifier \"%s\""
  8286. (match-string 0 form)))
  8287. (setq form (replace-match
  8288. (save-match-data
  8289. (org-table-make-reference x nil numbers lispp))
  8290. t t form)))
  8291. (if lispp
  8292. (setq ev (condition-case nil
  8293. (eval (eval (read form)))
  8294. (error "#ERROR"))
  8295. ev (if (numberp ev) (number-to-string ev) ev))
  8296. (or (fboundp 'calc-eval)
  8297. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8298. (setq ev (calc-eval (cons form modes)
  8299. (if numbers 'num))))
  8300. (when org-table-formula-debug
  8301. (with-output-to-temp-buffer "*Substitution History*"
  8302. (princ (format "Substitution history of formula
  8303. Orig: %s
  8304. $xyz-> %s
  8305. @r$c-> %s
  8306. $1-> %s\n" orig formula form0 form))
  8307. (if (listp ev)
  8308. (princ (format " %s^\nError: %s"
  8309. (make-string (car ev) ?\-) (nth 1 ev)))
  8310. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8311. ev (or fmt "NONE")
  8312. (if fmt (format fmt (string-to-number ev)) ev)))))
  8313. (setq bw (get-buffer-window "*Substitution History*"))
  8314. (shrink-window-if-larger-than-buffer bw)
  8315. (unless (and (interactive-p) (not ndown))
  8316. (unless (let (inhibit-redisplay)
  8317. (y-or-n-p "Debugging Formula. Continue to next? "))
  8318. (org-table-align)
  8319. (error "Abort"))
  8320. (delete-window bw)
  8321. (message "")))
  8322. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8323. (org-table-justify-field-maybe
  8324. (if fmt (format fmt (string-to-number ev)) ev))
  8325. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8326. (call-interactively 'org-return)
  8327. (setq ndown 0)))
  8328. (and down (org-table-maybe-recalculate-line))
  8329. (or suppress-align (and org-table-may-need-update
  8330. (org-table-align))))))
  8331. (defun org-table-put-field-property (prop value)
  8332. (save-excursion
  8333. (put-text-property (progn (skip-chars-backward "^|") (point))
  8334. (progn (skip-chars-forward "^|") (point))
  8335. prop value)))
  8336. (defun org-table-get-range (desc &optional tbeg col highlight)
  8337. "Get a calc vector from a column, accorting to descriptor DESC.
  8338. Optional arguments TBEG and COL can give the beginning of the table and
  8339. the current column, to avoid unnecessary parsing.
  8340. HIGHLIGHT means, just highlight the range."
  8341. (if (not (equal (string-to-char desc) ?@))
  8342. (setq desc (concat "@" desc)))
  8343. (save-excursion
  8344. (or tbeg (setq tbeg (org-table-begin)))
  8345. (or col (setq col (org-table-current-column)))
  8346. (let ((thisline (org-current-line))
  8347. beg end c1 c2 r1 r2 rangep tmp)
  8348. (unless (string-match org-table-range-regexp desc)
  8349. (error "Invalid table range specifier `%s'" desc))
  8350. (setq rangep (match-end 3)
  8351. r1 (and (match-end 1) (match-string 1 desc))
  8352. r2 (and (match-end 4) (match-string 4 desc))
  8353. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8354. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8355. (and c1 (setq c1 (+ (string-to-number c1)
  8356. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8357. (and c2 (setq c2 (+ (string-to-number c2)
  8358. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8359. (if (equal r1 "") (setq r1 nil))
  8360. (if (equal r2 "") (setq r2 nil))
  8361. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8362. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8363. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8364. (if (not r1) (setq r1 thisline))
  8365. (if (not r2) (setq r2 thisline))
  8366. (if (not c1) (setq c1 col))
  8367. (if (not c2) (setq c2 col))
  8368. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8369. ;; just one field
  8370. (progn
  8371. (goto-line r1)
  8372. (while (not (looking-at org-table-dataline-regexp))
  8373. (beginning-of-line 2))
  8374. (prog1 (org-trim (org-table-get-field c1))
  8375. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8376. ;; A range, return a vector
  8377. ;; First sort the numbers to get a regular ractangle
  8378. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8379. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8380. (goto-line r1)
  8381. (while (not (looking-at org-table-dataline-regexp))
  8382. (beginning-of-line 2))
  8383. (org-table-goto-column c1)
  8384. (setq beg (point))
  8385. (goto-line r2)
  8386. (while (not (looking-at org-table-dataline-regexp))
  8387. (beginning-of-line 0))
  8388. (org-table-goto-column c2)
  8389. (setq end (point))
  8390. (if highlight
  8391. (org-table-highlight-rectangle
  8392. beg (progn (skip-chars-forward "^|\n") (point))))
  8393. ;; return string representation of calc vector
  8394. (mapcar 'org-trim
  8395. (apply 'append (org-table-copy-region beg end)))))))
  8396. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8397. "Analyze descriptor DESC and retrieve the corresponding line number.
  8398. The cursor is currently in line CLINE, the table begins in line BLINE,
  8399. and TABLE is a vector with line types."
  8400. (if (string-match "^[0-9]+$" desc)
  8401. (aref org-table-dlines (string-to-number desc))
  8402. (setq cline (or cline (org-current-line))
  8403. bline (or bline org-table-current-begin-line)
  8404. table (or table org-table-current-line-types))
  8405. (if (or
  8406. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8407. ;; 1 2 3 4 5 6
  8408. (and (not (match-end 3)) (not (match-end 6)))
  8409. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8410. (error "invalid row descriptor `%s'" desc))
  8411. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8412. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8413. (odir (and (match-end 5) (match-string 5 desc)))
  8414. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8415. (i (- cline bline))
  8416. (rel (and (match-end 6)
  8417. (or (and (match-end 1) (not (match-end 3)))
  8418. (match-end 5)))))
  8419. (if (and hn (not hdir))
  8420. (progn
  8421. (setq i 0 hdir "+")
  8422. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8423. (if (and (not hn) on (not odir))
  8424. (error "should never happen");;(aref org-table-dlines on)
  8425. (if (and hn (> hn 0))
  8426. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8427. (if on
  8428. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8429. (+ bline i)))))
  8430. (defun org-find-row-type (table i type backwards relative n)
  8431. (let ((l (length table)))
  8432. (while (> n 0)
  8433. (while (and (setq i (+ i (if backwards -1 1)))
  8434. (>= i 0) (< i l)
  8435. (not (eq (aref table i) type))
  8436. (if (and relative (eq (aref table i) 'hline))
  8437. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8438. t)))
  8439. (setq n (1- n)))
  8440. (if (or (< i 0) (>= i l))
  8441. (error "Row descriptior leads outside table")
  8442. i)))
  8443. (defun org-rewrite-old-row-references (s)
  8444. (if (string-match "&[-+0-9I]" s)
  8445. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8446. s))
  8447. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8448. "Convert list ELEMENTS to something appropriate to insert into formula.
  8449. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8450. NUMBERS indicates that everything should be converted to numbers.
  8451. LISPP means to return something appropriate for a Lisp list."
  8452. (if (stringp elements) ; just a single val
  8453. (if lispp
  8454. (if (eq lispp 'literal)
  8455. elements
  8456. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8457. (if (equal elements "") (setq elements "0"))
  8458. (if numbers (number-to-string (string-to-number elements)) elements))
  8459. (unless keep-empty
  8460. (setq elements
  8461. (delq nil
  8462. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8463. elements))))
  8464. (setq elements (or elements '("0")))
  8465. (if lispp
  8466. (mapconcat
  8467. (lambda (x)
  8468. (if (eq lispp 'literal)
  8469. x
  8470. (prin1-to-string (if numbers (string-to-number x) x))))
  8471. elements " ")
  8472. (concat "[" (mapconcat
  8473. (lambda (x)
  8474. (if numbers (number-to-string (string-to-number x)) x))
  8475. elements
  8476. ",") "]"))))
  8477. (defun org-table-recalculate (&optional all noalign)
  8478. "Recalculate the current table line by applying all stored formulas.
  8479. With prefix arg ALL, do this for all lines in the table."
  8480. (interactive "P")
  8481. (or (memq this-command org-recalc-commands)
  8482. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8483. (unless (org-at-table-p) (error "Not at a table"))
  8484. (if (equal all '(16))
  8485. (org-table-iterate)
  8486. (org-table-get-specials)
  8487. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8488. (lambda (a b) (string< (car a) (car b)))))
  8489. (inhibit-redisplay (not debug-on-error))
  8490. (line-re org-table-dataline-regexp)
  8491. (thisline (org-current-line))
  8492. (thiscol (org-table-current-column))
  8493. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8494. ;; Insert constants in all formulas
  8495. (setq eqlist
  8496. (mapcar (lambda (x)
  8497. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8498. x)
  8499. eqlist))
  8500. ;; Split the equation list
  8501. (while (setq eq (pop eqlist))
  8502. (if (<= (string-to-char (car eq)) ?9)
  8503. (push eq eqlnum)
  8504. (push eq eqlname)))
  8505. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8506. (if all
  8507. (progn
  8508. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8509. (goto-char (setq beg (org-table-begin)))
  8510. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8511. ;; This is a table with marked lines, compute selected lines
  8512. (setq line-re org-table-recalculate-regexp)
  8513. ;; Move forward to the first non-header line
  8514. (if (and (re-search-forward org-table-dataline-regexp end t)
  8515. (re-search-forward org-table-hline-regexp end t)
  8516. (re-search-forward org-table-dataline-regexp end t))
  8517. (setq beg (match-beginning 0))
  8518. nil))) ;; just leave beg where it is
  8519. (setq beg (point-at-bol)
  8520. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8521. (goto-char beg)
  8522. (and all (message "Re-applying formulas to full table..."))
  8523. ;; First find the named fields, and mark them untouchanble
  8524. (remove-text-properties beg end '(org-untouchable t))
  8525. (while (setq eq (pop eqlname))
  8526. (setq name (car eq)
  8527. a (assoc name org-table-named-field-locations))
  8528. (and (not a)
  8529. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8530. (setq a (list name
  8531. (aref org-table-dlines
  8532. (string-to-number (match-string 1 name)))
  8533. (string-to-number (match-string 2 name)))))
  8534. (when (and a (or all (equal (nth 1 a) thisline)))
  8535. (message "Re-applying formula to field: %s" name)
  8536. (goto-line (nth 1 a))
  8537. (org-table-goto-column (nth 2 a))
  8538. (push (append a (list (cdr eq))) eqlname1)
  8539. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8540. ;; FIXME 'nostore 'noanalysis)
  8541. (org-table-put-field-property :org-untouchable t)))
  8542. ;; Now evauluate the column formulas, but skip fields covered by
  8543. ;; field formulas
  8544. (goto-char beg)
  8545. (while (re-search-forward line-re end t)
  8546. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8547. ;; Unprotected line, recalculate
  8548. (and all (message "Re-applying formulas to full table...(line %d)"
  8549. (setq cnt (1+ cnt))))
  8550. (setq org-last-recalc-line (org-current-line))
  8551. (setq eql eqlnum)
  8552. (while (setq entry (pop eql))
  8553. (goto-line org-last-recalc-line)
  8554. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8555. (unless (get-text-property (point) :org-untouchable)
  8556. (org-table-eval-formula nil (cdr entry)
  8557. 'noalign 'nocst 'nostore 'noanalysis)))))
  8558. ;; Now evaluate the field formulas
  8559. (while (setq eq (pop eqlname1))
  8560. (message "Re-applying formula to field: %s" (car eq))
  8561. (goto-line (nth 1 eq))
  8562. (org-table-goto-column (nth 2 eq))
  8563. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8564. 'nostore 'noanalysis))
  8565. (goto-line thisline)
  8566. (org-table-goto-column thiscol)
  8567. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8568. (or noalign (and org-table-may-need-update (org-table-align))
  8569. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8570. ;; back to initial position
  8571. (message "Re-applying formulas...done")
  8572. (goto-line thisline)
  8573. (org-table-goto-column thiscol)
  8574. (or noalign (and org-table-may-need-update (org-table-align))
  8575. (and all (message "Re-applying formulas...done"))))))
  8576. (defun org-table-iterate (&optional arg)
  8577. "Recalculate the table until it does not change anymore."
  8578. (interactive "P")
  8579. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8580. (i 0)
  8581. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8582. thistbl)
  8583. (catch 'exit
  8584. (while (< i imax)
  8585. (setq i (1+ i))
  8586. (org-table-recalculate 'all)
  8587. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8588. (if (not (string= lasttbl thistbl))
  8589. (setq lasttbl thistbl)
  8590. (if (> i 1)
  8591. (message "Convergence after %d iterations" i)
  8592. (message "Table was already stable"))
  8593. (throw 'exit t)))
  8594. (error "No convergence after %d iterations" i))))
  8595. (defun org-table-formula-substitute-names (f)
  8596. "Replace $const with values in string F."
  8597. (let ((start 0) a (f1 f))
  8598. ;; First, check for column names
  8599. (while (setq start (string-match org-table-column-name-regexp f start))
  8600. (setq start (1+ start))
  8601. (setq a (assoc (match-string 1 f) org-table-column-names))
  8602. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8603. ;; Parameters and constants
  8604. (setq start 0)
  8605. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8606. (setq start (1+ start))
  8607. (if (setq a (save-match-data
  8608. (org-table-get-constant (match-string 1 f))))
  8609. (setq f (replace-match (concat "(" a ")") t t f))))
  8610. (if org-table-formula-debug
  8611. (put-text-property 0 (length f) :orig-formula f1 f))
  8612. f))
  8613. (defun org-table-get-constant (const)
  8614. "Find the value for a parameter or constant in a formula.
  8615. Parameters get priority."
  8616. (or (cdr (assoc const org-table-local-parameters))
  8617. (cdr (assoc const org-table-formula-constants-local))
  8618. (cdr (assoc const org-table-formula-constants))
  8619. (and (fboundp 'constants-get) (constants-get const))
  8620. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8621. (org-entry-get nil (substring const 5) 'inherit))
  8622. "#UNDEFINED_NAME"))
  8623. (defvar org-table-fedit-map
  8624. (let ((map (make-sparse-keymap)))
  8625. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8626. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8627. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8628. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8629. (org-defkey map "\C-c?" 'org-table-show-reference)
  8630. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8631. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8632. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8633. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8634. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8635. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8636. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8637. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8638. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8639. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8640. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8641. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8642. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8643. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8644. map))
  8645. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8646. '("Edit-Formulas"
  8647. ["Finish and Install" org-table-fedit-finish t]
  8648. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8649. ["Abort" org-table-fedit-abort t]
  8650. "--"
  8651. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8652. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8653. "--"
  8654. "Shift Reference at Point"
  8655. ["Up" org-table-fedit-ref-up t]
  8656. ["Down" org-table-fedit-ref-down t]
  8657. ["Left" org-table-fedit-ref-left t]
  8658. ["Right" org-table-fedit-ref-right t]
  8659. "-"
  8660. "Change Test Row for Column Formulas"
  8661. ["Up" org-table-fedit-line-up t]
  8662. ["Down" org-table-fedit-line-down t]
  8663. "--"
  8664. ["Scroll Table Window" org-table-fedit-scroll t]
  8665. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8666. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8667. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8668. org-table-overlay-coordinates)]
  8669. "--"
  8670. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8671. :style toggle :selected org-table-buffer-is-an]))
  8672. (defvar org-pos)
  8673. (defun org-table-edit-formulas ()
  8674. "Edit the formulas of the current table in a separate buffer."
  8675. (interactive)
  8676. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8677. (beginning-of-line 0))
  8678. (unless (org-at-table-p) (error "Not at a table"))
  8679. (org-table-get-specials)
  8680. (let ((key (org-table-current-field-formula 'key 'noerror))
  8681. (eql (sort (org-table-get-stored-formulas 'noerror)
  8682. 'org-table-formula-less-p))
  8683. (pos (move-marker (make-marker) (point)))
  8684. (startline 1)
  8685. (wc (current-window-configuration))
  8686. (titles '((column . "# Column Formulas\n")
  8687. (field . "# Field Formulas\n")
  8688. (named . "# Named Field Formulas\n")))
  8689. entry s type title)
  8690. (org-switch-to-buffer-other-window "*Edit Formulas*")
  8691. (erase-buffer)
  8692. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8693. (let ((font-lock-global-modes '(not fundamental-mode)))
  8694. (fundamental-mode))
  8695. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8696. (org-set-local 'org-pos pos)
  8697. (org-set-local 'org-window-configuration wc)
  8698. (use-local-map org-table-fedit-map)
  8699. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8700. (easy-menu-add org-table-fedit-menu)
  8701. (setq startline (org-current-line))
  8702. (while (setq entry (pop eql))
  8703. (setq type (cond
  8704. ((equal (string-to-char (car entry)) ?@) 'field)
  8705. ((string-match "^[0-9]" (car entry)) 'column)
  8706. (t 'named)))
  8707. (when (setq title (assq type titles))
  8708. (or (bobp) (insert "\n"))
  8709. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8710. (setq titles (delq title titles)))
  8711. (if (equal key (car entry)) (setq startline (org-current-line)))
  8712. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8713. (car entry) " = " (cdr entry) "\n"))
  8714. (remove-text-properties 0 (length s) '(face nil) s)
  8715. (insert s))
  8716. (if (eq org-table-use-standard-references t)
  8717. (org-table-fedit-toggle-ref-type))
  8718. (goto-line startline)
  8719. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8720. (defun org-table-fedit-post-command ()
  8721. (when (not (memq this-command '(lisp-complete-symbol)))
  8722. (let ((win (selected-window)))
  8723. (save-excursion
  8724. (condition-case nil
  8725. (org-table-show-reference)
  8726. (error nil))
  8727. (select-window win)))))
  8728. (defun org-table-formula-to-user (s)
  8729. "Convert a formula from internal to user representation."
  8730. (if (eq org-table-use-standard-references t)
  8731. (org-table-convert-refs-to-an s)
  8732. s))
  8733. (defun org-table-formula-from-user (s)
  8734. "Convert a formula from user to internal representation."
  8735. (if org-table-use-standard-references
  8736. (org-table-convert-refs-to-rc s)
  8737. s))
  8738. (defun org-table-convert-refs-to-rc (s)
  8739. "Convert spreadsheet references from AB7 to @7$28.
  8740. Works for single references, but also for entire formulas and even the
  8741. full TBLFM line."
  8742. (let ((start 0))
  8743. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8744. (cond
  8745. ((match-end 3)
  8746. ;; format match, just advance
  8747. (setq start (match-end 0)))
  8748. ((and (> (match-beginning 0) 0)
  8749. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  8750. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  8751. ;; 3.e5 or something like this. FIXME: is this ok????
  8752. (setq start (match-end 0)))
  8753. (t
  8754. (setq start (match-beginning 0)
  8755. s (replace-match
  8756. (if (equal (match-string 2 s) "&")
  8757. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8758. (format "@%d$%d"
  8759. (string-to-number (match-string 2 s))
  8760. (org-letters-to-number (match-string 1 s))))
  8761. t t s)))))
  8762. s))
  8763. (defun org-table-convert-refs-to-an (s)
  8764. "Convert spreadsheet references from to @7$28 to AB7.
  8765. Works for single references, but also for entire formulas and even the
  8766. full TBLFM line."
  8767. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  8768. (setq s (replace-match
  8769. (format "%s%d"
  8770. (org-number-to-letters
  8771. (string-to-number (match-string 2 s)))
  8772. (string-to-number (match-string 1 s)))
  8773. t t s)))
  8774. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8775. (setq s (replace-match (concat "\\1"
  8776. (org-number-to-letters
  8777. (string-to-number (match-string 2 s))) "&")
  8778. t nil s)))
  8779. s)
  8780. (defun org-letters-to-number (s)
  8781. "Convert a base 26 number represented by letters into an integer.
  8782. For example: AB -> 28."
  8783. (let ((n 0))
  8784. (setq s (upcase s))
  8785. (while (> (length s) 0)
  8786. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8787. s (substring s 1)))
  8788. n))
  8789. (defun org-number-to-letters (n)
  8790. "Convert an integer into a base 26 number represented by letters.
  8791. For example: 28 -> AB."
  8792. (let ((s ""))
  8793. (while (> n 0)
  8794. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8795. n (/ (1- n) 26)))
  8796. s))
  8797. (defun org-table-fedit-convert-buffer (function)
  8798. "Convert all references in this buffer, using FUNTION."
  8799. (let ((line (org-current-line)))
  8800. (goto-char (point-min))
  8801. (while (not (eobp))
  8802. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8803. (delete-region (point) (point-at-eol))
  8804. (or (eobp) (forward-char 1)))
  8805. (goto-line line)))
  8806. (defun org-table-fedit-toggle-ref-type ()
  8807. "Convert all references in the buffer from B3 to @3$2 and back."
  8808. (interactive)
  8809. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8810. (org-table-fedit-convert-buffer
  8811. (if org-table-buffer-is-an
  8812. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8813. (message "Reference type switched to %s"
  8814. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8815. (defun org-table-fedit-ref-up ()
  8816. "Shift the reference at point one row/hline up."
  8817. (interactive)
  8818. (org-table-fedit-shift-reference 'up))
  8819. (defun org-table-fedit-ref-down ()
  8820. "Shift the reference at point one row/hline down."
  8821. (interactive)
  8822. (org-table-fedit-shift-reference 'down))
  8823. (defun org-table-fedit-ref-left ()
  8824. "Shift the reference at point one field to the left."
  8825. (interactive)
  8826. (org-table-fedit-shift-reference 'left))
  8827. (defun org-table-fedit-ref-right ()
  8828. "Shift the reference at point one field to the right."
  8829. (interactive)
  8830. (org-table-fedit-shift-reference 'right))
  8831. (defun org-table-fedit-shift-reference (dir)
  8832. (cond
  8833. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8834. (if (memq dir '(left right))
  8835. (org-rematch-and-replace 1 (eq dir 'left))
  8836. (error "Cannot shift reference in this direction")))
  8837. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8838. ;; A B3-like reference
  8839. (if (memq dir '(up down))
  8840. (org-rematch-and-replace 2 (eq dir 'up))
  8841. (org-rematch-and-replace 1 (eq dir 'left))))
  8842. ((org-at-regexp-p
  8843. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8844. ;; An internal reference
  8845. (if (memq dir '(up down))
  8846. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8847. (org-rematch-and-replace 5 (eq dir 'left))))))
  8848. (defun org-rematch-and-replace (n &optional decr hline)
  8849. "Re-match the group N, and replace it with the shifted refrence."
  8850. (or (match-end n) (error "Cannot shift reference in this direction"))
  8851. (goto-char (match-beginning n))
  8852. (and (looking-at (regexp-quote (match-string n)))
  8853. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8854. t t)))
  8855. (defun org-shift-refpart (ref &optional decr hline)
  8856. "Shift a refrence part REF.
  8857. If DECR is set, decrease the references row/column, else increase.
  8858. If HLINE is set, this may be a hline reference, it certainly is not
  8859. a translation reference."
  8860. (save-match-data
  8861. (let* ((sign (string-match "^[-+]" ref)) n)
  8862. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8863. (cond
  8864. ((and hline (string-match "^I+" ref))
  8865. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8866. (setq n (+ n (if decr -1 1)))
  8867. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8868. (if sign
  8869. (setq sign (if (< n 0) "-" "+") n (abs n))
  8870. (setq n (max 1 n)))
  8871. (concat sign (make-string n ?I)))
  8872. ((string-match "^[0-9]+" ref)
  8873. (setq n (string-to-number (concat sign ref)))
  8874. (setq n (+ n (if decr -1 1)))
  8875. (if sign
  8876. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8877. (number-to-string (max 1 n))))
  8878. ((string-match "^[a-zA-Z]+" ref)
  8879. (org-number-to-letters
  8880. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8881. (t (error "Cannot shift reference"))))))
  8882. (defun org-table-fedit-toggle-coordinates ()
  8883. "Toggle the display of coordinates in the refrenced table."
  8884. (interactive)
  8885. (let ((pos (marker-position org-pos)))
  8886. (with-current-buffer (marker-buffer org-pos)
  8887. (save-excursion
  8888. (goto-char pos)
  8889. (org-table-toggle-coordinate-overlays)))))
  8890. (defun org-table-fedit-finish (&optional arg)
  8891. "Parse the buffer for formula definitions and install them.
  8892. With prefix ARG, apply the new formulas to the table."
  8893. (interactive "P")
  8894. (org-table-remove-rectangle-highlight)
  8895. (if org-table-use-standard-references
  8896. (progn
  8897. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8898. (setq org-table-buffer-is-an nil)))
  8899. (let ((pos org-pos) eql var form)
  8900. (goto-char (point-min))
  8901. (while (re-search-forward
  8902. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8903. nil t)
  8904. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8905. form (match-string 3))
  8906. (setq form (org-trim form))
  8907. (when (not (equal form ""))
  8908. (while (string-match "[ \t]*\n[ \t]*" form)
  8909. (setq form (replace-match " " t t form)))
  8910. (when (assoc var eql)
  8911. (error "Double formulas for %s" var))
  8912. (push (cons var form) eql)))
  8913. (setq org-pos nil)
  8914. (set-window-configuration org-window-configuration)
  8915. (select-window (get-buffer-window (marker-buffer pos)))
  8916. (goto-char pos)
  8917. (unless (org-at-table-p)
  8918. (error "Lost table position - cannot install formulae"))
  8919. (org-table-store-formulas eql)
  8920. (move-marker pos nil)
  8921. (kill-buffer "*Edit Formulas*")
  8922. (if arg
  8923. (org-table-recalculate 'all)
  8924. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8925. (defun org-table-fedit-abort ()
  8926. "Abort editing formulas, without installing the changes."
  8927. (interactive)
  8928. (org-table-remove-rectangle-highlight)
  8929. (let ((pos org-pos))
  8930. (set-window-configuration org-window-configuration)
  8931. (select-window (get-buffer-window (marker-buffer pos)))
  8932. (goto-char pos)
  8933. (move-marker pos nil)
  8934. (message "Formula editing aborted without installing changes")))
  8935. (defun org-table-fedit-lisp-indent ()
  8936. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8937. (interactive)
  8938. (let ((pos (point)) beg end ind)
  8939. (beginning-of-line 1)
  8940. (cond
  8941. ((looking-at "[ \t]")
  8942. (goto-char pos)
  8943. (call-interactively 'lisp-indent-line))
  8944. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8945. ((not (fboundp 'pp-buffer))
  8946. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8947. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  8948. (goto-char (- (match-end 0) 2))
  8949. (setq beg (point))
  8950. (setq ind (make-string (current-column) ?\ ))
  8951. (condition-case nil (forward-sexp 1)
  8952. (error
  8953. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8954. (setq end (point))
  8955. (save-restriction
  8956. (narrow-to-region beg end)
  8957. (if (eq last-command this-command)
  8958. (progn
  8959. (goto-char (point-min))
  8960. (setq this-command nil)
  8961. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8962. (replace-match " ")))
  8963. (pp-buffer)
  8964. (untabify (point-min) (point-max))
  8965. (goto-char (1+ (point-min)))
  8966. (while (re-search-forward "^." nil t)
  8967. (beginning-of-line 1)
  8968. (insert ind))
  8969. (goto-char (point-max))
  8970. (backward-delete-char 1)))
  8971. (goto-char beg))
  8972. (t nil))))
  8973. (defvar org-show-positions nil)
  8974. (defun org-table-show-reference (&optional local)
  8975. "Show the location/value of the $ expression at point."
  8976. (interactive)
  8977. (org-table-remove-rectangle-highlight)
  8978. (catch 'exit
  8979. (let ((pos (if local (point) org-pos))
  8980. (face2 'highlight)
  8981. (org-inhibit-highlight-removal t)
  8982. (win (selected-window))
  8983. (org-show-positions nil)
  8984. var name e what match dest)
  8985. (if local (org-table-get-specials))
  8986. (setq what (cond
  8987. ((or (org-at-regexp-p org-table-range-regexp2)
  8988. (org-at-regexp-p org-table-translate-regexp)
  8989. (org-at-regexp-p org-table-range-regexp))
  8990. (setq match
  8991. (save-match-data
  8992. (org-table-convert-refs-to-rc (match-string 0))))
  8993. 'range)
  8994. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8995. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8996. ((not local) nil)
  8997. (t (error "No reference at point")))
  8998. match (and what (or match (match-string 0))))
  8999. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9000. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9001. 'secondary-selection))
  9002. (org-add-hook 'before-change-functions
  9003. 'org-table-remove-rectangle-highlight)
  9004. (if (eq what 'name) (setq var (substring match 1)))
  9005. (when (eq what 'range)
  9006. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9007. (setq match (org-table-formula-substitute-names match)))
  9008. (unless local
  9009. (save-excursion
  9010. (end-of-line 1)
  9011. (re-search-backward "^\\S-" nil t)
  9012. (beginning-of-line 1)
  9013. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9014. (setq dest
  9015. (save-match-data
  9016. (org-table-convert-refs-to-rc (match-string 1))))
  9017. (org-table-add-rectangle-overlay
  9018. (match-beginning 1) (match-end 1) face2))))
  9019. (if (and (markerp pos) (marker-buffer pos))
  9020. (if (get-buffer-window (marker-buffer pos))
  9021. (select-window (get-buffer-window (marker-buffer pos)))
  9022. (org-switch-to-buffer-other-window (get-buffer-window
  9023. (marker-buffer pos)))))
  9024. (goto-char pos)
  9025. (org-table-force-dataline)
  9026. (when dest
  9027. (setq name (substring dest 1))
  9028. (cond
  9029. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9030. (setq e (assoc name org-table-named-field-locations))
  9031. (goto-line (nth 1 e))
  9032. (org-table-goto-column (nth 2 e)))
  9033. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9034. (let ((l (string-to-number (match-string 1 dest)))
  9035. (c (string-to-number (match-string 2 dest))))
  9036. (goto-line (aref org-table-dlines l))
  9037. (org-table-goto-column c)))
  9038. (t (org-table-goto-column (string-to-number name))))
  9039. (move-marker pos (point))
  9040. (org-table-highlight-rectangle nil nil face2))
  9041. (cond
  9042. ((equal dest match))
  9043. ((not match))
  9044. ((eq what 'range)
  9045. (condition-case nil
  9046. (save-excursion
  9047. (org-table-get-range match nil nil 'highlight))
  9048. (error nil)))
  9049. ((setq e (assoc var org-table-named-field-locations))
  9050. (goto-line (nth 1 e))
  9051. (org-table-goto-column (nth 2 e))
  9052. (org-table-highlight-rectangle (point) (point))
  9053. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9054. ((setq e (assoc var org-table-column-names))
  9055. (org-table-goto-column (string-to-number (cdr e)))
  9056. (org-table-highlight-rectangle (point) (point))
  9057. (goto-char (org-table-begin))
  9058. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9059. (org-table-end) t)
  9060. (progn
  9061. (goto-char (match-beginning 1))
  9062. (org-table-highlight-rectangle)
  9063. (message "Named column (column %s)" (cdr e)))
  9064. (error "Column name not found")))
  9065. ((eq what 'column)
  9066. ;; column number
  9067. (org-table-goto-column (string-to-number (substring match 1)))
  9068. (org-table-highlight-rectangle (point) (point))
  9069. (message "Column %s" (substring match 1)))
  9070. ((setq e (assoc var org-table-local-parameters))
  9071. (goto-char (org-table-begin))
  9072. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9073. (progn
  9074. (goto-char (match-beginning 1))
  9075. (org-table-highlight-rectangle)
  9076. (message "Local parameter."))
  9077. (error "Parameter not found")))
  9078. (t
  9079. (cond
  9080. ((not var) (error "No reference at point"))
  9081. ((setq e (assoc var org-table-formula-constants-local))
  9082. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9083. var (cdr e)))
  9084. ((setq e (assoc var org-table-formula-constants))
  9085. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9086. var (cdr e)))
  9087. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9088. (message "Constant: $%s=%s, from `constants.el'%s."
  9089. var e (format " (%s units)" constants-unit-system)))
  9090. (t (error "Undefined name $%s" var)))))
  9091. (goto-char pos)
  9092. (when (and org-show-positions
  9093. (not (memq this-command '(org-table-fedit-scroll
  9094. org-table-fedit-scroll-down))))
  9095. (push pos org-show-positions)
  9096. (push org-table-current-begin-pos org-show-positions)
  9097. (let ((min (apply 'min org-show-positions))
  9098. (max (apply 'max org-show-positions)))
  9099. (goto-char min) (recenter 0)
  9100. (goto-char max)
  9101. (or (pos-visible-in-window-p max) (recenter -1))))
  9102. (select-window win))))
  9103. (defun org-table-force-dataline ()
  9104. "Make sure the cursor is in a dataline in a table."
  9105. (unless (save-excursion
  9106. (beginning-of-line 1)
  9107. (looking-at org-table-dataline-regexp))
  9108. (let* ((re org-table-dataline-regexp)
  9109. (p1 (save-excursion (re-search-forward re nil 'move)))
  9110. (p2 (save-excursion (re-search-backward re nil 'move))))
  9111. (cond ((and p1 p2)
  9112. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9113. p1 p2)))
  9114. ((or p1 p2) (goto-char (or p1 p2)))
  9115. (t (error "No table dataline around here"))))))
  9116. (defun org-table-fedit-line-up ()
  9117. "Move cursor one line up in the window showing the table."
  9118. (interactive)
  9119. (org-table-fedit-move 'previous-line))
  9120. (defun org-table-fedit-line-down ()
  9121. "Move cursor one line down in the window showing the table."
  9122. (interactive)
  9123. (org-table-fedit-move 'next-line))
  9124. (defun org-table-fedit-move (command)
  9125. "Move the cursor in the window shoinw the table.
  9126. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9127. (let ((org-table-allow-automatic-line-recalculation nil)
  9128. (pos org-pos) (win (selected-window)) p)
  9129. (select-window (get-buffer-window (marker-buffer org-pos)))
  9130. (setq p (point))
  9131. (call-interactively command)
  9132. (while (and (org-at-table-p)
  9133. (org-at-table-hline-p))
  9134. (call-interactively command))
  9135. (or (org-at-table-p) (goto-char p))
  9136. (move-marker pos (point))
  9137. (select-window win)))
  9138. (defun org-table-fedit-scroll (N)
  9139. (interactive "p")
  9140. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9141. (scroll-other-window N)))
  9142. (defun org-table-fedit-scroll-down (N)
  9143. (interactive "p")
  9144. (org-table-fedit-scroll (- N)))
  9145. (defvar org-table-rectangle-overlays nil)
  9146. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9147. "Add a new overlay."
  9148. (let ((ov (org-make-overlay beg end)))
  9149. (org-overlay-put ov 'face (or face 'secondary-selection))
  9150. (push ov org-table-rectangle-overlays)))
  9151. (defun org-table-highlight-rectangle (&optional beg end face)
  9152. "Highlight rectangular region in a table."
  9153. (setq beg (or beg (point)) end (or end (point)))
  9154. (let ((b (min beg end))
  9155. (e (max beg end))
  9156. l1 c1 l2 c2 tmp)
  9157. (and (boundp 'org-show-positions)
  9158. (setq org-show-positions (cons b (cons e org-show-positions))))
  9159. (goto-char (min beg end))
  9160. (setq l1 (org-current-line)
  9161. c1 (org-table-current-column))
  9162. (goto-char (max beg end))
  9163. (setq l2 (org-current-line)
  9164. c2 (org-table-current-column))
  9165. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9166. (goto-line l1)
  9167. (beginning-of-line 1)
  9168. (loop for line from l1 to l2 do
  9169. (when (looking-at org-table-dataline-regexp)
  9170. (org-table-goto-column c1)
  9171. (skip-chars-backward "^|\n") (setq beg (point))
  9172. (org-table-goto-column c2)
  9173. (skip-chars-forward "^|\n") (setq end (point))
  9174. (org-table-add-rectangle-overlay beg end face))
  9175. (beginning-of-line 2))
  9176. (goto-char b))
  9177. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9178. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9179. "Remove the rectangle overlays."
  9180. (unless org-inhibit-highlight-removal
  9181. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9182. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9183. (setq org-table-rectangle-overlays nil)))
  9184. (defvar org-table-coordinate-overlays nil
  9185. "Collects the cooordinate grid overlays, so that they can be removed.")
  9186. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9187. (defun org-table-overlay-coordinates ()
  9188. "Add overlays to the table at point, to show row/column coordinates."
  9189. (interactive)
  9190. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9191. (setq org-table-coordinate-overlays nil)
  9192. (save-excursion
  9193. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9194. (goto-char (org-table-begin))
  9195. (while (org-at-table-p)
  9196. (setq eol (point-at-eol))
  9197. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9198. (push ov org-table-coordinate-overlays)
  9199. (setq hline (looking-at org-table-hline-regexp))
  9200. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9201. (format "%4d" (setq id (1+ id)))))
  9202. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9203. (when hline
  9204. (setq ic 0)
  9205. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9206. (setq beg (1+ (match-beginning 0))
  9207. ic (1+ ic)
  9208. s1 (concat "$" (int-to-string ic))
  9209. s2 (org-number-to-letters ic)
  9210. str (if (eq org-table-use-standard-references t) s2 s1))
  9211. (setq ov (org-make-overlay beg (+ beg (length str))))
  9212. (push ov org-table-coordinate-overlays)
  9213. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9214. (beginning-of-line 2)))))
  9215. (defun org-table-toggle-coordinate-overlays ()
  9216. "Toggle the display of Row/Column numbers in tables."
  9217. (interactive)
  9218. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9219. (message "Row/Column number display turned %s"
  9220. (if org-table-overlay-coordinates "on" "off"))
  9221. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9222. (org-table-align))
  9223. (unless org-table-overlay-coordinates
  9224. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9225. (setq org-table-coordinate-overlays nil)))
  9226. (defun org-table-toggle-formula-debugger ()
  9227. "Toggle the formula debugger in tables."
  9228. (interactive)
  9229. (setq org-table-formula-debug (not org-table-formula-debug))
  9230. (message "Formula debugging has been turned %s"
  9231. (if org-table-formula-debug "on" "off")))
  9232. ;;; The orgtbl minor mode
  9233. ;; Define a minor mode which can be used in other modes in order to
  9234. ;; integrate the org-mode table editor.
  9235. ;; This is really a hack, because the org-mode table editor uses several
  9236. ;; keys which normally belong to the major mode, for example the TAB and
  9237. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9238. ;; necessary to operate the table editor, but wraps the commands into a
  9239. ;; function which tests if the cursor is currently inside a table. If that
  9240. ;; is the case, the table editor command is executed. However, when any of
  9241. ;; those keys is used outside a table, the function uses `key-binding' to
  9242. ;; look up if the key has an associated command in another currently active
  9243. ;; keymap (minor modes, major mode, global), and executes that command.
  9244. ;; There might be problems if any of the keys used by the table editor is
  9245. ;; otherwise used as a prefix key.
  9246. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9247. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9248. ;; addresses this by checking explicitly for both bindings.
  9249. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9250. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9251. ;; Some modes bind other commands to simple characters, for example
  9252. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9253. ;; active, this binding is ignored inside tables and replaced with a
  9254. ;; modified self-insert.
  9255. (defvar orgtbl-mode nil
  9256. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9257. table editor in arbitrary modes.")
  9258. (make-variable-buffer-local 'orgtbl-mode)
  9259. (defvar orgtbl-mode-map (make-keymap)
  9260. "Keymap for `orgtbl-mode'.")
  9261. ;;;###autoload
  9262. (defun turn-on-orgtbl ()
  9263. "Unconditionally turn on `orgtbl-mode'."
  9264. (orgtbl-mode 1))
  9265. (defvar org-old-auto-fill-inhibit-regexp nil
  9266. "Local variable used by `orgtbl-mode'")
  9267. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9268. "Matches a line belonging to an orgtbl.")
  9269. (defconst orgtbl-extra-font-lock-keywords
  9270. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9271. 0 (quote 'org-table) 'prepend))
  9272. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9273. ;;;###autoload
  9274. (defun orgtbl-mode (&optional arg)
  9275. "The `org-mode' table editor as a minor mode for use in other modes."
  9276. (interactive)
  9277. (if (org-mode-p)
  9278. ;; Exit without error, in case some hook functions calls this
  9279. ;; by accident in org-mode.
  9280. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9281. (setq orgtbl-mode
  9282. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9283. (if orgtbl-mode
  9284. (progn
  9285. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9286. ;; Make sure we are first in minor-mode-map-alist
  9287. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9288. (and c (setq minor-mode-map-alist
  9289. (cons c (delq c minor-mode-map-alist)))))
  9290. (org-set-local (quote org-table-may-need-update) t)
  9291. (org-add-hook 'before-change-functions 'org-before-change-function
  9292. nil 'local)
  9293. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9294. auto-fill-inhibit-regexp)
  9295. (org-set-local 'auto-fill-inhibit-regexp
  9296. (if auto-fill-inhibit-regexp
  9297. (concat orgtbl-line-start-regexp "\\|"
  9298. auto-fill-inhibit-regexp)
  9299. orgtbl-line-start-regexp))
  9300. (org-add-to-invisibility-spec '(org-cwidth))
  9301. (when (fboundp 'font-lock-add-keywords)
  9302. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9303. (org-restart-font-lock))
  9304. (easy-menu-add orgtbl-mode-menu)
  9305. (run-hooks 'orgtbl-mode-hook))
  9306. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9307. (org-cleanup-narrow-column-properties)
  9308. (org-remove-from-invisibility-spec '(org-cwidth))
  9309. (remove-hook 'before-change-functions 'org-before-change-function t)
  9310. (when (fboundp 'font-lock-remove-keywords)
  9311. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9312. (org-restart-font-lock))
  9313. (easy-menu-remove orgtbl-mode-menu)
  9314. (force-mode-line-update 'all))))
  9315. (defun org-cleanup-narrow-column-properties ()
  9316. "Remove all properties related to narrow-column invisibility."
  9317. (let ((s 1))
  9318. (while (setq s (text-property-any s (point-max)
  9319. 'display org-narrow-column-arrow))
  9320. (remove-text-properties s (1+ s) '(display t)))
  9321. (setq s 1)
  9322. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9323. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9324. (setq s 1)
  9325. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9326. (remove-text-properties s (1+ s) '(invisible t)))))
  9327. ;; Install it as a minor mode.
  9328. (put 'orgtbl-mode :included t)
  9329. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9330. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9331. (defun orgtbl-make-binding (fun n &rest keys)
  9332. "Create a function for binding in the table minor mode.
  9333. FUN is the command to call inside a table. N is used to create a unique
  9334. command name. KEYS are keys that should be checked in for a command
  9335. to execute outside of tables."
  9336. (eval
  9337. (list 'defun
  9338. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9339. '(arg)
  9340. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9341. "Outside of tables, run the binding of `"
  9342. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9343. "'.")
  9344. '(interactive "p")
  9345. (list 'if
  9346. '(org-at-table-p)
  9347. (list 'call-interactively (list 'quote fun))
  9348. (list 'let '(orgtbl-mode)
  9349. (list 'call-interactively
  9350. (append '(or)
  9351. (mapcar (lambda (k)
  9352. (list 'key-binding k))
  9353. keys)
  9354. '('orgtbl-error))))))))
  9355. (defun orgtbl-error ()
  9356. "Error when there is no default binding for a table key."
  9357. (interactive)
  9358. (error "This key is has no function outside tables"))
  9359. (defun orgtbl-setup ()
  9360. "Setup orgtbl keymaps."
  9361. (let ((nfunc 0)
  9362. (bindings
  9363. (list
  9364. '([(meta shift left)] org-table-delete-column)
  9365. '([(meta left)] org-table-move-column-left)
  9366. '([(meta right)] org-table-move-column-right)
  9367. '([(meta shift right)] org-table-insert-column)
  9368. '([(meta shift up)] org-table-kill-row)
  9369. '([(meta shift down)] org-table-insert-row)
  9370. '([(meta up)] org-table-move-row-up)
  9371. '([(meta down)] org-table-move-row-down)
  9372. '("\C-c\C-w" org-table-cut-region)
  9373. '("\C-c\M-w" org-table-copy-region)
  9374. '("\C-c\C-y" org-table-paste-rectangle)
  9375. '("\C-c-" org-table-insert-hline)
  9376. '("\C-c}" org-table-toggle-coordinate-overlays)
  9377. '("\C-c{" org-table-toggle-formula-debugger)
  9378. '("\C-m" org-table-next-row)
  9379. '([(shift return)] org-table-copy-down)
  9380. '("\C-c\C-q" org-table-wrap-region)
  9381. '("\C-c?" org-table-field-info)
  9382. '("\C-c " org-table-blank-field)
  9383. '("\C-c+" org-table-sum)
  9384. '("\C-c=" org-table-eval-formula)
  9385. '("\C-c'" org-table-edit-formulas)
  9386. '("\C-c`" org-table-edit-field)
  9387. '("\C-c*" org-table-recalculate)
  9388. '("\C-c|" org-table-create-or-convert-from-region)
  9389. '("\C-c^" org-table-sort-lines)
  9390. '([(control ?#)] org-table-rotate-recalc-marks)))
  9391. elt key fun cmd)
  9392. (while (setq elt (pop bindings))
  9393. (setq nfunc (1+ nfunc))
  9394. (setq key (org-key (car elt))
  9395. fun (nth 1 elt)
  9396. cmd (orgtbl-make-binding fun nfunc key))
  9397. (org-defkey orgtbl-mode-map key cmd))
  9398. ;; Special treatment needed for TAB and RET
  9399. (org-defkey orgtbl-mode-map [(return)]
  9400. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9401. (org-defkey orgtbl-mode-map "\C-m"
  9402. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9403. (org-defkey orgtbl-mode-map [(tab)]
  9404. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9405. (org-defkey orgtbl-mode-map "\C-i"
  9406. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9407. (org-defkey orgtbl-mode-map [(shift tab)]
  9408. (orgtbl-make-binding 'org-table-previous-field 104
  9409. [(shift tab)] [(tab)] "\C-i"))
  9410. (org-defkey orgtbl-mode-map "\M-\C-m"
  9411. (orgtbl-make-binding 'org-table-wrap-region 105
  9412. "\M-\C-m" [(meta return)]))
  9413. (org-defkey orgtbl-mode-map [(meta return)]
  9414. (orgtbl-make-binding 'org-table-wrap-region 106
  9415. [(meta return)] "\M-\C-m"))
  9416. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9417. (when orgtbl-optimized
  9418. ;; If the user wants maximum table support, we need to hijack
  9419. ;; some standard editing functions
  9420. (org-remap orgtbl-mode-map
  9421. 'self-insert-command 'orgtbl-self-insert-command
  9422. 'delete-char 'org-delete-char
  9423. 'delete-backward-char 'org-delete-backward-char)
  9424. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9425. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9426. '("OrgTbl"
  9427. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9428. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9429. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9430. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9431. "--"
  9432. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9433. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9434. ["Copy Field from Above"
  9435. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9436. "--"
  9437. ("Column"
  9438. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9439. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9440. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9441. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9442. ("Row"
  9443. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9444. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9445. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9446. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9447. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9448. "--"
  9449. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9450. ("Rectangle"
  9451. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9452. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9453. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9454. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9455. "--"
  9456. ("Radio tables"
  9457. ["Insert table template" orgtbl-insert-radio-table
  9458. (assq major-mode orgtbl-radio-table-templates)]
  9459. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9460. "--"
  9461. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9462. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9463. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9464. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9465. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9466. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9467. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9468. ["Sum Column/Rectangle" org-table-sum
  9469. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9470. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9471. ["Debug Formulas"
  9472. org-table-toggle-formula-debugger :active (org-at-table-p)
  9473. :keys "C-c {"
  9474. :style toggle :selected org-table-formula-debug]
  9475. ["Show Col/Row Numbers"
  9476. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9477. :keys "C-c }"
  9478. :style toggle :selected org-table-overlay-coordinates]
  9479. ))
  9480. t))
  9481. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9482. "If the cursor is inside a table, realign the table.
  9483. It it is a table to be sent away to a receiver, do it.
  9484. With prefix arg, also recompute table."
  9485. (interactive "P")
  9486. (let ((pos (point)) action)
  9487. (save-excursion
  9488. (beginning-of-line 1)
  9489. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9490. ((looking-at "[ \t]*|") pos)
  9491. ((looking-at "#\\+TBLFM:") 'recalc))))
  9492. (cond
  9493. ((integerp action)
  9494. (goto-char action)
  9495. (org-table-maybe-eval-formula)
  9496. (if arg
  9497. (call-interactively 'org-table-recalculate)
  9498. (org-table-maybe-recalculate-line))
  9499. (call-interactively 'org-table-align)
  9500. (orgtbl-send-table 'maybe))
  9501. ((eq action 'recalc)
  9502. (save-excursion
  9503. (beginning-of-line 1)
  9504. (skip-chars-backward " \r\n\t")
  9505. (if (org-at-table-p)
  9506. (org-call-with-arg 'org-table-recalculate t))))
  9507. (t (let (orgtbl-mode)
  9508. (call-interactively (key-binding "\C-c\C-c")))))))
  9509. (defun orgtbl-tab (arg)
  9510. "Justification and field motion for `orgtbl-mode'."
  9511. (interactive "P")
  9512. (if arg (org-table-edit-field t)
  9513. (org-table-justify-field-maybe)
  9514. (org-table-next-field)))
  9515. (defun orgtbl-ret ()
  9516. "Justification and field motion for `orgtbl-mode'."
  9517. (interactive)
  9518. (org-table-justify-field-maybe)
  9519. (org-table-next-row))
  9520. (defun orgtbl-self-insert-command (N)
  9521. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9522. If the cursor is in a table looking at whitespace, the whitespace is
  9523. overwritten, and the table is not marked as requiring realignment."
  9524. (interactive "p")
  9525. (if (and (org-at-table-p)
  9526. (or
  9527. (and org-table-auto-blank-field
  9528. (member last-command
  9529. '(orgtbl-hijacker-command-100
  9530. orgtbl-hijacker-command-101
  9531. orgtbl-hijacker-command-102
  9532. orgtbl-hijacker-command-103
  9533. orgtbl-hijacker-command-104
  9534. orgtbl-hijacker-command-105))
  9535. (org-table-blank-field))
  9536. t)
  9537. (eq N 1)
  9538. (looking-at "[^|\n]* +|"))
  9539. (let (org-table-may-need-update)
  9540. (goto-char (1- (match-end 0)))
  9541. (delete-backward-char 1)
  9542. (goto-char (match-beginning 0))
  9543. (self-insert-command N))
  9544. (setq org-table-may-need-update t)
  9545. (let (orgtbl-mode)
  9546. (call-interactively (key-binding (vector last-input-event))))))
  9547. (defun org-force-self-insert (N)
  9548. "Needed to enforce self-insert under remapping."
  9549. (interactive "p")
  9550. (self-insert-command N))
  9551. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9552. "Regula expression matching exponentials as produced by calc.")
  9553. (defvar org-table-clean-did-remove-column nil)
  9554. (defun orgtbl-export (table target)
  9555. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9556. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9557. org-table-last-alignment org-table-last-column-widths
  9558. maxcol column)
  9559. (if (not (fboundp func))
  9560. (error "Cannot export orgtbl table to %s" target))
  9561. (setq lines (org-table-clean-before-export lines))
  9562. (setq table
  9563. (mapcar
  9564. (lambda (x)
  9565. (if (string-match org-table-hline-regexp x)
  9566. 'hline
  9567. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9568. lines))
  9569. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9570. table)))
  9571. (loop for i from (1- maxcol) downto 0 do
  9572. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9573. (setq column (delq nil column))
  9574. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9575. (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))
  9576. (funcall func table nil)))
  9577. (defun orgtbl-send-table (&optional maybe)
  9578. "Send a tranformed version of this table to the receiver position.
  9579. With argument MAYBE, fail quietly if no transformation is defined for
  9580. this table."
  9581. (interactive)
  9582. (catch 'exit
  9583. (unless (org-at-table-p) (error "Not at a table"))
  9584. ;; when non-interactive, we assume align has just happened.
  9585. (when (interactive-p) (org-table-align))
  9586. (save-excursion
  9587. (goto-char (org-table-begin))
  9588. (beginning-of-line 0)
  9589. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9590. (if maybe
  9591. (throw 'exit nil)
  9592. (error "Don't know how to transform this table."))))
  9593. (let* ((name (match-string 1))
  9594. beg
  9595. (transform (intern (match-string 2)))
  9596. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9597. (skip (plist-get params :skip))
  9598. (skipcols (plist-get params :skipcols))
  9599. (txt (buffer-substring-no-properties
  9600. (org-table-begin) (org-table-end)))
  9601. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9602. (lines (org-table-clean-before-export lines))
  9603. (i0 (if org-table-clean-did-remove-column 2 1))
  9604. (table (mapcar
  9605. (lambda (x)
  9606. (if (string-match org-table-hline-regexp x)
  9607. 'hline
  9608. (org-remove-by-index
  9609. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9610. skipcols i0)))
  9611. lines))
  9612. (fun (if (= i0 2) 'cdr 'identity))
  9613. (org-table-last-alignment
  9614. (org-remove-by-index (funcall fun org-table-last-alignment)
  9615. skipcols i0))
  9616. (org-table-last-column-widths
  9617. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9618. skipcols i0)))
  9619. (unless (fboundp transform)
  9620. (error "No such transformation function %s" transform))
  9621. (setq txt (funcall transform table params))
  9622. ;; Find the insertion place
  9623. (save-excursion
  9624. (goto-char (point-min))
  9625. (unless (re-search-forward
  9626. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9627. (error "Don't know where to insert translated table"))
  9628. (goto-char (match-beginning 0))
  9629. (beginning-of-line 2)
  9630. (setq beg (point))
  9631. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9632. (error "Cannot find end of insertion region"))
  9633. (beginning-of-line 1)
  9634. (delete-region beg (point))
  9635. (goto-char beg)
  9636. (insert txt "\n"))
  9637. (message "Table converted and installed at receiver location"))))
  9638. (defun org-remove-by-index (list indices &optional i0)
  9639. "Remove the elements in LIST with indices in INDICES.
  9640. First element has index 0, or I0 if given."
  9641. (if (not indices)
  9642. list
  9643. (if (integerp indices) (setq indices (list indices)))
  9644. (setq i0 (1- (or i0 0)))
  9645. (delq :rm (mapcar (lambda (x)
  9646. (setq i0 (1+ i0))
  9647. (if (memq i0 indices) :rm x))
  9648. list))))
  9649. (defun orgtbl-toggle-comment ()
  9650. "Comment or uncomment the orgtbl at point."
  9651. (interactive)
  9652. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9653. (re2 (concat "^" orgtbl-line-start-regexp))
  9654. (commented (save-excursion (beginning-of-line 1)
  9655. (cond ((looking-at re1) t)
  9656. ((looking-at re2) nil)
  9657. (t (error "Not at an org table")))))
  9658. (re (if commented re1 re2))
  9659. beg end)
  9660. (save-excursion
  9661. (beginning-of-line 1)
  9662. (while (looking-at re) (beginning-of-line 0))
  9663. (beginning-of-line 2)
  9664. (setq beg (point))
  9665. (while (looking-at re) (beginning-of-line 2))
  9666. (setq end (point)))
  9667. (comment-region beg end (if commented '(4) nil))))
  9668. (defun orgtbl-insert-radio-table ()
  9669. "Insert a radio table template appropriate for this major mode."
  9670. (interactive)
  9671. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9672. (txt (nth 1 e))
  9673. name pos)
  9674. (unless e (error "No radio table setup defined for %s" major-mode))
  9675. (setq name (read-string "Table name: "))
  9676. (while (string-match "%n" txt)
  9677. (setq txt (replace-match name t t txt)))
  9678. (or (bolp) (insert "\n"))
  9679. (setq pos (point))
  9680. (insert txt)
  9681. (goto-char pos)))
  9682. (defun org-get-param (params header i sym &optional hsym)
  9683. "Get parameter value for symbol SYM.
  9684. If this is a header line, actually get the value for the symbol with an
  9685. additional \"h\" inserted after the colon.
  9686. If the value is a protperty list, get the element for the current column.
  9687. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9688. (let ((val (plist-get params sym)))
  9689. (and hsym header (setq val (or (plist-get params hsym) val)))
  9690. (if (consp val) (plist-get val i) val)))
  9691. (defun orgtbl-to-generic (table params)
  9692. "Convert the orgtbl-mode TABLE to some other format.
  9693. This generic routine can be used for many standard cases.
  9694. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9695. separator line, or a list of fields for that line.
  9696. PARAMS is a property list of parameters that can influence the conversion.
  9697. For the generic converter, some parameters are obligatory: You need to
  9698. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9699. :splice, you must have :tstart and :tend.
  9700. Valid parameters are
  9701. :tstart String to start the table. Ignored when :splice is t.
  9702. :tend String to end the table. Ignored when :splice is t.
  9703. :splice When set to t, return only table body lines, don't wrap
  9704. them into :tstart and :tend. Default is nil.
  9705. :hline String to be inserted on horizontal separation lines.
  9706. May be nil to ignore hlines.
  9707. :lstart String to start a new table line.
  9708. :lend String to end a table line
  9709. :sep Separator between two fields
  9710. :lfmt Format for entire line, with enough %s to capture all fields.
  9711. If this is present, :lstart, :lend, and :sep are ignored.
  9712. :fmt A format to be used to wrap the field, should contain
  9713. %s for the original field value. For example, to wrap
  9714. everything in dollars, you could use :fmt \"$%s$\".
  9715. This may also be a property list with column numbers and
  9716. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9717. :hlstart :hlend :hlsep :hlfmt :hfmt
  9718. Same as above, specific for the header lines in the table.
  9719. All lines before the first hline are treated as header.
  9720. If any of these is not present, the data line value is used.
  9721. :efmt Use this format to print numbers with exponentials.
  9722. The format should have %s twice for inserting mantissa
  9723. and exponent, for example \"%s\\\\times10^{%s}\". This
  9724. may also be a property list with column numbers and
  9725. formats. :fmt will still be applied after :efmt.
  9726. In addition to this, the parameters :skip and :skipcols are always handled
  9727. directly by `orgtbl-send-table'. See manual."
  9728. (interactive)
  9729. (let* ((p params)
  9730. (splicep (plist-get p :splice))
  9731. (hline (plist-get p :hline))
  9732. rtn line i fm efm lfmt h)
  9733. ;; Do we have a header?
  9734. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9735. (setq h t))
  9736. ;; Put header
  9737. (unless splicep
  9738. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9739. ;; Now loop over all lines
  9740. (while (setq line (pop table))
  9741. (if (eq line 'hline)
  9742. ;; A horizontal separator line
  9743. (progn (if hline (push hline rtn))
  9744. (setq h nil)) ; no longer in header
  9745. ;; A normal line. Convert the fields, push line onto the result list
  9746. (setq i 0)
  9747. (setq line
  9748. (mapcar
  9749. (lambda (f)
  9750. (setq i (1+ i)
  9751. fm (org-get-param p h i :fmt :hfmt)
  9752. efm (org-get-param p h i :efmt))
  9753. (if (and efm (string-match orgtbl-exp-regexp f))
  9754. (setq f (format
  9755. efm (match-string 1 f) (match-string 2 f))))
  9756. (if fm (setq f (format fm f)))
  9757. f)
  9758. line))
  9759. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9760. (push (apply 'format lfmt line) rtn)
  9761. (push (concat
  9762. (org-get-param p h i :lstart :hlstart)
  9763. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9764. (org-get-param p h i :lend :hlend))
  9765. rtn))))
  9766. (unless splicep
  9767. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9768. (mapconcat 'identity (nreverse rtn) "\n")))
  9769. (defun orgtbl-to-latex (table params)
  9770. "Convert the orgtbl-mode TABLE to LaTeX.
  9771. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9772. separator line, or a list of fields for that line.
  9773. PARAMS is a property list of parameters that can influence the conversion.
  9774. Supports all parameters from `orgtbl-to-generic'. Most important for
  9775. LaTeX are:
  9776. :splice When set to t, return only table body lines, don't wrap
  9777. them into a tabular environment. Default is nil.
  9778. :fmt A format to be used to wrap the field, should contain %s for the
  9779. original field value. For example, to wrap everything in dollars,
  9780. use :fmt \"$%s$\". This may also be a property list with column
  9781. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9782. :efmt Format for transforming numbers with exponentials. The format
  9783. should have %s twice for inserting mantissa and exponent, for
  9784. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9785. This may also be a property list with column numbers and formats.
  9786. The general parameters :skip and :skipcols have already been applied when
  9787. this function is called."
  9788. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9789. org-table-last-alignment ""))
  9790. (params2
  9791. (list
  9792. :tstart (concat "\\begin{tabular}{" alignment "}")
  9793. :tend "\\end{tabular}"
  9794. :lstart "" :lend " \\\\" :sep " & "
  9795. :efmt "%s\\,(%s)" :hline "\\hline")))
  9796. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9797. (defun orgtbl-to-html (table params)
  9798. "Convert the orgtbl-mode TABLE to LaTeX.
  9799. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9800. separator line, or a list of fields for that line.
  9801. PARAMS is a property list of parameters that can influence the conversion.
  9802. Currently this function recognizes the following parameters:
  9803. :splice When set to t, return only table body lines, don't wrap
  9804. them into a <table> environment. Default is nil.
  9805. The general parameters :skip and :skipcols have already been applied when
  9806. this function is called. The function does *not* use `orgtbl-to-generic',
  9807. so you cannot specify parameters for it."
  9808. (let* ((splicep (plist-get params :splice))
  9809. html)
  9810. ;; Just call the formatter we already have
  9811. ;; We need to make text lines for it, so put the fields back together.
  9812. (setq html (org-format-org-table-html
  9813. (mapcar
  9814. (lambda (x)
  9815. (if (eq x 'hline)
  9816. "|----+----|"
  9817. (concat "| " (mapconcat 'identity x " | ") " |")))
  9818. table)
  9819. splicep))
  9820. (if (string-match "\n+\\'" html)
  9821. (setq html (replace-match "" t t html)))
  9822. html))
  9823. (defun orgtbl-to-texinfo (table params)
  9824. "Convert the orgtbl-mode TABLE to TeXInfo.
  9825. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9826. separator line, or a list of fields for that line.
  9827. PARAMS is a property list of parameters that can influence the conversion.
  9828. Supports all parameters from `orgtbl-to-generic'. Most important for
  9829. TeXInfo are:
  9830. :splice nil/t When set to t, return only table body lines, don't wrap
  9831. them into a multitable environment. Default is nil.
  9832. :fmt fmt A format to be used to wrap the field, should contain
  9833. %s for the original field value. For example, to wrap
  9834. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9835. This may also be a property list with column numbers and
  9836. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9837. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9838. are computed automatically from the width of the columns
  9839. under org-mode.
  9840. The general parameters :skip and :skipcols have already been applied when
  9841. this function is called."
  9842. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9843. (colfrac (or (plist-get params :cf)
  9844. (mapconcat
  9845. (lambda (x) (format "%.3f" (/ (float x) total)))
  9846. org-table-last-column-widths " ")))
  9847. (params2
  9848. (list
  9849. :tstart (concat "@multitable @columnfractions " colfrac)
  9850. :tend "@end multitable"
  9851. :lstart "@item " :lend "" :sep " @tab "
  9852. :hlstart "@headitem ")))
  9853. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9854. ;;;; Link Stuff
  9855. ;;; Link abbreviations
  9856. (defun org-link-expand-abbrev (link)
  9857. "Apply replacements as defined in `org-link-abbrev-alist."
  9858. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  9859. (let* ((key (match-string 1 link))
  9860. (as (or (assoc key org-link-abbrev-alist-local)
  9861. (assoc key org-link-abbrev-alist)))
  9862. (tag (and (match-end 2) (match-string 3 link)))
  9863. rpl)
  9864. (if (not as)
  9865. link
  9866. (setq rpl (cdr as))
  9867. (cond
  9868. ((symbolp rpl) (funcall rpl tag))
  9869. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9870. (t (concat rpl tag)))))
  9871. link))
  9872. ;;; Storing and inserting links
  9873. (defvar org-insert-link-history nil
  9874. "Minibuffer history for links inserted with `org-insert-link'.")
  9875. (defvar org-stored-links nil
  9876. "Contains the links stored with `org-store-link'.")
  9877. (defvar org-store-link-plist nil
  9878. "Plist with info about the most recently link created with `org-store-link'.")
  9879. (defvar org-link-protocols nil
  9880. "Link protocols added to Org-mode using `org-add-link-type'.")
  9881. (defvar org-store-link-functions nil
  9882. "List of functions that are called to create and store a link.
  9883. Each function will be called in turn until one returns a non-nil
  9884. value. Each function should check if it is responsible for creating
  9885. this link (for example by looking at the major mode).
  9886. If not, it must exit and return nil.
  9887. If yes, it should return a non-nil value after a calling
  9888. `org-store-link-properties' with a list of properties and values.
  9889. Special properties are:
  9890. :type The link prefix. like \"http\". This must be given.
  9891. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  9892. This is obligatory as well.
  9893. :description Optional default description for the second pair
  9894. of brackets in an Org-mode link. The user can still change
  9895. this when inserting this link into an Org-mode buffer.
  9896. In addition to these, any additional properties can be specified
  9897. and then used in remember templates.")
  9898. (defun org-add-link-type (type &optional follow publish)
  9899. "Add TYPE to the list of `org-link-types'.
  9900. Re-compute all regular expressions depending on `org-link-types'
  9901. FOLLOW and PUBLISH are two functions. Both take the link path as
  9902. an argument.
  9903. FOLLOW should do whatever is necessary to follow the link, for example
  9904. to find a file or display a mail message.
  9905. PUBLISH takes the path and retuns the string that should be used when
  9906. this document is published."
  9907. (add-to-list 'org-link-types type t)
  9908. (org-make-link-regexps)
  9909. (add-to-list 'org-link-protocols
  9910. (list type follow publish)))
  9911. (defun org-add-agenda-custom-command (entry)
  9912. "Replace or add a command in `org-agenda-custom-commands'.
  9913. This is mostly for hacking and trying a new command - once the command
  9914. works you probably want to add it to `org-agenda-custom-commands' for good."
  9915. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  9916. (if ass
  9917. (setcdr ass (cdr entry))
  9918. (push entry org-agenda-custom-commands))))
  9919. ;;;###autoload
  9920. (defun org-store-link (arg)
  9921. "\\<org-mode-map>Store an org-link to the current location.
  9922. This link can later be inserted into an org-buffer with
  9923. \\[org-insert-link].
  9924. For some link types, a prefix arg is interpreted:
  9925. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9926. For file links, arg negates `org-context-in-file-links'."
  9927. (interactive "P")
  9928. (setq org-store-link-plist nil) ; reset
  9929. (let (link cpltxt desc description search txt)
  9930. (cond
  9931. ((run-hook-with-args-until-success 'org-store-link-functions)
  9932. (setq link (plist-get org-store-link-plist :link)
  9933. desc (or (plist-get org-store-link-plist :description) link)))
  9934. ((eq major-mode 'bbdb-mode)
  9935. (let ((name (bbdb-record-name (bbdb-current-record)))
  9936. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9937. (setq cpltxt (concat "bbdb:" (or name company))
  9938. link (org-make-link cpltxt))
  9939. (org-store-link-props :type "bbdb" :name name :company company)))
  9940. ((eq major-mode 'Info-mode)
  9941. (setq link (org-make-link "info:"
  9942. (file-name-nondirectory Info-current-file)
  9943. ":" Info-current-node))
  9944. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9945. ":" Info-current-node))
  9946. (org-store-link-props :type "info" :file Info-current-file
  9947. :node Info-current-node))
  9948. ((eq major-mode 'calendar-mode)
  9949. (let ((cd (calendar-cursor-to-date)))
  9950. (setq link
  9951. (format-time-string
  9952. (car org-time-stamp-formats)
  9953. (apply 'encode-time
  9954. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9955. nil nil nil))))
  9956. (org-store-link-props :type "calendar" :date cd)))
  9957. ((or (eq major-mode 'vm-summary-mode)
  9958. (eq major-mode 'vm-presentation-mode))
  9959. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9960. (vm-follow-summary-cursor)
  9961. (save-excursion
  9962. (vm-select-folder-buffer)
  9963. (let* ((message (car vm-message-pointer))
  9964. (folder buffer-file-name)
  9965. (subject (vm-su-subject message))
  9966. (to (vm-get-header-contents message "To"))
  9967. (from (vm-get-header-contents message "From"))
  9968. (message-id (vm-su-message-id message)))
  9969. (org-store-link-props :type "vm" :from from :to to :subject subject
  9970. :message-id message-id)
  9971. (setq message-id (org-remove-angle-brackets message-id))
  9972. (setq folder (abbreviate-file-name folder))
  9973. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9974. folder)
  9975. (setq folder (replace-match "" t t folder)))
  9976. (setq cpltxt (org-email-link-description))
  9977. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9978. ((eq major-mode 'wl-summary-mode)
  9979. (let* ((msgnum (wl-summary-message-number))
  9980. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9981. msgnum 'message-id))
  9982. (wl-message-entity
  9983. (if (fboundp 'elmo-message-entity)
  9984. (elmo-message-entity
  9985. wl-summary-buffer-elmo-folder msgnum)
  9986. (elmo-msgdb-overview-get-entity
  9987. msgnum (wl-summary-buffer-msgdb))))
  9988. (from (wl-summary-line-from))
  9989. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9990. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9991. (wl-summary-line-subject))))
  9992. (org-store-link-props :type "wl" :from from :to to
  9993. :subject subject :message-id message-id)
  9994. (setq message-id (org-remove-angle-brackets message-id))
  9995. (setq cpltxt (org-email-link-description))
  9996. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9997. "#" message-id))))
  9998. ((or (equal major-mode 'mh-folder-mode)
  9999. (equal major-mode 'mh-show-mode))
  10000. (let ((from (org-mhe-get-header "From:"))
  10001. (to (org-mhe-get-header "To:"))
  10002. (message-id (org-mhe-get-header "Message-Id:"))
  10003. (subject (org-mhe-get-header "Subject:")))
  10004. (org-store-link-props :type "mh" :from from :to to
  10005. :subject subject :message-id message-id)
  10006. (setq cpltxt (org-email-link-description))
  10007. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10008. (org-remove-angle-brackets message-id)))))
  10009. ((eq major-mode 'rmail-mode)
  10010. (save-excursion
  10011. (save-restriction
  10012. (rmail-narrow-to-non-pruned-header)
  10013. (let ((folder buffer-file-name)
  10014. (message-id (mail-fetch-field "message-id"))
  10015. (from (mail-fetch-field "from"))
  10016. (to (mail-fetch-field "to"))
  10017. (subject (mail-fetch-field "subject")))
  10018. (org-store-link-props
  10019. :type "rmail" :from from :to to
  10020. :subject subject :message-id message-id)
  10021. (setq message-id (org-remove-angle-brackets message-id))
  10022. (setq cpltxt (org-email-link-description))
  10023. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10024. ((eq major-mode 'gnus-group-mode)
  10025. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10026. (gnus-group-group-name)) ; version
  10027. ((fboundp 'gnus-group-name)
  10028. (gnus-group-name))
  10029. (t "???"))))
  10030. (unless group (error "Not on a group"))
  10031. (org-store-link-props :type "gnus" :group group)
  10032. (setq cpltxt (concat
  10033. (if (org-xor arg org-usenet-links-prefer-google)
  10034. "http://groups.google.com/groups?group="
  10035. "gnus:")
  10036. group)
  10037. link (org-make-link cpltxt))))
  10038. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10039. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10040. (let* ((group gnus-newsgroup-name)
  10041. (article (gnus-summary-article-number))
  10042. (header (gnus-summary-article-header article))
  10043. (from (mail-header-from header))
  10044. (message-id (mail-header-id header))
  10045. (date (mail-header-date header))
  10046. (subject (gnus-summary-subject-string)))
  10047. (org-store-link-props :type "gnus" :from from :subject subject
  10048. :message-id message-id :group group)
  10049. (setq cpltxt (org-email-link-description))
  10050. (if (org-xor arg org-usenet-links-prefer-google)
  10051. (setq link
  10052. (concat
  10053. cpltxt "\n "
  10054. (format "http://groups.google.com/groups?as_umsgid=%s"
  10055. (org-fixup-message-id-for-http message-id))))
  10056. (setq link (org-make-link "gnus:" group
  10057. "#" (number-to-string article))))))
  10058. ((eq major-mode 'w3-mode)
  10059. (setq cpltxt (url-view-url t)
  10060. link (org-make-link cpltxt))
  10061. (org-store-link-props :type "w3" :url (url-view-url t)))
  10062. ((eq major-mode 'w3m-mode)
  10063. (setq cpltxt (or w3m-current-title w3m-current-url)
  10064. link (org-make-link w3m-current-url))
  10065. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10066. ((setq search (run-hook-with-args-until-success
  10067. 'org-create-file-search-functions))
  10068. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10069. "::" search))
  10070. (setq cpltxt (or description link)))
  10071. ((eq major-mode 'image-mode)
  10072. (setq cpltxt (concat "file:"
  10073. (abbreviate-file-name buffer-file-name))
  10074. link (org-make-link cpltxt))
  10075. (org-store-link-props :type "image" :file buffer-file-name))
  10076. ((eq major-mode 'dired-mode)
  10077. ;; link to the file in the current line
  10078. (setq cpltxt (concat "file:"
  10079. (abbreviate-file-name
  10080. (expand-file-name
  10081. (dired-get-filename nil t))))
  10082. link (org-make-link cpltxt)))
  10083. ((and buffer-file-name (org-mode-p))
  10084. ;; Just link to current headline
  10085. (setq cpltxt (concat "file:"
  10086. (abbreviate-file-name buffer-file-name)))
  10087. ;; Add a context search string
  10088. (when (org-xor org-context-in-file-links arg)
  10089. ;; Check if we are on a target
  10090. (if (org-in-regexp "<<\\(.*?\\)>>")
  10091. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10092. (setq txt (cond
  10093. ((org-on-heading-p) nil)
  10094. ((org-region-active-p)
  10095. (buffer-substring (region-beginning) (region-end)))
  10096. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10097. (when (or (null txt) (string-match "\\S-" txt))
  10098. (setq cpltxt
  10099. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10100. desc "NONE"))))
  10101. (if (string-match "::\\'" cpltxt)
  10102. (setq cpltxt (substring cpltxt 0 -2)))
  10103. (setq link (org-make-link cpltxt)))
  10104. ((buffer-file-name (buffer-base-buffer))
  10105. ;; Just link to this file here.
  10106. (setq cpltxt (concat "file:"
  10107. (abbreviate-file-name
  10108. (buffer-file-name (buffer-base-buffer)))))
  10109. ;; Add a context string
  10110. (when (org-xor org-context-in-file-links arg)
  10111. (setq txt (if (org-region-active-p)
  10112. (buffer-substring (region-beginning) (region-end))
  10113. (buffer-substring (point-at-bol) (point-at-eol))))
  10114. ;; Only use search option if there is some text.
  10115. (when (string-match "\\S-" txt)
  10116. (setq cpltxt
  10117. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10118. desc "NONE")))
  10119. (setq link (org-make-link cpltxt)))
  10120. ((interactive-p)
  10121. (error "Cannot link to a buffer which is not visiting a file"))
  10122. (t (setq link nil)))
  10123. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10124. (setq link (or link cpltxt)
  10125. desc (or desc cpltxt))
  10126. (if (equal desc "NONE") (setq desc nil))
  10127. (if (and (interactive-p) link)
  10128. (progn
  10129. (setq org-stored-links
  10130. (cons (list link desc) org-stored-links))
  10131. (message "Stored: %s" (or desc link)))
  10132. (and link (org-make-link-string link desc)))))
  10133. (defun org-store-link-props (&rest plist)
  10134. "Store link properties, extract names and addresses."
  10135. (let (x adr)
  10136. (when (setq x (plist-get plist :from))
  10137. (setq adr (mail-extract-address-components x))
  10138. (plist-put plist :fromname (car adr))
  10139. (plist-put plist :fromaddress (nth 1 adr)))
  10140. (when (setq x (plist-get plist :to))
  10141. (setq adr (mail-extract-address-components x))
  10142. (plist-put plist :toname (car adr))
  10143. (plist-put plist :toaddress (nth 1 adr))))
  10144. (let ((from (plist-get plist :from))
  10145. (to (plist-get plist :to)))
  10146. (when (and from to org-from-is-user-regexp)
  10147. (plist-put plist :fromto
  10148. (if (string-match org-from-is-user-regexp from)
  10149. (concat "to %t")
  10150. (concat "from %f")))))
  10151. (setq org-store-link-plist plist))
  10152. (defun org-email-link-description (&optional fmt)
  10153. "Return the description part of an email link.
  10154. This takes information from `org-store-link-plist' and formats it
  10155. according to FMT (default from `org-email-link-description-format')."
  10156. (setq fmt (or fmt org-email-link-description-format))
  10157. (let* ((p org-store-link-plist)
  10158. (to (plist-get p :toaddress))
  10159. (from (plist-get p :fromaddress))
  10160. (table
  10161. (list
  10162. (cons "%c" (plist-get p :fromto))
  10163. (cons "%F" (plist-get p :from))
  10164. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10165. (cons "%T" (plist-get p :to))
  10166. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10167. (cons "%s" (plist-get p :subject))
  10168. (cons "%m" (plist-get p :message-id)))))
  10169. (when (string-match "%c" fmt)
  10170. ;; Check if the user wrote this message
  10171. (if (and org-from-is-user-regexp from to
  10172. (save-match-data (string-match org-from-is-user-regexp from)))
  10173. (setq fmt (replace-match "to %t" t t fmt))
  10174. (setq fmt (replace-match "from %f" t t fmt))))
  10175. (org-replace-escapes fmt table)))
  10176. (defun org-make-org-heading-search-string (&optional string heading)
  10177. "Make search string for STRING or current headline."
  10178. (interactive)
  10179. (let ((s (or string (org-get-heading))))
  10180. (unless (and string (not heading))
  10181. ;; We are using a headline, clean up garbage in there.
  10182. (if (string-match org-todo-regexp s)
  10183. (setq s (replace-match "" t t s)))
  10184. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10185. (setq s (replace-match "" t t s)))
  10186. (setq s (org-trim s))
  10187. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10188. org-comment-string "\\)") s)
  10189. (setq s (replace-match "" t t s)))
  10190. (while (string-match org-ts-regexp s)
  10191. (setq s (replace-match "" t t s))))
  10192. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10193. (setq s (replace-match " " t t s)))
  10194. (or string (setq s (concat "*" s))) ; Add * for headlines
  10195. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10196. (defun org-make-link (&rest strings)
  10197. "Concatenate STRINGS."
  10198. (apply 'concat strings))
  10199. (defun org-make-link-string (link &optional description)
  10200. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10201. (unless (string-match "\\S-" link)
  10202. (error "Empty link"))
  10203. (when (stringp description)
  10204. ;; Remove brackets from the description, they are fatal.
  10205. (while (string-match "\\[\\|\\]" description)
  10206. (setq description (replace-match "" t t description))))
  10207. (when (equal (org-link-escape link) description)
  10208. ;; No description needed, it is identical
  10209. (setq description nil))
  10210. (when (and (not description)
  10211. (not (equal link (org-link-escape link))))
  10212. (setq description link))
  10213. (concat "[[" (org-link-escape link) "]"
  10214. (if description (concat "[" description "]") "")
  10215. "]"))
  10216. (defconst org-link-escape-chars
  10217. '((" " . "%20")
  10218. ("[" . "%5B")
  10219. ("]" . "%5d")
  10220. ("\340" . "%E0") ; `a
  10221. ("\342" . "%E2") ; ^a
  10222. ("\347" . "%E7") ; ,c
  10223. ("\350" . "%E8") ; `e
  10224. ("\351" . "%E9") ; 'e
  10225. ("\352" . "%EA") ; ^e
  10226. ("\356" . "%EE") ; ^i
  10227. ("\364" . "%F4") ; ^o
  10228. ("\371" . "%F9") ; `u
  10229. ("\373" . "%FB") ; ^u
  10230. (";" . "%3B")
  10231. ("?" . "%3F")
  10232. ("=" . "%3D")
  10233. ("+" . "%2B")
  10234. )
  10235. "Association list of escapes for some characters problematic in links.
  10236. This is the list that is used for internal purposes.")
  10237. (defconst org-link-escape-chars-browser
  10238. '((" " . "%20"))
  10239. "Association list of escapes for some characters problematic in links.
  10240. This is the list that is used before handing over to the browser.")
  10241. (defun org-link-escape (text &optional table)
  10242. "Escape charaters in TEXT that are problematic for links."
  10243. (setq table (or table org-link-escape-chars))
  10244. (when text
  10245. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10246. table "\\|")))
  10247. (while (string-match re text)
  10248. (setq text
  10249. (replace-match
  10250. (cdr (assoc (match-string 0 text) table))
  10251. t t text)))
  10252. text)))
  10253. (defun org-link-unescape (text &optional table)
  10254. "Reverse the action of `org-link-escape'."
  10255. (setq table (or table org-link-escape-chars))
  10256. (when text
  10257. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10258. table "\\|")))
  10259. (while (string-match re text)
  10260. (setq text
  10261. (replace-match
  10262. (car (rassoc (match-string 0 text) table))
  10263. t t text)))
  10264. text)))
  10265. (defun org-xor (a b)
  10266. "Exclusive or."
  10267. (if a (not b) b))
  10268. (defun org-get-header (header)
  10269. "Find a header field in the current buffer."
  10270. (save-excursion
  10271. (goto-char (point-min))
  10272. (let ((case-fold-search t) s)
  10273. (cond
  10274. ((eq header 'from)
  10275. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10276. (setq s (match-string 1)))
  10277. (while (string-match "\"" s)
  10278. (setq s (replace-match "" t t s)))
  10279. (if (string-match "[<(].*" s)
  10280. (setq s (replace-match "" t t s))))
  10281. ((eq header 'message-id)
  10282. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10283. (setq s (match-string 1))))
  10284. ((eq header 'subject)
  10285. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10286. (setq s (match-string 1)))))
  10287. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10288. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10289. s)))
  10290. (defun org-fixup-message-id-for-http (s)
  10291. "Replace special characters in a message id, so it can be used in an http query."
  10292. (while (string-match "<" s)
  10293. (setq s (replace-match "%3C" t t s)))
  10294. (while (string-match ">" s)
  10295. (setq s (replace-match "%3E" t t s)))
  10296. (while (string-match "@" s)
  10297. (setq s (replace-match "%40" t t s)))
  10298. s)
  10299. ;;;###autoload
  10300. (defun org-insert-link-global ()
  10301. "Insert a link like Org-mode does.
  10302. This command can be called in any mode to insert a link in Org-mode syntax."
  10303. (interactive)
  10304. (org-run-like-in-org-mode 'org-insert-link))
  10305. (defun org-insert-link (&optional complete-file)
  10306. "Insert a link. At the prompt, enter the link.
  10307. Completion can be used to select a link previously stored with
  10308. `org-store-link'. When the empty string is entered (i.e. if you just
  10309. press RET at the prompt), the link defaults to the most recently
  10310. stored link. As SPC triggers completion in the minibuffer, you need to
  10311. use M-SPC or C-q SPC to force the insertion of a space character.
  10312. You will also be prompted for a description, and if one is given, it will
  10313. be displayed in the buffer instead of the link.
  10314. If there is already a link at point, this command will allow you to edit link
  10315. and description parts.
  10316. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10317. selected using completion. The path to the file will be relative to
  10318. the current directory if the file is in the current directory or a
  10319. subdirectory. Otherwise, the link will be the absolute path as
  10320. completed in the minibuffer (i.e. normally ~/path/to/file).
  10321. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10322. is in the current directory or below.
  10323. With three \\[universal-argument] prefixes, negate the meaning of
  10324. `org-keep-stored-link-after-insertion'."
  10325. (interactive "P")
  10326. (let ((wcf (current-window-configuration))
  10327. (region (if (org-region-active-p)
  10328. (prog1 (buffer-substring (region-beginning) (region-end))
  10329. (delete-region (region-beginning) (region-end)))))
  10330. tmphist ; byte-compile incorrectly complains about this
  10331. link desc entry remove file)
  10332. (cond
  10333. ((org-in-regexp org-bracket-link-regexp 1)
  10334. ;; We do have a link at point, and we are going to edit it.
  10335. (setq remove (list (match-beginning 0) (match-end 0)))
  10336. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10337. (setq link (read-string "Link: "
  10338. (org-link-unescape
  10339. (org-match-string-no-properties 1)))))
  10340. ((or (org-in-regexp org-angle-link-re)
  10341. (org-in-regexp org-plain-link-re))
  10342. ;; Convert to bracket link
  10343. (setq remove (list (match-beginning 0) (match-end 0))
  10344. link (read-string "Link: "
  10345. (org-remove-angle-brackets (match-string 0)))))
  10346. ((equal complete-file '(4))
  10347. ;; Completing read for file names.
  10348. (setq file (read-file-name "File: "))
  10349. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10350. (pwd1 (file-name-as-directory (abbreviate-file-name
  10351. (expand-file-name ".")))))
  10352. (cond
  10353. ((equal complete-file '(16))
  10354. (setq link (org-make-link
  10355. "file:"
  10356. (abbreviate-file-name (expand-file-name file)))))
  10357. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10358. (setq link (org-make-link "file:" (match-string 1 file))))
  10359. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10360. (expand-file-name file))
  10361. (setq link (org-make-link
  10362. "file:" (match-string 1 (expand-file-name file)))))
  10363. (t (setq link (org-make-link "file:" file))))))
  10364. (t
  10365. ;; Read link, with completion for stored links.
  10366. (with-output-to-temp-buffer "*Org Links*"
  10367. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10368. (when org-stored-links
  10369. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  10370. (princ (mapconcat
  10371. (lambda (x)
  10372. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  10373. (reverse org-stored-links) "\n"))))
  10374. (let ((cw (selected-window)))
  10375. (select-window (get-buffer-window "*Org Links*"))
  10376. (shrink-window-if-larger-than-buffer)
  10377. (setq truncate-lines t)
  10378. (select-window cw))
  10379. ;; Fake a link history, containing the stored links.
  10380. (setq tmphist (append (mapcar 'car org-stored-links)
  10381. org-insert-link-history))
  10382. (unwind-protect
  10383. (setq link (org-completing-read
  10384. "Link: "
  10385. (append
  10386. (mapcar (lambda (x) (list (concat (car x) ":")))
  10387. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10388. (mapcar (lambda (x) (list (concat x ":")))
  10389. org-link-types))
  10390. nil nil nil
  10391. 'tmphist
  10392. (or (car (car org-stored-links)))))
  10393. (set-window-configuration wcf)
  10394. (kill-buffer "*Org Links*"))
  10395. (setq entry (assoc link org-stored-links))
  10396. (or entry (push link org-insert-link-history))
  10397. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10398. (not org-keep-stored-link-after-insertion))
  10399. (setq org-stored-links (delq (assoc link org-stored-links)
  10400. org-stored-links)))
  10401. (setq desc (or region desc (nth 1 entry)))))
  10402. (if (string-match org-plain-link-re link)
  10403. ;; URL-like link, normalize the use of angular brackets.
  10404. (setq link (org-make-link (org-remove-angle-brackets link))))
  10405. ;; Check if we are linking to the current file with a search option
  10406. ;; If yes, simplify the link by using only the search option.
  10407. (when (and buffer-file-name
  10408. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10409. (let* ((path (match-string 1 link))
  10410. (case-fold-search nil)
  10411. (search (match-string 2 link)))
  10412. (save-match-data
  10413. (if (equal (file-truename buffer-file-name) (file-truename path))
  10414. ;; We are linking to this same file, with a search option
  10415. (setq link search)))))
  10416. ;; Check if we can/should use a relative path. If yes, simplify the link
  10417. (when (string-match "\\<file:\\(.*\\)" link)
  10418. (let* ((path (match-string 1 link))
  10419. (desc-is-link (equal link desc))
  10420. (case-fold-search nil))
  10421. (cond
  10422. ((eq org-link-file-path-type 'absolute)
  10423. (setq path (abbreviate-file-name (expand-file-name path))))
  10424. ((eq org-link-file-path-type 'noabbrev)
  10425. (setq path (expand-file-name path)))
  10426. ((eq org-link-file-path-type 'relative)
  10427. (setq path (file-relative-name path)))
  10428. (t
  10429. (save-match-data
  10430. (if (string-match (concat "^" (regexp-quote
  10431. (file-name-as-directory
  10432. (expand-file-name "."))))
  10433. (expand-file-name path))
  10434. ;; We are linking a file with relative path name.
  10435. (setq path (substring (expand-file-name path)
  10436. (match-end 0)))))))
  10437. (setq link (concat "file:" path))
  10438. (if desc (setq desc link))))
  10439. (setq desc (read-string "Description: " desc))
  10440. (unless (string-match "\\S-" desc) (setq desc nil))
  10441. (if remove (apply 'delete-region remove))
  10442. (insert (org-make-link-string link desc))))
  10443. (defun org-completing-read (&rest args)
  10444. (let ((minibuffer-local-completion-map
  10445. (copy-keymap minibuffer-local-completion-map)))
  10446. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10447. (apply 'completing-read args)))
  10448. ;;; Opening/following a link
  10449. (defvar org-link-search-failed nil)
  10450. (defun org-next-link ()
  10451. "Move forward to the next link.
  10452. If the link is in hidden text, expose it."
  10453. (interactive)
  10454. (when (and org-link-search-failed (eq this-command last-command))
  10455. (goto-char (point-min))
  10456. (message "Link search wrapped back to beginning of buffer"))
  10457. (setq org-link-search-failed nil)
  10458. (let* ((pos (point))
  10459. (ct (org-context))
  10460. (a (assoc :link ct)))
  10461. (if a (goto-char (nth 2 a)))
  10462. (if (re-search-forward org-any-link-re nil t)
  10463. (progn
  10464. (goto-char (match-beginning 0))
  10465. (if (org-invisible-p) (org-show-context)))
  10466. (goto-char pos)
  10467. (setq org-link-search-failed t)
  10468. (error "No further link found"))))
  10469. (defun org-previous-link ()
  10470. "Move backward to the previous link.
  10471. If the link is in hidden text, expose it."
  10472. (interactive)
  10473. (when (and org-link-search-failed (eq this-command last-command))
  10474. (goto-char (point-max))
  10475. (message "Link search wrapped back to end of buffer"))
  10476. (setq org-link-search-failed nil)
  10477. (let* ((pos (point))
  10478. (ct (org-context))
  10479. (a (assoc :link ct)))
  10480. (if a (goto-char (nth 1 a)))
  10481. (if (re-search-backward org-any-link-re nil t)
  10482. (progn
  10483. (goto-char (match-beginning 0))
  10484. (if (org-invisible-p) (org-show-context)))
  10485. (goto-char pos)
  10486. (setq org-link-search-failed t)
  10487. (error "No further link found"))))
  10488. (defun org-find-file-at-mouse (ev)
  10489. "Open file link or URL at mouse."
  10490. (interactive "e")
  10491. (mouse-set-point ev)
  10492. (org-open-at-point 'in-emacs))
  10493. (defun org-open-at-mouse (ev)
  10494. "Open file link or URL at mouse."
  10495. (interactive "e")
  10496. (mouse-set-point ev)
  10497. (org-open-at-point))
  10498. (defvar org-window-config-before-follow-link nil
  10499. "The window configuration before following a link.
  10500. This is saved in case the need arises to restore it.")
  10501. (defvar org-open-link-marker (make-marker)
  10502. "Marker pointing to the location where `org-open-at-point; was called.")
  10503. ;;;###autoload
  10504. (defun org-open-at-point-global ()
  10505. "Follow a link like Org-mode does.
  10506. This command can be called in any mode to follow a link that has
  10507. Org-mode syntax."
  10508. (interactive)
  10509. (org-run-like-in-org-mode 'org-open-at-point))
  10510. (defun org-open-at-point (&optional in-emacs)
  10511. "Open link at or after point.
  10512. If there is no link at point, this function will search forward up to
  10513. the end of the current subtree.
  10514. Normally, files will be opened by an appropriate application. If the
  10515. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10516. (interactive "P")
  10517. (move-marker org-open-link-marker (point))
  10518. (setq org-window-config-before-follow-link (current-window-configuration))
  10519. (org-remove-occur-highlights nil nil t)
  10520. (if (org-at-timestamp-p t)
  10521. (org-follow-timestamp-link)
  10522. (let (type path link line search (pos (point)))
  10523. (catch 'match
  10524. (save-excursion
  10525. (skip-chars-forward "^]\n\r")
  10526. (when (org-in-regexp org-bracket-link-regexp)
  10527. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10528. (while (string-match " *\n *" link)
  10529. (setq link (replace-match " " t t link)))
  10530. (setq link (org-link-expand-abbrev link))
  10531. (if (string-match org-link-re-with-space2 link)
  10532. (setq type (match-string 1 link) path (match-string 2 link))
  10533. (setq type "thisfile" path link))
  10534. (throw 'match t)))
  10535. (when (get-text-property (point) 'org-linked-text)
  10536. (setq type "thisfile"
  10537. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10538. (1+ (point)) (point))
  10539. path (buffer-substring
  10540. (previous-single-property-change pos 'org-linked-text)
  10541. (next-single-property-change pos 'org-linked-text)))
  10542. (throw 'match t))
  10543. (save-excursion
  10544. (when (or (org-in-regexp org-angle-link-re)
  10545. (org-in-regexp org-plain-link-re))
  10546. (setq type (match-string 1) path (match-string 2))
  10547. (throw 'match t)))
  10548. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10549. (setq type "tree-match"
  10550. path (match-string 1))
  10551. (throw 'match t))
  10552. (save-excursion
  10553. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10554. (setq type "tags"
  10555. path (match-string 1))
  10556. (while (string-match ":" path)
  10557. (setq path (replace-match "+" t t path)))
  10558. (throw 'match t))))
  10559. (unless path
  10560. (error "No link found"))
  10561. ;; Remove any trailing spaces in path
  10562. (if (string-match " +\\'" path)
  10563. (setq path (replace-match "" t t path)))
  10564. (cond
  10565. ((assoc type org-link-protocols)
  10566. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10567. ((equal type "mailto")
  10568. (let ((cmd (car org-link-mailto-program))
  10569. (args (cdr org-link-mailto-program)) args1
  10570. (address path) (subject "") a)
  10571. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10572. (setq address (match-string 1 path)
  10573. subject (org-link-escape (match-string 2 path))))
  10574. (while args
  10575. (cond
  10576. ((not (stringp (car args))) (push (pop args) args1))
  10577. (t (setq a (pop args))
  10578. (if (string-match "%a" a)
  10579. (setq a (replace-match address t t a)))
  10580. (if (string-match "%s" a)
  10581. (setq a (replace-match subject t t a)))
  10582. (push a args1))))
  10583. (apply cmd (nreverse args1))))
  10584. ((member type '("http" "https" "ftp" "news"))
  10585. (browse-url (concat type ":" (org-link-escape
  10586. path org-link-escape-chars-browser))))
  10587. ((string= type "tags")
  10588. (org-tags-view in-emacs path))
  10589. ((string= type "thisfile")
  10590. (if in-emacs
  10591. (switch-to-buffer-other-window
  10592. (org-get-buffer-for-internal-link (current-buffer)))
  10593. (org-mark-ring-push))
  10594. (let ((cmd `(org-link-search
  10595. ,path
  10596. ,(cond ((equal in-emacs '(4)) 'occur)
  10597. ((equal in-emacs '(16)) 'org-occur)
  10598. (t nil))
  10599. ,pos)))
  10600. (condition-case nil (eval cmd)
  10601. (error (progn (widen) (eval cmd))))))
  10602. ((string= type "tree-match")
  10603. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10604. ((string= type "file")
  10605. (if (string-match "::\\([0-9]+\\)\\'" path)
  10606. (setq line (string-to-number (match-string 1 path))
  10607. path (substring path 0 (match-beginning 0)))
  10608. (if (string-match "::\\(.+\\)\\'" path)
  10609. (setq search (match-string 1 path)
  10610. path (substring path 0 (match-beginning 0)))))
  10611. (org-open-file path in-emacs line search))
  10612. ((string= type "news")
  10613. (org-follow-gnus-link path))
  10614. ((string= type "bbdb")
  10615. (org-follow-bbdb-link path))
  10616. ((string= type "info")
  10617. (org-follow-info-link path))
  10618. ((string= type "gnus")
  10619. (let (group article)
  10620. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10621. (error "Error in Gnus link"))
  10622. (setq group (match-string 1 path)
  10623. article (match-string 3 path))
  10624. (org-follow-gnus-link group article)))
  10625. ((string= type "vm")
  10626. (let (folder article)
  10627. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10628. (error "Error in VM link"))
  10629. (setq folder (match-string 1 path)
  10630. article (match-string 3 path))
  10631. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10632. (org-follow-vm-link folder article in-emacs)))
  10633. ((string= type "wl")
  10634. (let (folder article)
  10635. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10636. (error "Error in Wanderlust link"))
  10637. (setq folder (match-string 1 path)
  10638. article (match-string 3 path))
  10639. (org-follow-wl-link folder article)))
  10640. ((string= type "mhe")
  10641. (let (folder article)
  10642. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10643. (error "Error in MHE link"))
  10644. (setq folder (match-string 1 path)
  10645. article (match-string 3 path))
  10646. (org-follow-mhe-link folder article)))
  10647. ((string= type "rmail")
  10648. (let (folder article)
  10649. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10650. (error "Error in RMAIL link"))
  10651. (setq folder (match-string 1 path)
  10652. article (match-string 3 path))
  10653. (org-follow-rmail-link folder article)))
  10654. ((string= type "shell")
  10655. (let ((cmd path))
  10656. ;; FIXME: the following is only for backward compatibility
  10657. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10658. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10659. (if (or (not org-confirm-shell-link-function)
  10660. (funcall org-confirm-shell-link-function
  10661. (format "Execute \"%s\" in shell? "
  10662. (org-add-props cmd nil
  10663. 'face 'org-warning))))
  10664. (progn
  10665. (message "Executing %s" cmd)
  10666. (shell-command cmd))
  10667. (error "Abort"))))
  10668. ((string= type "elisp")
  10669. (let ((cmd path))
  10670. (if (or (not org-confirm-elisp-link-function)
  10671. (funcall org-confirm-elisp-link-function
  10672. (format "Execute \"%s\" as elisp? "
  10673. (org-add-props cmd nil
  10674. 'face 'org-warning))))
  10675. (message "%s => %s" cmd (eval (read cmd)))
  10676. (error "Abort"))))
  10677. (t
  10678. (browse-url-at-point)))))
  10679. (move-marker org-open-link-marker nil))
  10680. ;;; File search
  10681. (defvar org-create-file-search-functions nil
  10682. "List of functions to construct the right search string for a file link.
  10683. These functions are called in turn with point at the location to
  10684. which the link should point.
  10685. A function in the hook should first test if it would like to
  10686. handle this file type, for example by checking the major-mode or
  10687. the file extension. If it decides not to handle this file, it
  10688. should just return nil to give other functions a chance. If it
  10689. does handle the file, it must return the search string to be used
  10690. when following the link. The search string will be part of the
  10691. file link, given after a double colon, and `org-open-at-point'
  10692. will automatically search for it. If special measures must be
  10693. taken to make the search successful, another function should be
  10694. added to the companion hook `org-execute-file-search-functions',
  10695. which see.
  10696. A function in this hook may also use `setq' to set the variable
  10697. `description' to provide a suggestion for the descriptive text to
  10698. be used for this link when it gets inserted into an Org-mode
  10699. buffer with \\[org-insert-link].")
  10700. (defvar org-execute-file-search-functions nil
  10701. "List of functions to execute a file search triggered by a link.
  10702. Functions added to this hook must accept a single argument, the
  10703. search string that was part of the file link, the part after the
  10704. double colon. The function must first check if it would like to
  10705. handle this search, for example by checking the major-mode or the
  10706. file extension. If it decides not to handle this search, it
  10707. should just return nil to give other functions a chance. If it
  10708. does handle the search, it must return a non-nil value to keep
  10709. other functions from trying.
  10710. Each function can access the current prefix argument through the
  10711. variable `current-prefix-argument'. Note that a single prefix is
  10712. used to force opening a link in Emacs, so it may be good to only
  10713. use a numeric or double prefix to guide the search function.
  10714. In case this is needed, a function in this hook can also restore
  10715. the window configuration before `org-open-at-point' was called using:
  10716. (set-window-configuration org-window-config-before-follow-link)")
  10717. (defun org-link-search (s &optional type avoid-pos)
  10718. "Search for a link search option.
  10719. If S is surrounded by forward slashes, it is interpreted as a
  10720. regular expression. In org-mode files, this will create an `org-occur'
  10721. sparse tree. In ordinary files, `occur' will be used to list matches.
  10722. If the current buffer is in `dired-mode', grep will be used to search
  10723. in all files. If AVOID-POS is given, ignore matches near that position."
  10724. (let ((case-fold-search t)
  10725. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10726. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10727. (append '(("") (" ") ("\t") ("\n"))
  10728. org-emphasis-alist)
  10729. "\\|") "\\)"))
  10730. (pos (point))
  10731. (pre "") (post "")
  10732. words re0 re1 re2 re3 re4 re5 re2a reall)
  10733. (cond
  10734. ;; First check if there are any special
  10735. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10736. ;; Now try the builtin stuff
  10737. ((save-excursion
  10738. (goto-char (point-min))
  10739. (and
  10740. (re-search-forward
  10741. (concat "<<" (regexp-quote s0) ">>") nil t)
  10742. (setq pos (match-beginning 0))))
  10743. ;; There is an exact target for this
  10744. (goto-char pos))
  10745. ((string-match "^/\\(.*\\)/$" s)
  10746. ;; A regular expression
  10747. (cond
  10748. ((org-mode-p)
  10749. (org-occur (match-string 1 s)))
  10750. ;;((eq major-mode 'dired-mode)
  10751. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10752. (t (org-do-occur (match-string 1 s)))))
  10753. (t
  10754. ;; A normal search strings
  10755. (when (equal (string-to-char s) ?*)
  10756. ;; Anchor on headlines, post may include tags.
  10757. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10758. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10759. s (substring s 1)))
  10760. (remove-text-properties
  10761. 0 (length s)
  10762. '(face nil mouse-face nil keymap nil fontified nil) s)
  10763. ;; Make a series of regular expressions to find a match
  10764. (setq words (org-split-string s "[ \n\r\t]+")
  10765. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10766. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10767. "\\)" markers)
  10768. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10769. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10770. re1 (concat pre re2 post)
  10771. re3 (concat pre re4 post)
  10772. re5 (concat pre ".*" re4)
  10773. re2 (concat pre re2)
  10774. re2a (concat pre re2a)
  10775. re4 (concat pre re4)
  10776. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10777. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10778. re5 "\\)"
  10779. ))
  10780. (cond
  10781. ((eq type 'org-occur) (org-occur reall))
  10782. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10783. (t (goto-char (point-min))
  10784. (if (or (org-search-not-self 1 re0 nil t)
  10785. (org-search-not-self 1 re1 nil t)
  10786. (org-search-not-self 1 re2 nil t)
  10787. (org-search-not-self 1 re2a nil t)
  10788. (org-search-not-self 1 re3 nil t)
  10789. (org-search-not-self 1 re4 nil t)
  10790. (org-search-not-self 1 re5 nil t)
  10791. )
  10792. (goto-char (match-beginning 1))
  10793. (goto-char pos)
  10794. (error "No match")))))
  10795. (t
  10796. ;; Normal string-search
  10797. (goto-char (point-min))
  10798. (if (search-forward s nil t)
  10799. (goto-char (match-beginning 0))
  10800. (error "No match"))))
  10801. (and (org-mode-p) (org-show-context 'link-search))))
  10802. (defun org-search-not-self (group &rest args)
  10803. "Execute `re-search-forward', but only accept matches that do not
  10804. enclose the position of `org-open-link-marker'."
  10805. (let ((m org-open-link-marker))
  10806. (catch 'exit
  10807. (while (apply 're-search-forward args)
  10808. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10809. (goto-char (match-end group))
  10810. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10811. (> (match-beginning 0) (marker-position m))
  10812. (< (match-end 0) (marker-position m)))
  10813. (save-match-data
  10814. (or (not (org-in-regexp
  10815. org-bracket-link-analytic-regexp 1))
  10816. (not (match-end 4)) ; no description
  10817. (and (<= (match-beginning 4) (point))
  10818. (>= (match-end 4) (point))))))
  10819. (throw 'exit (point))))))))
  10820. (defun org-get-buffer-for-internal-link (buffer)
  10821. "Return a buffer to be used for displaying the link target of internal links."
  10822. (cond
  10823. ((not org-display-internal-link-with-indirect-buffer)
  10824. buffer)
  10825. ((string-match "(Clone)$" (buffer-name buffer))
  10826. (message "Buffer is already a clone, not making another one")
  10827. ;; we also do not modify visibility in this case
  10828. buffer)
  10829. (t ; make a new indirect buffer for displaying the link
  10830. (let* ((bn (buffer-name buffer))
  10831. (ibn (concat bn "(Clone)"))
  10832. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10833. (with-current-buffer ib (org-overview))
  10834. ib))))
  10835. (defun org-do-occur (regexp &optional cleanup)
  10836. "Call the Emacs command `occur'.
  10837. If CLEANUP is non-nil, remove the printout of the regular expression
  10838. in the *Occur* buffer. This is useful if the regex is long and not useful
  10839. to read."
  10840. (occur regexp)
  10841. (when cleanup
  10842. (let ((cwin (selected-window)) win beg end)
  10843. (when (setq win (get-buffer-window "*Occur*"))
  10844. (select-window win))
  10845. (goto-char (point-min))
  10846. (when (re-search-forward "match[a-z]+" nil t)
  10847. (setq beg (match-end 0))
  10848. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10849. (setq end (1- (match-beginning 0)))))
  10850. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10851. (goto-char (point-min))
  10852. (select-window cwin))))
  10853. ;;; The mark ring for links jumps
  10854. (defvar org-mark-ring nil
  10855. "Mark ring for positions before jumps in Org-mode.")
  10856. (defvar org-mark-ring-last-goto nil
  10857. "Last position in the mark ring used to go back.")
  10858. ;; Fill and close the ring
  10859. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10860. (loop for i from 1 to org-mark-ring-length do
  10861. (push (make-marker) org-mark-ring))
  10862. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10863. org-mark-ring)
  10864. (defun org-mark-ring-push (&optional pos buffer)
  10865. "Put the current position or POS into the mark ring and rotate it."
  10866. (interactive)
  10867. (setq pos (or pos (point)))
  10868. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10869. (move-marker (car org-mark-ring)
  10870. (or pos (point))
  10871. (or buffer (current-buffer)))
  10872. (message
  10873. (substitute-command-keys
  10874. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10875. (defun org-mark-ring-goto (&optional n)
  10876. "Jump to the previous position in the mark ring.
  10877. With prefix arg N, jump back that many stored positions. When
  10878. called several times in succession, walk through the entire ring.
  10879. Org-mode commands jumping to a different position in the current file,
  10880. or to another Org-mode file, automatically push the old position
  10881. onto the ring."
  10882. (interactive "p")
  10883. (let (p m)
  10884. (if (eq last-command this-command)
  10885. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10886. (setq p org-mark-ring))
  10887. (setq org-mark-ring-last-goto p)
  10888. (setq m (car p))
  10889. (switch-to-buffer (marker-buffer m))
  10890. (goto-char m)
  10891. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10892. (defun org-remove-angle-brackets (s)
  10893. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10894. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10895. s)
  10896. (defun org-add-angle-brackets (s)
  10897. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10898. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10899. s)
  10900. ;;; Following specific links
  10901. (defun org-follow-timestamp-link ()
  10902. (cond
  10903. ((org-at-date-range-p t)
  10904. (let ((org-agenda-start-on-weekday)
  10905. (t1 (match-string 1))
  10906. (t2 (match-string 2)))
  10907. (setq t1 (time-to-days (org-time-string-to-time t1))
  10908. t2 (time-to-days (org-time-string-to-time t2)))
  10909. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10910. ((org-at-timestamp-p t)
  10911. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10912. (substring (match-string 1) 0 10)))
  10913. 1))
  10914. (t (error "This should not happen"))))
  10915. (defun org-follow-bbdb-link (name)
  10916. "Follow a BBDB link to NAME."
  10917. (require 'bbdb)
  10918. (let ((inhibit-redisplay (not debug-on-error))
  10919. (bbdb-electric-p nil))
  10920. (catch 'exit
  10921. ;; Exact match on name
  10922. (bbdb-name (concat "\\`" name "\\'") nil)
  10923. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10924. ;; Exact match on name
  10925. (bbdb-company (concat "\\`" name "\\'") nil)
  10926. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10927. ;; Partial match on name
  10928. (bbdb-name name nil)
  10929. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10930. ;; Partial match on company
  10931. (bbdb-company name nil)
  10932. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10933. ;; General match including network address and notes
  10934. (bbdb name nil)
  10935. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10936. (delete-window (get-buffer-window "*BBDB*"))
  10937. (error "No matching BBDB record")))))
  10938. (defun org-follow-info-link (name)
  10939. "Follow an info file & node link to NAME."
  10940. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10941. (string-match "\\(.*\\)" name))
  10942. (progn
  10943. (require 'info)
  10944. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10945. (Info-find-node (match-string 1 name) (match-string 2 name))
  10946. (Info-find-node (match-string 1 name) "Top")))
  10947. (message (concat "Could not open: " name))))
  10948. (defun org-follow-gnus-link (&optional group article)
  10949. "Follow a Gnus link to GROUP and ARTICLE."
  10950. (require 'gnus)
  10951. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10952. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10953. (cond ((and group article)
  10954. (gnus-group-read-group 1 nil group)
  10955. (gnus-summary-goto-article (string-to-number article) nil t))
  10956. (group (gnus-group-jump-to-group group))))
  10957. (defun org-follow-vm-link (&optional folder article readonly)
  10958. "Follow a VM link to FOLDER and ARTICLE."
  10959. (require 'vm)
  10960. (setq article (org-add-angle-brackets article))
  10961. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10962. ;; ange-ftp or efs or tramp access
  10963. (let ((user (or (match-string 1 folder) (user-login-name)))
  10964. (host (match-string 2 folder))
  10965. (file (match-string 3 folder)))
  10966. (cond
  10967. ((featurep 'tramp)
  10968. ;; use tramp to access the file
  10969. (if (featurep 'xemacs)
  10970. (setq folder (format "[%s@%s]%s" user host file))
  10971. (setq folder (format "/%s@%s:%s" user host file))))
  10972. (t
  10973. ;; use ange-ftp or efs
  10974. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10975. (setq folder (format "/%s@%s:%s" user host file))))))
  10976. (when folder
  10977. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10978. (sit-for 0.1)
  10979. (when article
  10980. (vm-select-folder-buffer)
  10981. (widen)
  10982. (let ((case-fold-search t))
  10983. (goto-char (point-min))
  10984. (if (not (re-search-forward
  10985. (concat "^" "message-id: *" (regexp-quote article))))
  10986. (error "Could not find the specified message in this folder"))
  10987. (vm-isearch-update)
  10988. (vm-isearch-narrow)
  10989. (vm-beginning-of-message)
  10990. (vm-summarize)))))
  10991. (defun org-follow-wl-link (folder article)
  10992. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10993. (if (and (string= folder "%")
  10994. article
  10995. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10996. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10997. ;; Thus, we recompose folder and article ids.
  10998. (setq folder (format "%s#%s" folder (match-string 1 article))
  10999. article (match-string 3 article)))
  11000. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11001. (error "No such folder: %s" folder))
  11002. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11003. (and article
  11004. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11005. (wl-summary-redisplay)))
  11006. (defun org-follow-rmail-link (folder article)
  11007. "Follow an RMAIL link to FOLDER and ARTICLE."
  11008. (setq article (org-add-angle-brackets article))
  11009. (let (message-number)
  11010. (save-excursion
  11011. (save-window-excursion
  11012. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11013. (setq message-number
  11014. (save-restriction
  11015. (widen)
  11016. (goto-char (point-max))
  11017. (if (re-search-backward
  11018. (concat "^Message-ID:\\s-+" (regexp-quote
  11019. (or article "")))
  11020. nil t)
  11021. (rmail-what-message))))))
  11022. (if message-number
  11023. (progn
  11024. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11025. (rmail-show-message message-number)
  11026. message-number)
  11027. (error "Message not found"))))
  11028. ;;; mh-e integration based on planner-mode
  11029. (defun org-mhe-get-message-real-folder ()
  11030. "Return the name of the current message real folder, so if you use
  11031. sequences, it will now work."
  11032. (save-excursion
  11033. (let* ((folder
  11034. (if (equal major-mode 'mh-folder-mode)
  11035. mh-current-folder
  11036. ;; Refer to the show buffer
  11037. mh-show-folder-buffer))
  11038. (end-index
  11039. (if (boundp 'mh-index-folder)
  11040. (min (length mh-index-folder) (length folder))))
  11041. )
  11042. ;; a simple test on mh-index-data does not work, because
  11043. ;; mh-index-data is always nil in a show buffer.
  11044. (if (and (boundp 'mh-index-folder)
  11045. (string= mh-index-folder (substring folder 0 end-index)))
  11046. (if (equal major-mode 'mh-show-mode)
  11047. (save-window-excursion
  11048. (let (pop-up-frames)
  11049. (when (buffer-live-p (get-buffer folder))
  11050. (progn
  11051. (pop-to-buffer folder)
  11052. (org-mhe-get-message-folder-from-index)
  11053. )
  11054. )))
  11055. (org-mhe-get-message-folder-from-index)
  11056. )
  11057. folder
  11058. )
  11059. )))
  11060. (defun org-mhe-get-message-folder-from-index ()
  11061. "Returns the name of the message folder in a index folder buffer."
  11062. (save-excursion
  11063. (mh-index-previous-folder)
  11064. (re-search-forward "^\\(+.*\\)$" nil t)
  11065. (message (match-string 1))))
  11066. (defun org-mhe-get-message-folder ()
  11067. "Return the name of the current message folder. Be careful if you
  11068. use sequences."
  11069. (save-excursion
  11070. (if (equal major-mode 'mh-folder-mode)
  11071. mh-current-folder
  11072. ;; Refer to the show buffer
  11073. mh-show-folder-buffer)))
  11074. (defun org-mhe-get-message-num ()
  11075. "Return the number of the current message. Be careful if you
  11076. use sequences."
  11077. (save-excursion
  11078. (if (equal major-mode 'mh-folder-mode)
  11079. (mh-get-msg-num nil)
  11080. ;; Refer to the show buffer
  11081. (mh-show-buffer-message-number))))
  11082. (defun org-mhe-get-header (header)
  11083. "Return a header of the message in folder mode. This will create a
  11084. show buffer for the corresponding message. If you have a more clever
  11085. idea..."
  11086. (let* ((folder (org-mhe-get-message-folder))
  11087. (num (org-mhe-get-message-num))
  11088. (buffer (get-buffer-create (concat "show-" folder)))
  11089. (header-field))
  11090. (with-current-buffer buffer
  11091. (mh-display-msg num folder)
  11092. (if (equal major-mode 'mh-folder-mode)
  11093. (mh-header-display)
  11094. (mh-show-header-display))
  11095. (set-buffer buffer)
  11096. (setq header-field (mh-get-header-field header))
  11097. (if (equal major-mode 'mh-folder-mode)
  11098. (mh-show)
  11099. (mh-show-show))
  11100. header-field)))
  11101. (defun org-follow-mhe-link (folder article)
  11102. "Follow an MHE link to FOLDER and ARTICLE.
  11103. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11104. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11105. ARTICLE is searched in all folders. Indexed searches (swish++,
  11106. namazu, and others supported by MH-E) will always search in all
  11107. folders."
  11108. (require 'mh-e)
  11109. (require 'mh-search)
  11110. (require 'mh-utils)
  11111. (mh-find-path)
  11112. (if (not article)
  11113. (mh-visit-folder (mh-normalize-folder-name folder))
  11114. (setq article (org-add-angle-brackets article))
  11115. (mh-search-choose)
  11116. (if (equal mh-searcher 'pick)
  11117. (progn
  11118. (mh-search folder (list "--message-id" article))
  11119. (when (and org-mhe-search-all-folders
  11120. (not (org-mhe-get-message-real-folder)))
  11121. (kill-this-buffer)
  11122. (mh-search "+" (list "--message-id" article))))
  11123. (mh-search "+" article))
  11124. (if (org-mhe-get-message-real-folder)
  11125. (mh-show-msg 1)
  11126. (kill-this-buffer)
  11127. (error "Message not found"))))
  11128. ;;; BibTeX links
  11129. ;; Use the custom search meachnism to construct and use search strings for
  11130. ;; file links to BibTeX database entries.
  11131. (defun org-create-file-search-in-bibtex ()
  11132. "Create the search string and description for a BibTeX database entry."
  11133. (when (eq major-mode 'bibtex-mode)
  11134. ;; yes, we want to construct this search string.
  11135. ;; Make a good description for this entry, using names, year and the title
  11136. ;; Put it into the `description' variable which is dynamically scoped.
  11137. (let ((bibtex-autokey-names 1)
  11138. (bibtex-autokey-names-stretch 1)
  11139. (bibtex-autokey-name-case-convert-function 'identity)
  11140. (bibtex-autokey-name-separator " & ")
  11141. (bibtex-autokey-additional-names " et al.")
  11142. (bibtex-autokey-year-length 4)
  11143. (bibtex-autokey-name-year-separator " ")
  11144. (bibtex-autokey-titlewords 3)
  11145. (bibtex-autokey-titleword-separator " ")
  11146. (bibtex-autokey-titleword-case-convert-function 'identity)
  11147. (bibtex-autokey-titleword-length 'infty)
  11148. (bibtex-autokey-year-title-separator ": "))
  11149. (setq description (bibtex-generate-autokey)))
  11150. ;; Now parse the entry, get the key and return it.
  11151. (save-excursion
  11152. (bibtex-beginning-of-entry)
  11153. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11154. (defun org-execute-file-search-in-bibtex (s)
  11155. "Find the link search string S as a key for a database entry."
  11156. (when (eq major-mode 'bibtex-mode)
  11157. ;; Yes, we want to do the search in this file.
  11158. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11159. (goto-char (point-min))
  11160. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11161. (regexp-quote s) "[ \t\n]*,") nil t)
  11162. (goto-char (match-beginning 0)))
  11163. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11164. ;; Use double prefix to indicate that any web link should be browsed
  11165. (let ((b (current-buffer)) (p (point)))
  11166. ;; Restore the window configuration because we just use the web link
  11167. (set-window-configuration org-window-config-before-follow-link)
  11168. (save-excursion (set-buffer b) (goto-char p)
  11169. (bibtex-url)))
  11170. (recenter 0)) ; Move entry start to beginning of window
  11171. ;; return t to indicate that the search is done.
  11172. t))
  11173. ;; Finally add the functions to the right hooks.
  11174. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11175. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11176. ;; end of Bibtex link setup
  11177. ;;; Following file links
  11178. (defun org-open-file (path &optional in-emacs line search)
  11179. "Open the file at PATH.
  11180. First, this expands any special file name abbreviations. Then the
  11181. configuration variable `org-file-apps' is checked if it contains an
  11182. entry for this file type, and if yes, the corresponding command is launched.
  11183. If no application is found, Emacs simply visits the file.
  11184. With optional argument IN-EMACS, Emacs will visit the file.
  11185. Optional LINE specifies a line to go to, optional SEARCH a string to
  11186. search for. If LINE or SEARCH is given, the file will always be
  11187. opened in Emacs.
  11188. If the file does not exist, an error is thrown."
  11189. (setq in-emacs (or in-emacs line search))
  11190. (let* ((file (if (equal path "")
  11191. buffer-file-name
  11192. (substitute-in-file-name (expand-file-name path))))
  11193. (apps (append org-file-apps (org-default-apps)))
  11194. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11195. (dirp (if remp nil (file-directory-p file)))
  11196. (dfile (downcase file))
  11197. (old-buffer (current-buffer))
  11198. (old-pos (point))
  11199. (old-mode major-mode)
  11200. ext cmd)
  11201. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11202. (setq ext (match-string 1 dfile))
  11203. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11204. (setq ext (match-string 1 dfile))))
  11205. (if in-emacs
  11206. (setq cmd 'emacs)
  11207. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11208. (and dirp (cdr (assoc 'directory apps)))
  11209. (cdr (assoc ext apps))
  11210. (cdr (assoc t apps)))))
  11211. (when (eq cmd 'mailcap)
  11212. (require 'mailcap)
  11213. (mailcap-parse-mailcaps)
  11214. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11215. (command (mailcap-mime-info mime-type)))
  11216. (if (stringp command)
  11217. (setq cmd command)
  11218. (setq cmd 'emacs))))
  11219. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11220. (not (file-exists-p file))
  11221. (not org-open-non-existing-files))
  11222. (error "No such file: %s" file))
  11223. (cond
  11224. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11225. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11226. (if (string-match "['\"]%s['\"]" cmd)
  11227. (setq cmd (replace-match "%s" t t cmd)))
  11228. (setq cmd (format cmd (shell-quote-argument file)))
  11229. (save-window-excursion
  11230. (shell-command (concat cmd " &"))))
  11231. ((or (stringp cmd)
  11232. (eq cmd 'emacs))
  11233. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11234. (widen)
  11235. (if line (goto-line line)
  11236. (if search (org-link-search search))))
  11237. ((consp cmd)
  11238. (eval cmd))
  11239. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11240. (and (org-mode-p) (eq old-mode 'org-mode)
  11241. (or (not (equal old-buffer (current-buffer)))
  11242. (not (equal old-pos (point))))
  11243. (org-mark-ring-push old-pos old-buffer))))
  11244. (defun org-default-apps ()
  11245. "Return the default applications for this operating system."
  11246. (cond
  11247. ((eq system-type 'darwin)
  11248. org-file-apps-defaults-macosx)
  11249. ((eq system-type 'windows-nt)
  11250. org-file-apps-defaults-windowsnt)
  11251. (t org-file-apps-defaults-gnu)))
  11252. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11253. (defun org-file-remote-p (file)
  11254. "Test whether FILE specifies a location on a remote system.
  11255. Return non-nil if the location is indeed remote.
  11256. For example, the filename \"/user@host:/foo\" specifies a location
  11257. on the system \"/user@host:\"."
  11258. (cond ((fboundp 'file-remote-p)
  11259. (file-remote-p file))
  11260. ((fboundp 'tramp-handle-file-remote-p)
  11261. (tramp-handle-file-remote-p file))
  11262. ((and (boundp 'ange-ftp-name-format)
  11263. (string-match (car ange-ftp-name-format) file))
  11264. t)
  11265. (t nil)))
  11266. ;;;; Hooks for remember.el
  11267. ;;;###autoload
  11268. (defun org-remember-annotation ()
  11269. "Return a link to the current location as an annotation for remember.el.
  11270. If you are using Org-mode files as target for data storage with
  11271. remember.el, then the annotations should include a link compatible with the
  11272. conventions in Org-mode. This function returns such a link."
  11273. (org-store-link nil))
  11274. (defconst org-remember-help
  11275. "Select a destination location for the note.
  11276. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  11277. RET on headline -> Store as sublevel entry to current headline
  11278. RET at beg-of-buf -> Append to file as level 2 headline
  11279. <left>/<right> -> before/after current headline, same headings level")
  11280. (defvar org-remember-previous-location nil)
  11281. ;;;###autoload
  11282. (defun org-remember-apply-template (&optional use-char skip-interactive)
  11283. "Initialize *remember* buffer with template, invoke `org-mode'.
  11284. This function should be placed into `remember-mode-hook' and in fact requires
  11285. to be run from that hook to fucntion properly."
  11286. (if org-remember-templates
  11287. (let* ((char (or use-char
  11288. (if (= (length org-remember-templates) 1)
  11289. (caar org-remember-templates)
  11290. (message "Select template: %s"
  11291. (mapconcat
  11292. (lambda (x) (char-to-string (car x)))
  11293. org-remember-templates " "))
  11294. (read-char-exclusive))))
  11295. (entry (cdr (assoc char org-remember-templates)))
  11296. (tpl (car entry))
  11297. (plist-p (if org-store-link-plist t nil))
  11298. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  11299. (string-match "\\S-" (nth 1 entry)))
  11300. (nth 1 entry)
  11301. org-default-notes-file))
  11302. (headline (nth 2 entry))
  11303. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  11304. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  11305. (v-u (concat "[" (substring v-t 1 -1) "]"))
  11306. (v-U (concat "[" (substring v-T 1 -1) "]"))
  11307. (v-i initial) ; defined in `remember-mode'
  11308. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  11309. (v-A (if (and v-a
  11310. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  11311. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  11312. v-a))
  11313. (v-n user-full-name)
  11314. (org-startup-folded nil)
  11315. org-time-was-given org-end-time-was-given x prompt char time)
  11316. (setq org-store-link-plist
  11317. (append (list :annotation v-a :initial v-i)
  11318. org-store-link-plist))
  11319. (unless tpl (setq tpl "") (message "No template") (ding))
  11320. (erase-buffer)
  11321. (insert (substitute-command-keys
  11322. (format
  11323. "## Filing location: Select interactively, default, or last used:
  11324. ## %s to select file and header location interactively.
  11325. ## %s \"%s\" -> \"* %s\"
  11326. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  11327. ## To switch templates, use `\\[org-remember]'.\n\n"
  11328. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  11329. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  11330. (abbreviate-file-name (or file org-default-notes-file))
  11331. (or headline "")
  11332. (or (car org-remember-previous-location) "???")
  11333. (or (cdr org-remember-previous-location) "???"))))
  11334. (insert tpl) (goto-char (point-min))
  11335. ;; Simple %-escapes
  11336. (while (re-search-forward "%\\([tTuUaiA]\\)" nil t)
  11337. (when (and initial (equal (match-string 0) "%i"))
  11338. (save-match-data
  11339. (let* ((lead (buffer-substring
  11340. (point-at-bol) (match-beginning 0))))
  11341. (setq v-i (mapconcat 'identity
  11342. (org-split-string initial "\n")
  11343. (concat "\n" lead))))))
  11344. (replace-match
  11345. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11346. t t))
  11347. ;; From the property list
  11348. (when plist-p
  11349. (goto-char (point-min))
  11350. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11351. (and (setq x (plist-get org-store-link-plist
  11352. (intern (match-string 1))))
  11353. (replace-match x t t))))
  11354. ;; Turn on org-mode in the remember buffer, set local variables
  11355. (org-mode)
  11356. (org-set-local 'org-finish-function 'remember-buffer)
  11357. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11358. (org-set-local 'org-default-notes-file file))
  11359. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11360. (org-set-local 'org-remember-default-headline headline))
  11361. ;; Interactive template entries
  11362. (goto-char (point-min))
  11363. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11364. (setq char (if (match-end 3) (match-string 3))
  11365. prompt (if (match-end 2) (match-string 2)))
  11366. (goto-char (match-beginning 0))
  11367. (replace-match "")
  11368. (cond
  11369. ((member char '("G" "g"))
  11370. (let* ((org-last-tags-completion-table
  11371. (org-global-tags-completion-table
  11372. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11373. (org-add-colon-after-tag-completion t)
  11374. (ins (completing-read
  11375. (if prompt (concat prompt ": ") "Tags: ")
  11376. 'org-tags-completion-function nil nil nil
  11377. 'org-tags-history)))
  11378. (setq ins (mapconcat 'identity
  11379. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11380. ":"))
  11381. (when (string-match "\\S-" ins)
  11382. (or (equal (char-before) ?:) (insert ":"))
  11383. (insert ins)
  11384. (or (equal (char-after) ?:) (insert ":")))))
  11385. (char
  11386. (setq org-time-was-given (equal (upcase char) char))
  11387. (setq time (org-read-date (equal (upcase char) "U") t nil
  11388. prompt))
  11389. (org-insert-time-stamp time org-time-was-given
  11390. (member char '("u" "U"))
  11391. nil nil (list org-end-time-was-given)))
  11392. (t
  11393. (insert (read-string
  11394. (if prompt (concat prompt ": ") "Enter string"))))))
  11395. (goto-char (point-min))
  11396. (if (re-search-forward "%\\?" nil t)
  11397. (replace-match "")
  11398. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11399. (org-mode)
  11400. (org-set-local 'org-finish-function 'remember-buffer)))
  11401. ;;;###autoload
  11402. (defun org-remember ()
  11403. "Call `remember'. If this is already a remember buffer, re-apply template.
  11404. If there is an active region, make sure remember uses it as initial content
  11405. of the remember buffer."
  11406. (interactive)
  11407. (if (eq org-finish-function 'remember-buffer)
  11408. (progn
  11409. (when (< (length org-remember-templates) 2)
  11410. (error "No other template available"))
  11411. (erase-buffer)
  11412. (let ((annotation (plist-get org-store-link-plist :annotation))
  11413. (initial (plist-get org-store-link-plist :initial)))
  11414. (org-remember-apply-template))
  11415. (message "Press C-c C-c to remember data"))
  11416. (if (org-region-active-p)
  11417. (remember (buffer-substring (point) (mark)))
  11418. (call-interactively 'remember))))
  11419. ;;;###autoload
  11420. (defun org-remember-handler ()
  11421. "Store stuff from remember.el into an org file.
  11422. First prompts for an org file. If the user just presses return, the value
  11423. of `org-default-notes-file' is used.
  11424. Then the command offers the headings tree of the selected file in order to
  11425. file the text at a specific location.
  11426. You can either immediately press RET to get the note appended to the
  11427. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11428. find a better place. Then press RET or <left> or <right> in insert the note.
  11429. Key Cursor position Note gets inserted
  11430. -----------------------------------------------------------------------------
  11431. RET buffer-start as level 1 heading at end of file
  11432. RET on headline as sublevel of the heading at cursor
  11433. RET no heading at cursor position, level taken from context.
  11434. Or use prefix arg to specify level manually.
  11435. <left> on headline as same level, before current heading
  11436. <right> on headline as same level, after current heading
  11437. So the fastest way to store the note is to press RET RET to append it to
  11438. the default file. This way your current train of thought is not
  11439. interrupted, in accordance with the principles of remember.el.
  11440. You can also get the fast execution without prompting by using
  11441. C-u C-c C-c to exit the remember buffer. See also the variable
  11442. `org-remember-store-without-prompt'.
  11443. Before being stored away, the function ensures that the text has a
  11444. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11445. is constructed from the current date and some additional data.
  11446. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11447. also indented so that it starts in the same column as the headline
  11448. \(i.e. after the stars).
  11449. See also the variable `org-reverse-note-order'."
  11450. (goto-char (point-min))
  11451. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11452. (replace-match ""))
  11453. (catch 'quit
  11454. (let* ((txt (buffer-substring (point-min) (point-max)))
  11455. (fastp (org-xor (equal current-prefix-arg '(4))
  11456. org-remember-store-without-prompt))
  11457. (file (if fastp org-default-notes-file (org-get-org-file)))
  11458. (heading org-remember-default-headline)
  11459. (visiting (org-find-base-buffer-visiting file))
  11460. (org-startup-folded nil)
  11461. (org-startup-align-all-tables nil)
  11462. (org-goto-start-pos 1)
  11463. spos exitcmd level indent reversed)
  11464. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  11465. (setq file (car org-remember-previous-location)
  11466. heading (cdr org-remember-previous-location)))
  11467. (setq current-prefix-arg nil)
  11468. ;; Modify text so that it becomes a nice subtree which can be inserted
  11469. ;; into an org tree.
  11470. (let* ((lines (split-string txt "\n"))
  11471. first)
  11472. (setq first (car lines) lines (cdr lines))
  11473. (if (string-match "^\\*+ " first)
  11474. ;; Is already a headline
  11475. (setq indent nil)
  11476. ;; We need to add a headline: Use time and first buffer line
  11477. (setq lines (cons first lines)
  11478. first (concat "* " (current-time-string)
  11479. " (" (remember-buffer-desc) ")")
  11480. indent " "))
  11481. (if (and org-adapt-indentation indent)
  11482. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11483. (setq txt (concat first "\n"
  11484. (mapconcat 'identity lines "\n"))))
  11485. ;; Find the file
  11486. (if (not visiting) (find-file-noselect file))
  11487. (with-current-buffer (or visiting (get-file-buffer file))
  11488. (unless (org-mode-p)
  11489. (error "Target files for remember notes must be in Org-mode"))
  11490. (save-excursion
  11491. (save-restriction
  11492. (widen)
  11493. (and (goto-char (point-min))
  11494. (not (re-search-forward "^\\* " nil t))
  11495. (insert "\n* " (or heading "Notes") "\n"))
  11496. (setq reversed (org-notes-order-reversed-p))
  11497. ;; Find the default location
  11498. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11499. (goto-char (point-min))
  11500. (if (re-search-forward
  11501. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11502. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11503. nil t)
  11504. (setq org-goto-start-pos (match-beginning 0))
  11505. (when fastp
  11506. (goto-char (point-max))
  11507. (unless (bolp) (newline))
  11508. (insert "* " heading "\n")
  11509. (setq org-goto-start-pos (point-at-bol 0)))))
  11510. ;; Ask the User for a location
  11511. (if fastp
  11512. (setq spos org-goto-start-pos
  11513. exitcmd 'return)
  11514. (setq spos (org-get-location (current-buffer) org-remember-help)
  11515. exitcmd (cdr spos)
  11516. spos (car spos)))
  11517. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11518. ; not handle this note
  11519. (goto-char spos)
  11520. (cond ((org-on-heading-p t)
  11521. (org-back-to-heading t)
  11522. (setq level (funcall outline-level))
  11523. (cond
  11524. ((eq exitcmd 'return)
  11525. ;; sublevel of current
  11526. (setq org-remember-previous-location
  11527. (cons (abbreviate-file-name file)
  11528. (org-get-heading 'notags)))
  11529. (if reversed
  11530. (outline-next-heading)
  11531. (org-end-of-subtree)
  11532. (if (not (bolp))
  11533. (if (looking-at "[ \t]*\n")
  11534. (beginning-of-line 2)
  11535. (end-of-line 1)
  11536. (insert "\n"))))
  11537. (org-paste-subtree (org-get-legal-level level 1) txt))
  11538. ((eq exitcmd 'left)
  11539. ;; before current
  11540. (org-paste-subtree level txt))
  11541. ((eq exitcmd 'right)
  11542. ;; after current
  11543. (org-end-of-subtree t)
  11544. (org-paste-subtree level txt))
  11545. (t (error "This should not happen"))))
  11546. ((and (bobp) (not reversed))
  11547. ;; Put it at the end, one level below level 1
  11548. (save-restriction
  11549. (widen)
  11550. (goto-char (point-max))
  11551. (if (not (bolp)) (newline))
  11552. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11553. ((and (bobp) reversed)
  11554. ;; Put it at the start, as level 1
  11555. (save-restriction
  11556. (widen)
  11557. (goto-char (point-min))
  11558. (re-search-forward "^\\*+ " nil t)
  11559. (beginning-of-line 1)
  11560. (org-paste-subtree 1 txt)))
  11561. (t
  11562. ;; Put it right there, with automatic level determined by
  11563. ;; org-paste-subtree or from prefix arg
  11564. (org-paste-subtree
  11565. (if (numberp current-prefix-arg) current-prefix-arg)
  11566. txt)))
  11567. (when remember-save-after-remembering
  11568. (save-buffer)
  11569. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11570. t) ;; return t to indicate that we took care of this note.
  11571. (defun org-get-org-file ()
  11572. "Read a filename, with default directory `org-directory'."
  11573. (let ((default (or org-default-notes-file remember-data-file)))
  11574. (read-file-name (format "File name [%s]: " default)
  11575. (file-name-as-directory org-directory)
  11576. default)))
  11577. (defun org-notes-order-reversed-p ()
  11578. "Check if the current file should receive notes in reversed order."
  11579. (cond
  11580. ((not org-reverse-note-order) nil)
  11581. ((eq t org-reverse-note-order) t)
  11582. ((not (listp org-reverse-note-order)) nil)
  11583. (t (catch 'exit
  11584. (let ((all org-reverse-note-order)
  11585. entry)
  11586. (while (setq entry (pop all))
  11587. (if (string-match (car entry) buffer-file-name)
  11588. (throw 'exit (cdr entry))))
  11589. nil)))))
  11590. ;;;; Dynamic blocks
  11591. (defun org-find-dblock (name)
  11592. "Find the first dynamic block with name NAME in the buffer.
  11593. If not found, stay at current position and return nil."
  11594. (let (pos)
  11595. (save-excursion
  11596. (goto-char (point-min))
  11597. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11598. nil t)
  11599. (match-beginning 0))))
  11600. (if pos (goto-char pos))
  11601. pos))
  11602. (defconst org-dblock-start-re
  11603. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11604. "Matches the startline of a dynamic block, with parameters.")
  11605. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11606. "Matches the end of a dyhamic block.")
  11607. (defun org-create-dblock (plist)
  11608. "Create a dynamic block section, with parameters taken from PLIST.
  11609. PLIST must containe a :name entry which is used as name of the block."
  11610. (unless (bolp) (newline))
  11611. (let ((name (plist-get plist :name)))
  11612. (insert "#+BEGIN: " name)
  11613. (while plist
  11614. (if (eq (car plist) :name)
  11615. (setq plist (cddr plist))
  11616. (insert " " (prin1-to-string (pop plist)))))
  11617. (insert "\n\n#+END:\n")
  11618. (beginning-of-line -2)))
  11619. (defun org-prepare-dblock ()
  11620. "Prepare dynamic block for refresh.
  11621. This empties the block, puts the cursor at the insert position and returns
  11622. the property list including an extra property :name with the block name."
  11623. (unless (looking-at org-dblock-start-re)
  11624. (error "Not at a dynamic block"))
  11625. (let* ((begdel (1+ (match-end 0)))
  11626. (name (match-string 1))
  11627. (params (append (list :name name)
  11628. (read (concat "(" (match-string 3) ")")))))
  11629. (unless (re-search-forward org-dblock-end-re nil t)
  11630. (error "Dynamic block not terminated"))
  11631. (delete-region begdel (match-beginning 0))
  11632. (goto-char begdel)
  11633. (open-line 1)
  11634. params))
  11635. (defun org-map-dblocks (&optional command)
  11636. "Apply COMMAND to all dynamic blocks in the current buffer.
  11637. If COMMAND is not given, use `org-update-dblock'."
  11638. (let ((cmd (or command 'org-update-dblock))
  11639. pos)
  11640. (save-excursion
  11641. (goto-char (point-min))
  11642. (while (re-search-forward org-dblock-start-re nil t)
  11643. (goto-char (setq pos (match-beginning 0)))
  11644. (condition-case nil
  11645. (funcall cmd)
  11646. (error (message "Error during update of dynamic block")))
  11647. (goto-char pos)
  11648. (unless (re-search-forward org-dblock-end-re nil t)
  11649. (error "Dynamic block not terminated"))))))
  11650. (defun org-dblock-update (&optional arg)
  11651. "User command for updating dynamic blocks.
  11652. Update the dynamic block at point. With prefix ARG, update all dynamic
  11653. blocks in the buffer."
  11654. (interactive "P")
  11655. (if arg
  11656. (org-update-all-dblocks)
  11657. (or (looking-at org-dblock-start-re)
  11658. (org-beginning-of-dblock))
  11659. (org-update-dblock)))
  11660. (defun org-update-dblock ()
  11661. "Update the dynamic block at point
  11662. This means to empty the block, parse for parameters and then call
  11663. the correct writing function."
  11664. (let* ((pos (point))
  11665. (params (org-prepare-dblock))
  11666. (name (plist-get params :name))
  11667. (cmd (intern (concat "org-dblock-write:" name))))
  11668. (funcall cmd params)
  11669. (goto-char pos)))
  11670. (defun org-beginning-of-dblock ()
  11671. "Find the beginning of the dynamic block at point.
  11672. Error if there is no scuh block at point."
  11673. (let ((pos (point))
  11674. beg)
  11675. (end-of-line 1)
  11676. (if (and (re-search-backward org-dblock-start-re nil t)
  11677. (setq beg (match-beginning 0))
  11678. (re-search-forward org-dblock-end-re nil t)
  11679. (> (match-end 0) pos))
  11680. (goto-char beg)
  11681. (goto-char pos)
  11682. (error "Not in a dynamic block"))))
  11683. (defun org-update-all-dblocks ()
  11684. "Update all dynamic blocks in the buffer.
  11685. This function can be used in a hook."
  11686. (when (org-mode-p)
  11687. (org-map-dblocks 'org-update-dblock)))
  11688. ;;;; Completion
  11689. (defun org-complete (&optional arg)
  11690. "Perform completion on word at point.
  11691. At the beginning of a headline, this completes TODO keywords as given in
  11692. `org-todo-keywords'.
  11693. If the current word is preceded by a backslash, completes the TeX symbols
  11694. that are supported for HTML support.
  11695. If the current word is preceded by \"#+\", completes special words for
  11696. setting file options.
  11697. In the line after \"#+STARTUP:, complete valid keywords.\"
  11698. At all other locations, this simply calls `ispell-complete-word'."
  11699. (interactive "P")
  11700. (catch 'exit
  11701. (let* ((end (point))
  11702. (beg1 (save-excursion
  11703. (skip-chars-backward (org-re "[:alnum:]_@"))
  11704. (point)))
  11705. (beg (save-excursion
  11706. (skip-chars-backward "a-zA-Z0-9_:$")
  11707. (point)))
  11708. (confirm (lambda (x) (stringp (car x))))
  11709. (searchhead (equal (char-before beg) ?*))
  11710. (tag (and (equal (char-before beg1) ?:)
  11711. (equal (char-after (point-at-bol)) ?*)))
  11712. (prop (and (equal (char-before beg1) ?:)
  11713. (not (equal (char-after (point-at-bol)) ?*))))
  11714. (texp (equal (char-before beg) ?\\))
  11715. (link (equal (char-before beg) ?\[))
  11716. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11717. beg)
  11718. "#+"))
  11719. (startup (string-match "^#\\+STARTUP:.*"
  11720. (buffer-substring (point-at-bol) (point))))
  11721. (completion-ignore-case opt)
  11722. (type nil)
  11723. (tbl nil)
  11724. (table (cond
  11725. (opt
  11726. (setq type :opt)
  11727. (mapcar (lambda (x)
  11728. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11729. (cons (match-string 2 x) (match-string 1 x)))
  11730. (org-split-string (org-get-current-options) "\n")))
  11731. (startup
  11732. (setq type :startup)
  11733. org-startup-options)
  11734. (link (append org-link-abbrev-alist-local
  11735. org-link-abbrev-alist))
  11736. (texp
  11737. (setq type :tex)
  11738. org-html-entities)
  11739. ((string-match "\\`\\*+[ \t]+\\'"
  11740. (buffer-substring (point-at-bol) beg))
  11741. (setq type :todo)
  11742. (mapcar 'list org-todo-keywords-1))
  11743. (searchhead
  11744. (setq type :searchhead)
  11745. (save-excursion
  11746. (goto-char (point-min))
  11747. (while (re-search-forward org-todo-line-regexp nil t)
  11748. (push (list
  11749. (org-make-org-heading-search-string
  11750. (match-string 3) t))
  11751. tbl)))
  11752. tbl)
  11753. (tag (setq type :tag beg beg1)
  11754. (or org-tag-alist (org-get-buffer-tags)))
  11755. (prop (setq type :prop beg beg1)
  11756. (mapcar 'list (org-buffer-property-keys)))
  11757. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11758. (pattern (buffer-substring-no-properties beg end))
  11759. (completion (try-completion pattern table confirm)))
  11760. (cond ((eq completion t)
  11761. (if (equal type :opt)
  11762. (insert (substring (cdr (assoc (upcase pattern) table))
  11763. (length pattern)))
  11764. (if (memq type '(:tag :prop)) (insert ":"))))
  11765. ((null completion)
  11766. (message "Can't find completion for \"%s\"" pattern)
  11767. (ding))
  11768. ((not (string= pattern completion))
  11769. (delete-region beg end)
  11770. (if (string-match " +$" completion)
  11771. (setq completion (replace-match "" t t completion)))
  11772. (insert completion)
  11773. (if (get-buffer-window "*Completions*")
  11774. (delete-window (get-buffer-window "*Completions*")))
  11775. (if (assoc completion table)
  11776. (if (eq type :todo) (insert " ")
  11777. (if (memq type '(:tag :prop)) (insert ":"))))
  11778. (if (and (equal type :opt) (assoc completion table))
  11779. (message "%s" (substitute-command-keys
  11780. "Press \\[org-complete] again to insert example settings"))))
  11781. (t
  11782. (message "Making completion list...")
  11783. (let ((list (sort (all-completions pattern table confirm)
  11784. 'string<)))
  11785. (with-output-to-temp-buffer "*Completions*"
  11786. (condition-case nil
  11787. ;; Protection needed for XEmacs and emacs 21
  11788. (display-completion-list list pattern)
  11789. (error (display-completion-list list)))))
  11790. (message "Making completion list...%s" "done"))))))
  11791. ;;;; TODO, DEADLINE, Comments
  11792. (defun org-toggle-comment ()
  11793. "Change the COMMENT state of an entry."
  11794. (interactive)
  11795. (save-excursion
  11796. (org-back-to-heading)
  11797. (if (looking-at (concat outline-regexp
  11798. "\\( *\\<" org-comment-string "\\>\\)"))
  11799. (replace-match "" t t nil 1)
  11800. (if (looking-at outline-regexp)
  11801. (progn
  11802. (goto-char (match-end 0))
  11803. (insert org-comment-string " "))))))
  11804. (defvar org-last-todo-state-is-todo nil
  11805. "This is non-nil when the last TODO state change led to a TODO state.
  11806. If the last change removed the TODO tag or switched to DONE, then
  11807. this is nil.")
  11808. (defvar org-setting-tags nil) ; dynamically skiped
  11809. (defun org-todo (&optional arg)
  11810. "Change the TODO state of an item.
  11811. The state of an item is given by a keyword at the start of the heading,
  11812. like
  11813. *** TODO Write paper
  11814. *** DONE Call mom
  11815. The different keywords are specified in the variable `org-todo-keywords'.
  11816. By default the available states are \"TODO\" and \"DONE\".
  11817. So for this example: when the item starts with TODO, it is changed to DONE.
  11818. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11819. DONE are present, add TODO at the beginning of the heading.
  11820. With C-u prefix arg, use completion to determine the new state.
  11821. With numeric prefix arg, switch to that state.
  11822. For calling through lisp, arg is also interpreted in the following way:
  11823. 'none -> empty state
  11824. \"\"(empty string) -> switch to empty state
  11825. 'done -> switch to DONE
  11826. 'nextset -> switch to the next set of keywords
  11827. 'previousset -> switch to the previous set of keywords
  11828. \"WAITING\" -> switch to the specified keyword, but only if it
  11829. really is a member of `org-todo-keywords'."
  11830. (interactive "P")
  11831. (save-excursion
  11832. (org-back-to-heading)
  11833. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11834. (or (looking-at (concat " +" org-todo-regexp " *"))
  11835. (looking-at " *"))
  11836. (let* ((this (match-string 1))
  11837. (hl-pos (match-beginning 0))
  11838. (head (org-get-todo-sequence-head this))
  11839. (ass (assoc head org-todo-kwd-alist))
  11840. (interpret (nth 1 ass))
  11841. (done-word (nth 3 ass))
  11842. (final-done-word (nth 4 ass))
  11843. (last-state (or this ""))
  11844. (completion-ignore-case t)
  11845. (member (member this org-todo-keywords-1))
  11846. (tail (cdr member))
  11847. (state (cond
  11848. ((and org-todo-key-trigger
  11849. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  11850. (and (not arg) org-use-fast-todo-selection
  11851. (not (eq org-use-fast-todo-selection 'prefix)))))
  11852. ;; Use fast selection
  11853. (org-fast-todo-selection))
  11854. ((and (equal arg '(4))
  11855. (or (not org-use-fast-todo-selection)
  11856. (not org-todo-key-trigger)))
  11857. ;; Read a state with completion
  11858. (completing-read "State: " (mapcar (lambda(x) (list x))
  11859. org-todo-keywords-1)
  11860. nil t))
  11861. ((eq arg 'right)
  11862. (if this
  11863. (if tail (car tail) nil)
  11864. (car org-todo-keywords-1)))
  11865. ((eq arg 'left)
  11866. (if (equal member org-todo-keywords-1)
  11867. nil
  11868. (if this
  11869. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11870. org-todo-keywords-1)
  11871. (org-last org-todo-keywords-1))))
  11872. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11873. (setq arg nil))) ; hack to fall back to cycling
  11874. (arg
  11875. ;; user or caller requests a specific state
  11876. (cond
  11877. ((equal arg "") nil)
  11878. ((eq arg 'none) nil)
  11879. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11880. ((eq arg 'nextset)
  11881. (or (car (cdr (member head org-todo-heads)))
  11882. (car org-todo-heads)))
  11883. ((eq arg 'previousset)
  11884. (let ((org-todo-heads (reverse org-todo-heads)))
  11885. (or (car (cdr (member head org-todo-heads)))
  11886. (car org-todo-heads))))
  11887. ((car (member arg org-todo-keywords-1)))
  11888. ((nth (1- (prefix-numeric-value arg))
  11889. org-todo-keywords-1))))
  11890. ((null member) (or head (car org-todo-keywords-1)))
  11891. ((equal this final-done-word) nil) ;; -> make empty
  11892. ((null tail) nil) ;; -> first entry
  11893. ((eq interpret 'sequence)
  11894. (car tail))
  11895. ((memq interpret '(type priority))
  11896. (if (eq this-command last-command)
  11897. (car tail)
  11898. (if (> (length tail) 0)
  11899. (or done-word (car org-done-keywords))
  11900. nil)))
  11901. (t nil)))
  11902. (next (if state (concat " " state " ") " "))
  11903. dostates)
  11904. (replace-match next t t)
  11905. (unless (pos-visible-in-window-p hl-pos)
  11906. (message "TODO state changed to %s" (org-trim next)))
  11907. (unless head
  11908. (setq head (org-get-todo-sequence-head state)
  11909. ass (assoc head org-todo-kwd-alist)
  11910. interpret (nth 1 ass)
  11911. done-word (nth 3 ass)
  11912. final-done-word (nth 4 ass)))
  11913. (when (memq arg '(nextset previousset))
  11914. (message "Keyword-Set %d/%d: %s"
  11915. (- (length org-todo-sets) -1
  11916. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  11917. (length org-todo-sets)
  11918. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11919. (setq org-last-todo-state-is-todo
  11920. (not (member state org-done-keywords)))
  11921. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11922. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  11923. (or (not org-todo-log-states)
  11924. (member state org-todo-log-states))))
  11925. (cond
  11926. ((and state (member state org-not-done-keywords)
  11927. (not (member this org-not-done-keywords)))
  11928. ;; This is now a todo state and was not one before
  11929. ;; Remove any CLOSED timestamp, and possibly log the state change
  11930. (org-add-planning-info nil nil 'closed)
  11931. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11932. ((and state dostates)
  11933. ;; This is a non-nil state, and we need to log it
  11934. (org-add-log-maybe 'state state 'findpos))
  11935. ((and (member state org-done-keywords)
  11936. (not (member this org-done-keywords)))
  11937. ;; It is now done, and it was not done before
  11938. ;; FIXME: We used to remove scheduling info....
  11939. ; (org-add-planning-info 'closed (org-current-time)
  11940. ; (if (org-get-repeat) nil 'scheduled))
  11941. (org-add-planning-info 'closed (org-current-time))
  11942. (org-add-log-maybe 'done state 'findpos))))
  11943. ;; Fixup tag positioning
  11944. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11945. (run-hooks 'org-after-todo-state-change-hook)
  11946. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11947. (if (and arg (not (member state org-done-keywords)))
  11948. (setq head (org-get-todo-sequence-head state)))
  11949. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11950. ;; Fixup cursor location if close to the keyword
  11951. (if (and (outline-on-heading-p)
  11952. (not (bolp))
  11953. (save-excursion (beginning-of-line 1)
  11954. (looking-at org-todo-line-regexp))
  11955. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11956. (progn
  11957. (goto-char (or (match-end 2) (match-end 1)))
  11958. (just-one-space))))
  11959. (defun org-get-todo-sequence-head (kwd)
  11960. "Return the head of the TODO sequence to which KWD belongs.
  11961. If KWD is not set, check if there is a text property remembering the
  11962. right sequence."
  11963. (let (p)
  11964. (cond
  11965. ((not kwd)
  11966. (or (get-text-property (point-at-bol) 'org-todo-head)
  11967. (progn
  11968. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11969. nil (point-at-eol)))
  11970. (get-text-property p 'org-todo-head))))
  11971. ((not (member kwd org-todo-keywords-1))
  11972. (car org-todo-keywords-1))
  11973. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11974. (defun org-fast-todo-selection ()
  11975. "Fast TODO keyword selection with single keys.
  11976. Returns the new TODO keyword, or nil if no state change should occur."
  11977. (let* ((fulltable org-todo-key-alist)
  11978. (done-keywords org-done-keywords) ;; needed for the faces.
  11979. (maxlen (apply 'max (mapcar
  11980. (lambda (x)
  11981. (if (stringp (car x)) (string-width (car x)) 0))
  11982. fulltable)))
  11983. (buf (current-buffer))
  11984. (expert nil)
  11985. (fwidth (+ maxlen 3 1 3))
  11986. (ncol (/ (- (window-width) 4) fwidth))
  11987. tg cnt e c char c1 c2 ntable tbl rtn
  11988. groups ingroup)
  11989. (save-window-excursion
  11990. (if expert
  11991. (set-buffer (get-buffer-create " *Org todo*"))
  11992. ; (delete-other-windows)
  11993. ; (split-window-vertically)
  11994. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11995. (erase-buffer)
  11996. (org-set-local 'org-done-keywords done-keywords)
  11997. (setq tbl fulltable char ?a cnt 0)
  11998. (while (setq e (pop tbl))
  11999. (cond
  12000. ((equal e '(:startgroup))
  12001. (push '() groups) (setq ingroup t)
  12002. (when (not (= cnt 0))
  12003. (setq cnt 0)
  12004. (insert "\n"))
  12005. (insert "{ "))
  12006. ((equal e '(:endgroup))
  12007. (setq ingroup nil cnt 0)
  12008. (insert "}\n"))
  12009. (t
  12010. (setq tg (car e) c (cdr e))
  12011. (if ingroup (push tg (car groups)))
  12012. (setq tg (org-add-props tg nil 'face
  12013. (org-get-todo-face tg)))
  12014. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12015. (insert "[" c "] " tg (make-string
  12016. (- fwidth 4 (length tg)) ?\ ))
  12017. (when (= (setq cnt (1+ cnt)) ncol)
  12018. (insert "\n")
  12019. (if ingroup (insert " "))
  12020. (setq cnt 0)))))
  12021. (insert "\n")
  12022. (goto-char (point-min))
  12023. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12024. (fit-window-to-buffer))
  12025. (message "[a-z..]:Set [SPC]:clear")
  12026. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12027. (cond
  12028. ((or (= c ?\C-g)
  12029. (and (= c ?q) (not (rassoc c fulltable))))
  12030. (setq quit-flag t))
  12031. ((= c ?\ ) nil)
  12032. ((setq e (rassoc c fulltable) tg (car e))
  12033. tg)
  12034. (t (setq quit-flag t))))))
  12035. (defun org-get-repeat ()
  12036. "Check if tere is a deadline/schedule with repeater in this entry."
  12037. (save-match-data
  12038. (save-excursion
  12039. (org-back-to-heading t)
  12040. (if (re-search-forward
  12041. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  12042. (match-string 1)))))
  12043. (defvar org-last-changed-timestamp)
  12044. (defvar org-log-post-message)
  12045. (defun org-auto-repeat-maybe ()
  12046. "Check if the current headline contains a repeated deadline/schedule.
  12047. If yes, set TODO state back to what it was and change the base date
  12048. of repeating deadline/scheduled time stamps to new date.
  12049. This function should be run in the `org-after-todo-state-change-hook'."
  12050. ;; last-state is dynamically scoped into this function
  12051. (let* ((repeat (org-get-repeat))
  12052. (aa (assoc last-state org-todo-kwd-alist))
  12053. (interpret (nth 1 aa))
  12054. (head (nth 2 aa))
  12055. (done-word (nth 3 aa))
  12056. (whata '(("d" . day) ("m" . month) ("y" . year)))
  12057. (msg "Entry repeats: ")
  12058. (org-log-done)
  12059. re type n what ts)
  12060. (when repeat
  12061. (org-todo (if (eq interpret 'type) last-state head))
  12062. (when (and org-log-repeat
  12063. (not (memq 'org-add-log-note
  12064. (default-value 'post-command-hook))))
  12065. ;; Make sure a note is taken
  12066. (let ((org-log-done '(done)))
  12067. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  12068. 'findpos)))
  12069. (org-back-to-heading t)
  12070. (org-add-planning-info nil nil 'closed)
  12071. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12072. org-deadline-time-regexp "\\)"))
  12073. (while (re-search-forward
  12074. re (save-excursion (outline-next-heading) (point)) t)
  12075. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  12076. ts (match-string (if (match-end 2) 2 4)))
  12077. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  12078. (setq n (string-to-number (match-string 1 ts))
  12079. what (match-string 2 ts))
  12080. (if (equal what "w") (setq n (* n 7) what "d"))
  12081. (org-timestamp-change n (cdr (assoc what whata))))
  12082. (setq msg (concat msg type org-last-changed-timestamp " ")))
  12083. (setq org-log-post-message msg)
  12084. (message msg))))
  12085. (defun org-show-todo-tree (arg)
  12086. "Make a compact tree which shows all headlines marked with TODO.
  12087. The tree will show the lines where the regexp matches, and all higher
  12088. headlines above the match.
  12089. With \\[universal-argument] prefix, also show the DONE entries.
  12090. With a numeric prefix N, construct a sparse tree for the Nth element
  12091. of `org-todo-keywords-1'."
  12092. (interactive "P")
  12093. (let ((case-fold-search nil)
  12094. (kwd-re
  12095. (cond ((null arg) org-not-done-regexp)
  12096. ((equal arg '(4))
  12097. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  12098. (mapcar 'list org-todo-keywords-1))))
  12099. (concat "\\("
  12100. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12101. "\\)\\>")))
  12102. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12103. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12104. org-todo-keywords-1)))
  12105. (t (error "Invalid prefix argument: %s" arg)))))
  12106. (message "%d TODO entries found"
  12107. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  12108. (defun org-deadline ()
  12109. "Insert the DEADLINE: string to make a deadline.
  12110. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  12111. to modify it to the correct date."
  12112. (interactive)
  12113. (org-add-planning-info 'deadline nil 'closed))
  12114. (defun org-schedule ()
  12115. "Insert the SCHEDULED: string to schedule a TODO item.
  12116. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  12117. to modify it to the correct date."
  12118. (interactive)
  12119. (org-add-planning-info 'scheduled nil 'closed))
  12120. (defun org-add-planning-info (what &optional time &rest remove)
  12121. "Insert new timestamp with keyword in the line directly after the headline.
  12122. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  12123. If non is given, the user is prompted for a date.
  12124. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12125. be removed."
  12126. (interactive)
  12127. (let (org-time-was-given org-end-time-was-given)
  12128. (when what (setq time (or time (org-read-date nil 'to-time))))
  12129. (when (and org-insert-labeled-timestamps-at-point
  12130. (member what '(scheduled deadline)))
  12131. (insert
  12132. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12133. (org-insert-time-stamp time org-time-was-given
  12134. nil nil nil (list org-end-time-was-given))
  12135. (setq what nil))
  12136. (save-excursion
  12137. (save-restriction
  12138. (let (col list elt ts buffer-invisibility-spec)
  12139. (org-back-to-heading t)
  12140. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  12141. (goto-char (match-end 1))
  12142. (setq col (current-column))
  12143. (goto-char (match-end 0))
  12144. (if (eobp) (insert "\n"))
  12145. (forward-char 1)
  12146. (when (and (not org-insert-labeled-timestamps-before-properties-drawer)
  12147. (looking-at "[ \t]*:PROPERTIES:[ \t]*$"))
  12148. (goto-char (match-end 0))
  12149. (if (eobp) (insert "\n"))
  12150. (forward-char 1))
  12151. (if (and (not (looking-at outline-regexp))
  12152. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12153. "[^\r\n]*"))
  12154. (not (equal (match-string 1) org-clock-string)))
  12155. (narrow-to-region (match-beginning 0) (match-end 0))
  12156. (insert-before-markers "\n")
  12157. (backward-char 1)
  12158. (narrow-to-region (point) (point))
  12159. (indent-to-column col))
  12160. ;; Check if we have to remove something.
  12161. (setq list (cons what remove))
  12162. (while list
  12163. (setq elt (pop list))
  12164. (goto-char (point-min))
  12165. (when (or (and (eq elt 'scheduled)
  12166. (re-search-forward org-scheduled-time-regexp nil t))
  12167. (and (eq elt 'deadline)
  12168. (re-search-forward org-deadline-time-regexp nil t))
  12169. (and (eq elt 'closed)
  12170. (re-search-forward org-closed-time-regexp nil t)))
  12171. (replace-match "")
  12172. (if (looking-at "--+<[^>]+>") (replace-match ""))
  12173. (if (looking-at " +") (replace-match ""))))
  12174. (goto-char (point-max))
  12175. (when what
  12176. (insert
  12177. (if (not (equal (char-before) ?\ )) " " "")
  12178. (cond ((eq what 'scheduled) org-scheduled-string)
  12179. ((eq what 'deadline) org-deadline-string)
  12180. ((eq what 'closed) org-closed-string))
  12181. " ")
  12182. (org-insert-time-stamp
  12183. time
  12184. (or org-time-was-given
  12185. (and (eq what 'closed) org-log-done-with-time))
  12186. (eq what 'closed)
  12187. nil nil (list org-end-time-was-given))
  12188. (end-of-line 1))
  12189. (goto-char (point-min))
  12190. (widen)
  12191. (if (looking-at "[ \t]+\r?\n")
  12192. (replace-match ""))
  12193. ts)))))
  12194. (defvar org-log-note-marker (make-marker))
  12195. (defvar org-log-note-purpose nil)
  12196. (defvar org-log-note-state nil)
  12197. (defvar org-log-note-window-configuration nil)
  12198. (defvar org-log-note-return-to (make-marker))
  12199. (defvar org-log-post-message nil
  12200. "Message to be displayed after a log note has been stored.
  12201. The auto-repeater uses this.")
  12202. (defun org-add-log-maybe (&optional purpose state findpos)
  12203. "Set up the post command hook to take a note."
  12204. (save-excursion
  12205. (when (and (listp org-log-done)
  12206. (memq purpose org-log-done))
  12207. (when findpos
  12208. (org-back-to-heading t)
  12209. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  12210. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12211. "[^\r\n]*\\)?"))
  12212. (goto-char (match-end 0))
  12213. (unless org-log-states-order-reversed
  12214. (and (= (char-after) ?\n) (forward-char 1))
  12215. (org-skip-over-state-notes)
  12216. (skip-chars-backward " \t\n\r")))
  12217. (move-marker org-log-note-marker (point))
  12218. (setq org-log-note-purpose purpose)
  12219. (setq org-log-note-state state)
  12220. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  12221. (defun org-skip-over-state-notes ()
  12222. "Skip past the list of State notes in an entry."
  12223. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12224. (while (looking-at "[ \t]*- State")
  12225. (condition-case nil
  12226. (org-next-item)
  12227. (error (org-end-of-item)))))
  12228. (defun org-add-log-note (&optional purpose)
  12229. "Pop up a window for taking a note, and add this note later at point."
  12230. (remove-hook 'post-command-hook 'org-add-log-note)
  12231. (setq org-log-note-window-configuration (current-window-configuration))
  12232. (delete-other-windows)
  12233. (move-marker org-log-note-return-to (point))
  12234. (switch-to-buffer (marker-buffer org-log-note-marker))
  12235. (goto-char org-log-note-marker)
  12236. (org-switch-to-buffer-other-window "*Org Note*")
  12237. (erase-buffer)
  12238. (let ((org-inhibit-startup t)) (org-mode))
  12239. (insert (format "# Insert note for %s, finish with C-c C-c, or cancel with C-u C-c C-c.\n\n"
  12240. (cond
  12241. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12242. ((eq org-log-note-purpose 'done) "closed todo item")
  12243. ((eq org-log-note-purpose 'state) "state change")
  12244. (t (error "This should not happen")))))
  12245. (org-set-local 'org-finish-function 'org-store-log-note))
  12246. (defun org-store-log-note ()
  12247. "Finish taking a log note, and insert it to where it belongs."
  12248. (let ((txt (buffer-string))
  12249. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12250. lines ind)
  12251. (kill-buffer (current-buffer))
  12252. (if (string-match "^#.*\n[ \t\n]*" txt)
  12253. (setq txt (replace-match "" t t txt)))
  12254. (if (string-match "\\s-+\\'" txt)
  12255. (setq txt (replace-match "" t t txt)))
  12256. (setq lines (org-split-string txt "\n"))
  12257. (when (and note (string-match "\\S-" note))
  12258. (setq note
  12259. (org-replace-escapes
  12260. note
  12261. (list (cons "%u" (user-login-name))
  12262. (cons "%U" user-full-name)
  12263. (cons "%t" (format-time-string
  12264. (org-time-stamp-format 'long 'inactive)
  12265. (current-time)))
  12266. (cons "%s" (if org-log-note-state
  12267. (concat "\"" org-log-note-state "\"")
  12268. "")))))
  12269. (if lines (setq note (concat note " \\\\")))
  12270. (push note lines))
  12271. (when current-prefix-arg (setq lines nil))
  12272. (when lines
  12273. (save-excursion
  12274. (set-buffer (marker-buffer org-log-note-marker))
  12275. (save-excursion
  12276. (goto-char org-log-note-marker)
  12277. (move-marker org-log-note-marker nil)
  12278. (end-of-line 1)
  12279. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  12280. (insert " - " (pop lines))
  12281. (org-indent-line-function)
  12282. (beginning-of-line 1)
  12283. (looking-at "[ \t]*")
  12284. (setq ind (concat (match-string 0) " "))
  12285. (end-of-line 1)
  12286. (while lines (insert "\n" ind (pop lines)))))))
  12287. (set-window-configuration org-log-note-window-configuration)
  12288. (with-current-buffer (marker-buffer org-log-note-return-to)
  12289. (goto-char org-log-note-return-to))
  12290. (move-marker org-log-note-return-to nil)
  12291. (and org-log-post-message (message org-log-post-message)))
  12292. (defvar org-occur-highlights nil)
  12293. (make-variable-buffer-local 'org-occur-highlights)
  12294. (defun org-occur (regexp &optional keep-previous callback)
  12295. "Make a compact tree which shows all matches of REGEXP.
  12296. The tree will show the lines where the regexp matches, and all higher
  12297. headlines above the match. It will also show the heading after the match,
  12298. to make sure editing the matching entry is easy.
  12299. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12300. call to `org-occur' will be kept, to allow stacking of calls to this
  12301. command.
  12302. If CALLBACK is non-nil, it is a function which is called to confirm
  12303. that the match should indeed be shown."
  12304. (interactive "sRegexp: \nP")
  12305. (or keep-previous (org-remove-occur-highlights nil nil t))
  12306. (let ((cnt 0))
  12307. (save-excursion
  12308. (goto-char (point-min))
  12309. (if (or (not keep-previous) ; do not want to keep
  12310. (not org-occur-highlights)) ; no previous matches
  12311. ;; hide everything
  12312. (org-overview))
  12313. (while (re-search-forward regexp nil t)
  12314. (when (or (not callback)
  12315. (save-match-data (funcall callback)))
  12316. (setq cnt (1+ cnt))
  12317. (when org-highlight-sparse-tree-matches
  12318. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12319. (org-show-context 'occur-tree))))
  12320. (when org-remove-highlights-with-change
  12321. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12322. nil 'local))
  12323. (unless org-sparse-tree-open-archived-trees
  12324. (org-hide-archived-subtrees (point-min) (point-max)))
  12325. (run-hooks 'org-occur-hook)
  12326. (if (interactive-p)
  12327. (message "%d match(es) for regexp %s" cnt regexp))
  12328. cnt))
  12329. (defun org-show-context (&optional key)
  12330. "Make sure point and context and visible.
  12331. How much context is shown depends upon the variables
  12332. `org-show-hierarchy-above', `org-show-following-heading'. and
  12333. `org-show-siblings'."
  12334. (let ((heading-p (org-on-heading-p t))
  12335. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12336. (following-p (org-get-alist-option org-show-following-heading key))
  12337. (siblings-p (org-get-alist-option org-show-siblings key)))
  12338. (catch 'exit
  12339. ;; Show heading or entry text
  12340. (if heading-p
  12341. (org-flag-heading nil) ; only show the heading
  12342. (and (or (org-invisible-p) (org-invisible-p2))
  12343. (org-show-hidden-entry))) ; show entire entry
  12344. (when following-p
  12345. ;; Show next sibling, or heading below text
  12346. (save-excursion
  12347. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12348. (org-flag-heading nil))))
  12349. (when siblings-p (org-show-siblings))
  12350. (when hierarchy-p
  12351. ;; show all higher headings, possibly with siblings
  12352. (save-excursion
  12353. (while (and (condition-case nil
  12354. (progn (org-up-heading-all 1) t)
  12355. (error nil))
  12356. (not (bobp)))
  12357. (org-flag-heading nil)
  12358. (when siblings-p (org-show-siblings))))))))
  12359. (defun org-reveal (&optional siblings)
  12360. "Show current entry, hierarchy above it, and the following headline.
  12361. This can be used to show a consistent set of context around locations
  12362. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12363. not t for the search context.
  12364. With optional argument SIBLINGS, on each level of the hierarchy all
  12365. siblings are shown. This repairs the tree structure to what it would
  12366. look like when opened with hierarchical calls to `org-cycle'."
  12367. (interactive "P")
  12368. (let ((org-show-hierarchy-above t)
  12369. (org-show-following-heading t)
  12370. (org-show-siblings (if siblings t org-show-siblings)))
  12371. (org-show-context nil)))
  12372. (defun org-highlight-new-match (beg end)
  12373. "Highlight from BEG to END and mark the highlight is an occur headline."
  12374. (let ((ov (org-make-overlay beg end)))
  12375. (org-overlay-put ov 'face 'secondary-selection)
  12376. (push ov org-occur-highlights)))
  12377. (defun org-remove-occur-highlights (&optional beg end noremove)
  12378. "Remove the occur highlights from the buffer.
  12379. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12380. from the `before-change-functions' in the current buffer."
  12381. (interactive)
  12382. (unless org-inhibit-highlight-removal
  12383. (mapc 'org-delete-overlay org-occur-highlights)
  12384. (setq org-occur-highlights nil)
  12385. (unless noremove
  12386. (remove-hook 'before-change-functions
  12387. 'org-remove-occur-highlights 'local))))
  12388. ;;;; Priorities
  12389. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12390. "Regular expression matching the priority indicator.")
  12391. (defvar org-remove-priority-next-time nil)
  12392. (defun org-priority-up ()
  12393. "Increase the priority of the current item."
  12394. (interactive)
  12395. (org-priority 'up))
  12396. (defun org-priority-down ()
  12397. "Decrease the priority of the current item."
  12398. (interactive)
  12399. (org-priority 'down))
  12400. (defun org-priority (&optional action)
  12401. "Change the priority of an item by ARG.
  12402. ACTION can be `set', `up', `down', or a character."
  12403. (interactive)
  12404. (setq action (or action 'set))
  12405. (let (current new news have remove)
  12406. (save-excursion
  12407. (org-back-to-heading)
  12408. (if (looking-at org-priority-regexp)
  12409. (setq current (string-to-char (match-string 2))
  12410. have t)
  12411. (setq current org-default-priority))
  12412. (cond
  12413. ((or (eq action 'set) (integerp action))
  12414. (if (integerp action)
  12415. (setq new action)
  12416. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  12417. (setq new (read-char-exclusive)))
  12418. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12419. (= (upcase org-lowest-priority) org-lowest-priority))
  12420. (setq new (upcase new)))
  12421. (cond ((equal new ?\ ) (setq remove t))
  12422. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12423. (error "Priority must be between `%c' and `%c'"
  12424. org-highest-priority org-lowest-priority))))
  12425. ((eq action 'up)
  12426. (if (and (not have) (eq last-command this-command))
  12427. (setq new org-lowest-priority)
  12428. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12429. org-default-priority (1- current)))))
  12430. ((eq action 'down)
  12431. (if (and (not have) (eq last-command this-command))
  12432. (setq new org-highest-priority)
  12433. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12434. org-default-priority (1+ current)))))
  12435. (t (error "Invalid action")))
  12436. (if (or (< (upcase new) org-highest-priority)
  12437. (> (upcase new) org-lowest-priority))
  12438. (setq remove t))
  12439. (setq news (format "%c" new))
  12440. (if have
  12441. (if remove
  12442. (replace-match "" t t nil 1)
  12443. (replace-match news t t nil 2))
  12444. (if remove
  12445. (error "No priority cookie found in line")
  12446. (looking-at org-todo-line-regexp)
  12447. (if (match-end 2)
  12448. (progn
  12449. (goto-char (match-end 2))
  12450. (insert " [#" news "]"))
  12451. (goto-char (match-beginning 3))
  12452. (insert "[#" news "] ")))))
  12453. (org-preserve-lc (org-set-tags nil 'align))
  12454. (if remove
  12455. (message "Priority removed")
  12456. (message "Priority of current item set to %s" news))))
  12457. (defun org-get-priority (s)
  12458. "Find priority cookie and return priority."
  12459. (save-match-data
  12460. (if (not (string-match org-priority-regexp s))
  12461. (* 1000 (- org-lowest-priority org-default-priority))
  12462. (* 1000 (- org-lowest-priority
  12463. (string-to-char (match-string 2 s)))))))
  12464. ;;;; Tags
  12465. (defun org-scan-tags (action matcher &optional todo-only)
  12466. "Scan headline tags with inheritance and produce output ACTION.
  12467. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  12468. evaluated, testing if a given set of tags qualifies a headline for
  12469. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12470. are included in the output."
  12471. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12472. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12473. (org-re
  12474. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12475. (props (list 'face nil
  12476. 'done-face 'org-done
  12477. 'undone-face nil
  12478. 'mouse-face 'highlight
  12479. 'org-not-done-regexp org-not-done-regexp
  12480. 'org-todo-regexp org-todo-regexp
  12481. 'keymap org-agenda-keymap
  12482. 'help-echo
  12483. (format "mouse-2 or RET jump to org file %s"
  12484. (abbreviate-file-name buffer-file-name))))
  12485. (case-fold-search nil)
  12486. lspos
  12487. tags tags-list tags-alist (llast 0) rtn level category i txt
  12488. todo marker entry priority)
  12489. (save-excursion
  12490. (goto-char (point-min))
  12491. (when (eq action 'sparse-tree) (org-overview))
  12492. (while (re-search-forward re nil t)
  12493. (catch :skip
  12494. (setq todo (if (match-end 1) (match-string 2))
  12495. tags (if (match-end 4) (match-string 4)))
  12496. (goto-char (setq lspos (1+ (match-beginning 0))))
  12497. (setq level (org-reduced-level (funcall outline-level))
  12498. category (org-get-category))
  12499. (setq i llast llast level)
  12500. ;; remove tag lists from same and sublevels
  12501. (while (>= i level)
  12502. (when (setq entry (assoc i tags-alist))
  12503. (setq tags-alist (delete entry tags-alist)))
  12504. (setq i (1- i)))
  12505. ;; add the nex tags
  12506. (when tags
  12507. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12508. tags-alist
  12509. (cons (cons level tags) tags-alist)))
  12510. ;; compile tags for current headline
  12511. (setq tags-list
  12512. (if org-use-tag-inheritance
  12513. (apply 'append (mapcar 'cdr tags-alist))
  12514. tags))
  12515. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12516. (eval matcher)
  12517. (or (not org-agenda-skip-archived-trees)
  12518. (not (member org-archive-tag tags-list))))
  12519. (and (eq action 'agenda) (org-agenda-skip))
  12520. ;; list this headline
  12521. (if (eq action 'sparse-tree)
  12522. (progn
  12523. (org-show-context 'tags-tree))
  12524. (setq txt (org-format-agenda-item
  12525. ""
  12526. (concat
  12527. (if org-tags-match-list-sublevels
  12528. (make-string (1- level) ?.) "")
  12529. (org-get-heading))
  12530. category tags-list)
  12531. priority (org-get-priority txt))
  12532. (goto-char lspos)
  12533. (setq marker (org-agenda-new-marker))
  12534. (org-add-props txt props
  12535. 'org-marker marker 'org-hd-marker marker 'org-category category
  12536. 'priority priority 'type "tagsmatch")
  12537. (push txt rtn))
  12538. ;; if we are to skip sublevels, jump to end of subtree
  12539. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12540. (when (and (eq action 'sparse-tree)
  12541. (not org-sparse-tree-open-archived-trees))
  12542. (org-hide-archived-subtrees (point-min) (point-max)))
  12543. (nreverse rtn)))
  12544. (defvar todo-only) ;; dynamically scoped
  12545. (defun org-tags-sparse-tree (&optional todo-only match)
  12546. "Create a sparse tree according to tags string MATCH.
  12547. MATCH can contain positive and negative selection of tags, like
  12548. \"+WORK+URGENT-WITHBOSS\".
  12549. If optional argument TODO_ONLY is non-nil, only select lines that are
  12550. also TODO lines."
  12551. (interactive "P")
  12552. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12553. (defvar org-cached-props nil)
  12554. (defun org-cached-entry-get (pom property)
  12555. (cdr (assoc property (or org-cached-props
  12556. (setq org-cached-props
  12557. (org-entry-properties pom))))))
  12558. (defun org-global-tags-completion-table (&optional files)
  12559. "Return the list of all tags in all agenda buffer/files."
  12560. (save-excursion
  12561. (org-uniquify
  12562. (apply 'append
  12563. (mapcar
  12564. (lambda (file)
  12565. (set-buffer (find-file-noselect file))
  12566. (org-get-buffer-tags))
  12567. (if (and files (car files))
  12568. files
  12569. (org-agenda-files)))))))
  12570. (defun org-make-tags-matcher (match)
  12571. "Create the TAGS//TODO matcher form for the selection string MATCH."
  12572. ;; todo-only is scoped dynamically into this function, and the function
  12573. ;; may change it it the matcher asksk for it.
  12574. (unless match
  12575. ;; Get a new match request, with completion
  12576. (let ((org-last-tags-completion-table
  12577. (org-global-tags-completion-table)))
  12578. (setq match (completing-read
  12579. "Match: " 'org-tags-completion-function nil nil nil
  12580. 'org-tags-history))))
  12581. ;; Parse the string and create a lisp form
  12582. (let ((match0 match)
  12583. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  12584. minus tag mm
  12585. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12586. orterms term orlist re-p level-p prop-p pn pv)
  12587. (if (string-match "/+" match)
  12588. ;; match contains also a todo-matching request
  12589. (progn
  12590. (setq tagsmatch (substring match 0 (match-beginning 0))
  12591. todomatch (substring match (match-end 0)))
  12592. (if (string-match "^!" todomatch)
  12593. (setq todo-only t todomatch (substring todomatch 1)))
  12594. (if (string-match "^\\s-*$" todomatch)
  12595. (setq todomatch nil)))
  12596. ;; only matching tags
  12597. (setq tagsmatch match todomatch nil))
  12598. ;; Make the tags matcher
  12599. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12600. (setq tagsmatcher t)
  12601. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12602. (while (setq term (pop orterms))
  12603. (while (and (equal (substring term -1) "\\") orterms)
  12604. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12605. (while (string-match re term)
  12606. (setq minus (and (match-end 1)
  12607. (equal (match-string 1 term) "-"))
  12608. tag (match-string 2 term)
  12609. re-p (equal (string-to-char tag) ?{)
  12610. level-p (match-end 3)
  12611. prop-p (match-end 4)
  12612. mm (cond
  12613. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12614. (level-p `(= level ,(string-to-number
  12615. (match-string 3 term))))
  12616. (prop-p
  12617. (setq pn (match-string 4 term)
  12618. pv (match-string 5 term)
  12619. re-p (equal (string-to-char pv) ?{)
  12620. pv (substring pv 1 -1))
  12621. (if re-p
  12622. `(string-match ,pv (org-cached-entry-get nil ,pn))
  12623. `(equal ,pv (org-cached-entry-get nil ,pn))))
  12624. (t `(member ,(downcase tag) tags-list)))
  12625. mm (if minus (list 'not mm) mm)
  12626. term (substring term (match-end 0)))
  12627. (push mm tagsmatcher))
  12628. (push (if (> (length tagsmatcher) 1)
  12629. (cons 'and tagsmatcher)
  12630. (car tagsmatcher))
  12631. orlist)
  12632. (setq tagsmatcher nil))
  12633. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12634. (setq tagsmatcher
  12635. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12636. ;; Make the todo matcher
  12637. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12638. (setq todomatcher t)
  12639. (setq orterms (org-split-string todomatch "|") orlist nil)
  12640. (while (setq term (pop orterms))
  12641. (while (string-match re term)
  12642. (setq minus (and (match-end 1)
  12643. (equal (match-string 1 term) "-"))
  12644. kwd (match-string 2 term)
  12645. re-p (equal (string-to-char kwd) ?{)
  12646. term (substring term (match-end 0))
  12647. mm (if re-p
  12648. `(string-match ,(substring kwd 1 -1) todo)
  12649. (list 'equal 'todo kwd))
  12650. mm (if minus (list 'not mm) mm))
  12651. (push mm todomatcher))
  12652. (push (if (> (length todomatcher) 1)
  12653. (cons 'and todomatcher)
  12654. (car todomatcher))
  12655. orlist)
  12656. (setq todomatcher nil))
  12657. (setq todomatcher (if (> (length orlist) 1)
  12658. (cons 'or orlist) (car orlist))))
  12659. ;; Return the string and lisp forms of the matcher
  12660. (setq matcher (if todomatcher
  12661. (list 'and tagsmatcher todomatcher)
  12662. tagsmatcher))
  12663. (cons match0 matcher)))
  12664. (defun org-match-any-p (re list)
  12665. "Does re match any element of list?"
  12666. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12667. (delq nil list))
  12668. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  12669. (defvar org-tags-overlay (org-make-overlay 1 1))
  12670. (org-detach-overlay org-tags-overlay)
  12671. (defun org-align-tags-here (to-col)
  12672. ;; Assumes that this is a headline
  12673. (let ((pos (point)) (col (current-column)) tags)
  12674. (beginning-of-line 1)
  12675. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12676. (< pos (match-beginning 2)))
  12677. (progn
  12678. (setq tags (match-string 2))
  12679. (goto-char (match-beginning 1))
  12680. (insert " ")
  12681. (delete-region (point) (1+ (match-end 0)))
  12682. (backward-char 1)
  12683. (move-to-column
  12684. (max (1+ (current-column))
  12685. (1+ col)
  12686. (if (> to-col 0)
  12687. to-col
  12688. (- (abs to-col) (length tags))))
  12689. t)
  12690. (insert tags)
  12691. (move-to-column (min (current-column) col) t))
  12692. (goto-char pos))))
  12693. (defun org-set-tags (&optional arg just-align)
  12694. "Set the tags for the current headline.
  12695. With prefix ARG, realign all tags in headings in the current buffer."
  12696. (interactive "P")
  12697. (let* ((re (concat "^" outline-regexp))
  12698. (current (org-get-tags-string))
  12699. (col (current-column))
  12700. (org-setting-tags t)
  12701. table current-tags inherited-tags ; computed below when needed
  12702. tags p0 c0 c1 rpl)
  12703. (if arg
  12704. (save-excursion
  12705. (goto-char (point-min))
  12706. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12707. (while (re-search-forward re nil t)
  12708. (org-set-tags nil t)
  12709. (end-of-line 1)))
  12710. (message "All tags realigned to column %d" org-tags-column))
  12711. (if just-align
  12712. (setq tags current)
  12713. ;; Get a new set of tags from the user
  12714. (save-excursion
  12715. (setq table (or org-tag-alist (org-get-buffer-tags))
  12716. org-last-tags-completion-table table
  12717. current-tags (org-split-string current ":")
  12718. inherited-tags (nreverse
  12719. (nthcdr (length current-tags)
  12720. (nreverse (org-get-tags-at))))
  12721. tags
  12722. (if (or (eq t org-use-fast-tag-selection)
  12723. (and org-use-fast-tag-selection
  12724. (delq nil (mapcar 'cdr table))))
  12725. (org-fast-tag-selection
  12726. current-tags inherited-tags table
  12727. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  12728. (let ((org-add-colon-after-tag-completion t))
  12729. (org-trim
  12730. (completing-read "Tags: " 'org-tags-completion-function
  12731. nil nil current 'org-tags-history))))))
  12732. (while (string-match "[-+&]+" tags)
  12733. ;; No boolean logic, just a list
  12734. (setq tags (replace-match ":" t t tags))))
  12735. (if (string-match "\\`[\t ]*\\'" tags)
  12736. (setq tags "")
  12737. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12738. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12739. ;; Insert new tags at the correct column
  12740. (beginning-of-line 1)
  12741. (cond
  12742. ((and (equal current "") (equal tags "")))
  12743. ((re-search-forward
  12744. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12745. (point-at-eol) t)
  12746. (if (equal tags "")
  12747. (setq rpl "")
  12748. (goto-char (match-beginning 0))
  12749. (setq c0 (current-column) p0 (point)
  12750. c1 (max (1+ c0) (if (> org-tags-column 0)
  12751. org-tags-column
  12752. (- (- org-tags-column) (length tags))))
  12753. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12754. (replace-match rpl t t)
  12755. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12756. tags)
  12757. (t (error "Tags alignment failed")))
  12758. (move-to-column col)
  12759. (unless just-align
  12760. (run-hooks 'org-after-tags-change-hook)))))
  12761. (defun org-change-tag-in-region (beg end tag off)
  12762. "Add or remove TAG for each entry in the region.
  12763. This works in the agenda, and also in an org-mode buffer."
  12764. (interactive
  12765. (list (region-beginning) (region-end)
  12766. (let ((org-last-tags-completion-table
  12767. (if (org-mode-p)
  12768. (org-get-buffer-tags)
  12769. (org-global-tags-completion-table))))
  12770. (completing-read
  12771. "Tag: " 'org-tags-completion-function nil nil nil
  12772. 'org-tags-history))
  12773. (progn
  12774. (message "[s]et or [r]emove? ")
  12775. (equal (read-char-exclusive) ?r))))
  12776. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12777. (let ((agendap (equal major-mode 'org-agenda-mode))
  12778. l1 l2 m buf pos newhead (cnt 0))
  12779. (goto-char end)
  12780. (setq l2 (1- (org-current-line)))
  12781. (goto-char beg)
  12782. (setq l1 (org-current-line))
  12783. (loop for l from l1 to l2 do
  12784. (goto-line l)
  12785. (setq m (get-text-property (point) 'org-hd-marker))
  12786. (when (or (and (org-mode-p) (org-on-heading-p))
  12787. (and agendap m))
  12788. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12789. pos (if agendap m (point)))
  12790. (with-current-buffer buf
  12791. (save-excursion
  12792. (save-restriction
  12793. (goto-char pos)
  12794. (setq cnt (1+ cnt))
  12795. (org-toggle-tag tag (if off 'off 'on))
  12796. (setq newhead (org-get-heading)))))
  12797. (and agendap (org-agenda-change-all-lines newhead m))))
  12798. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12799. (defun org-tags-completion-function (string predicate &optional flag)
  12800. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12801. (confirm (lambda (x) (stringp (car x)))))
  12802. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12803. (setq s1 (match-string 1 string)
  12804. s2 (match-string 2 string))
  12805. (setq s1 "" s2 string))
  12806. (cond
  12807. ((eq flag nil)
  12808. ;; try completion
  12809. (setq rtn (try-completion s2 ctable confirm))
  12810. (if (stringp rtn)
  12811. (setq rtn
  12812. (concat s1 s2 (substring rtn (length s2))
  12813. (if (and org-add-colon-after-tag-completion
  12814. (assoc rtn ctable))
  12815. ":" ""))))
  12816. rtn)
  12817. ((eq flag t)
  12818. ;; all-completions
  12819. (all-completions s2 ctable confirm)
  12820. )
  12821. ((eq flag 'lambda)
  12822. ;; exact match?
  12823. (assoc s2 ctable)))
  12824. ))
  12825. (defun org-fast-tag-insert (kwd tags face &optional end)
  12826. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12827. (insert (format "%-12s" (concat kwd ":"))
  12828. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12829. (or end "")))
  12830. (defun org-fast-tag-show-exit (flag)
  12831. (save-excursion
  12832. (goto-line 3)
  12833. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12834. (replace-match ""))
  12835. (when flag
  12836. (end-of-line 1)
  12837. (move-to-column (- (window-width) 19) t)
  12838. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12839. (defun org-set-current-tags-overlay (current prefix)
  12840. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12841. (if (featurep 'xemacs)
  12842. (org-overlay-display org-tags-overlay (concat prefix s)
  12843. 'secondary-selection)
  12844. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12845. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12846. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12847. "Fast tag selection with single keys.
  12848. CURRENT is the current list of tags in the headline, INHERITED is the
  12849. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12850. possibly with grouping information. TODO-TABLE is a similar table with
  12851. TODO keywords, should these have keys assigned to them.
  12852. If the keys are nil, a-z are automatically assigned.
  12853. Returns the new tags string, or nil to not change the current settings."
  12854. (let* ((fulltable (append table todo-table))
  12855. (maxlen (apply 'max (mapcar
  12856. (lambda (x)
  12857. (if (stringp (car x)) (string-width (car x)) 0))
  12858. fulltable)))
  12859. (buf (current-buffer))
  12860. (expert (eq org-fast-tag-selection-single-key 'expert))
  12861. (buffer-tags nil)
  12862. (fwidth (+ maxlen 3 1 3))
  12863. (ncol (/ (- (window-width) 4) fwidth))
  12864. (i-face 'org-done)
  12865. (c-face 'org-todo)
  12866. tg cnt e c char c1 c2 ntable tbl rtn
  12867. ov-start ov-end ov-prefix
  12868. (exit-after-next org-fast-tag-selection-single-key)
  12869. (done-keywords org-done-keywords)
  12870. groups ingroup)
  12871. (save-excursion
  12872. (beginning-of-line 1)
  12873. (if (looking-at
  12874. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12875. (setq ov-start (match-beginning 1)
  12876. ov-end (match-end 1)
  12877. ov-prefix "")
  12878. (setq ov-start (1- (point-at-eol))
  12879. ov-end (1+ ov-start))
  12880. (skip-chars-forward "^\n\r")
  12881. (setq ov-prefix
  12882. (concat
  12883. (buffer-substring (1- (point)) (point))
  12884. (if (> (current-column) org-tags-column)
  12885. " "
  12886. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12887. (org-move-overlay org-tags-overlay ov-start ov-end)
  12888. (save-window-excursion
  12889. (if expert
  12890. (set-buffer (get-buffer-create " *Org tags*"))
  12891. (delete-other-windows)
  12892. (split-window-vertically)
  12893. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12894. (erase-buffer)
  12895. (org-set-local 'org-done-keywords done-keywords)
  12896. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12897. (org-fast-tag-insert "Current" current c-face "\n\n")
  12898. (org-fast-tag-show-exit exit-after-next)
  12899. (org-set-current-tags-overlay current ov-prefix)
  12900. (setq tbl fulltable char ?a cnt 0)
  12901. (while (setq e (pop tbl))
  12902. (cond
  12903. ((equal e '(:startgroup))
  12904. (push '() groups) (setq ingroup t)
  12905. (when (not (= cnt 0))
  12906. (setq cnt 0)
  12907. (insert "\n"))
  12908. (insert "{ "))
  12909. ((equal e '(:endgroup))
  12910. (setq ingroup nil cnt 0)
  12911. (insert "}\n"))
  12912. (t
  12913. (setq tg (car e) c2 nil)
  12914. (if (cdr e)
  12915. (setq c (cdr e))
  12916. ;; automatically assign a character.
  12917. (setq c1 (string-to-char
  12918. (downcase (substring
  12919. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12920. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12921. (while (or (rassoc char ntable) (rassoc char table))
  12922. (setq char (1+ char)))
  12923. (setq c2 c1))
  12924. (setq c (or c2 char)))
  12925. (if ingroup (push tg (car groups)))
  12926. (setq tg (org-add-props tg nil 'face
  12927. (cond
  12928. ((not (assoc tg table))
  12929. (org-get-todo-face tg))
  12930. ((member tg current) c-face)
  12931. ((member tg inherited) i-face)
  12932. (t nil))))
  12933. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12934. (insert "[" c "] " tg (make-string
  12935. (- fwidth 4 (length tg)) ?\ ))
  12936. (push (cons tg c) ntable)
  12937. (when (= (setq cnt (1+ cnt)) ncol)
  12938. (insert "\n")
  12939. (if ingroup (insert " "))
  12940. (setq cnt 0)))))
  12941. (setq ntable (nreverse ntable))
  12942. (insert "\n")
  12943. (goto-char (point-min))
  12944. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12945. (fit-window-to-buffer))
  12946. (setq rtn
  12947. (catch 'exit
  12948. (while t
  12949. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12950. (if groups " [!] no groups" " [!]groups")
  12951. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12952. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12953. (cond
  12954. ((= c ?\r) (throw 'exit t))
  12955. ((= c ?!)
  12956. (setq groups (not groups))
  12957. (goto-char (point-min))
  12958. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12959. ((= c ?\C-c)
  12960. (if (not expert)
  12961. (org-fast-tag-show-exit
  12962. (setq exit-after-next (not exit-after-next)))
  12963. (setq expert nil)
  12964. (delete-other-windows)
  12965. (split-window-vertically)
  12966. (org-switch-to-buffer-other-window " *Org tags*")
  12967. (and (fboundp 'fit-window-to-buffer)
  12968. (fit-window-to-buffer))))
  12969. ((or (= c ?\C-g)
  12970. (and (= c ?q) (not (rassoc c ntable))))
  12971. (org-detach-overlay org-tags-overlay)
  12972. (setq quit-flag t))
  12973. ((= c ?\ )
  12974. (setq current nil)
  12975. (if exit-after-next (setq exit-after-next 'now)))
  12976. ((= c ?\t)
  12977. (condition-case nil
  12978. (setq tg (completing-read
  12979. "Tag: "
  12980. (or buffer-tags
  12981. (with-current-buffer buf
  12982. (org-get-buffer-tags)))))
  12983. (quit (setq tg "")))
  12984. (when (string-match "\\S-" tg)
  12985. (add-to-list 'buffer-tags (list tg))
  12986. (if (member tg current)
  12987. (setq current (delete tg current))
  12988. (push tg current)))
  12989. (if exit-after-next (setq exit-after-next 'now)))
  12990. ((setq e (rassoc c todo-table) tg (car e))
  12991. (with-current-buffer buf
  12992. (save-excursion (org-todo tg)))
  12993. (if exit-after-next (setq exit-after-next 'now)))
  12994. ((setq e (rassoc c ntable) tg (car e))
  12995. (if (member tg current)
  12996. (setq current (delete tg current))
  12997. (loop for g in groups do
  12998. (if (member tg g)
  12999. (mapcar (lambda (x)
  13000. (setq current (delete x current)))
  13001. g)))
  13002. (push tg current))
  13003. (if exit-after-next (setq exit-after-next 'now))))
  13004. ;; Create a sorted list
  13005. (setq current
  13006. (sort current
  13007. (lambda (a b)
  13008. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13009. (if (eq exit-after-next 'now) (throw 'exit t))
  13010. (goto-char (point-min))
  13011. (beginning-of-line 2)
  13012. (delete-region (point) (point-at-eol))
  13013. (org-fast-tag-insert "Current" current c-face)
  13014. (org-set-current-tags-overlay current ov-prefix)
  13015. (while (re-search-forward
  13016. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  13017. (setq tg (match-string 1))
  13018. (add-text-properties
  13019. (match-beginning 1) (match-end 1)
  13020. (list 'face
  13021. (cond
  13022. ((member tg current) c-face)
  13023. ((member tg inherited) i-face)
  13024. (t (get-text-property (match-beginning 1) 'face))))))
  13025. (goto-char (point-min)))))
  13026. (org-detach-overlay org-tags-overlay)
  13027. (if rtn
  13028. (mapconcat 'identity current ":")
  13029. nil))))
  13030. (defun org-get-tags-string ()
  13031. "Get the TAGS string in the current headline."
  13032. (unless (org-on-heading-p t)
  13033. (error "Not on a heading"))
  13034. (save-excursion
  13035. (beginning-of-line 1)
  13036. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13037. (org-match-string-no-properties 1)
  13038. "")))
  13039. (defun org-get-tags ()
  13040. "Get the list of tags specified in the current headline."
  13041. (org-split-string (org-get-tags-string) ":"))
  13042. (defun org-get-buffer-tags ()
  13043. "Get a table of all tags used in the buffer, for completion."
  13044. (let (tags)
  13045. (save-excursion
  13046. (goto-char (point-min))
  13047. (while (re-search-forward
  13048. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  13049. (when (equal (char-after (point-at-bol 0)) ?*)
  13050. (mapc (lambda (x) (add-to-list 'tags x))
  13051. (org-split-string (org-match-string-no-properties 1) ":")))))
  13052. (mapcar 'list tags)))
  13053. ;;;; Properties
  13054. ;;; Setting and retrieving properties
  13055. (defconst org-special-properties
  13056. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  13057. "CLOCK" "PRIORITY")
  13058. "The special properties valid in Org-mode.
  13059. These are properties that are not defined in the property drawer,
  13060. but in some other way.")
  13061. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13062. "Regular expression matching the first line of a property drawer.")
  13063. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13064. "Regular expression matching the first line of a property drawer.")
  13065. (defun org-property-action ()
  13066. "Do an action on properties."
  13067. (interactive)
  13068. (let (c prop)
  13069. (org-at-property-p)
  13070. (setq prop (match-string 2))
  13071. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13072. (setq c (read-char-exclusive))
  13073. (cond
  13074. ((equal c ?s)
  13075. (call-interactively 'org-set-property))
  13076. ((equal c ?d)
  13077. (call-interactively 'org-delete-property))
  13078. ((equal c ?D)
  13079. (call-interactively 'org-delete-property-globally))
  13080. ((equal c ?c)
  13081. (call-interactively 'org-compute-property-at-point))
  13082. (t (error "No such property action %c" c)))))
  13083. (defun org-at-property-p ()
  13084. "Is the cursor in a property line?"
  13085. ;; FIXME: Does not check if we are actually in the drawer.
  13086. ;; FIXME: also returns true on any drawers.....
  13087. ;; This is used by C-c C-c for property action.
  13088. (save-excursion
  13089. (beginning-of-line 1)
  13090. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  13091. (defmacro org-with-point-at (pom &rest body)
  13092. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  13093. (declare (indent 1) (debug t))
  13094. `(save-excursion
  13095. (if (markerp pom) (set-buffer (marker-buffer pom)))
  13096. (save-excursion
  13097. (goto-char (or pom (point)))
  13098. ,@body)))
  13099. (defun org-get-property-block (&optional beg end force)
  13100. "Return the (beg . end) range of the body of the property drawer.
  13101. BEG and END can be beginning and end of subtree, if not given
  13102. they will be found.
  13103. If the drawer does not exist and FORCE is non-nil, create the drawer."
  13104. (catch 'exit
  13105. (save-excursion
  13106. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  13107. (end (or end (progn (outline-next-heading) (point)))))
  13108. (goto-char beg)
  13109. (if (re-search-forward org-property-start-re end t)
  13110. (setq beg (1+ (match-end 0)))
  13111. (if force
  13112. (save-excursion
  13113. (org-insert-property-drawer)
  13114. (setq end (progn (outline-next-heading) (point))))
  13115. (throw 'exit nil))
  13116. (goto-char beg)
  13117. (if (re-search-forward org-property-start-re end t)
  13118. (setq beg (1+ (match-end 0)))))
  13119. (if (re-search-forward org-property-end-re end t)
  13120. (setq end (match-beginning 0))
  13121. (or force (throw 'exit nil))
  13122. (goto-char beg)
  13123. (setq end beg)
  13124. (org-indent-line-function)
  13125. (insert ":END:\n"))
  13126. (cons beg end)))))
  13127. (defun org-entry-properties (&optional pom which)
  13128. "Get all properties of the entry at point-or-marker POM.
  13129. This includes the TODO keyword, the tags, time strings for deadline,
  13130. scheduled, and clocking, and any additional properties defined in the
  13131. entry. The return value is an alist, keys may occur multiple times
  13132. if the property key was used several times.
  13133. POM may also be nil, in which case the current entry is used.
  13134. If WHICH is nil or `all', get all properties. If WHICH is
  13135. `special' or `standard', only get that subclass."
  13136. (setq which (or which 'all))
  13137. (org-with-point-at pom
  13138. (let ((clockstr (substring org-clock-string 0 -1))
  13139. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  13140. beg end range props sum-props key value)
  13141. (save-excursion
  13142. (when (condition-case nil (org-back-to-heading t) (error nil))
  13143. (setq beg (point))
  13144. (setq sum-props (get-text-property (point) 'org-summaries))
  13145. (outline-next-heading)
  13146. (setq end (point))
  13147. (when (memq which '(all special))
  13148. ;; Get the special properties, like TODO and tags
  13149. (goto-char beg)
  13150. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  13151. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13152. (when (looking-at org-priority-regexp)
  13153. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13154. (when (and (setq value (org-get-tags-string))
  13155. (string-match "\\S-" value))
  13156. (push (cons "TAGS" value) props))
  13157. (when (setq value (org-get-tags-at))
  13158. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  13159. props))
  13160. (while (re-search-forward org-keyword-time-regexp end t)
  13161. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  13162. (unless (member key excluded) (push key excluded))
  13163. (push (cons key
  13164. (if (equal key clockstr)
  13165. (org-no-properties
  13166. (org-trim
  13167. (buffer-substring
  13168. (match-beginning 2) (point-at-eol))))
  13169. (org-match-string-no-properties 2)))
  13170. props)))
  13171. (when (memq which '(all standard))
  13172. ;; Get the standard properties, like :PORP: ...
  13173. (setq range (org-get-property-block beg end))
  13174. (when range
  13175. (goto-char (car range))
  13176. (while (re-search-forward
  13177. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13178. (cdr range) t)
  13179. (setq key (org-match-string-no-properties 1)
  13180. value (org-trim (or (org-match-string-no-properties 2) "")))
  13181. (unless (member key excluded)
  13182. (push (cons key (or value "")) props)))))
  13183. (append sum-props (nreverse props)))))))
  13184. (defun org-entry-get (pom property &optional inherit)
  13185. "Get value of PROPERTY for entry at point-or-marker POM.
  13186. If INHERIT is non-nil and the entry does not have the property,
  13187. then also check higher levels of the hierarchy.
  13188. If the property is present but empty, the return value is the empty string.
  13189. If the property is not present at all, nil is returned."
  13190. (org-with-point-at pom
  13191. (if inherit
  13192. (org-entry-get-with-inheritance property)
  13193. (if (member property org-special-properties)
  13194. ;; We need a special property. Use brute force, get all properties.
  13195. (cdr (assoc property (org-entry-properties nil 'special)))
  13196. (let ((range (org-get-property-block)))
  13197. (if (and range
  13198. (goto-char (car range))
  13199. (re-search-forward
  13200. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  13201. (cdr range) t))
  13202. ;; Found the property, return it.
  13203. (if (match-end 1)
  13204. (org-match-string-no-properties 1)
  13205. "")))))))
  13206. (defun org-entry-delete (pom property)
  13207. "Delete the property PROPERTY from entry at point-or-marker POM."
  13208. (org-with-point-at pom
  13209. (if (member property org-special-properties)
  13210. nil ; cannot delete these properties.
  13211. (let ((range (org-get-property-block)))
  13212. (if (and range
  13213. (goto-char (car range))
  13214. (re-search-forward
  13215. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  13216. (cdr range) t))
  13217. (progn
  13218. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13219. t)
  13220. nil)))))
  13221. (defvar org-entry-property-inherited-from (make-marker))
  13222. (defun org-entry-get-with-inheritance (property)
  13223. "Get entry property, and search higher levels if not present."
  13224. (let (tmp)
  13225. (save-excursion
  13226. (catch 'ex
  13227. (while t
  13228. (when (setq tmp (org-entry-get nil property))
  13229. (org-back-to-heading t)
  13230. (move-marker org-entry-property-inherited-from (point))
  13231. (throw 'ex tmp))
  13232. (or (org-up-heading-safe) (throw 'ex nil)))))
  13233. (or tmp (cdr (assoc property org-local-properties))
  13234. (cdr (assoc property org-global-properties)))))
  13235. (defun org-entry-put (pom property value)
  13236. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13237. (org-with-point-at pom
  13238. (org-back-to-heading t)
  13239. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13240. range)
  13241. (cond
  13242. ((equal property "TODO")
  13243. (when (and (stringp value) (string-match "\\S-" value)
  13244. (not (member value org-todo-keywords-1)))
  13245. (error "\"%s\" is not a valid TODO state" value))
  13246. (if (or (not value)
  13247. (not (string-match "\\S-" value)))
  13248. (setq value 'none))
  13249. (org-todo value)
  13250. (org-set-tags nil 'align))
  13251. ((equal property "PRIORITY")
  13252. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13253. (string-to-char value) ?\ ))
  13254. (org-set-tags nil 'align))
  13255. ((equal property "SCHEDULED")
  13256. (if (re-search-forward org-scheduled-time-regexp end t)
  13257. (cond
  13258. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13259. ((eq value 'later) (org-timestamp-change 1 'day))
  13260. (t (call-interactively 'org-schedule)))
  13261. (call-interactively 'org-schedule)))
  13262. ((equal property "DEADLINE")
  13263. (if (re-search-forward org-deadline-time-regexp end t)
  13264. (cond
  13265. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13266. ((eq value 'later) (org-timestamp-change 1 'day))
  13267. (t (call-interactively 'org-deadline)))
  13268. (call-interactively 'org-deadline)))
  13269. ((and (member property org-special-properties)
  13270. (not (member property '("CATEGORY" "ARCHIVE"))))
  13271. (error "The %s property can not yet be set with `org-entry-put'"
  13272. property))
  13273. (t ; a non-special property
  13274. (setq range (org-get-property-block beg end 'force))
  13275. (goto-char (car range))
  13276. (if (re-search-forward
  13277. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  13278. (progn
  13279. (delete-region (match-beginning 1) (match-end 1))
  13280. (goto-char (match-beginning 1)))
  13281. (goto-char (cdr range))
  13282. (insert "\n")
  13283. (backward-char 1)
  13284. (org-indent-line-function)
  13285. (insert ":" property ":"))
  13286. (and value (insert " " value))
  13287. (org-indent-line-function))))))
  13288. (defun org-buffer-property-keys (&optional include-specials)
  13289. "Get all property keys in the current buffer."
  13290. (let (rtn range)
  13291. (save-excursion
  13292. (save-restriction
  13293. (widen)
  13294. (goto-char (point-min))
  13295. (while (re-search-forward org-property-start-re nil t)
  13296. (setq range (org-get-property-block))
  13297. (goto-char (car range))
  13298. (while (re-search-forward
  13299. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  13300. (cdr range) t)
  13301. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13302. (outline-next-heading))))
  13303. (when include-specials
  13304. (setq rtn (append org-special-properties rtn)))
  13305. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13306. (defun org-insert-property-drawer ()
  13307. "Insert a property drawer into the current entry."
  13308. (interactive)
  13309. (org-back-to-heading t)
  13310. (let ((beg (point))
  13311. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13312. end hiddenp)
  13313. (outline-next-heading)
  13314. (setq end (point))
  13315. (goto-char beg)
  13316. (while (re-search-forward re end t))
  13317. (setq hiddenp (org-invisible-p))
  13318. (end-of-line 1)
  13319. (and (= (char-after) ?\n) (forward-char 1))
  13320. (org-skip-over-state-notes)
  13321. (end-of-line 0)
  13322. (insert "\n:PROPERTIES:\n:END:")
  13323. (beginning-of-line 0)
  13324. (org-indent-line-function)
  13325. (beginning-of-line 2)
  13326. (org-indent-line-function)
  13327. (beginning-of-line 0)
  13328. (if hiddenp
  13329. (save-excursion
  13330. (org-back-to-heading t)
  13331. (hide-entry))
  13332. (org-flag-drawer t))))
  13333. (defun org-set-property (property value)
  13334. "In the current entry, set PROPERTY to VALUE."
  13335. (interactive
  13336. (let* ((prop (completing-read "Property: "
  13337. (mapcar 'list (org-buffer-property-keys))))
  13338. (cur (org-entry-get nil prop))
  13339. (allowed (org-property-get-allowed-values nil prop 'table))
  13340. (val (if allowed
  13341. (completing-read "Value: " allowed nil 'req-match)
  13342. (read-string
  13343. (concat "Value" (if (and cur (string-match "\\S-" cur))
  13344. (concat "[" cur "]") "")
  13345. ": ")
  13346. "" cur))))
  13347. (list prop (if (equal val "") cur val))))
  13348. (unless (equal (org-entry-get nil property) value)
  13349. (org-entry-put nil property value)))
  13350. (defun org-delete-property (property)
  13351. "In the current entry, delete PROPERTY."
  13352. (interactive
  13353. (let* ((prop (completing-read
  13354. "Property: " (org-entry-properties nil 'standard))))
  13355. (list prop)))
  13356. (message (concat "Property " property
  13357. (if (org-entry-delete nil property)
  13358. " deleted"
  13359. " was not present in the entry"))))
  13360. (defun org-delete-property-globally (property)
  13361. "Remove PROPERTY globally, from all entries."
  13362. (interactive
  13363. (let* ((prop (completing-read
  13364. "Globally remove property: "
  13365. (mapcar 'list (org-buffer-property-keys)))))
  13366. (list prop)))
  13367. (save-excursion
  13368. (save-restriction
  13369. (widen)
  13370. (goto-char (point-min))
  13371. (let ((cnt 0))
  13372. (while (re-search-forward
  13373. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  13374. nil t)
  13375. (setq cnt (1+ cnt))
  13376. (replace-match ""))
  13377. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13378. (defvar org-columns-current-fmt-compiled) ; defined below
  13379. (defun org-compute-property-at-point ()
  13380. "FIXME:"
  13381. (interactive)
  13382. (unless (org-at-property-p)
  13383. (error "Not at a property"))
  13384. (let ((prop (org-match-string-no-properties 2)))
  13385. (org-columns-get-format-and-top-level)
  13386. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13387. (error "No operator defined for property %s" prop))
  13388. (org-columns-compute prop)))
  13389. (defun org-property-get-allowed-values (pom property &optional table)
  13390. "Get allowed values for the property PROPERTY.
  13391. When TABLE is non-nil, return an alist that can directly be used for
  13392. completion."
  13393. (let (vals)
  13394. (cond
  13395. ((equal property "TODO")
  13396. (setq vals (org-with-point-at pom
  13397. (append org-todo-keywords-1 '("")))))
  13398. ((equal property "PRIORITY")
  13399. (let ((n org-lowest-priority))
  13400. (while (>= n org-highest-priority)
  13401. (push (char-to-string n) vals)
  13402. (setq n (1- n)))))
  13403. ((member property org-special-properties))
  13404. (t
  13405. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13406. (when (and vals (string-match "\\S-" vals))
  13407. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13408. (setq vals (mapcar (lambda (x)
  13409. (cond ((stringp x) x)
  13410. ((numberp x) (number-to-string x))
  13411. ((symbolp x) (symbol-name x))
  13412. (t "???")))
  13413. vals)))))
  13414. (if table (mapcar 'list vals) vals)))
  13415. (defun org-property-previous-allowed-value (&optional previous)
  13416. "Switch to the next allowed value for this property."
  13417. (interactive)
  13418. (org-property-next-allowed-value t))
  13419. (defun org-property-next-allowed-value (&optional previous)
  13420. "Switch to the next allowed value for this property."
  13421. (interactive)
  13422. (unless (org-at-property-p)
  13423. (error "Not at a property"))
  13424. (let* ((key (match-string 2))
  13425. (value (match-string 3))
  13426. (allowed (or (org-property-get-allowed-values (point) key)
  13427. (and (member value '("[ ]" "[-]" "[X]"))
  13428. '("[ ]" "[X]"))))
  13429. nval)
  13430. (unless allowed
  13431. (error "Allowed values for this property have not been defined"))
  13432. (if previous (setq allowed (reverse allowed)))
  13433. (if (member value allowed)
  13434. (setq nval (car (cdr (member value allowed)))))
  13435. (setq nval (or nval (car allowed)))
  13436. (if (equal nval value)
  13437. (error "Only one allowed value for this property"))
  13438. (org-at-property-p)
  13439. (replace-match (concat " :" key ": " nval) t t)
  13440. (org-indent-line-function)
  13441. (beginning-of-line 1)
  13442. (skip-chars-forward " \t")))
  13443. ;;; Column View
  13444. (defvar org-columns-overlays nil
  13445. "Holds the list of current column overlays.")
  13446. (defvar org-columns-current-fmt nil
  13447. "Local variable, holds the currently active column format.")
  13448. (defvar org-columns-current-fmt-compiled nil
  13449. "Local variable, holds the currently active column format.
  13450. This is the compiled version of the format.")
  13451. (defvar org-columns-current-maxwidths nil
  13452. "Loval variable, holds the currently active maximum column widths.")
  13453. (defvar org-columns-begin-marker (make-marker)
  13454. "Points to the position where last a column creation command was called.")
  13455. (defvar org-columns-top-level-marker (make-marker)
  13456. "Points to the position where current columns region starts.")
  13457. (defvar org-columns-map (make-sparse-keymap)
  13458. "The keymap valid in column display.")
  13459. (defun org-columns-content ()
  13460. "Switch to contents view while in columns view."
  13461. (interactive)
  13462. (org-overview)
  13463. (org-content))
  13464. (org-defkey org-columns-map "c" 'org-columns-content)
  13465. (org-defkey org-columns-map "o" 'org-overview)
  13466. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  13467. (org-defkey org-columns-map "v" 'org-columns-show-value)
  13468. (org-defkey org-columns-map "q" 'org-columns-quit)
  13469. (org-defkey org-columns-map "r" 'org-columns-redo)
  13470. (org-defkey org-columns-map [left] 'backward-char)
  13471. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  13472. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  13473. (org-defkey org-columns-map [right] 'forward-char)
  13474. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  13475. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  13476. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  13477. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  13478. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  13479. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  13480. (org-defkey org-columns-map "<" 'org-columns-narrow)
  13481. (org-defkey org-columns-map ">" 'org-columns-widen)
  13482. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  13483. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  13484. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  13485. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  13486. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  13487. '("Column"
  13488. ["Edit property" org-columns-edit-value t]
  13489. ["Next allowed value" org-columns-next-allowed-value t]
  13490. ["Previous allowed value" org-columns-previous-allowed-value t]
  13491. ["Show full value" org-columns-show-value t]
  13492. ["Edit allowed" org-columns-edit-allowed t]
  13493. "--"
  13494. ["Edit column attributes" org-columns-edit-attributes t]
  13495. ["Increase column width" org-columns-widen t]
  13496. ["Decrease column width" org-columns-narrow t]
  13497. "--"
  13498. ["Move column right" org-columns-move-right t]
  13499. ["Move column left" org-columns-move-left t]
  13500. ["Add column" org-columns-new t]
  13501. ["Delete column" org-columns-delete t]
  13502. "--"
  13503. ["CONTENTS" org-columns-content t]
  13504. ["OVERVIEW" org-overview t]
  13505. ["Refresh columns display" org-columns-redo t]
  13506. "--"
  13507. ["Quit" org-columns-quit t]))
  13508. (defun org-columns-new-overlay (beg end &optional string face)
  13509. "Create a new column overlay and add it to the list."
  13510. (let ((ov (org-make-overlay beg end)))
  13511. (org-overlay-put ov 'face (or face 'secondary-selection))
  13512. (org-overlay-display ov string face)
  13513. (push ov org-columns-overlays)
  13514. ov))
  13515. (defun org-columns-display-here (&optional props)
  13516. "Overlay the current line with column display."
  13517. (interactive)
  13518. (let* ((fmt org-columns-current-fmt-compiled)
  13519. (beg (point-at-bol))
  13520. (level-face (save-excursion
  13521. (beginning-of-line 1)
  13522. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  13523. (org-get-level-face 2))))
  13524. (color (list :foreground
  13525. (face-attribute (or level-face 'default) :foreground)))
  13526. props pom property ass width f string ov column)
  13527. ;; Check if the entry is in another buffer.
  13528. (unless props
  13529. (if (eq major-mode 'org-agenda-mode)
  13530. (setq pom (or (get-text-property (point) 'org-hd-marker)
  13531. (get-text-property (point) 'org-marker))
  13532. props (if pom (org-entry-properties pom) nil))
  13533. (setq props (org-entry-properties nil))))
  13534. ;; Walk the format
  13535. (while (setq column (pop fmt))
  13536. (setq property (car column)
  13537. ass (if (equal property "ITEM")
  13538. (cons "ITEM"
  13539. (save-match-data
  13540. (org-no-properties
  13541. (org-remove-tabs
  13542. (buffer-substring-no-properties
  13543. (point-at-bol) (point-at-eol))))))
  13544. (assoc property props))
  13545. width (or (cdr (assoc property org-columns-current-maxwidths))
  13546. (nth 2 column))
  13547. f (format "%%-%d.%ds | " width width)
  13548. string (format f (or (cdr ass) "")))
  13549. ;; Create the overlay
  13550. (org-unmodified
  13551. (setq ov (org-columns-new-overlay
  13552. beg (setq beg (1+ beg)) string
  13553. (list color 'org-column)))
  13554. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  13555. (org-overlay-put ov 'keymap org-columns-map)
  13556. (org-overlay-put ov 'org-columns-key property)
  13557. (org-overlay-put ov 'org-columns-value (cdr ass))
  13558. (org-overlay-put ov 'org-columns-pom pom)
  13559. (org-overlay-put ov 'org-columns-format f))
  13560. (if (or (not (char-after beg))
  13561. (equal (char-after beg) ?\n))
  13562. (let ((inhibit-read-only t))
  13563. (save-excursion
  13564. (goto-char beg)
  13565. (insert " ")))))
  13566. ;; Make the rest of the line disappear.
  13567. (org-unmodified
  13568. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  13569. (org-overlay-put ov 'invisible t)
  13570. (org-overlay-put ov 'keymap org-columns-map)
  13571. (org-overlay-put ov 'intangible t)
  13572. (push ov org-columns-overlays)
  13573. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  13574. (org-overlay-put ov 'keymap org-columns-map)
  13575. (push ov org-columns-overlays)
  13576. (let ((inhibit-read-only t))
  13577. (put-text-property (max (point-min) (1- (point-at-bol)))
  13578. (min (point-max) (1+ (point-at-eol)))
  13579. 'read-only "Type `e' to edit property")))))
  13580. (defvar org-previous-header-line-format nil
  13581. "The header line format before column view was turned on.")
  13582. (defvar org-columns-inhibit-recalculation nil
  13583. "Inhibit recomputing of columns on column view startup.")
  13584. (defvar header-line-format)
  13585. (defun org-columns-display-here-title ()
  13586. "Overlay the newline before the current line with the table title."
  13587. (interactive)
  13588. (let ((fmt org-columns-current-fmt-compiled)
  13589. string (title "")
  13590. property width f column str)
  13591. (while (setq column (pop fmt))
  13592. (setq property (car column)
  13593. str (or (nth 1 column) property)
  13594. width (or (cdr (assoc property org-columns-current-maxwidths))
  13595. (nth 2 column))
  13596. f (format "%%-%d.%ds | " width width)
  13597. string (format f str)
  13598. title (concat title string)))
  13599. (setq title (concat
  13600. (org-add-props " " nil 'display '(space :align-to 0))
  13601. (org-add-props title nil 'face '(:weight bold :underline t))))
  13602. (org-set-local 'org-previous-header-line-format header-line-format)
  13603. (setq header-line-format title)))
  13604. (defun org-columns-remove-overlays ()
  13605. "Remove all currently active column overlays."
  13606. (interactive)
  13607. (when (marker-buffer org-columns-begin-marker)
  13608. (with-current-buffer (marker-buffer org-columns-begin-marker)
  13609. (when (local-variable-p 'org-previous-header-line-format)
  13610. (setq header-line-format org-previous-header-line-format)
  13611. (kill-local-variable 'org-previous-header-line-format))
  13612. (move-marker org-columns-begin-marker nil)
  13613. (move-marker org-columns-top-level-marker nil)
  13614. (org-unmodified
  13615. (mapc 'org-delete-overlay org-columns-overlays)
  13616. (setq org-columns-overlays nil)
  13617. (let ((inhibit-read-only t))
  13618. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  13619. (defun org-columns-show-value ()
  13620. "Show the full value of the property."
  13621. (interactive)
  13622. (let ((value (get-char-property (point) 'org-columns-value)))
  13623. (message "Value is: %s" (or value ""))))
  13624. (defun org-columns-quit ()
  13625. "Remove the column overlays and in this way exit column editing."
  13626. (interactive)
  13627. (org-unmodified
  13628. (org-columns-remove-overlays)
  13629. (let ((inhibit-read-only t))
  13630. ;; FIXME: is this safe???
  13631. ;; or are there other reasons why there may be a read-only property????
  13632. (remove-text-properties (point-min) (point-max) '(read-only t))))
  13633. (when (eq major-mode 'org-agenda-mode)
  13634. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  13635. (defun org-columns-check-computed ()
  13636. "Check if this column value is computed.
  13637. If yes, throw an error indicating that changing it does not make sense."
  13638. (let ((val (get-char-property (point) 'org-columns-value)))
  13639. (when (and (stringp val)
  13640. (get-char-property 0 'org-computed val))
  13641. (error "This value is computed from the entry's children"))))
  13642. (defun org-columns-edit-value ()
  13643. "Edit the value of the property at point in column view.
  13644. Where possible, use the standard interface for changing this line."
  13645. (interactive)
  13646. (org-columns-check-computed)
  13647. (let* ((col (current-column))
  13648. (key (get-char-property (point) 'org-columns-key))
  13649. (value (get-char-property (point) 'org-columns-value))
  13650. (bol (point-at-bol)) (eol (point-at-eol))
  13651. (pom (or (get-text-property bol 'org-hd-marker)
  13652. (point))) ; keep despite of compiler waring
  13653. (line-overlays
  13654. (delq nil (mapcar (lambda (x)
  13655. (and (eq (overlay-buffer x) (current-buffer))
  13656. (>= (overlay-start x) bol)
  13657. (<= (overlay-start x) eol)
  13658. x))
  13659. org-columns-overlays)))
  13660. nval eval allowed)
  13661. (when (equal key "ITEM")
  13662. (error "Cannot edit item headline from here"))
  13663. (cond
  13664. ((equal key "TODO")
  13665. (setq eval '(org-with-point-at pom
  13666. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  13667. ((equal key "PRIORITY")
  13668. (setq eval '(org-with-point-at pom
  13669. (call-interactively 'org-priority))))
  13670. ((equal key "TAGS")
  13671. (setq eval '(org-with-point-at pom
  13672. (let ((org-fast-tag-selection-single-key
  13673. (if (eq org-fast-tag-selection-single-key 'expert)
  13674. t org-fast-tag-selection-single-key)))
  13675. (call-interactively 'org-set-tags)))))
  13676. ((equal key "DEADLINE")
  13677. (setq eval '(org-with-point-at pom
  13678. (call-interactively 'org-deadline))))
  13679. ((equal key "SCHEDULED")
  13680. (setq eval '(org-with-point-at pom
  13681. (call-interactively 'org-schedule))))
  13682. (t
  13683. (setq allowed (org-property-get-allowed-values pom key 'table))
  13684. (if allowed
  13685. (setq nval (completing-read "Value: " allowed nil t))
  13686. (setq nval (read-string "Edit: " value)))
  13687. (setq nval (org-trim nval))
  13688. (when (not (equal nval value))
  13689. (setq eval '(org-entry-put pom key nval)))))
  13690. (when eval
  13691. (let ((inhibit-read-only t))
  13692. (remove-text-properties (1- bol) eol '(read-only t))
  13693. (unwind-protect
  13694. (progn
  13695. (setq org-columns-overlays
  13696. (org-delete-all line-overlays org-columns-overlays))
  13697. (mapc 'org-delete-overlay line-overlays)
  13698. (org-columns-eval eval))
  13699. (org-columns-display-here))))
  13700. (move-to-column col)
  13701. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13702. (org-columns-update key))))
  13703. (defun org-columns-edit-allowed ()
  13704. "Edit the list of allowed values for the current property."
  13705. (interactive)
  13706. (let* ((col (current-column))
  13707. (key (get-char-property (point) 'org-columns-key))
  13708. (key1 (concat key "_ALL"))
  13709. (value (get-char-property (point) 'org-columns-value))
  13710. (allowed (org-entry-get (point) key1 t))
  13711. nval)
  13712. (setq nval (read-string "Allowed: " allowed))
  13713. (org-entry-put
  13714. (cond ((marker-position org-entry-property-inherited-from)
  13715. org-entry-property-inherited-from)
  13716. ((marker-position org-columns-top-level-marker)
  13717. org-columns-top-level-marker))
  13718. key1 nval)))
  13719. (defun org-columns-eval (form)
  13720. (let (hidep)
  13721. (save-excursion
  13722. (beginning-of-line 1)
  13723. (next-line 1)
  13724. (setq hidep (org-on-heading-p 1)))
  13725. (eval form)
  13726. (and hidep (hide-entry))))
  13727. (defun org-columns-previous-allowed-value ()
  13728. "Switch to the previous allowed value for this column."
  13729. (interactive)
  13730. (org-columns-next-allowed-value t))
  13731. (defun org-columns-next-allowed-value (&optional previous)
  13732. "Switch to the next allowed value for this column."
  13733. (interactive)
  13734. (org-columns-check-computed)
  13735. (let* ((col (current-column))
  13736. (key (get-char-property (point) 'org-columns-key))
  13737. (value (get-char-property (point) 'org-columns-value))
  13738. (bol (point-at-bol)) (eol (point-at-eol))
  13739. (pom (or (get-text-property bol 'org-hd-marker)
  13740. (point))) ; keep despite of compiler waring
  13741. (line-overlays
  13742. (delq nil (mapcar (lambda (x)
  13743. (and (eq (overlay-buffer x) (current-buffer))
  13744. (>= (overlay-start x) bol)
  13745. (<= (overlay-start x) eol)
  13746. x))
  13747. org-columns-overlays)))
  13748. (allowed (or (org-property-get-allowed-values pom key)
  13749. (and (equal
  13750. (nth 4 (assoc key org-columns-current-fmt-compiled))
  13751. 'checkbox) '("[ ]" "[X]"))))
  13752. nval)
  13753. (when (equal key "ITEM")
  13754. (error "Cannot edit item headline from here"))
  13755. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  13756. (error "Allowed values for this property have not been defined"))
  13757. (if (member key '("SCHEDULED" "DEADLINE"))
  13758. (setq nval (if previous 'earlier 'later))
  13759. (if previous (setq allowed (reverse allowed)))
  13760. (if (member value allowed)
  13761. (setq nval (car (cdr (member value allowed)))))
  13762. (setq nval (or nval (car allowed)))
  13763. (if (equal nval value)
  13764. (error "Only one allowed value for this property")))
  13765. (let ((inhibit-read-only t))
  13766. (remove-text-properties (1- bol) eol '(read-only t))
  13767. (unwind-protect
  13768. (progn
  13769. (setq org-columns-overlays
  13770. (org-delete-all line-overlays org-columns-overlays))
  13771. (mapc 'org-delete-overlay line-overlays)
  13772. (org-columns-eval '(org-entry-put pom key nval)))
  13773. (org-columns-display-here)))
  13774. (move-to-column col)
  13775. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13776. (org-columns-update key))))
  13777. (defun org-verify-version (task)
  13778. (cond
  13779. ((eq task 'columns)
  13780. (if (or (featurep 'xemacs)
  13781. (< emacs-major-version 22))
  13782. (error "Emacs 22 is required for the columns feature")))))
  13783. (defun org-columns-get-format-and-top-level ()
  13784. (let (fmt)
  13785. (when (condition-case nil (org-back-to-heading) (error nil))
  13786. (move-marker org-entry-property-inherited-from nil)
  13787. (setq fmt (org-entry-get nil "COLUMNS" t)))
  13788. (setq fmt (or fmt org-columns-default-format))
  13789. (org-set-local 'org-columns-current-fmt fmt)
  13790. (org-columns-compile-format fmt)
  13791. (if (marker-position org-entry-property-inherited-from)
  13792. (move-marker org-columns-top-level-marker
  13793. org-entry-property-inherited-from)
  13794. (move-marker org-columns-top-level-marker (point)))
  13795. fmt))
  13796. (defun org-columns ()
  13797. "Turn on column view on an org-mode file."
  13798. (interactive)
  13799. (org-verify-version 'columns)
  13800. (org-columns-remove-overlays)
  13801. (move-marker org-columns-begin-marker (point))
  13802. (let (beg end fmt cache maxwidths)
  13803. (setq fmt (org-columns-get-format-and-top-level))
  13804. (save-excursion
  13805. (goto-char org-columns-top-level-marker)
  13806. (setq beg (point))
  13807. (unless org-columns-inhibit-recalculation
  13808. (org-columns-compute-all))
  13809. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  13810. (point-max)))
  13811. (goto-char beg)
  13812. ;; Get and cache the properties
  13813. (while (re-search-forward (concat "^" outline-regexp) end t)
  13814. (push (cons (org-current-line) (org-entry-properties)) cache))
  13815. (when cache
  13816. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13817. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13818. (org-columns-display-here-title)
  13819. (mapc (lambda (x)
  13820. (goto-line (car x))
  13821. (org-columns-display-here (cdr x)))
  13822. cache)))))
  13823. (defun org-columns-new (&optional prop title width op fmt)
  13824. "Insert a new column, to the leeft o the current column."
  13825. (interactive)
  13826. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  13827. cell)
  13828. (setq prop (completing-read
  13829. "Property: " (mapcar 'list (org-buffer-property-keys t))
  13830. nil nil prop))
  13831. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  13832. (setq width (read-string "Column width: " (if width (number-to-string width))))
  13833. (if (string-match "\\S-" width)
  13834. (setq width (string-to-number width))
  13835. (setq width nil))
  13836. (setq fmt (completing-read "Summary [none]: "
  13837. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  13838. nil t))
  13839. (if (string-match "\\S-" fmt)
  13840. (setq fmt (intern fmt))
  13841. (setq fmt nil))
  13842. (if (eq fmt 'none) (setq fmt nil))
  13843. (if editp
  13844. (progn
  13845. (setcar editp prop)
  13846. (setcdr editp (list title width nil fmt)))
  13847. (setq cell (nthcdr (1- (current-column))
  13848. org-columns-current-fmt-compiled))
  13849. (setcdr cell (cons (list prop title width nil fmt)
  13850. (cdr cell))))
  13851. (org-columns-store-format)
  13852. (org-columns-redo)))
  13853. (defun org-columns-delete ()
  13854. "Delete the column at point from columns view."
  13855. (interactive)
  13856. (let* ((n (current-column))
  13857. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  13858. (when (y-or-n-p
  13859. (format "Are you sure you want to remove column \"%s\"? " title))
  13860. (setq org-columns-current-fmt-compiled
  13861. (delq (nth n org-columns-current-fmt-compiled)
  13862. org-columns-current-fmt-compiled))
  13863. (org-columns-store-format)
  13864. (org-columns-redo)
  13865. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  13866. (backward-char 1)))))
  13867. (defun org-columns-edit-attributes ()
  13868. "Edit the attributes of the current column."
  13869. (interactive)
  13870. (let* ((n (current-column))
  13871. (info (nth n org-columns-current-fmt-compiled)))
  13872. (apply 'org-columns-new info)))
  13873. (defun org-columns-widen (arg)
  13874. "Make the column wider by ARG characters."
  13875. (interactive "p")
  13876. (let* ((n (current-column))
  13877. (entry (nth n org-columns-current-fmt-compiled))
  13878. (width (or (nth 2 entry)
  13879. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  13880. (setq width (max 1 (+ width arg)))
  13881. (setcar (nthcdr 2 entry) width)
  13882. (org-columns-store-format)
  13883. (org-columns-redo)))
  13884. (defun org-columns-narrow (arg)
  13885. "Make the column nrrower by ARG characters."
  13886. (interactive "p")
  13887. (org-columns-widen (- arg)))
  13888. (defun org-columns-move-right ()
  13889. "Swap this column with the one to the right."
  13890. (interactive)
  13891. (let* ((n (current-column))
  13892. (cell (nthcdr n org-columns-current-fmt-compiled))
  13893. e)
  13894. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  13895. (error "Cannot shift this column further to the right"))
  13896. (setq e (car cell))
  13897. (setcar cell (car (cdr cell)))
  13898. (setcdr cell (cons e (cdr (cdr cell))))
  13899. (org-columns-store-format)
  13900. (org-columns-redo)
  13901. (forward-char 1)))
  13902. (defun org-columns-move-left ()
  13903. "Swap this column with the one to the left."
  13904. (interactive)
  13905. (let* ((n (current-column)))
  13906. (when (= n 0)
  13907. (error "Cannot shift this column further to the left"))
  13908. (backward-char 1)
  13909. (org-columns-move-right)
  13910. (backward-char 1)))
  13911. (defun org-columns-store-format ()
  13912. "Store the text version of the current columns format in appropriate place.
  13913. This is either in the COLUMNS property of the node starting the current column
  13914. display, or in the #+COLUMNS line of the current buffer."
  13915. (let (fmt)
  13916. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  13917. (if (marker-position org-columns-top-level-marker)
  13918. (save-excursion
  13919. (goto-char org-columns-top-level-marker)
  13920. (if (org-entry-get nil "COLUMNS")
  13921. (org-entry-put nil "COLUMNS" fmt)
  13922. (goto-char (point-min))
  13923. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  13924. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  13925. (setq org-columns-current-fmt fmt))))
  13926. (defvar org-overriding-columns-format nil
  13927. "When set, overrides any other definition.")
  13928. (defvar org-agenda-view-columns-initially nil
  13929. "When set, switch to columns view immediately after creating the agenda.")
  13930. (defun org-agenda-columns ()
  13931. "Turn on column view in the agenda."
  13932. (interactive)
  13933. (org-verify-version 'columns)
  13934. (org-columns-remove-overlays)
  13935. (move-marker org-columns-begin-marker (point))
  13936. (let (fmt cache maxwidths m)
  13937. (cond
  13938. ((and (local-variable-p 'org-overriding-columns-format)
  13939. org-overriding-columns-format)
  13940. (setq fmt org-overriding-columns-format))
  13941. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  13942. (setq fmt (org-entry-get m "COLUMNS" t)))
  13943. ((and (boundp 'org-columns-current-fmt)
  13944. (local-variable-p 'org-columns-current-fmt)
  13945. org-columns-current-fmt)
  13946. (setq fmt org-columns-current-fmt))
  13947. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  13948. (setq m (get-text-property m 'org-hd-marker))
  13949. (setq fmt (org-entry-get m "COLUMNS" t))))
  13950. (setq fmt (or fmt org-columns-default-format))
  13951. (org-set-local 'org-columns-current-fmt fmt)
  13952. (org-columns-compile-format fmt)
  13953. (save-excursion
  13954. ;; Get and cache the properties
  13955. (goto-char (point-min))
  13956. (while (not (eobp))
  13957. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  13958. (get-text-property (point) 'org-marker)))
  13959. (push (cons (org-current-line) (org-entry-properties m)) cache))
  13960. (beginning-of-line 2))
  13961. (when cache
  13962. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13963. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13964. (org-columns-display-here-title)
  13965. (mapc (lambda (x)
  13966. (goto-line (car x))
  13967. (org-columns-display-here (cdr x)))
  13968. cache)))))
  13969. (defun org-columns-get-autowidth-alist (s cache)
  13970. "Derive the maximum column widths from the format and the cache."
  13971. (let ((start 0) rtn)
  13972. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  13973. (push (cons (match-string 1 s) 1) rtn)
  13974. (setq start (match-end 0)))
  13975. (mapc (lambda (x)
  13976. (setcdr x (apply 'max
  13977. (mapcar
  13978. (lambda (y)
  13979. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  13980. cache))))
  13981. rtn)
  13982. rtn))
  13983. (defun org-columns-compute-all ()
  13984. "Compute all columns that have operators defined."
  13985. (org-unmodified
  13986. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  13987. (let ((columns org-columns-current-fmt-compiled) col)
  13988. (while (setq col (pop columns))
  13989. (when (nth 3 col)
  13990. (save-excursion
  13991. (org-columns-compute (car col)))))))
  13992. (defun org-columns-update (property)
  13993. "Recompute PROPERTY, and update the columns display for it."
  13994. (org-columns-compute property)
  13995. (let (fmt val pos)
  13996. (save-excursion
  13997. (mapc (lambda (ov)
  13998. (when (equal (org-overlay-get ov 'org-columns-key) property)
  13999. (setq pos (org-overlay-start ov))
  14000. (goto-char pos)
  14001. (when (setq val (cdr (assoc property
  14002. (get-text-property
  14003. (point-at-bol) 'org-summaries))))
  14004. (setq fmt (org-overlay-get ov 'org-columns-format))
  14005. (org-overlay-put ov 'org-columns-value val)
  14006. (org-overlay-put ov 'display (format fmt val)))))
  14007. org-columns-overlays))))
  14008. (defun org-columns-compute (property)
  14009. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  14010. (interactive)
  14011. (let* ((re (concat "^" outline-regexp))
  14012. (lmax 30) ; Does anyone use deeper levels???
  14013. (lsum (make-vector lmax 0))
  14014. (lflag (make-vector lmax nil))
  14015. (level 0)
  14016. (ass (assoc property org-columns-current-fmt-compiled))
  14017. (format (nth 4 ass))
  14018. (beg org-columns-top-level-marker)
  14019. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  14020. (save-excursion
  14021. ;; Find the region to compute
  14022. (goto-char beg)
  14023. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  14024. (goto-char end)
  14025. ;; Walk the tree from the back and do the computations
  14026. (while (re-search-backward re beg t)
  14027. (setq sumpos (match-beginning 0)
  14028. last-level level
  14029. level (org-outline-level)
  14030. val (org-entry-get nil property)
  14031. valflag (and val (string-match "\\S-" val)))
  14032. (cond
  14033. ((< level last-level)
  14034. ;; put the sum of lower levels here as a property
  14035. (setq sum (aref lsum last-level) ; current sum
  14036. flag (aref lflag last-level) ; any valid entries from children?
  14037. str (org-column-number-to-string sum format)
  14038. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  14039. useval (if flag str1 (if valflag val ""))
  14040. sum-alist (get-text-property sumpos 'org-summaries))
  14041. (if (assoc property sum-alist)
  14042. (setcdr (assoc property sum-alist) useval)
  14043. (push (cons property useval) sum-alist)
  14044. (org-unmodified
  14045. (add-text-properties sumpos (1+ sumpos)
  14046. (list 'org-summaries sum-alist))))
  14047. (when val
  14048. (org-entry-put nil property (if flag str val)))
  14049. ;; add current to current level accumulator
  14050. (aset lsum level (+ (aref lsum level)
  14051. (if flag sum (org-column-string-to-number
  14052. val format))))
  14053. (if (or flag valflag) (aset lflag level t))
  14054. ;; clear accumulators for deeper levels
  14055. (loop for l from (1+ level) to (1- lmax) do
  14056. (aset lsum l 0)
  14057. (aset lflag l nil)))
  14058. ((>= level last-level)
  14059. ;; add what we have here to the accumulator for this level
  14060. (aset lsum level (+ (aref lsum level)
  14061. (org-column-string-to-number (or val "0") format)))
  14062. (and valflag (aset lflag level t)))
  14063. (t (error "This should not happen")))))))
  14064. (defun org-columns-redo ()
  14065. "Construct the column display again."
  14066. (interactive)
  14067. (message "Recomputing columns...")
  14068. (save-excursion
  14069. (if (marker-position org-columns-begin-marker)
  14070. (goto-char org-columns-begin-marker))
  14071. (org-columns-remove-overlays)
  14072. (if (org-mode-p)
  14073. (call-interactively 'org-columns)
  14074. (call-interactively 'org-agenda-columns)))
  14075. (message "Recomputing columns...done"))
  14076. (defun org-columns-not-in-agenda ()
  14077. (if (eq major-mode 'org-agenda-mode)
  14078. (error "This command is only allowed in Org-mode buffers")))
  14079. (defun org-string-to-number (s)
  14080. "Convert string to number, and interpret hh:mm:ss."
  14081. (if (not (string-match ":" s))
  14082. (string-to-number s)
  14083. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14084. (while l
  14085. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14086. sum)))
  14087. (defun org-column-number-to-string (n fmt)
  14088. "Convert a computed column number to a string value, according to FMT."
  14089. (cond
  14090. ((eq fmt 'add_times)
  14091. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  14092. (format "%d:%02d" h m)))
  14093. ((eq fmt 'checkbox)
  14094. (cond ((= n (floor n)) "[X]")
  14095. ((> n 1.) "[-]")
  14096. (t "[ ]")))
  14097. (t (number-to-string n))))
  14098. (defun org-column-string-to-number (s fmt)
  14099. "Convert a column value to a number that can be used for column computing."
  14100. (cond
  14101. ((string-match ":" s)
  14102. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14103. (while l
  14104. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14105. sum))
  14106. ((eq fmt 'checkbox)
  14107. (if (equal s "[X]") 1. 0.000001))
  14108. (t (string-to-number s))))
  14109. (defun org-columns-uncompile-format (cfmt)
  14110. "Turn the compiled columns format back into a string representation."
  14111. (let ((rtn "") e s prop title op width fmt)
  14112. (while (setq e (pop cfmt))
  14113. (setq prop (car e)
  14114. title (nth 1 e)
  14115. width (nth 2 e)
  14116. op (nth 3 e)
  14117. fmt (nth 4 e))
  14118. (cond
  14119. ((eq fmt 'add_times) (setq op ":"))
  14120. ((eq fmt 'checkbox) (setq op "X"))
  14121. ((eq fmt 'add_numbers) (setq op "+")))
  14122. (if (equal title prop) (setq title nil))
  14123. (setq s (concat "%" (if width (number-to-string width))
  14124. prop
  14125. (if title (concat "(" title ")"))
  14126. (if op (concat "{" op "}"))))
  14127. (setq rtn (concat rtn " " s)))
  14128. (org-trim rtn)))
  14129. (defun org-columns-compile-format (fmt)
  14130. "FIXME"
  14131. (let ((start 0) width prop title op f)
  14132. (setq org-columns-current-fmt-compiled nil)
  14133. (while (string-match
  14134. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  14135. fmt start)
  14136. (setq start (match-end 0)
  14137. width (match-string 1 fmt)
  14138. prop (match-string 2 fmt)
  14139. title (or (match-string 3 fmt) prop)
  14140. op (match-string 4 fmt)
  14141. f nil)
  14142. (if width (setq width (string-to-number width)))
  14143. (cond
  14144. ((equal op "+") (setq f 'add_numbers))
  14145. ((equal op ":") (setq f 'add_times))
  14146. ((equal op "X") (setq f 'checkbox)))
  14147. (push (list prop title width op f) org-columns-current-fmt-compiled))
  14148. (setq org-columns-current-fmt-compiled
  14149. (nreverse org-columns-current-fmt-compiled))))
  14150. ;;;; Timestamps
  14151. (defvar org-last-changed-timestamp nil)
  14152. (defvar org-time-was-given) ; dynamically scoped parameter
  14153. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14154. (defvar org-ts-what) ; dynamically scoped parameter
  14155. (defun org-time-stamp (arg)
  14156. "Prompt for a date/time and insert a time stamp.
  14157. If the user specifies a time like HH:MM, or if this command is called
  14158. with a prefix argument, the time stamp will contain date and time.
  14159. Otherwise, only the date will be included. All parts of a date not
  14160. specified by the user will be filled in from the current date/time.
  14161. So if you press just return without typing anything, the time stamp
  14162. will represent the current date/time. If there is already a timestamp
  14163. at the cursor, it will be modified."
  14164. (interactive "P")
  14165. (let ((default-time
  14166. ;; Default time is either today, or, when entering a range,
  14167. ;; the range start.
  14168. (if (or (org-at-timestamp-p t)
  14169. (save-excursion
  14170. (re-search-backward
  14171. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14172. (- (point) 20) t)))
  14173. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14174. (current-time)))
  14175. org-time-was-given org-end-time-was-given time)
  14176. (cond
  14177. ((and (org-at-timestamp-p)
  14178. (eq last-command 'org-time-stamp)
  14179. (eq this-command 'org-time-stamp))
  14180. (insert "--")
  14181. (setq time (let ((this-command this-command))
  14182. (org-read-date arg 'totime nil nil default-time)))
  14183. (org-insert-time-stamp time (or org-time-was-given arg)))
  14184. ((org-at-timestamp-p)
  14185. (setq time (let ((this-command this-command))
  14186. (org-read-date arg 'totime nil nil default-time)))
  14187. (when (org-at-timestamp-p) ; just to get the match data
  14188. (replace-match "")
  14189. (setq org-last-changed-timestamp
  14190. (org-insert-time-stamp
  14191. time (or org-time-was-given arg)
  14192. nil nil nil (list org-end-time-was-given))))
  14193. (message "Timestamp updated"))
  14194. (t
  14195. (setq time (let ((this-command this-command))
  14196. (org-read-date arg 'totime nil nil default-time)))
  14197. (org-insert-time-stamp time (or org-time-was-given arg)
  14198. nil nil nil (list org-end-time-was-given))))))
  14199. (defun org-time-stamp-inactive (&optional arg)
  14200. "Insert an inactive time stamp.
  14201. An inactive time stamp is enclosed in square brackets instead of angle
  14202. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14203. does not link to the calendar and cannot be changed with the S-cursor keys.
  14204. So these are more for recording a certain time/date."
  14205. (interactive "P")
  14206. (let (org-time-was-given org-end-time-was-given time)
  14207. (setq time (org-read-date arg 'totime))
  14208. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  14209. nil nil (list org-end-time-was-given))))
  14210. (defvar org-date-ovl (org-make-overlay 1 1))
  14211. (org-overlay-put org-date-ovl 'face 'org-warning)
  14212. (org-detach-overlay org-date-ovl)
  14213. (defvar org-ans1) ; dynamically scoped parameter
  14214. (defvar org-ans2) ; dynamically scoped parameter
  14215. (defvar org-plain-time-of-day-regexp) ; defined below
  14216. (defun org-read-date (&optional with-time to-time from-string prompt
  14217. default-time)
  14218. "Read a date and make things smooth for the user.
  14219. The prompt will suggest to enter an ISO date, but you can also enter anything
  14220. which will at least partially be understood by `parse-time-string'.
  14221. Unrecognized parts of the date will default to the current day, month, year,
  14222. hour and minute. If this command is called to replace a timestamp at point,
  14223. of to enter the second timestamp of a range, the default time is taken from the
  14224. existing stamp. For example,
  14225. 3-2-5 --> 2003-02-05
  14226. feb 15 --> currentyear-02-15
  14227. sep 12 9 --> 2009-09-12
  14228. 12:45 --> today 12:45
  14229. 22 sept 0:34 --> currentyear-09-22 0:34
  14230. 12 --> currentyear-currentmonth-12
  14231. Fri --> nearest Friday (today or later)
  14232. +4 --> four days from today (only if +N is the only thing given)
  14233. etc.
  14234. The function understands only English month and weekday abbreviations,
  14235. but this can be configured with the variables `parse-time-months' and
  14236. `parse-time-weekdays'.
  14237. While prompting, a calendar is popped up - you can also select the
  14238. date with the mouse (button 1). The calendar shows a period of three
  14239. months. To scroll it to other months, use the keys `>' and `<'.
  14240. If you don't like the calendar, turn it off with
  14241. \(setq org-popup-calendar-for-date-prompt nil)
  14242. With optional argument TO-TIME, the date will immediately be converted
  14243. to an internal time.
  14244. With an optional argument WITH-TIME, the prompt will suggest to also
  14245. insert a time. Note that when WITH-TIME is not set, you can still
  14246. enter a time, and this function will inform the calling routine about
  14247. this change. The calling routine may then choose to change the format
  14248. used to insert the time stamp into the buffer to include the time.
  14249. With optional argument FROM-STRING, read fomr this string instead from
  14250. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14251. the time/date that is used for everything that is not specified by the
  14252. user."
  14253. (require 'parse-time)
  14254. (let* ((org-time-stamp-rounding-minutes
  14255. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  14256. (ct (org-current-time))
  14257. (def (or default-time ct))
  14258. (calendar-move-hook nil)
  14259. (view-diary-entries-initially nil)
  14260. (view-calendar-holidays-initially nil)
  14261. (timestr (format-time-string
  14262. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  14263. (prompt (concat (if prompt (concat prompt " ") "")
  14264. (format "Date and/or time (default [%s]): " timestr)))
  14265. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  14266. second minute hour day month year tl wday wday1 pm h2 m2)
  14267. (cond
  14268. (from-string (setq ans from-string))
  14269. (org-popup-calendar-for-date-prompt
  14270. (save-excursion
  14271. (save-window-excursion
  14272. (calendar)
  14273. (calendar-forward-day (- (time-to-days def)
  14274. (calendar-absolute-from-gregorian
  14275. (calendar-current-date))))
  14276. (org-eval-in-calendar nil t)
  14277. (let* ((old-map (current-local-map))
  14278. (map (copy-keymap calendar-mode-map))
  14279. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14280. (org-defkey map (kbd "RET") 'org-calendar-select)
  14281. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  14282. 'org-calendar-select-mouse)
  14283. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  14284. 'org-calendar-select-mouse)
  14285. (org-defkey minibuffer-local-map [(meta shift left)]
  14286. (lambda () (interactive)
  14287. (org-eval-in-calendar '(calendar-backward-month 1))))
  14288. (org-defkey minibuffer-local-map [(meta shift right)]
  14289. (lambda () (interactive)
  14290. (org-eval-in-calendar '(calendar-forward-month 1))))
  14291. (org-defkey minibuffer-local-map [(shift up)]
  14292. (lambda () (interactive)
  14293. (org-eval-in-calendar '(calendar-backward-week 1))))
  14294. (org-defkey minibuffer-local-map [(shift down)]
  14295. (lambda () (interactive)
  14296. (org-eval-in-calendar '(calendar-forward-week 1))))
  14297. (org-defkey minibuffer-local-map [(shift left)]
  14298. (lambda () (interactive)
  14299. (org-eval-in-calendar '(calendar-backward-day 1))))
  14300. (org-defkey minibuffer-local-map [(shift right)]
  14301. (lambda () (interactive)
  14302. (org-eval-in-calendar '(calendar-forward-day 1))))
  14303. (org-defkey minibuffer-local-map ">"
  14304. (lambda () (interactive)
  14305. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14306. (org-defkey minibuffer-local-map "<"
  14307. (lambda () (interactive)
  14308. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14309. (unwind-protect
  14310. (progn
  14311. (use-local-map map)
  14312. (setq org-ans0 (read-string prompt "" nil nil))
  14313. ;; org-ans0: from prompt
  14314. ;; org-ans1: from mouse click
  14315. ;; org-ans2: from calendar motion
  14316. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14317. (use-local-map old-map))))))
  14318. (t ; Naked prompt only
  14319. (setq ans (read-string prompt "" nil timestr))))
  14320. (org-detach-overlay org-date-ovl)
  14321. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  14322. (setq deltadays (string-to-number ans) ans ""))
  14323. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  14324. (when (string-match
  14325. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14326. (setq year (if (match-end 2)
  14327. (string-to-number (match-string 2 ans))
  14328. (string-to-number (format-time-string "%Y")))
  14329. month (string-to-number (match-string 3 ans))
  14330. day (string-to-number (match-string 4 ans)))
  14331. (if (< year 100) (setq year (+ 2000 year)))
  14332. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14333. t nil ans)))
  14334. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14335. ;; If there is a time with am/pm, and *no* time without it, we convert
  14336. ;; so that matching will be successful.
  14337. ;; FIXME: make this replace twice, so that we catch the end time.
  14338. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14339. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14340. (setq hour (string-to-number (match-string 1 ans))
  14341. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  14342. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  14343. (if (and (= hour 12) (not pm))
  14344. (setq hour 0)
  14345. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14346. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  14347. ;; Check if a time range is given as a duration
  14348. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14349. (setq hour (string-to-number (match-string 1 ans))
  14350. h2 (+ hour (string-to-number (match-string 3 ans)))
  14351. minute (string-to-number (match-string 2 ans))
  14352. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  14353. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  14354. ;; Check if there is a time range
  14355. (when (and (boundp 'org-end-time-was-given)
  14356. (string-match org-plain-time-of-day-regexp ans)
  14357. (match-end 8))
  14358. (setq org-end-time-was-given (match-string 8 ans))
  14359. (setq ans (concat (substring ans 0 (match-beginning 7))
  14360. (substring ans (match-end 7)))))
  14361. (setq tl (parse-time-string ans)
  14362. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" def)))
  14363. month (or (nth 4 tl) (string-to-number (format-time-string "%m" def)))
  14364. day (or (nth 3 tl) (string-to-number (format-time-string "%d" def)))
  14365. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" def)))
  14366. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" def)))
  14367. second (or (nth 0 tl) 0)
  14368. wday (nth 6 tl))
  14369. (setq day (+ day deltadays))
  14370. (when (and wday (not (nth 3 tl)))
  14371. ;; Weekday was given, but no day, so pick that day in the week
  14372. ;; on or after the derived date.
  14373. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14374. (unless (equal wday wday1)
  14375. (setq day (+ day (% (- wday wday1 -7) 7)))))
  14376. (if (and (boundp 'org-time-was-given)
  14377. (nth 2 tl))
  14378. (setq org-time-was-given t))
  14379. (if (< year 100) (setq year (+ 2000 year)))
  14380. (if to-time
  14381. (encode-time second minute hour day month year)
  14382. (if (or (nth 1 tl) (nth 2 tl))
  14383. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  14384. (format "%04d-%02d-%02d" year month day)))))
  14385. (defun org-eval-in-calendar (form &optional keepdate)
  14386. "Eval FORM in the calendar window and return to current window.
  14387. Also, store the cursor date in variable org-ans2."
  14388. (let ((sw (selected-window)))
  14389. (select-window (get-buffer-window "*Calendar*"))
  14390. (eval form)
  14391. (when (and (not keepdate) (calendar-cursor-to-date))
  14392. (let* ((date (calendar-cursor-to-date))
  14393. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14394. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14395. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14396. (select-window sw)
  14397. ;; Update the prompt to show new default date
  14398. (save-excursion
  14399. (goto-char (point-min))
  14400. (when (and org-ans2
  14401. (re-search-forward "\\[[-0-9]+\\]" nil t)
  14402. (get-text-property (match-end 0) 'field))
  14403. (let ((inhibit-read-only t))
  14404. (replace-match (concat "[" org-ans2 "]") t t)
  14405. (add-text-properties (point-min) (1+ (match-end 0))
  14406. (text-properties-at (1+ (point-min)))))))))
  14407. (defun org-calendar-select ()
  14408. "Return to `org-read-date' with the date currently selected.
  14409. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14410. (interactive)
  14411. (when (calendar-cursor-to-date)
  14412. (let* ((date (calendar-cursor-to-date))
  14413. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14414. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14415. (if (active-minibuffer-window) (exit-minibuffer))))
  14416. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14417. "Insert a date stamp for the date given by the internal TIME.
  14418. WITH-HM means, use the stamp format that includes the time of the day.
  14419. INACTIVE means use square brackets instead of angular ones, so that the
  14420. stamp will not contribute to the agenda.
  14421. PRE and POST are optional strings to be inserted before and after the
  14422. stamp.
  14423. The command returns the inserted time stamp."
  14424. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14425. stamp)
  14426. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14427. (insert (or pre ""))
  14428. (insert (setq stamp (format-time-string fmt time)))
  14429. (when (listp extra)
  14430. (setq extra (car extra))
  14431. (if (and (stringp extra)
  14432. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14433. (setq extra (format "-%02d:%02d"
  14434. (string-to-number (match-string 1 extra))
  14435. (string-to-number (match-string 2 extra))))
  14436. (setq extra nil)))
  14437. (when extra
  14438. (backward-char 1)
  14439. (insert extra)
  14440. (forward-char 1))
  14441. (insert (or post ""))
  14442. stamp))
  14443. (defun org-toggle-time-stamp-overlays ()
  14444. "Toggle the use of custom time stamp formats."
  14445. (interactive)
  14446. (setq org-display-custom-times (not org-display-custom-times))
  14447. (unless org-display-custom-times
  14448. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14449. (while (setq p (next-single-property-change p 'display))
  14450. (if (and (get-text-property p 'display)
  14451. (eq (get-text-property p 'face) 'org-date))
  14452. (remove-text-properties
  14453. p (setq p (next-single-property-change p 'display))
  14454. '(display t))))
  14455. (set-buffer-modified-p bmp)))
  14456. (if (featurep 'xemacs)
  14457. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14458. (org-restart-font-lock)
  14459. (setq org-table-may-need-update t)
  14460. (if org-display-custom-times
  14461. (message "Time stamps are overlayed with custom format")
  14462. (message "Time stamp overlays removed")))
  14463. (defun org-display-custom-time (beg end)
  14464. "Overlay modified time stamp format over timestamp between BED and END."
  14465. (let* ((ts (buffer-substring beg end))
  14466. t1 w1 with-hm tf time str w2 (off 0))
  14467. (save-match-data
  14468. (setq t1 (org-parse-time-string ts t))
  14469. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  14470. (setq off (- (match-end 0) (match-beginning 0)))))
  14471. (setq end (- end off))
  14472. (setq w1 (- end beg)
  14473. with-hm (and (nth 1 t1) (nth 2 t1))
  14474. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14475. time (org-fix-decoded-time t1)
  14476. str (org-add-props
  14477. (format-time-string
  14478. (substring tf 1 -1) (apply 'encode-time time))
  14479. nil 'mouse-face 'highlight)
  14480. w2 (length str))
  14481. (if (not (= w2 w1))
  14482. (add-text-properties (1+ beg) (+ 2 beg)
  14483. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14484. (if (featurep 'xemacs)
  14485. (progn
  14486. (put-text-property beg end 'invisible t)
  14487. (put-text-property beg end 'end-glyph (make-glyph str)))
  14488. (put-text-property beg end 'display str))))
  14489. (defun org-translate-time (string)
  14490. "Translate all timestamps in STRING to custom format.
  14491. But do this only if the variable `org-display-custom-times' is set."
  14492. (when org-display-custom-times
  14493. (save-match-data
  14494. (let* ((start 0)
  14495. (re org-ts-regexp-both)
  14496. t1 with-hm inactive tf time str beg end)
  14497. (while (setq start (string-match re string start))
  14498. (setq beg (match-beginning 0)
  14499. end (match-end 0)
  14500. t1 (save-match-data
  14501. (org-parse-time-string (substring string beg end) t))
  14502. with-hm (and (nth 1 t1) (nth 2 t1))
  14503. inactive (equal (substring string beg (1+ beg)) "[")
  14504. tf (funcall (if with-hm 'cdr 'car)
  14505. org-time-stamp-custom-formats)
  14506. time (org-fix-decoded-time t1)
  14507. str (format-time-string
  14508. (concat
  14509. (if inactive "[" "<") (substring tf 1 -1)
  14510. (if inactive "]" ">"))
  14511. (apply 'encode-time time))
  14512. string (replace-match str t t string)
  14513. start (+ start (length str)))))))
  14514. string)
  14515. (defun org-fix-decoded-time (time)
  14516. "Set 0 instead of nil for the first 6 elements of time.
  14517. Don't touch the rest."
  14518. (let ((n 0))
  14519. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14520. (defun org-days-to-time (timestamp-string)
  14521. "Difference between TIMESTAMP-STRING and now in days."
  14522. (- (time-to-days (org-time-string-to-time timestamp-string))
  14523. (time-to-days (current-time))))
  14524. (defun org-deadline-close (timestamp-string &optional ndays)
  14525. "Is the time in TIMESTAMP-STRING close to the current date?"
  14526. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14527. (and (< (org-days-to-time timestamp-string) ndays)
  14528. (not (org-entry-is-done-p))))
  14529. (defun org-get-wdays (ts)
  14530. "Get the deadline lead time appropriate for timestring TS."
  14531. (cond
  14532. ((<= org-deadline-warning-days 0)
  14533. ;; 0 or negative, enforce this value no matter what
  14534. (- org-deadline-warning-days))
  14535. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  14536. ;; lead time is specified.
  14537. (floor (* (string-to-number (match-string 1 ts))
  14538. (cdr (assoc (match-string 2 ts)
  14539. '(("d" . 1) ("w" . 7)
  14540. ("m" . 30.4) ("y" . 365.25)))))))
  14541. ;; go for the default.
  14542. (t org-deadline-warning-days)))
  14543. (defun org-calendar-select-mouse (ev)
  14544. "Return to `org-read-date' with the date currently selected.
  14545. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14546. (interactive "e")
  14547. (mouse-set-point ev)
  14548. (when (calendar-cursor-to-date)
  14549. (let* ((date (calendar-cursor-to-date))
  14550. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14551. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14552. (if (active-minibuffer-window) (exit-minibuffer))))
  14553. (defun org-check-deadlines (ndays)
  14554. "Check if there are any deadlines due or past due.
  14555. A deadline is considered due if it happens within `org-deadline-warning-days'
  14556. days from today's date. If the deadline appears in an entry marked DONE,
  14557. it is not shown. The prefix arg NDAYS can be used to test that many
  14558. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14559. (interactive "P")
  14560. (let* ((org-warn-days
  14561. (cond
  14562. ((equal ndays '(4)) 100000)
  14563. (ndays (prefix-numeric-value ndays))
  14564. (t (abs org-deadline-warning-days))))
  14565. (case-fold-search nil)
  14566. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14567. (callback
  14568. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14569. (message "%d deadlines past-due or due within %d days"
  14570. (org-occur regexp nil callback)
  14571. org-warn-days)))
  14572. (defun org-evaluate-time-range (&optional to-buffer)
  14573. "Evaluate a time range by computing the difference between start and end.
  14574. Normally the result is just printed in the echo area, but with prefix arg
  14575. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14576. If the time range is actually in a table, the result is inserted into the
  14577. next column.
  14578. For time difference computation, a year is assumed to be exactly 365
  14579. days in order to avoid rounding problems."
  14580. (interactive "P")
  14581. (or
  14582. (org-clock-update-time-maybe)
  14583. (save-excursion
  14584. (unless (org-at-date-range-p t)
  14585. (goto-char (point-at-bol))
  14586. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14587. (if (not (org-at-date-range-p t))
  14588. (error "Not at a time-stamp range, and none found in current line")))
  14589. (let* ((ts1 (match-string 1))
  14590. (ts2 (match-string 2))
  14591. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14592. (match-end (match-end 0))
  14593. (time1 (org-time-string-to-time ts1))
  14594. (time2 (org-time-string-to-time ts2))
  14595. (t1 (time-to-seconds time1))
  14596. (t2 (time-to-seconds time2))
  14597. (diff (abs (- t2 t1)))
  14598. (negative (< (- t2 t1) 0))
  14599. ;; (ys (floor (* 365 24 60 60)))
  14600. (ds (* 24 60 60))
  14601. (hs (* 60 60))
  14602. (fy "%dy %dd %02d:%02d")
  14603. (fy1 "%dy %dd")
  14604. (fd "%dd %02d:%02d")
  14605. (fd1 "%dd")
  14606. (fh "%02d:%02d")
  14607. y d h m align)
  14608. (if havetime
  14609. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14610. y 0
  14611. d (floor (/ diff ds)) diff (mod diff ds)
  14612. h (floor (/ diff hs)) diff (mod diff hs)
  14613. m (floor (/ diff 60)))
  14614. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14615. y 0
  14616. d (floor (+ (/ diff ds) 0.5))
  14617. h 0 m 0))
  14618. (if (not to-buffer)
  14619. (message (org-make-tdiff-string y d h m))
  14620. (when (org-at-table-p)
  14621. (goto-char match-end)
  14622. (setq align t)
  14623. (and (looking-at " *|") (goto-char (match-end 0))))
  14624. (if (looking-at
  14625. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14626. (replace-match ""))
  14627. (if negative (insert " -"))
  14628. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14629. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14630. (insert " " (format fh h m))))
  14631. (if align (org-table-align))
  14632. (message "Time difference inserted")))))
  14633. (defun org-make-tdiff-string (y d h m)
  14634. (let ((fmt "")
  14635. (l nil))
  14636. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14637. l (push y l)))
  14638. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14639. l (push d l)))
  14640. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14641. l (push h l)))
  14642. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14643. l (push m l)))
  14644. (apply 'format fmt (nreverse l))))
  14645. (defun org-time-string-to-time (s)
  14646. (apply 'encode-time (org-parse-time-string s)))
  14647. (defun org-time-string-to-absolute (s &optional daynr)
  14648. "Convert a time stamp to an absolute day number.
  14649. If there is a specifyer for a cyclic time stamp, get the closest date to
  14650. DAYNR."
  14651. (cond
  14652. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14653. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14654. daynr
  14655. (+ daynr 1000)))
  14656. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14657. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14658. (time-to-days (current-time))) (match-string 0 s)))
  14659. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14660. (defun org-time-from-absolute (d)
  14661. "Return the time corresponding to date D.
  14662. D may be an absolute day number, or a calendar-type list (month day year)."
  14663. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14664. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14665. (defun org-calendar-holiday ()
  14666. "List of holidays, for Diary display in Org-mode."
  14667. (let ((hl (check-calendar-holidays date)))
  14668. (if hl (mapconcat 'identity hl "; "))))
  14669. (defun org-diary-sexp-entry (sexp entry date)
  14670. "Process a SEXP diary ENTRY for DATE."
  14671. (require 'diary-lib)
  14672. (let ((result (if calendar-debug-sexp
  14673. (let ((stack-trace-on-error t))
  14674. (eval (car (read-from-string sexp))))
  14675. (condition-case nil
  14676. (eval (car (read-from-string sexp)))
  14677. (error
  14678. (beep)
  14679. (message "Bad sexp at line %d in %s: %s"
  14680. (org-current-line)
  14681. (buffer-file-name) sexp)
  14682. (sleep-for 2))))))
  14683. (cond ((stringp result) result)
  14684. ((and (consp result)
  14685. (stringp (cdr result))) (cdr result))
  14686. (result entry)
  14687. (t nil))))
  14688. (defun org-diary-to-ical-string (frombuf)
  14689. "FIXME"
  14690. (let* ((tmpdir (if (featurep 'xemacs)
  14691. (temp-directory)
  14692. temporary-file-directory))
  14693. (tmpfile (make-temp-name
  14694. (expand-file-name "orgics" tmpdir)))
  14695. buf rtn b e)
  14696. (save-excursion
  14697. (set-buffer frombuf)
  14698. (icalendar-export-region (point-min) (point-max) tmpfile)
  14699. (setq buf (find-buffer-visiting tmpfile))
  14700. (set-buffer buf)
  14701. (goto-char (point-min))
  14702. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14703. (setq b (match-beginning 0)))
  14704. (goto-char (point-max))
  14705. (if (re-search-backward "^END:VEVENT" nil t)
  14706. (setq e (match-end 0)))
  14707. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14708. (kill-buffer buf)
  14709. (kill-buffer frombuf)
  14710. (delete-file tmpfile)
  14711. rtn))
  14712. (defun org-closest-date (start current change)
  14713. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  14714. ;; Make the proper lists from the dates
  14715. (catch 'exit
  14716. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14717. dn dw sday cday n1 n2
  14718. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14719. (setq start (org-date-to-gregorian start)
  14720. current (org-date-to-gregorian
  14721. (if org-agenda-repeating-timestamp-show-all
  14722. current
  14723. (time-to-days (current-time))))
  14724. sday (calendar-absolute-from-gregorian start)
  14725. cday (calendar-absolute-from-gregorian current))
  14726. (if (<= cday sday) (throw 'exit sday))
  14727. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14728. (setq dn (string-to-number (match-string 1 change))
  14729. dw (cdr (assoc (match-string 2 change) a1)))
  14730. (error "Invalid change specifyer: %s" change))
  14731. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14732. (cond
  14733. ((eq dw 'day)
  14734. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14735. n2 (+ n1 dn)))
  14736. ((eq dw 'year)
  14737. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14738. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14739. (setq date1 (list m d y1)
  14740. n1 (calendar-absolute-from-gregorian date1)
  14741. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14742. n2 (calendar-absolute-from-gregorian date2)))
  14743. ((eq dw 'month)
  14744. ;; approx number of month between the tow dates
  14745. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14746. ;; How often does dn fit in there?
  14747. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14748. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14749. m (+ m nm)
  14750. ny (floor (/ m 12))
  14751. y (+ y ny)
  14752. m (- m (* ny 12)))
  14753. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14754. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14755. (setq m2 (+ m dn) y2 y)
  14756. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14757. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14758. (while (< n2 cday)
  14759. (setq n1 n2 m m2 y y2)
  14760. (setq m2 (+ m dn) y2 y)
  14761. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14762. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14763. (if org-agenda-repeating-timestamp-show-all
  14764. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  14765. (if (= cday n1) n1 n2)))))
  14766. (defun org-date-to-gregorian (date)
  14767. "Turn any specification of DATE into a gregorian date for the calendar."
  14768. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14769. ((and (listp date) (= (length date) 3)) date)
  14770. ((stringp date)
  14771. (setq date (org-parse-time-string date))
  14772. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14773. ((listp date)
  14774. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14775. (defun org-parse-time-string (s &optional nodefault)
  14776. "Parse the standard Org-mode time string.
  14777. This should be a lot faster than the normal `parse-time-string'.
  14778. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14779. hour and minute fields will be nil if not given."
  14780. (if (string-match org-ts-regexp0 s)
  14781. (list 0
  14782. (if (or (match-beginning 8) (not nodefault))
  14783. (string-to-number (or (match-string 8 s) "0")))
  14784. (if (or (match-beginning 7) (not nodefault))
  14785. (string-to-number (or (match-string 7 s) "0")))
  14786. (string-to-number (match-string 4 s))
  14787. (string-to-number (match-string 3 s))
  14788. (string-to-number (match-string 2 s))
  14789. nil nil nil)
  14790. (make-list 9 0)))
  14791. (defun org-timestamp-up (&optional arg)
  14792. "Increase the date item at the cursor by one.
  14793. If the cursor is on the year, change the year. If it is on the month or
  14794. the day, change that.
  14795. With prefix ARG, change by that many units."
  14796. (interactive "p")
  14797. (org-timestamp-change (prefix-numeric-value arg)))
  14798. (defun org-timestamp-down (&optional arg)
  14799. "Decrease the date item at the cursor by one.
  14800. If the cursor is on the year, change the year. If it is on the month or
  14801. the day, change that.
  14802. With prefix ARG, change by that many units."
  14803. (interactive "p")
  14804. (org-timestamp-change (- (prefix-numeric-value arg))))
  14805. (defun org-timestamp-up-day (&optional arg)
  14806. "Increase the date in the time stamp by one day.
  14807. With prefix ARG, change that many days."
  14808. (interactive "p")
  14809. (if (and (not (org-at-timestamp-p t))
  14810. (org-on-heading-p))
  14811. (org-todo 'up)
  14812. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  14813. (defun org-timestamp-down-day (&optional arg)
  14814. "Decrease the date in the time stamp by one day.
  14815. With prefix ARG, change that many days."
  14816. (interactive "p")
  14817. (if (and (not (org-at-timestamp-p t))
  14818. (org-on-heading-p))
  14819. (org-todo 'down)
  14820. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  14821. (defsubst org-pos-in-match-range (pos n)
  14822. (and (match-beginning n)
  14823. (<= (match-beginning n) pos)
  14824. (>= (match-end n) pos)))
  14825. (defun org-at-timestamp-p (&optional inactive-ok)
  14826. "Determine if the cursor is in or at a timestamp."
  14827. (interactive)
  14828. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14829. (pos (point))
  14830. (ans (or (looking-at tsr)
  14831. (save-excursion
  14832. (skip-chars-backward "^[<\n\r\t")
  14833. (if (> (point) 1) (backward-char 1))
  14834. (and (looking-at tsr)
  14835. (> (- (match-end 0) pos) -1))))))
  14836. (and (boundp 'org-ts-what)
  14837. (setq org-ts-what
  14838. (cond
  14839. ((org-pos-in-match-range pos 2) 'year)
  14840. ((org-pos-in-match-range pos 3) 'month)
  14841. ((org-pos-in-match-range pos 7) 'hour)
  14842. ((org-pos-in-match-range pos 8) 'minute)
  14843. ((or (org-pos-in-match-range pos 4)
  14844. (org-pos-in-match-range pos 5)) 'day)
  14845. ((and (> pos (or (match-end 8) (match-end 5)))
  14846. (< pos (match-end 0)))
  14847. (- pos (or (match-end 8) (match-end 5))))
  14848. (t 'day))))
  14849. ans))
  14850. (defun org-timestamp-change (n &optional what)
  14851. "Change the date in the time stamp at point.
  14852. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14853. `year', `minute', `second'. If WHAT is not given, the cursor position
  14854. in the timestamp determines what will be changed."
  14855. (let ((pos (point))
  14856. with-hm inactive
  14857. org-ts-what
  14858. extra
  14859. ts time time0)
  14860. (if (not (org-at-timestamp-p t))
  14861. (error "Not at a timestamp"))
  14862. (if (and (not what) (not (eq org-ts-what 'day))
  14863. org-display-custom-times
  14864. (get-text-property (point) 'display)
  14865. (not (get-text-property (1- (point)) 'display)))
  14866. (setq org-ts-what 'day))
  14867. (setq org-ts-what (or what org-ts-what)
  14868. inactive (= (char-after (match-beginning 0)) ?\[)
  14869. ts (match-string 0))
  14870. (replace-match "")
  14871. (if (string-match
  14872. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  14873. ts)
  14874. (setq extra (match-string 1 ts)))
  14875. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14876. (setq with-hm t))
  14877. (setq time0 (org-parse-time-string ts))
  14878. (setq time
  14879. (apply 'encode-time
  14880. (append
  14881. (list (or (car time0) 0))
  14882. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  14883. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  14884. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  14885. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  14886. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  14887. (nthcdr 6 time0))))
  14888. (when (integerp org-ts-what)
  14889. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  14890. (if (eq what 'calendar)
  14891. (let ((cal-date
  14892. (save-excursion
  14893. (save-match-data
  14894. (set-buffer "*Calendar*")
  14895. (calendar-cursor-to-date)))))
  14896. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14897. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14898. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14899. (setcar time0 (or (car time0) 0))
  14900. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14901. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  14902. (setq time (apply 'encode-time time0))))
  14903. (setq org-last-changed-timestamp
  14904. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14905. (org-clock-update-time-maybe)
  14906. (goto-char pos)
  14907. ;; Try to recenter the calendar window, if any
  14908. (if (and org-calendar-follow-timestamp-change
  14909. (get-buffer-window "*Calendar*" t)
  14910. (memq org-ts-what '(day month year)))
  14911. (org-recenter-calendar (time-to-days time)))))
  14912. (defun org-modify-ts-extra (s pos n)
  14913. "FIXME"
  14914. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14915. ng h m new)
  14916. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14917. (cond
  14918. ((or (org-pos-in-match-range pos 2)
  14919. (org-pos-in-match-range pos 3))
  14920. (setq m (string-to-number (match-string 3 s))
  14921. h (string-to-number (match-string 2 s)))
  14922. (if (org-pos-in-match-range pos 2)
  14923. (setq h (+ h n))
  14924. (setq m (+ m n)))
  14925. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14926. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14927. (setq h (min 24 (max 0 h)))
  14928. (setq ng 1 new (format "-%02d:%02d" h m)))
  14929. ((org-pos-in-match-range pos 6)
  14930. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14931. ((org-pos-in-match-range pos 5)
  14932. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  14933. (when ng
  14934. (setq s (concat
  14935. (substring s 0 (match-beginning ng))
  14936. new
  14937. (substring s (match-end ng))))))
  14938. s))
  14939. (defun org-recenter-calendar (date)
  14940. "If the calendar is visible, recenter it to DATE."
  14941. (let* ((win (selected-window))
  14942. (cwin (get-buffer-window "*Calendar*" t))
  14943. (calendar-move-hook nil))
  14944. (when cwin
  14945. (select-window cwin)
  14946. (calendar-goto-date (if (listp date) date
  14947. (calendar-gregorian-from-absolute date)))
  14948. (select-window win))))
  14949. (defun org-goto-calendar (&optional arg)
  14950. "Go to the Emacs calendar at the current date.
  14951. If there is a time stamp in the current line, go to that date.
  14952. A prefix ARG can be used to force the current date."
  14953. (interactive "P")
  14954. (let ((tsr org-ts-regexp) diff
  14955. (calendar-move-hook nil)
  14956. (view-calendar-holidays-initially nil)
  14957. (view-diary-entries-initially nil))
  14958. (if (or (org-at-timestamp-p)
  14959. (save-excursion
  14960. (beginning-of-line 1)
  14961. (looking-at (concat ".*" tsr))))
  14962. (let ((d1 (time-to-days (current-time)))
  14963. (d2 (time-to-days
  14964. (org-time-string-to-time (match-string 1)))))
  14965. (setq diff (- d2 d1))))
  14966. (calendar)
  14967. (calendar-goto-today)
  14968. (if (and diff (not arg)) (calendar-forward-day diff))))
  14969. (defun org-date-from-calendar ()
  14970. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14971. If there is already a time stamp at the cursor position, update it."
  14972. (interactive)
  14973. (org-timestamp-change 0 'calendar))
  14974. ;;; The clock for measuring work time.
  14975. (defvar org-mode-line-string "")
  14976. (put 'org-mode-line-string 'risky-local-variable t)
  14977. (defvar org-mode-line-timer nil)
  14978. (defvar org-clock-heading "")
  14979. (defvar org-clock-start-time "")
  14980. (defun org-update-mode-line ()
  14981. (let* ((delta (- (time-to-seconds (current-time))
  14982. (time-to-seconds org-clock-start-time)))
  14983. (h (floor delta 3600))
  14984. (m (floor (- delta (* 3600 h)) 60)))
  14985. (setq org-mode-line-string
  14986. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  14987. 'help-echo "Org-mode clock is running"))
  14988. (force-mode-line-update)))
  14989. (defvar org-clock-marker (make-marker)
  14990. "Marker recording the last clock-in.")
  14991. (defvar org-clock-mode-line-entry nil
  14992. "Information for the modeline about the running clock.")
  14993. (defun org-clock-in ()
  14994. "Start the clock on the current item.
  14995. If necessary, clock-out of the currently active clock."
  14996. (interactive)
  14997. (org-clock-out t)
  14998. (let (ts)
  14999. (save-excursion
  15000. (org-back-to-heading t)
  15001. (if (looking-at org-todo-line-regexp)
  15002. (setq org-clock-heading (match-string 3))
  15003. (setq org-clock-heading "???"))
  15004. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  15005. (beginning-of-line 2)
  15006. (while
  15007. (or (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15008. (not (equal (match-string 1) org-clock-string)))
  15009. (and (looking-at "[ \t]*:PROPERTIES:")
  15010. (not org-insert-labeled-timestamps-before-properties-drawer)))
  15011. ;; Scheduling info, or properties drawer, move one line further
  15012. (beginning-of-line 2)
  15013. (or (bolp) (newline)))
  15014. (insert "\n") (backward-char 1)
  15015. (indent-relative)
  15016. (insert org-clock-string " ")
  15017. (setq org-clock-start-time (current-time))
  15018. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15019. (move-marker org-clock-marker (point) (buffer-base-buffer))
  15020. (or global-mode-string (setq global-mode-string '("")))
  15021. (or (memq 'org-mode-line-string global-mode-string)
  15022. (setq global-mode-string
  15023. (append global-mode-string '(org-mode-line-string))))
  15024. (org-update-mode-line)
  15025. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  15026. (message "Clock started at %s" ts))))
  15027. (defun org-clock-out (&optional fail-quietly)
  15028. "Stop the currently running clock.
  15029. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  15030. (interactive)
  15031. (catch 'exit
  15032. (if (not (marker-buffer org-clock-marker))
  15033. (if fail-quietly (throw 'exit t) (error "No active clock")))
  15034. (let (ts te s h m)
  15035. (save-excursion
  15036. (set-buffer (marker-buffer org-clock-marker))
  15037. (goto-char org-clock-marker)
  15038. (beginning-of-line 1)
  15039. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15040. (equal (match-string 1) org-clock-string))
  15041. (setq ts (match-string 2))
  15042. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  15043. (goto-char (match-end 0))
  15044. (delete-region (point) (point-at-eol))
  15045. (insert "--")
  15046. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15047. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  15048. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  15049. h (floor (/ s 3600))
  15050. s (- s (* 3600 h))
  15051. m (floor (/ s 60))
  15052. s (- s (* 60 s)))
  15053. (insert " => " (format "%2d:%02d" h m))
  15054. (move-marker org-clock-marker nil)
  15055. (org-add-log-maybe 'clock-out)
  15056. (when org-mode-line-timer
  15057. (cancel-timer org-mode-line-timer)
  15058. (setq org-mode-line-timer nil))
  15059. (setq global-mode-string
  15060. (delq 'org-mode-line-string global-mode-string))
  15061. (force-mode-line-update)
  15062. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  15063. (defun org-clock-cancel ()
  15064. "Cancel the running clock be removing the start timestamp."
  15065. (interactive)
  15066. (if (not (marker-buffer org-clock-marker))
  15067. (error "No active clock"))
  15068. (save-excursion
  15069. (set-buffer (marker-buffer org-clock-marker))
  15070. (goto-char org-clock-marker)
  15071. (delete-region (1- (point-at-bol)) (point-at-eol)))
  15072. (message "Clock canceled"))
  15073. (defvar org-clock-file-total-minutes nil
  15074. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  15075. (make-variable-buffer-local 'org-clock-file-total-minutes)
  15076. (defun org-clock-sum (&optional tstart tend)
  15077. "Sum the times for each subtree.
  15078. Puts the resulting times in minutes as a text property on each headline."
  15079. (interactive)
  15080. (let* ((bmp (buffer-modified-p))
  15081. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  15082. org-clock-string
  15083. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  15084. (lmax 30)
  15085. (ltimes (make-vector lmax 0))
  15086. (t1 0)
  15087. (level 0)
  15088. ts te dt
  15089. time)
  15090. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  15091. (save-excursion
  15092. (goto-char (point-max))
  15093. (while (re-search-backward re nil t)
  15094. (cond
  15095. ((match-end 2)
  15096. ;; Two time stamps
  15097. (setq ts (match-string 2)
  15098. te (match-string 3)
  15099. ts (time-to-seconds
  15100. (apply 'encode-time (org-parse-time-string ts)))
  15101. te (time-to-seconds
  15102. (apply 'encode-time (org-parse-time-string te)))
  15103. ts (if tstart (max ts tstart) ts)
  15104. te (if tend (min te tend) te)
  15105. dt (- te ts)
  15106. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  15107. ((match-end 4)
  15108. ;; A naket time
  15109. (setq t1 (+ t1 (string-to-number (match-string 5))
  15110. (* 60 (string-to-number (match-string 4))))))
  15111. (t ;; A headline
  15112. (setq level (- (match-end 1) (match-beginning 1)))
  15113. (when (or (> t1 0) (> (aref ltimes level) 0))
  15114. (loop for l from 0 to level do
  15115. (aset ltimes l (+ (aref ltimes l) t1)))
  15116. (setq t1 0 time (aref ltimes level))
  15117. (loop for l from level to (1- lmax) do
  15118. (aset ltimes l 0))
  15119. (goto-char (match-beginning 0))
  15120. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  15121. (setq org-clock-file-total-minutes (aref ltimes 0)))
  15122. (set-buffer-modified-p bmp)))
  15123. (defun org-clock-display (&optional total-only)
  15124. "Show subtree times in the entire buffer.
  15125. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  15126. in the echo area."
  15127. (interactive)
  15128. (org-remove-clock-overlays)
  15129. (let (time h m p)
  15130. (org-clock-sum)
  15131. (unless total-only
  15132. (save-excursion
  15133. (goto-char (point-min))
  15134. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  15135. (goto-char p)
  15136. (when (setq time (get-text-property p :org-clock-minutes))
  15137. (org-put-clock-overlay time (funcall outline-level))))
  15138. (setq h (/ org-clock-file-total-minutes 60)
  15139. m (- org-clock-file-total-minutes (* 60 h)))
  15140. ;; Arrange to remove the overlays upon next change.
  15141. (when org-remove-highlights-with-change
  15142. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  15143. nil 'local))))
  15144. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  15145. (defvar org-clock-overlays nil)
  15146. (make-variable-buffer-local 'org-clock-overlays)
  15147. (defun org-put-clock-overlay (time &optional level)
  15148. "Put an overlays on the current line, displaying TIME.
  15149. If LEVEL is given, prefix time with a corresponding number of stars.
  15150. This creates a new overlay and stores it in `org-clock-overlays', so that it
  15151. will be easy to remove."
  15152. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  15153. (l (if level (org-get-legal-level level 0) 0))
  15154. (off 0)
  15155. ov tx)
  15156. (move-to-column c)
  15157. (unless (eolp) (skip-chars-backward "^ \t"))
  15158. (skip-chars-backward " \t")
  15159. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  15160. tx (concat (buffer-substring (1- (point)) (point))
  15161. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  15162. (org-add-props (format "%s %2d:%02d%s"
  15163. (make-string l ?*) h m
  15164. (make-string (- 10 l) ?\ ))
  15165. '(face secondary-selection))
  15166. ""))
  15167. (if (not (featurep 'xemacs))
  15168. (org-overlay-put ov 'display tx)
  15169. (org-overlay-put ov 'invisible t)
  15170. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  15171. (push ov org-clock-overlays)))
  15172. (defun org-remove-clock-overlays (&optional beg end noremove)
  15173. "Remove the occur highlights from the buffer.
  15174. BEG and END are ignored. If NOREMOVE is nil, remove this function
  15175. from the `before-change-functions' in the current buffer."
  15176. (interactive)
  15177. (unless org-inhibit-highlight-removal
  15178. (mapc 'org-delete-overlay org-clock-overlays)
  15179. (setq org-clock-overlays nil)
  15180. (unless noremove
  15181. (remove-hook 'before-change-functions
  15182. 'org-remove-clock-overlays 'local))))
  15183. (defun org-clock-out-if-current ()
  15184. "Clock out if the current entry contains the running clock.
  15185. This is used to stop the clock after a TODO entry is marked DONE,
  15186. and is only done if the variable `org-clock-out-when-done' is not nil."
  15187. (when (and org-clock-out-when-done
  15188. (member state org-done-keywords)
  15189. (equal (marker-buffer org-clock-marker) (current-buffer))
  15190. (< (point) org-clock-marker)
  15191. (> (save-excursion (outline-next-heading) (point))
  15192. org-clock-marker))
  15193. ;; Clock out, but don't accept a logging message for this.
  15194. (let ((org-log-done (if (and (listp org-log-done)
  15195. (member 'clock-out org-log-done))
  15196. '(done)
  15197. org-log-done)))
  15198. (org-clock-out))))
  15199. (add-hook 'org-after-todo-state-change-hook
  15200. 'org-clock-out-if-current)
  15201. (defun org-check-running-clock ()
  15202. "Check if the current buffer contains the running clock.
  15203. If yes, offer to stop it and to save the buffer with the changes."
  15204. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  15205. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  15206. (buffer-name))))
  15207. (org-clock-out)
  15208. (when (y-or-n-p "Save changed buffer?")
  15209. (save-buffer))))
  15210. (defun org-clock-report ()
  15211. "Create a table containing a report about clocked time.
  15212. If the buffer contains lines
  15213. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  15214. #+END: clocktable
  15215. then the table will be inserted between these lines, replacing whatever
  15216. is was there before. If these lines are not in the buffer, the table
  15217. is inserted at point, surrounded by the special lines.
  15218. The BEGIN line can contain parameters. Allowed are:
  15219. :maxlevel The maximum level to be included in the table. Default is 3.
  15220. :emphasize t/nil, if level 1 and level 2 should be bold/italic in the table.
  15221. :scope nil/subtree/tree/treeN/file/agenda/'(\"file\"...).
  15222. The scope of file content to be searched for clock entries.
  15223. The default nil means the entire file (or the region to
  15224. which it is currently narrowed). \"subtree\" means the
  15225. enclosing subtree, \"treeN\" means the surrounding tree of level N.
  15226. \"agenda\" are all agenda files, a list of files searches
  15227. these files."
  15228. (interactive)
  15229. (org-remove-clock-overlays)
  15230. (unless (org-find-dblock "clocktable")
  15231. (org-create-dblock (list :name "clocktable"
  15232. :maxlevel 2 :emphasize nil)))
  15233. (org-update-dblock))
  15234. (defun org-clock-update-time-maybe ()
  15235. "If this is a CLOCK line, update it and return t.
  15236. Otherwise, return nil."
  15237. (interactive)
  15238. (save-excursion
  15239. (beginning-of-line 1)
  15240. (skip-chars-forward " \t")
  15241. (when (looking-at org-clock-string)
  15242. (let ((re (concat "[ \t]*" org-clock-string
  15243. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  15244. "\\([ \t]*=>.*\\)?"))
  15245. ts te h m s)
  15246. (if (not (looking-at re))
  15247. nil
  15248. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  15249. (end-of-line 1)
  15250. (setq ts (match-string 1)
  15251. te (match-string 2))
  15252. (setq s (- (time-to-seconds
  15253. (apply 'encode-time (org-parse-time-string te)))
  15254. (time-to-seconds
  15255. (apply 'encode-time (org-parse-time-string ts))))
  15256. h (floor (/ s 3600))
  15257. s (- s (* 3600 h))
  15258. m (floor (/ s 60))
  15259. s (- s (* 60 s)))
  15260. (insert " => " (format "%2d:%02d" h m))
  15261. t)))))
  15262. (defun org-clock-special-range (key &optional time as-strings)
  15263. "Return two times bordering a special time range.
  15264. Key is a symbol specifying the range and can be one of `today', `yesterday',
  15265. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  15266. A week starts Monday 0:00 and ends Sunday 24:00.
  15267. The range is determined relative to TIME. TIME defaults to the current time.
  15268. The return value is a cons cell with two internal times like the ones
  15269. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  15270. the returned times will be formatted strings."
  15271. (let* ((tm (decode-time (or time (current-time))))
  15272. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  15273. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  15274. (dow (nth 6 tm))
  15275. s1 m1 h1 d1 month1 y1 diff ts te fm)
  15276. (cond
  15277. ((eq key 'today)
  15278. (setq h 0 m 0 h1 24 m1 0))
  15279. ((eq key 'yesterday)
  15280. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  15281. ((eq key 'thisweek)
  15282. (setq diff (if (= dow 0) 6 (1- dow))
  15283. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  15284. ((eq key 'lastweek)
  15285. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  15286. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  15287. ((eq key 'thismonth)
  15288. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  15289. ((eq key 'lastmonth)
  15290. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  15291. ((eq key 'thisyear)
  15292. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  15293. ((eq key 'lastyear)
  15294. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  15295. (t (error "No such time block %s" key)))
  15296. (setq ts (encode-time s m h d month y)
  15297. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  15298. (or d1 d) (or month1 month) (or y1 y)))
  15299. (setq fm (cdr org-time-stamp-formats))
  15300. (if as-strings
  15301. (cons (format-time-string fm ts) (format-time-string fm te))
  15302. (cons ts te))))
  15303. (defun org-dblock-write:clocktable (params)
  15304. "Write the standard clocktable."
  15305. (let ((hlchars '((1 . "*") (2 . ?/)))
  15306. (emph nil)
  15307. (ins (make-marker))
  15308. (total-time nil)
  15309. ipos time h m p level hlc hdl maxlevel
  15310. ts te cc block beg end pos scope tbl tostring)
  15311. (setq scope (plist-get params :scope)
  15312. tostring (plist-get params :tostring)
  15313. maxlevel (or (plist-get params :maxlevel) 3)
  15314. emph (plist-get params :emphasize)
  15315. ts (plist-get params :tstart)
  15316. te (plist-get params :tend)
  15317. block (plist-get params :block))
  15318. (when block
  15319. (setq cc (org-clock-special-range block nil t)
  15320. ts (car cc) te (cdr cc)))
  15321. (if ts (setq ts (time-to-seconds
  15322. (apply 'encode-time (org-parse-time-string ts)))))
  15323. (if te (setq te (time-to-seconds
  15324. (apply 'encode-time (org-parse-time-string te)))))
  15325. (move-marker ins (point))
  15326. (setq ipos (point))
  15327. ;; Get the right scope
  15328. (setq pos (point))
  15329. (save-restriction
  15330. (cond
  15331. ((not scope))
  15332. ((eq scope 'file) (widen))
  15333. ((eq scope 'subtree) (org-narrow-to-subtree))
  15334. ((eq scope 'tree)
  15335. (while (org-up-heading-safe))
  15336. (org-narrow-to-subtree))
  15337. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  15338. (symbol-name scope)))
  15339. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  15340. (catch 'exit
  15341. (while (org-up-heading-safe)
  15342. (looking-at outline-regexp) ;; FIXME do we need to test this?
  15343. (if (<= (org-reduced-level (funcall outline-level)) level)
  15344. (throw 'exit nil))))
  15345. (org-narrow-to-subtree))
  15346. ((or (listp scope) (eq scope 'agenda))
  15347. (let* ((files (if (listp scope) scope (org-agenda-files)))
  15348. (scope 'agenda)
  15349. (p1 (copy-sequence params))
  15350. file)
  15351. (plist-put p1 :tostring t)
  15352. (plist-put p1 :scope 'file)
  15353. (org-prepare-agenda-buffers files)
  15354. (setq total-time (+ (or total-time 0) org-clock-file-total-minutes))
  15355. (while (setq file (pop files))
  15356. (with-current-buffer (find-buffer-visiting file)
  15357. (push (org-clocktable-add-file
  15358. file(org-dblock-write:clocktable p1)) tbl))))))
  15359. (goto-char pos)
  15360. (unless (eq scope 'agenda)
  15361. (org-clock-sum ts te)
  15362. (goto-char (point-min))
  15363. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  15364. (goto-char p)
  15365. (when (setq time (get-text-property p :org-clock-minutes))
  15366. (save-excursion
  15367. (beginning-of-line 1)
  15368. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  15369. (setq level (- (match-end 1) (match-beginning 1)))
  15370. (<= level maxlevel))
  15371. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  15372. hdl (match-string 2)
  15373. h (/ time 60)
  15374. m (- time (* 60 h)))
  15375. (if (= level 1) (push "|-\n" tbl))
  15376. (push (concat
  15377. "| " (int-to-string level) "|" hlc hdl hlc " |"
  15378. (make-string (1- level) ?|)
  15379. hlc (format "%d:%02d" h m) hlc
  15380. " |\n") tbl))))))
  15381. (setq tbl (nreverse tbl))
  15382. (if tostring
  15383. (mapconcat 'identity tbl "")
  15384. (goto-char ins)
  15385. (insert-before-markers
  15386. "Clock summary at ["
  15387. (substring
  15388. (format-time-string (cdr org-time-stamp-formats))
  15389. 1 -1)
  15390. "]."
  15391. (if block
  15392. (format " Considered range is /%s/." block)
  15393. "")
  15394. "\n\n"
  15395. (if (eq scope 'agenda) "|File" "")
  15396. "|L|Headline|Time|\n")
  15397. (setq total-time (or total-time org-clock-file-total-minutes)
  15398. h (/ total-time 60)
  15399. m (- total-time (* 60 h)))
  15400. (insert-before-markers
  15401. "|-\n|"
  15402. (if (eq scope 'agenda) "|" "")
  15403. "|"
  15404. "*Total time*| "
  15405. (format "*%d:%02d*" h m)
  15406. "|\n")
  15407. (insert-before-markers (mapconcat 'identity (nreverse tbl)
  15408. (if (eq scope 'agenda) "|-\n" "")))
  15409. (backward-delete-char 1)
  15410. (goto-char ipos)
  15411. (skip-chars-forward "^|")
  15412. (org-table-align)))))
  15413. (defun org-clocktable-add-file (file table)
  15414. (let ((lines (org-split-string table "\n"))
  15415. (ff (file-name-nondirectory file)))
  15416. (mapconcat 'identity
  15417. (mapcar (lambda (x)
  15418. (if (string-match org-table-dataline-regexp x)
  15419. (concat "|" ff x)
  15420. x))
  15421. lines)
  15422. "\n")))
  15423. ;; FIXME: I don't think anybody uses this, ask David
  15424. (defun org-collect-clock-time-entries ()
  15425. "Return an internal list with clocking information.
  15426. This list has one entry for each CLOCK interval.
  15427. FIXME: describe the elements."
  15428. (interactive)
  15429. (let ((re (concat "^[ \t]*" org-clock-string
  15430. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  15431. rtn beg end next cont level title total closedp leafp
  15432. clockpos titlepos h m donep)
  15433. (save-excursion
  15434. (org-clock-sum)
  15435. (goto-char (point-min))
  15436. (while (re-search-forward re nil t)
  15437. (setq clockpos (match-beginning 0)
  15438. beg (match-string 1) end (match-string 2)
  15439. cont (match-end 0))
  15440. (setq beg (apply 'encode-time (org-parse-time-string beg))
  15441. end (apply 'encode-time (org-parse-time-string end)))
  15442. (org-back-to-heading t)
  15443. (setq donep (org-entry-is-done-p))
  15444. (setq titlepos (point)
  15445. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  15446. h (/ total 60) m (- total (* 60 h))
  15447. total (cons h m))
  15448. (looking-at "\\(\\*+\\) +\\(.*\\)")
  15449. (setq level (- (match-end 1) (match-beginning 1))
  15450. title (org-match-string-no-properties 2))
  15451. (save-excursion (outline-next-heading) (setq next (point)))
  15452. (setq closedp (re-search-forward org-closed-time-regexp next t))
  15453. (goto-char next)
  15454. (setq leafp (and (looking-at "^\\*+ ")
  15455. (<= (- (match-end 0) (point)) level)))
  15456. (push (list beg end clockpos closedp donep
  15457. total title titlepos level leafp)
  15458. rtn)
  15459. (goto-char cont)))
  15460. (nreverse rtn)))
  15461. ;;;; Agenda, and Diary Integration
  15462. ;;; Define the Org-agenda-mode
  15463. (defvar org-agenda-mode-map (make-sparse-keymap)
  15464. "Keymap for `org-agenda-mode'.")
  15465. (defvar org-agenda-menu) ; defined later in this file.
  15466. (defvar org-agenda-follow-mode nil)
  15467. (defvar org-agenda-show-log nil)
  15468. (defvar org-agenda-redo-command nil)
  15469. (defvar org-agenda-mode-hook nil)
  15470. (defvar org-agenda-type nil)
  15471. (defvar org-agenda-force-single-file nil)
  15472. (defun org-agenda-mode ()
  15473. "Mode for time-sorted view on action items in Org-mode files.
  15474. The following commands are available:
  15475. \\{org-agenda-mode-map}"
  15476. (interactive)
  15477. (kill-all-local-variables)
  15478. (setq org-agenda-undo-list nil
  15479. org-agenda-pending-undo-list nil)
  15480. (setq major-mode 'org-agenda-mode)
  15481. ;; Keep global-font-lock-mode from turning on font-lock-mode
  15482. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  15483. (setq mode-name "Org-Agenda")
  15484. (use-local-map org-agenda-mode-map)
  15485. (easy-menu-add org-agenda-menu)
  15486. (if org-startup-truncated (setq truncate-lines t))
  15487. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  15488. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  15489. ;; Make sure properties are removed when copying text
  15490. (when (boundp 'buffer-substring-filters)
  15491. (org-set-local 'buffer-substring-filters
  15492. (cons (lambda (x)
  15493. (set-text-properties 0 (length x) nil x) x)
  15494. buffer-substring-filters)))
  15495. (unless org-agenda-keep-modes
  15496. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  15497. org-agenda-show-log nil))
  15498. (easy-menu-change
  15499. '("Agenda") "Agenda Files"
  15500. (append
  15501. (list
  15502. (vector
  15503. (if (get 'org-agenda-files 'org-restrict)
  15504. "Restricted to single file"
  15505. "Edit File List")
  15506. '(org-edit-agenda-file-list)
  15507. (not (get 'org-agenda-files 'org-restrict)))
  15508. "--")
  15509. (mapcar 'org-file-menu-entry (org-agenda-files))))
  15510. (org-agenda-set-mode-name)
  15511. (apply
  15512. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  15513. (list 'org-agenda-mode-hook)))
  15514. (substitute-key-definition 'undo 'org-agenda-undo
  15515. org-agenda-mode-map global-map)
  15516. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  15517. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  15518. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  15519. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  15520. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  15521. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  15522. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  15523. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  15524. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  15525. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  15526. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  15527. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  15528. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  15529. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  15530. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  15531. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  15532. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  15533. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  15534. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  15535. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  15536. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  15537. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  15538. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  15539. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  15540. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  15541. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  15542. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  15543. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  15544. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  15545. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  15546. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  15547. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  15548. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  15549. (while l (org-defkey org-agenda-mode-map
  15550. (int-to-string (pop l)) 'digit-argument)))
  15551. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  15552. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  15553. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  15554. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  15555. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  15556. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  15557. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  15558. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  15559. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  15560. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  15561. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  15562. (org-defkey org-agenda-mode-map "n" 'next-line)
  15563. (org-defkey org-agenda-mode-map "p" 'previous-line)
  15564. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  15565. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  15566. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  15567. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  15568. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  15569. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  15570. (eval-after-load "calendar"
  15571. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  15572. 'org-calendar-goto-agenda))
  15573. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  15574. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  15575. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  15576. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  15577. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  15578. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  15579. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  15580. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  15581. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  15582. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  15583. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  15584. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  15585. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  15586. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  15587. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  15588. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  15589. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  15590. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  15591. "Local keymap for agenda entries from Org-mode.")
  15592. (org-defkey org-agenda-keymap
  15593. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  15594. (org-defkey org-agenda-keymap
  15595. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  15596. (when org-agenda-mouse-1-follows-link
  15597. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  15598. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  15599. '("Agenda"
  15600. ("Agenda Files")
  15601. "--"
  15602. ["Show" org-agenda-show t]
  15603. ["Go To (other window)" org-agenda-goto t]
  15604. ["Go To (this window)" org-agenda-switch-to t]
  15605. ["Follow Mode" org-agenda-follow-mode
  15606. :style toggle :selected org-agenda-follow-mode :active t]
  15607. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  15608. "--"
  15609. ["Cycle TODO" org-agenda-todo t]
  15610. ["Archive subtree" org-agenda-archive t]
  15611. ["Delete subtree" org-agenda-kill t]
  15612. "--"
  15613. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  15614. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  15615. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  15616. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  15617. "--"
  15618. ("Tags and Properties"
  15619. ["Show all Tags" org-agenda-show-tags t]
  15620. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  15621. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  15622. "--"
  15623. ["Column View" org-columns t])
  15624. ("Date/Schedule"
  15625. ["Schedule" org-agenda-schedule t]
  15626. ["Set Deadline" org-agenda-deadline t]
  15627. "--"
  15628. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  15629. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  15630. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  15631. ("Priority"
  15632. ["Set Priority" org-agenda-priority t]
  15633. ["Increase Priority" org-agenda-priority-up t]
  15634. ["Decrease Priority" org-agenda-priority-down t]
  15635. ["Show Priority" org-agenda-show-priority t])
  15636. ("Calendar/Diary"
  15637. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  15638. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  15639. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  15640. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  15641. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  15642. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  15643. "--"
  15644. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  15645. "--"
  15646. ("View"
  15647. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  15648. :style radio :selected (equal org-agenda-ndays 1)]
  15649. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  15650. :style radio :selected (equal org-agenda-ndays 7)]
  15651. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  15652. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  15653. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  15654. :style radio :selected (member org-agenda-ndays '(365 366))]
  15655. "--"
  15656. ["Show Logbook entries" org-agenda-log-mode
  15657. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  15658. ["Include Diary" org-agenda-toggle-diary
  15659. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  15660. ["Use Time Grid" org-agenda-toggle-time-grid
  15661. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  15662. ["Write view to file" org-write-agenda t]
  15663. ["Rebuild buffer" org-agenda-redo t]
  15664. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  15665. "--"
  15666. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  15667. "--"
  15668. ["Quit" org-agenda-quit t]
  15669. ["Exit and Release Buffers" org-agenda-exit t]
  15670. ))
  15671. ;;; Agenda undo
  15672. (defvar org-agenda-allow-remote-undo t
  15673. "Non-nil means, allow remote undo from the agenda buffer.")
  15674. (defvar org-agenda-undo-list nil
  15675. "List of undoable operations in the agenda since last refresh.")
  15676. (defvar org-agenda-undo-has-started-in nil
  15677. "Buffers that have already seen `undo-start' in the current undo sequence.")
  15678. (defvar org-agenda-pending-undo-list nil
  15679. "In a series of undo commands, this is the list of remaning undo items.")
  15680. (defmacro org-if-unprotected (&rest body)
  15681. "Execute BODY if there is no `org-protected' text property at point."
  15682. (declare (debug t))
  15683. `(unless (get-text-property (point) 'org-protected)
  15684. ,@body))
  15685. (defmacro org-with-remote-undo (_buffer &rest _body)
  15686. "Execute BODY while recording undo information in two buffers."
  15687. (declare (indent 1) (debug t))
  15688. `(let ((_cline (org-current-line))
  15689. (_cmd this-command)
  15690. (_buf1 (current-buffer))
  15691. (_buf2 ,_buffer)
  15692. (_undo1 buffer-undo-list)
  15693. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  15694. _c1 _c2)
  15695. ,@_body
  15696. (when org-agenda-allow-remote-undo
  15697. (setq _c1 (org-verify-change-for-undo
  15698. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  15699. _c2 (org-verify-change-for-undo
  15700. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  15701. (when (or _c1 _c2)
  15702. ;; make sure there are undo boundaries
  15703. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  15704. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  15705. ;; remember which buffer to undo
  15706. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  15707. org-agenda-undo-list)))))
  15708. (defun org-agenda-undo ()
  15709. "Undo a remote editing step in the agenda.
  15710. This undoes changes both in the agenda buffer and in the remote buffer
  15711. that have been changed along."
  15712. (interactive)
  15713. (or org-agenda-allow-remote-undo
  15714. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  15715. (if (not (eq this-command last-command))
  15716. (setq org-agenda-undo-has-started-in nil
  15717. org-agenda-pending-undo-list org-agenda-undo-list))
  15718. (if (not org-agenda-pending-undo-list)
  15719. (error "No further undo information"))
  15720. (let* ((entry (pop org-agenda-pending-undo-list))
  15721. buf line cmd rembuf)
  15722. (setq cmd (pop entry) line (pop entry))
  15723. (setq rembuf (nth 2 entry))
  15724. (org-with-remote-undo rembuf
  15725. (while (bufferp (setq buf (pop entry)))
  15726. (if (pop entry)
  15727. (with-current-buffer buf
  15728. (let ((last-undo-buffer buf)
  15729. (inhibit-read-only t))
  15730. (unless (memq buf org-agenda-undo-has-started-in)
  15731. (push buf org-agenda-undo-has-started-in)
  15732. (make-local-variable 'pending-undo-list)
  15733. (undo-start))
  15734. (while (and pending-undo-list
  15735. (listp pending-undo-list)
  15736. (not (car pending-undo-list)))
  15737. (pop pending-undo-list))
  15738. (undo-more 1))))))
  15739. (goto-line line)
  15740. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  15741. (defun org-verify-change-for-undo (l1 l2)
  15742. "Verify that a real change occurred between the undo lists L1 and L2."
  15743. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  15744. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  15745. (not (eq l1 l2)))
  15746. ;;; Agenda dispatch
  15747. (defvar org-agenda-restrict nil)
  15748. (defvar org-agenda-restrict-begin (make-marker))
  15749. (defvar org-agenda-restrict-end (make-marker))
  15750. (defvar org-agenda-last-dispatch-buffer nil)
  15751. ;;;###autoload
  15752. (defun org-agenda (arg)
  15753. "Dispatch agenda commands to collect entries to the agenda buffer.
  15754. Prompts for a character to select a command. Any prefix arg will be passed
  15755. on to the selected command. The default selections are:
  15756. g
  15757. a Call `org-agenda-list' to display the agenda for current day or week.
  15758. t Call `org-todo-list' to display the global todo list.
  15759. T Call `org-todo-list' to display the global todo list, select only
  15760. entries with a specific TODO keyword (the user gets a prompt).
  15761. m Call `org-tags-view' to display headlines with tags matching
  15762. a condition (the user is prompted for the condition).
  15763. M Like `m', but select only TODO entries, no ordinary headlines.
  15764. L Create a timeline for the current buffer.
  15765. e Export views to associated files.
  15766. More commands can be added by configuring the variable
  15767. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  15768. searches can be pre-defined in this way.
  15769. If the current buffer is in Org-mode and visiting a file, you can also
  15770. first press `1' to indicate that the agenda should be temporarily (until the
  15771. next use of \\[org-agenda]) restricted to the current file."
  15772. (interactive "P")
  15773. (catch 'exit
  15774. (let* ((buf (current-buffer))
  15775. (bfn (buffer-file-name (buffer-base-buffer)))
  15776. (restrict-ok (and bfn (org-mode-p)))
  15777. (custom org-agenda-custom-commands)
  15778. c entry key type match lprops)
  15779. ;; Turn off restriction
  15780. (put 'org-agenda-files 'org-restrict nil)
  15781. (setq org-agenda-restrict nil)
  15782. (move-marker org-agenda-restrict-begin nil)
  15783. (move-marker org-agenda-restrict-end nil)
  15784. ;; Delete old local properties
  15785. (put 'org-agenda-redo-command 'org-lprops nil)
  15786. ;; Remember where this call originated
  15787. (setq org-agenda-last-dispatch-buffer (current-buffer))
  15788. (save-window-excursion
  15789. (delete-other-windows)
  15790. (org-switch-to-buffer-other-window " *Agenda Commands*")
  15791. (erase-buffer)
  15792. (insert (eval-when-compile
  15793. (let ((header
  15794. "Press key for an agenda command:
  15795. -------------------------------- C Configure custom agenda commands
  15796. a Agenda for current week or day e Export agenda views
  15797. t List of all TODO entries T Entries with special TODO kwd
  15798. m Match a TAGS query M Like m, but only TODO entries
  15799. L Timeline for current buffer # List stuck projects (!=configure)
  15800. ")
  15801. (start 0))
  15802. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  15803. (setq start (match-end 0))
  15804. (add-text-properties (match-beginning 2) (match-end 2)
  15805. '(face bold) header))
  15806. header)))
  15807. (while (setq entry (pop custom))
  15808. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  15809. (insert (format "\n%-4s%-14s: %s"
  15810. (org-add-props (copy-sequence key)
  15811. '(face bold))
  15812. (cond
  15813. ((stringp type) type)
  15814. ((eq type 'agenda) "Agenda for current week or day")
  15815. ((eq type 'alltodo) "List of all TODO entries")
  15816. ((eq type 'stuck) "List of stuck projects")
  15817. ((eq type 'todo) "TODO keyword")
  15818. ((eq type 'tags) "Tags query")
  15819. ((eq type 'tags-todo) "Tags (TODO)")
  15820. ((eq type 'tags-tree) "Tags tree")
  15821. ((eq type 'todo-tree) "TODO kwd tree")
  15822. ((eq type 'occur-tree) "Occur tree")
  15823. ((functionp type) (symbol-name type))
  15824. (t "???"))
  15825. (if (stringp match)
  15826. (org-add-props match nil 'face 'org-warning)
  15827. (format "set of %d commands" (length match))))))
  15828. (if restrict-ok
  15829. (insert "\n"
  15830. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  15831. (goto-char (point-min))
  15832. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  15833. (message "Press key for agenda command%s"
  15834. (if restrict-ok ", or [1] or [0] to restrict" ""))
  15835. (setq c (read-char-exclusive))
  15836. (message "")
  15837. (when (memq c '(?L ?1 ?0))
  15838. (if restrict-ok
  15839. (put 'org-agenda-files 'org-restrict (list bfn))
  15840. (error "Cannot restrict agenda to current buffer"))
  15841. (with-current-buffer " *Agenda Commands*"
  15842. (goto-char (point-max))
  15843. (delete-region (point-at-bol) (point))
  15844. (goto-char (point-min)))
  15845. (when (eq c ?0)
  15846. (setq org-agenda-restrict t)
  15847. (with-current-buffer buf
  15848. (if (org-region-active-p)
  15849. (progn
  15850. (move-marker org-agenda-restrict-begin (region-beginning))
  15851. (move-marker org-agenda-restrict-end (region-end)))
  15852. (save-excursion
  15853. (org-back-to-heading t)
  15854. (move-marker org-agenda-restrict-begin (point))
  15855. (move-marker org-agenda-restrict-end
  15856. (progn (org-end-of-subtree t)))))))
  15857. (unless (eq c ?L)
  15858. (message "Press key for agenda command%s"
  15859. (if restrict-ok " (restricted to current file)" ""))
  15860. (setq c (read-char-exclusive)))
  15861. (message "")))
  15862. (require 'calendar) ; FIXME: can we avoid this for some commands?
  15863. ;; For example the todo list should not need it (but does...)
  15864. (cond
  15865. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  15866. (if (symbolp (nth 1 entry))
  15867. (progn
  15868. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  15869. lprops (nth 3 entry))
  15870. (put 'org-agenda-redo-command 'org-lprops lprops)
  15871. (cond
  15872. ((eq type 'agenda)
  15873. (org-let lprops '(org-agenda-list current-prefix-arg)))
  15874. ((eq type 'alltodo)
  15875. (org-let lprops '(org-todo-list current-prefix-arg)))
  15876. ((eq type 'stuck)
  15877. (org-let lprops '(org-agenda-list-stuck-projects
  15878. current-prefix-arg)))
  15879. ((eq type 'tags)
  15880. (org-let lprops '(org-tags-view current-prefix-arg match)))
  15881. ((eq type 'tags-todo)
  15882. (org-let lprops '(org-tags-view '(4) match)))
  15883. ((eq type 'todo)
  15884. (org-let lprops '(org-todo-list match)))
  15885. ((eq type 'tags-tree)
  15886. (org-check-for-org-mode)
  15887. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  15888. ((eq type 'todo-tree)
  15889. (org-check-for-org-mode)
  15890. (org-let lprops
  15891. '(org-occur (concat "^" outline-regexp "[ \t]*"
  15892. (regexp-quote match) "\\>"))))
  15893. ((eq type 'occur-tree)
  15894. (org-check-for-org-mode)
  15895. (org-let lprops '(org-occur match)))
  15896. ((fboundp type)
  15897. (org-let lprops '(funcall type match)))
  15898. (t (error "Invalid custom agenda command type %s" type))))
  15899. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  15900. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  15901. ((equal c ?a) (call-interactively 'org-agenda-list))
  15902. ((equal c ?t) (call-interactively 'org-todo-list))
  15903. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  15904. ((equal c ?m) (call-interactively 'org-tags-view))
  15905. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  15906. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  15907. ((equal c ?L)
  15908. (unless restrict-ok
  15909. (error "This is not an Org-mode file"))
  15910. (org-call-with-arg 'org-timeline arg))
  15911. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  15912. ((equal c ?!) (customize-variable 'org-stuck-projects))
  15913. (t (error "Invalid key"))))))
  15914. (defun org-run-agenda-series (name series)
  15915. (org-prepare-agenda name)
  15916. (let* ((org-agenda-multi t)
  15917. (redo (list 'org-run-agenda-series name (list 'quote series)))
  15918. (cmds (car series))
  15919. (gprops (nth 1 series))
  15920. match ;; The byte compiler incorrectly complains about this. Keep it!
  15921. cmd type lprops)
  15922. (while (setq cmd (pop cmds))
  15923. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  15924. (cond
  15925. ((eq type 'agenda)
  15926. (org-let2 gprops lprops
  15927. '(call-interactively 'org-agenda-list)))
  15928. ((eq type 'alltodo)
  15929. (org-let2 gprops lprops
  15930. '(call-interactively 'org-todo-list)))
  15931. ((eq type 'stuck)
  15932. (org-let2 gprops lprops
  15933. '(call-interactively 'org-agenda-list-stuck-projects)))
  15934. ((eq type 'tags)
  15935. (org-let2 gprops lprops
  15936. '(org-tags-view current-prefix-arg match)))
  15937. ((eq type 'tags-todo)
  15938. (org-let2 gprops lprops
  15939. '(org-tags-view '(4) match)))
  15940. ((eq type 'todo)
  15941. (org-let2 gprops lprops
  15942. '(org-todo-list match)))
  15943. ((fboundp type)
  15944. (org-let2 gprops lprops
  15945. '(funcall type match)))
  15946. (t (error "Invalid type in command series"))))
  15947. (widen)
  15948. (setq org-agenda-redo-command redo)
  15949. (goto-char (point-min)))
  15950. (org-finalize-agenda))
  15951. ;;;###autoload
  15952. (defmacro org-batch-agenda (cmd-key &rest parameters)
  15953. "Run an agenda command in batch mode and send the result to STDOUT.
  15954. If CMD-KEY is a string of length 1, it is used as a key in
  15955. `org-agenda-custom-commands' and triggers this command. If it is a
  15956. longer string is is used as a tags/todo match string.
  15957. Paramters are alternating variable names and values that will be bound
  15958. before running the agenda command."
  15959. (let (pars)
  15960. (while parameters
  15961. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15962. (if (> (length cmd-key) 1)
  15963. (eval (list 'let (nreverse pars)
  15964. (list 'org-tags-view nil cmd-key)))
  15965. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15966. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15967. (set-buffer org-agenda-buffer-name)
  15968. (princ (org-encode-for-stdout (buffer-string)))))
  15969. (defun org-encode-for-stdout (string)
  15970. (if (fboundp 'encode-coding-string)
  15971. (encode-coding-string string buffer-file-coding-system)
  15972. string))
  15973. (defvar org-agenda-info nil)
  15974. ;;;###autoload
  15975. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  15976. "Run an agenda command in batch mode and send the result to STDOUT.
  15977. If CMD-KEY is a string of length 1, it is used as a key in
  15978. `org-agenda-custom-commands' and triggers this command. If it is a
  15979. longer string is is used as a tags/todo match string.
  15980. Paramters are alternating variable names and values that will be bound
  15981. before running the agenda command.
  15982. The output gives a line for each selected agenda item. Each
  15983. item is a list of comma-separated values, like this:
  15984. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  15985. category The category of the item
  15986. head The headline, without TODO kwd, TAGS and PRIORITY
  15987. type The type of the agenda entry, can be
  15988. todo selected in TODO match
  15989. tagsmatch selected in tags match
  15990. diary imported from diary
  15991. deadline a deadline on given date
  15992. scheduled scheduled on given date
  15993. timestamp entry has timestamp on given date
  15994. closed entry was closed on given date
  15995. upcoming-deadline warning about deadline
  15996. past-scheduled forwarded scheduled item
  15997. block entry has date block including g. date
  15998. todo The todo keyword, if any
  15999. tags All tags including inherited ones, separated by colons
  16000. date The relevant date, like 2007-2-14
  16001. time The time, like 15:00-16:50
  16002. extra Sting with extra planning info
  16003. priority-l The priority letter if any was given
  16004. priority-n The computed numerical priority
  16005. agenda-day The day in the agenda where this is listed"
  16006. (let (pars)
  16007. (while parameters
  16008. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  16009. (push (list 'org-agenda-remove-tags t) pars)
  16010. (if (> (length cmd-key) 1)
  16011. (eval (list 'let (nreverse pars)
  16012. (list 'org-tags-view nil cmd-key)))
  16013. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  16014. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  16015. (set-buffer org-agenda-buffer-name)
  16016. (let* ((lines (org-split-string (buffer-string) "\n"))
  16017. line)
  16018. (while (setq line (pop lines))
  16019. (catch 'next
  16020. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  16021. (setq org-agenda-info
  16022. (org-fix-agenda-info (text-properties-at 0 line)))
  16023. (princ
  16024. (org-encode-for-stdout
  16025. (mapconcat 'org-agenda-export-csv-mapper
  16026. '(org-category txt type todo tags date time-of-day extra
  16027. priority-letter priority agenda-day)
  16028. ",")))
  16029. (princ "\n"))))))
  16030. (defun org-fix-agenda-info (props)
  16031. "FIXME"
  16032. (let (tmp re)
  16033. (when (setq tmp (plist-get props 'tags))
  16034. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  16035. (when (setq tmp (plist-get props 'date))
  16036. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  16037. (let ((calendar-date-display-form '(year "-" month "-" day)))
  16038. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  16039. (setq tmp (calendar-date-string tmp)))
  16040. (setq props (plist-put props 'date tmp)))
  16041. (when (setq tmp (plist-get props 'day))
  16042. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  16043. (let ((calendar-date-display-form '(year "-" month "-" day)))
  16044. (setq tmp (calendar-date-string tmp)))
  16045. (setq props (plist-put props 'day tmp))
  16046. (setq props (plist-put props 'agenda-day tmp)))
  16047. (when (setq tmp (plist-get props 'txt))
  16048. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  16049. (plist-put props 'priority-letter (match-string 1 tmp))
  16050. (setq tmp (replace-match "" t t tmp)))
  16051. (when (and (setq re (plist-get props 'org-todo-regexp))
  16052. (setq re (concat "\\`\\.*" re " ?"))
  16053. (string-match re tmp))
  16054. (plist-put props 'todo (match-string 1 tmp))
  16055. (setq tmp (replace-match "" t t tmp)))
  16056. (plist-put props 'txt tmp)))
  16057. props)
  16058. (defun org-agenda-export-csv-mapper (prop)
  16059. (let ((res (plist-get org-agenda-info prop)))
  16060. (setq res
  16061. (cond
  16062. ((not res) "")
  16063. ((stringp res) res)
  16064. (t (prin1-to-string res))))
  16065. (while (string-match "," res)
  16066. (setq res (replace-match ";" t t res)))
  16067. (org-trim res)))
  16068. ;;;###autoload
  16069. (defun org-store-agenda-views (&rest parameters)
  16070. (interactive)
  16071. (eval (list 'org-batch-store-agenda-views)))
  16072. ;; FIXME, why is this a macro?????
  16073. ;;;###autoload
  16074. (defmacro org-batch-store-agenda-views (&rest parameters)
  16075. "Run all custom agenda commands that have a file argument."
  16076. (let ((cmds org-agenda-custom-commands)
  16077. (pop-up-frames nil)
  16078. (dir default-directory)
  16079. pars cmd thiscmdkey files opts)
  16080. (while parameters
  16081. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  16082. (setq pars (reverse pars))
  16083. (save-window-excursion
  16084. (while cmds
  16085. (setq cmd (pop cmds)
  16086. thiscmdkey (car cmd)
  16087. opts (nth 3 cmd)
  16088. files (nth 4 cmd))
  16089. (if (stringp files) (setq files (list files)))
  16090. (when files
  16091. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  16092. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  16093. '(org-agenda nil))))
  16094. (set-buffer org-agenda-buffer-name)
  16095. (while files
  16096. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  16097. (list 'org-write-agenda
  16098. (expand-file-name (pop files) dir) t))))
  16099. (and (get-buffer org-agenda-buffer-name)
  16100. (kill-buffer org-agenda-buffer-name)))))))
  16101. (defun org-write-agenda (file &optional nosettings)
  16102. "Write the current buffer (an agenda view) as a file.
  16103. Depending on the extension of the file name, plain text (.txt),
  16104. HTML (.html or .htm) or Postscript (.ps) is produced.
  16105. If NOSETTINGS is given, do not scope the settings of
  16106. `org-agenda-exporter-settings' into the export commands. This is used when
  16107. the settings have already been scoped and we do not wish to overrule other,
  16108. higher priority settings."
  16109. (interactive "FWrite agenda to file: ")
  16110. (if (not (file-writable-p file))
  16111. (error "Cannot write agenda to file %s" file))
  16112. (cond
  16113. ((string-match "\\.html?\\'" file) (require 'htmlize))
  16114. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  16115. (org-let (if nosettings nil org-agenda-exporter-settings)
  16116. '(save-excursion
  16117. (save-window-excursion
  16118. (cond
  16119. ((string-match "\\.html?\\'" file)
  16120. (set-buffer (htmlize-buffer (current-buffer)))
  16121. (when (and org-agenda-export-html-style
  16122. (string-match "<style>" org-agenda-export-html-style))
  16123. ;; replace <style> section with org-agenda-export-html-style
  16124. (goto-char (point-min))
  16125. (kill-region (- (search-forward "<style") 6)
  16126. (search-forward "</style>"))
  16127. (insert org-agenda-export-html-style))
  16128. (write-file file)
  16129. (kill-buffer (current-buffer))
  16130. (message "HTML written to %s" file))
  16131. ((string-match "\\.ps\\'" file)
  16132. (ps-print-buffer-with-faces file)
  16133. (message "Postscript written to %s" file))
  16134. (t
  16135. (let ((bs (buffer-string)))
  16136. (find-file file)
  16137. (insert bs)
  16138. (save-buffer 0)
  16139. (kill-buffer (current-buffer))
  16140. (message "Plain text written to %s" file))))))
  16141. (set-buffer org-agenda-buffer-name)))
  16142. (defmacro org-no-read-only (&rest body)
  16143. "Inhibit read-only for BODY."
  16144. `(let ((inhibit-read-only t)) ,@body))
  16145. (defun org-check-for-org-mode ()
  16146. "Make sure current buffer is in org-mode. Error if not."
  16147. (or (org-mode-p)
  16148. (error "Cannot execute org-mode agenda command on buffer in %s."
  16149. major-mode)))
  16150. (defun org-fit-agenda-window ()
  16151. "Fit the window to the buffer size."
  16152. (and (memq org-agenda-window-setup '(reorganize-frame))
  16153. (fboundp 'fit-window-to-buffer)
  16154. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  16155. (/ (frame-height) 2))))
  16156. ;;; Agenda file list
  16157. (defun org-agenda-files (&optional unrestricted)
  16158. "Get the list of agenda files.
  16159. Optional UNRESTRICTED means return the full list even if a restriction
  16160. is currently in place."
  16161. (let ((files
  16162. (cond
  16163. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16164. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16165. ((listp org-agenda-files) org-agenda-files)
  16166. (t (error "Invalid value of `org-agenda-files'")))))
  16167. (if org-agenda-skip-unavailable-files
  16168. (delq nil
  16169. (mapcar (function
  16170. (lambda (file)
  16171. (and (file-readable-p file) file)))
  16172. files))
  16173. files))) ; `org-check-agenda-file' will remove them from the list
  16174. (defun org-edit-agenda-file-list ()
  16175. "Edit the list of agenda files.
  16176. Depending on setup, this either uses customize to edit the variable
  16177. `org-agenda-files', or it visits the file that is holding the list. In the
  16178. latter case, the buffer is set up in a way that saving it automatically kills
  16179. the buffer and restores the previous window configuration."
  16180. (interactive)
  16181. (if (stringp org-agenda-files)
  16182. (let ((cw (current-window-configuration)))
  16183. (find-file org-agenda-files)
  16184. (org-set-local 'org-window-configuration cw)
  16185. (org-add-hook 'after-save-hook
  16186. (lambda ()
  16187. (set-window-configuration
  16188. (prog1 org-window-configuration
  16189. (kill-buffer (current-buffer))))
  16190. (org-install-agenda-files-menu)
  16191. (message "New agenda file list installed"))
  16192. nil 'local)
  16193. (message (substitute-command-keys
  16194. "Edit list and finish with \\[save-buffer]")))
  16195. (customize-variable 'org-agenda-files)))
  16196. (defun org-store-new-agenda-file-list (list)
  16197. "Set new value for the agenda file list and save it correcly."
  16198. (if (stringp org-agenda-files)
  16199. (let ((f org-agenda-files) b)
  16200. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  16201. (with-temp-file f
  16202. (insert (mapconcat 'identity list "\n") "\n")))
  16203. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  16204. (setq org-agenda-files list)
  16205. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16206. (defun org-read-agenda-file-list ()
  16207. "Read the list of agenda files from a file."
  16208. (when (stringp org-agenda-files)
  16209. (with-temp-buffer
  16210. (insert-file-contents org-agenda-files)
  16211. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  16212. ;;;###autoload
  16213. (defun org-cycle-agenda-files ()
  16214. "Cycle through the files in `org-agenda-files'.
  16215. If the current buffer visits an agenda file, find the next one in the list.
  16216. If the current buffer does not, find the first agenda file."
  16217. (interactive)
  16218. (let* ((fs (org-agenda-files t))
  16219. (files (append fs (list (car fs))))
  16220. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16221. file)
  16222. (unless files (error "No agenda files"))
  16223. (catch 'exit
  16224. (while (setq file (pop files))
  16225. (if (equal (file-truename file) tcf)
  16226. (when (car files)
  16227. (find-file (car files))
  16228. (throw 'exit t))))
  16229. (find-file (car fs)))
  16230. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  16231. (defun org-agenda-file-to-front (&optional to-end)
  16232. "Move/add the current file to the top of the agenda file list.
  16233. If the file is not present in the list, it is added to the front. If it is
  16234. present, it is moved there. With optional argument TO-END, add/move to the
  16235. end of the list."
  16236. (interactive "P")
  16237. (let ((org-agenda-skip-unavailable-files nil)
  16238. (file-alist (mapcar (lambda (x)
  16239. (cons (file-truename x) x))
  16240. (org-agenda-files t)))
  16241. (ctf (file-truename buffer-file-name))
  16242. x had)
  16243. (setq x (assoc ctf file-alist) had x)
  16244. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16245. (if to-end
  16246. (setq file-alist (append (delq x file-alist) (list x)))
  16247. (setq file-alist (cons x (delq x file-alist))))
  16248. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16249. (org-install-agenda-files-menu)
  16250. (message "File %s to %s of agenda file list"
  16251. (if had "moved" "added") (if to-end "end" "front"))))
  16252. (defun org-remove-file (&optional file)
  16253. "Remove current file from the list of files in variable `org-agenda-files'.
  16254. These are the files which are being checked for agenda entries.
  16255. Optional argument FILE means, use this file instead of the current."
  16256. (interactive)
  16257. (let* ((org-agenda-skip-unavailable-files nil)
  16258. (file (or file buffer-file-name))
  16259. (true-file (file-truename file))
  16260. (afile (abbreviate-file-name file))
  16261. (files (delq nil (mapcar
  16262. (lambda (x)
  16263. (if (equal true-file
  16264. (file-truename x))
  16265. nil x))
  16266. (org-agenda-files t)))))
  16267. (if (not (= (length files) (length (org-agenda-files t))))
  16268. (progn
  16269. (org-store-new-agenda-file-list files)
  16270. (org-install-agenda-files-menu)
  16271. (message "Removed file: %s" afile))
  16272. (message "File was not in list: %s" afile))))
  16273. (defun org-file-menu-entry (file)
  16274. (vector file (list 'find-file file) t))
  16275. (defun org-check-agenda-file (file)
  16276. "Make sure FILE exists. If not, ask user what to do."
  16277. (when (not (file-exists-p file))
  16278. (message "non-existent file %s. [R]emove from list or [A]bort?"
  16279. (abbreviate-file-name file))
  16280. (let ((r (downcase (read-char-exclusive))))
  16281. (cond
  16282. ((equal r ?r)
  16283. (org-remove-file file)
  16284. (throw 'nextfile t))
  16285. (t (error "Abort"))))))
  16286. ;;; Agenda prepare and finalize
  16287. (defvar org-agenda-multi nil) ; dynammically scoped
  16288. (defvar org-agenda-buffer-name "*Org Agenda*")
  16289. (defvar org-pre-agenda-window-conf nil)
  16290. (defvar org-agenda-name nil)
  16291. (defun org-prepare-agenda (&optional name)
  16292. (setq org-todo-keywords-for-agenda nil)
  16293. (setq org-done-keywords-for-agenda nil)
  16294. (if org-agenda-multi
  16295. (progn
  16296. (setq buffer-read-only nil)
  16297. (goto-char (point-max))
  16298. (unless (bobp)
  16299. (insert "\n" (make-string (window-width) ?=) "\n"))
  16300. (narrow-to-region (point) (point-max)))
  16301. (org-agenda-maybe-reset-markers 'force)
  16302. (org-prepare-agenda-buffers (org-agenda-files))
  16303. (setq org-todo-keywords-for-agenda
  16304. (org-uniquify org-todo-keywords-for-agenda))
  16305. (setq org-done-keywords-for-agenda
  16306. (org-uniquify org-done-keywords-for-agenda))
  16307. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  16308. (awin (get-buffer-window abuf)))
  16309. (cond
  16310. ((equal (current-buffer) abuf) nil)
  16311. (awin (select-window awin))
  16312. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  16313. ((equal org-agenda-window-setup 'current-window)
  16314. (switch-to-buffer abuf))
  16315. ((equal org-agenda-window-setup 'other-window)
  16316. (org-switch-to-buffer-other-window abuf))
  16317. ((equal org-agenda-window-setup 'other-frame)
  16318. (switch-to-buffer-other-frame abuf))
  16319. ((equal org-agenda-window-setup 'reorganize-frame)
  16320. (delete-other-windows)
  16321. (org-switch-to-buffer-other-window abuf))))
  16322. (setq buffer-read-only nil)
  16323. (erase-buffer)
  16324. (org-agenda-mode)
  16325. (and name (not org-agenda-name)
  16326. (org-set-local 'org-agenda-name name)))
  16327. (setq buffer-read-only nil))
  16328. (defun org-finalize-agenda ()
  16329. "Finishing touch for the agenda buffer, called just before displaying it."
  16330. (unless org-agenda-multi
  16331. (save-excursion
  16332. (let ((inhibit-read-only t))
  16333. (goto-char (point-min))
  16334. (while (org-activate-bracket-links (point-max))
  16335. (add-text-properties (match-beginning 0) (match-end 0)
  16336. '(face org-link)))
  16337. (org-agenda-align-tags)
  16338. (unless org-agenda-with-colors
  16339. (remove-text-properties (point-min) (point-max) '(face nil))))
  16340. (if (and (boundp 'org-overriding-columns-format)
  16341. org-overriding-columns-format)
  16342. (org-set-local 'org-overriding-columns-format
  16343. org-overriding-columns-format))
  16344. (if (and (boundp 'org-agenda-view-columns-initially)
  16345. org-agenda-view-columns-initially)
  16346. (org-agenda-columns))
  16347. (run-hooks 'org-finalize-agenda-hook))))
  16348. (defun org-prepare-agenda-buffers (files)
  16349. "Create buffers for all agenda files, protect archived trees and comments."
  16350. (interactive)
  16351. (let ((pa '(:org-archived t))
  16352. (pc '(:org-comment t))
  16353. (pall '(:org-archived t :org-comment t))
  16354. (inhibit-read-only t)
  16355. (rea (concat ":" org-archive-tag ":"))
  16356. bmp file re)
  16357. (save-excursion
  16358. (save-restriction
  16359. (while (setq file (pop files))
  16360. (org-check-agenda-file file)
  16361. (set-buffer (org-get-agenda-file-buffer file))
  16362. (widen)
  16363. (setq bmp (buffer-modified-p))
  16364. (org-refresh-category-properties)
  16365. (setq org-todo-keywords-for-agenda
  16366. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16367. (setq org-done-keywords-for-agenda
  16368. (append org-done-keywords-for-agenda org-done-keywords))
  16369. (save-excursion
  16370. (remove-text-properties (point-min) (point-max) pall)
  16371. (when org-agenda-skip-archived-trees
  16372. (goto-char (point-min))
  16373. (while (re-search-forward rea nil t)
  16374. (if (org-on-heading-p t)
  16375. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16376. (goto-char (point-min))
  16377. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  16378. (while (re-search-forward re nil t)
  16379. (add-text-properties
  16380. (match-beginning 0) (org-end-of-subtree t) pc)))
  16381. (set-buffer-modified-p bmp))))))
  16382. (defvar org-agenda-skip-function nil
  16383. "Function to be called at each match during agenda construction.
  16384. If this function returns nil, the current match should not be skipped.
  16385. Otherwise, the function must return a position from where the search
  16386. should be continued.
  16387. This may also be a Lisp form, it will be evaluated.
  16388. Never set this variable using `setq' or so, because then it will apply
  16389. to all future agenda commands. Instead, bind it with `let' to scope
  16390. it dynamically into the agenda-constructing command. A good way to set
  16391. it is through options in org-agenda-custom-commands.")
  16392. (defun org-agenda-skip ()
  16393. "Throw to `:skip' in places that should be skipped.
  16394. Also moves point to the end of the skipped region, so that search can
  16395. continue from there."
  16396. (let ((p (point-at-bol)) to fp)
  16397. (and org-agenda-skip-archived-trees
  16398. (get-text-property p :org-archived)
  16399. (org-end-of-subtree t)
  16400. (throw :skip t))
  16401. (and (get-text-property p :org-comment)
  16402. (org-end-of-subtree t)
  16403. (throw :skip t))
  16404. (if (equal (char-after p) ?#) (throw :skip t))
  16405. (when (and (or (setq fp (functionp org-agenda-skip-function))
  16406. (consp org-agenda-skip-function))
  16407. (setq to (save-excursion
  16408. (save-match-data
  16409. (if fp
  16410. (funcall org-agenda-skip-function)
  16411. (eval org-agenda-skip-function))))))
  16412. (goto-char to)
  16413. (throw :skip t))))
  16414. (defvar org-agenda-markers nil
  16415. "List of all currently active markers created by `org-agenda'.")
  16416. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  16417. "Creation time of the last agenda marker.")
  16418. (defun org-agenda-new-marker (&optional pos)
  16419. "Return a new agenda marker.
  16420. Org-mode keeps a list of these markers and resets them when they are
  16421. no longer in use."
  16422. (let ((m (copy-marker (or pos (point)))))
  16423. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  16424. (push m org-agenda-markers)
  16425. m))
  16426. (defun org-agenda-maybe-reset-markers (&optional force)
  16427. "Reset markers created by `org-agenda'. But only if they are old enough."
  16428. (if (or (and force (not org-agenda-multi))
  16429. (> (- (time-to-seconds (current-time))
  16430. org-agenda-last-marker-time)
  16431. 5))
  16432. (while org-agenda-markers
  16433. (move-marker (pop org-agenda-markers) nil))))
  16434. (defvar org-agenda-new-buffers nil
  16435. "Buffers created to visit agenda files.")
  16436. (defun org-get-agenda-file-buffer (file)
  16437. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16438. it to the list of buffers which might be released later."
  16439. (let ((buf (org-find-base-buffer-visiting file)))
  16440. (if buf
  16441. buf ; just return it
  16442. ;; Make a new buffer and remember it
  16443. (setq buf (find-file-noselect file))
  16444. (if buf (push buf org-agenda-new-buffers))
  16445. buf)))
  16446. (defun org-release-buffers (blist)
  16447. "Release all buffers in list, asking the user for confirmation when needed.
  16448. When a buffer is unmodified, it is just killed. When modified, it is saved
  16449. \(if the user agrees) and then killed."
  16450. (let (buf file)
  16451. (while (setq buf (pop blist))
  16452. (setq file (buffer-file-name buf))
  16453. (when (and (buffer-modified-p buf)
  16454. file
  16455. (y-or-n-p (format "Save file %s? " file)))
  16456. (with-current-buffer buf (save-buffer)))
  16457. (kill-buffer buf))))
  16458. (defun org-get-category (&optional pos)
  16459. "Get the category applying to position POS."
  16460. (get-text-property (or pos (point)) 'org-category))
  16461. ;;; Agenda timeline
  16462. (defun org-timeline (&optional include-all)
  16463. "Show a time-sorted view of the entries in the current org file.
  16464. Only entries with a time stamp of today or later will be listed. With
  16465. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  16466. under the current date.
  16467. If the buffer contains an active region, only check the region for
  16468. dates."
  16469. (interactive "P")
  16470. (require 'calendar)
  16471. (org-compile-prefix-format 'timeline)
  16472. (org-set-sorting-strategy 'timeline)
  16473. (let* ((dopast t)
  16474. (dotodo include-all)
  16475. (doclosed org-agenda-show-log)
  16476. (entry buffer-file-name)
  16477. (date (calendar-current-date))
  16478. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  16479. (end (if (org-region-active-p) (region-end) (point-max)))
  16480. (day-numbers (org-get-all-dates beg end 'no-ranges
  16481. t doclosed ; always include today
  16482. org-timeline-show-empty-dates))
  16483. (today (time-to-days (current-time)))
  16484. (past t)
  16485. args
  16486. s e rtn d emptyp)
  16487. (setq org-agenda-redo-command
  16488. (list 'progn
  16489. (list 'org-switch-to-buffer-other-window (current-buffer))
  16490. (list 'org-timeline (list 'quote include-all))))
  16491. (if (not dopast)
  16492. ;; Remove past dates from the list of dates.
  16493. (setq day-numbers (delq nil (mapcar (lambda(x)
  16494. (if (>= x today) x nil))
  16495. day-numbers))))
  16496. (org-prepare-agenda (concat "Timeline "
  16497. (file-name-nondirectory buffer-file-name)))
  16498. (if doclosed (push :closed args))
  16499. (push :timestamp args)
  16500. (push :sexp args)
  16501. (if dotodo (push :todo args))
  16502. (while (setq d (pop day-numbers))
  16503. (if (and (listp d) (eq (car d) :omitted))
  16504. (progn
  16505. (setq s (point))
  16506. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  16507. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  16508. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  16509. (if (and (>= d today)
  16510. dopast
  16511. past)
  16512. (progn
  16513. (setq past nil)
  16514. (insert (make-string 79 ?-) "\n")))
  16515. (setq date (calendar-gregorian-from-absolute d))
  16516. (setq s (point))
  16517. (setq rtn (and (not emptyp)
  16518. (apply 'org-agenda-get-day-entries entry
  16519. date args)))
  16520. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  16521. (progn
  16522. (insert
  16523. (if (stringp org-agenda-format-date)
  16524. (format-time-string org-agenda-format-date
  16525. (org-time-from-absolute date))
  16526. (funcall org-agenda-format-date date))
  16527. "\n")
  16528. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16529. (put-text-property s (1- (point)) 'org-date-line t)
  16530. (if (equal d today)
  16531. (put-text-property s (1- (point)) 'org-today t))
  16532. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  16533. (put-text-property s (1- (point)) 'day d)))))
  16534. (goto-char (point-min))
  16535. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  16536. (point-min)))
  16537. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  16538. (org-finalize-agenda)
  16539. (setq buffer-read-only t)))
  16540. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  16541. "Return a list of all relevant day numbers from BEG to END buffer positions.
  16542. If NO-RANGES is non-nil, include only the start and end dates of a range,
  16543. not every single day in the range. If FORCE-TODAY is non-nil, make
  16544. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  16545. inactive time stamps (those in square brackets) are included.
  16546. When EMPTY is non-nil, also include days without any entries."
  16547. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  16548. dates dates1 date day day1 day2 ts1 ts2)
  16549. (if force-today
  16550. (setq dates (list (time-to-days (current-time)))))
  16551. (save-excursion
  16552. (goto-char beg)
  16553. (while (re-search-forward re end t)
  16554. (setq day (time-to-days (org-time-string-to-time
  16555. (substring (match-string 1) 0 10))))
  16556. (or (memq day dates) (push day dates)))
  16557. (unless no-ranges
  16558. (goto-char beg)
  16559. (while (re-search-forward org-tr-regexp end t)
  16560. (setq ts1 (substring (match-string 1) 0 10)
  16561. ts2 (substring (match-string 2) 0 10)
  16562. day1 (time-to-days (org-time-string-to-time ts1))
  16563. day2 (time-to-days (org-time-string-to-time ts2)))
  16564. (while (< (setq day1 (1+ day1)) day2)
  16565. (or (memq day1 dates) (push day1 dates)))))
  16566. (setq dates (sort dates '<))
  16567. (when empty
  16568. (while (setq day (pop dates))
  16569. (setq day2 (car dates))
  16570. (push day dates1)
  16571. (when (and day2 empty)
  16572. (if (or (eq empty t)
  16573. (and (numberp empty) (<= (- day2 day) empty)))
  16574. (while (< (setq day (1+ day)) day2)
  16575. (push (list day) dates1))
  16576. (push (cons :omitted (- day2 day)) dates1))))
  16577. (setq dates (nreverse dates1)))
  16578. dates)))
  16579. ;;; Agenda Daily/Weekly
  16580. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  16581. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  16582. (defvar org-agenda-last-arguments nil
  16583. "The arguments of the previous call to org-agenda")
  16584. (defvar org-starting-day nil) ; local variable in the agenda buffer
  16585. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  16586. (defvar org-include-all-loc nil) ; local variable
  16587. (defvar org-agenda-remove-date nil) ; dynamically scoped
  16588. ;;;###autoload
  16589. (defun org-agenda-list (&optional include-all start-day ndays)
  16590. "Produce a weekly view from all files in variable `org-agenda-files'.
  16591. The view will be for the current week, but from the overview buffer you
  16592. will be able to go to other weeks.
  16593. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  16594. also be shown, under the current date.
  16595. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  16596. on the days are also shown. See the variable `org-log-done' for how
  16597. to turn on logging.
  16598. START-DAY defaults to TODAY, or to the most recent match for the weekday
  16599. given in `org-agenda-start-on-weekday'.
  16600. NDAYS defaults to `org-agenda-ndays'."
  16601. (interactive "P")
  16602. (setq ndays (or ndays org-agenda-ndays)
  16603. start-day (or start-day org-agenda-start-day))
  16604. (if org-agenda-overriding-arguments
  16605. (setq include-all (car org-agenda-overriding-arguments)
  16606. start-day (nth 1 org-agenda-overriding-arguments)
  16607. ndays (nth 2 org-agenda-overriding-arguments)))
  16608. (if (stringp start-day)
  16609. ;; Convert to an absolute day number
  16610. (setq start-day (time-to-days (org-read-date nil t start-day))))
  16611. (setq org-agenda-last-arguments (list include-all start-day ndays))
  16612. (org-compile-prefix-format 'agenda)
  16613. (org-set-sorting-strategy 'agenda)
  16614. (require 'calendar)
  16615. (let* ((org-agenda-start-on-weekday
  16616. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  16617. org-agenda-start-on-weekday nil))
  16618. (thefiles (org-agenda-files))
  16619. (files thefiles)
  16620. (today (time-to-days (current-time)))
  16621. (sd (or start-day today))
  16622. (start (if (or (null org-agenda-start-on-weekday)
  16623. (< org-agenda-ndays 7))
  16624. sd
  16625. (let* ((nt (calendar-day-of-week
  16626. (calendar-gregorian-from-absolute sd)))
  16627. (n1 org-agenda-start-on-weekday)
  16628. (d (- nt n1)))
  16629. (- sd (+ (if (< d 0) 7 0) d)))))
  16630. (day-numbers (list start))
  16631. (inhibit-redisplay (not debug-on-error))
  16632. s e rtn rtnall file date d start-pos end-pos todayp nd)
  16633. (setq org-agenda-redo-command
  16634. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  16635. ;; Make the list of days
  16636. (setq ndays (or ndays org-agenda-ndays)
  16637. nd ndays)
  16638. (while (> ndays 1)
  16639. (push (1+ (car day-numbers)) day-numbers)
  16640. (setq ndays (1- ndays)))
  16641. (setq day-numbers (nreverse day-numbers))
  16642. (org-prepare-agenda "Day/Week")
  16643. (org-set-local 'org-starting-day (car day-numbers))
  16644. (org-set-local 'org-include-all-loc include-all)
  16645. (org-set-local 'org-agenda-span
  16646. (org-agenda-ndays-to-span nd))
  16647. (when (and (or include-all org-agenda-include-all-todo)
  16648. (member today day-numbers))
  16649. (setq files thefiles
  16650. rtnall nil)
  16651. (while (setq file (pop files))
  16652. (catch 'nextfile
  16653. (org-check-agenda-file file)
  16654. (setq date (calendar-gregorian-from-absolute today)
  16655. rtn (org-agenda-get-day-entries
  16656. file date :todo))
  16657. (setq rtnall (append rtnall rtn))))
  16658. (when rtnall
  16659. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  16660. (add-text-properties (point-min) (1- (point))
  16661. (list 'face 'org-agenda-structure))
  16662. (insert (org-finalize-agenda-entries rtnall) "\n")))
  16663. (setq s (point))
  16664. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  16665. "-agenda:\n")
  16666. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  16667. 'org-date-line t))
  16668. (while (setq d (pop day-numbers))
  16669. (setq date (calendar-gregorian-from-absolute d)
  16670. s (point))
  16671. (if (or (setq todayp (= d today))
  16672. (and (not start-pos) (= d sd)))
  16673. (setq start-pos (point))
  16674. (if (and start-pos (not end-pos))
  16675. (setq end-pos (point))))
  16676. (setq files thefiles
  16677. rtnall nil)
  16678. (while (setq file (pop files))
  16679. (catch 'nextfile
  16680. (org-check-agenda-file file)
  16681. (if org-agenda-show-log
  16682. (setq rtn (org-agenda-get-day-entries
  16683. file date
  16684. :deadline :scheduled :timestamp :sexp :closed))
  16685. (setq rtn (org-agenda-get-day-entries
  16686. file date
  16687. :deadline :scheduled :sexp :timestamp)))
  16688. (setq rtnall (append rtnall rtn))))
  16689. (if org-agenda-include-diary
  16690. (progn
  16691. (require 'diary-lib)
  16692. (setq rtn (org-get-entries-from-diary date))
  16693. (setq rtnall (append rtnall rtn))))
  16694. (if (or rtnall org-agenda-show-all-dates)
  16695. (progn
  16696. (insert
  16697. (if (stringp org-agenda-format-date)
  16698. (format-time-string org-agenda-format-date
  16699. (org-time-from-absolute date))
  16700. (funcall org-agenda-format-date date))
  16701. "\n")
  16702. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16703. (put-text-property s (1- (point)) 'org-date-line t)
  16704. (if todayp (put-text-property s (1- (point)) 'org-today t))
  16705. (if rtnall (insert
  16706. (org-finalize-agenda-entries
  16707. (org-agenda-add-time-grid-maybe
  16708. rtnall nd todayp))
  16709. "\n"))
  16710. (put-text-property s (1- (point)) 'day d))))
  16711. (goto-char (point-min))
  16712. (org-fit-agenda-window)
  16713. (unless (and (pos-visible-in-window-p (point-min))
  16714. (pos-visible-in-window-p (point-max)))
  16715. (goto-char (1- (point-max)))
  16716. (recenter -1)
  16717. (if (not (pos-visible-in-window-p (or start-pos 1)))
  16718. (progn
  16719. (goto-char (or start-pos 1))
  16720. (recenter 1))))
  16721. (goto-char (or start-pos 1))
  16722. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  16723. (org-finalize-agenda)
  16724. (setq buffer-read-only t)
  16725. (message "")))
  16726. (defun org-agenda-ndays-to-span (n)
  16727. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  16728. ;;; Agenda TODO list
  16729. (defvar org-select-this-todo-keyword nil)
  16730. (defvar org-last-arg nil)
  16731. ;;;###autoload
  16732. (defun org-todo-list (arg)
  16733. "Show all TODO entries from all agenda file in a single list.
  16734. The prefix arg can be used to select a specific TODO keyword and limit
  16735. the list to these. When using \\[universal-argument], you will be prompted
  16736. for a keyword. A numeric prefix directly selects the Nth keyword in
  16737. `org-todo-keywords-1'."
  16738. (interactive "P")
  16739. (require 'calendar)
  16740. (org-compile-prefix-format 'todo)
  16741. (org-set-sorting-strategy 'todo)
  16742. (org-prepare-agenda "TODO")
  16743. (let* ((today (time-to-days (current-time)))
  16744. (date (calendar-gregorian-from-absolute today))
  16745. (kwds org-todo-keywords-for-agenda)
  16746. (completion-ignore-case t)
  16747. (org-select-this-todo-keyword
  16748. (if (stringp arg) arg
  16749. (and arg (integerp arg) (> arg 0)
  16750. (nth (1- arg) kwds))))
  16751. rtn rtnall files file pos)
  16752. (when (equal arg '(4))
  16753. (setq org-select-this-todo-keyword
  16754. (completing-read "Keyword (or KWD1|K2D2|...): "
  16755. (mapcar 'list kwds) nil nil)))
  16756. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  16757. (org-set-local 'org-last-arg arg)
  16758. (setq org-agenda-redo-command
  16759. '(org-todo-list (or current-prefix-arg org-last-arg)))
  16760. (setq files (org-agenda-files)
  16761. rtnall nil)
  16762. (while (setq file (pop files))
  16763. (catch 'nextfile
  16764. (org-check-agenda-file file)
  16765. (setq rtn (org-agenda-get-day-entries file date :todo))
  16766. (setq rtnall (append rtnall rtn))))
  16767. (if org-agenda-overriding-header
  16768. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16769. nil 'face 'org-agenda-structure) "\n")
  16770. (insert "Global list of TODO items of type: ")
  16771. (add-text-properties (point-min) (1- (point))
  16772. (list 'face 'org-agenda-structure))
  16773. (setq pos (point))
  16774. (insert (or org-select-this-todo-keyword "ALL") "\n")
  16775. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16776. (setq pos (point))
  16777. (unless org-agenda-multi
  16778. (insert "Available with `N r': (0)ALL")
  16779. (let ((n 0) s)
  16780. (mapc (lambda (x)
  16781. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  16782. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  16783. (insert "\n "))
  16784. (insert " " s))
  16785. kwds))
  16786. (insert "\n"))
  16787. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16788. (when rtnall
  16789. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16790. (goto-char (point-min))
  16791. (org-fit-agenda-window)
  16792. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  16793. (org-finalize-agenda)
  16794. (setq buffer-read-only t)))
  16795. ;;; Agenda tags match
  16796. ;;;###autoload
  16797. (defun org-tags-view (&optional todo-only match)
  16798. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  16799. The prefix arg TODO-ONLY limits the search to TODO entries."
  16800. (interactive "P")
  16801. (org-compile-prefix-format 'tags)
  16802. (org-set-sorting-strategy 'tags)
  16803. (let* ((org-tags-match-list-sublevels
  16804. (if todo-only t org-tags-match-list-sublevels))
  16805. (completion-ignore-case t)
  16806. rtn rtnall files file pos matcher
  16807. buffer)
  16808. (setq matcher (org-make-tags-matcher match)
  16809. match (car matcher) matcher (cdr matcher))
  16810. (org-prepare-agenda (concat "TAGS " match))
  16811. (setq org-agenda-redo-command
  16812. (list 'org-tags-view (list 'quote todo-only)
  16813. (list 'if 'current-prefix-arg nil match)))
  16814. (setq files (org-agenda-files)
  16815. rtnall nil)
  16816. (while (setq file (pop files))
  16817. (catch 'nextfile
  16818. (org-check-agenda-file file)
  16819. (setq buffer (if (file-exists-p file)
  16820. (org-get-agenda-file-buffer file)
  16821. (error "No such file %s" file)))
  16822. (if (not buffer)
  16823. ;; If file does not exist, merror message to agenda
  16824. (setq rtn (list
  16825. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16826. rtnall (append rtnall rtn))
  16827. (with-current-buffer buffer
  16828. (unless (org-mode-p)
  16829. (error "Agenda file %s is not in `org-mode'" file))
  16830. (save-excursion
  16831. (save-restriction
  16832. (if org-agenda-restrict
  16833. (narrow-to-region org-agenda-restrict-begin
  16834. org-agenda-restrict-end)
  16835. (widen))
  16836. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  16837. (setq rtnall (append rtnall rtn))))))))
  16838. (if org-agenda-overriding-header
  16839. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16840. nil 'face 'org-agenda-structure) "\n")
  16841. (insert "Headlines with TAGS match: ")
  16842. (add-text-properties (point-min) (1- (point))
  16843. (list 'face 'org-agenda-structure))
  16844. (setq pos (point))
  16845. (insert match "\n")
  16846. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16847. (setq pos (point))
  16848. (unless org-agenda-multi
  16849. (insert "Press `C-u r' to search again with new search string\n"))
  16850. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16851. (when rtnall
  16852. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16853. (goto-char (point-min))
  16854. (org-fit-agenda-window)
  16855. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  16856. (org-finalize-agenda)
  16857. (setq buffer-read-only t)))
  16858. ;;; Agenda Finding stuck projects
  16859. (defvar org-agenda-skip-regexp nil
  16860. "Regular expression used in skipping subtrees for the agenda.
  16861. This is basically a temporary global variable that can be set and then
  16862. used by user-defined selections using `org-agenda-skip-function'.")
  16863. (defvar org-agenda-overriding-header nil
  16864. "When this is set during todo and tags searches, will replace header.")
  16865. (defun org-agenda-skip-subtree-when-regexp-matches ()
  16866. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  16867. If yes, it returns the end position of this tree, causing agenda commands
  16868. to skip this subtree. This is a function that can be put into
  16869. `org-agenda-skip-function' for the duration of a command."
  16870. (let ((end (save-excursion (org-end-of-subtree t)))
  16871. skip)
  16872. (save-excursion
  16873. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  16874. (and skip end)))
  16875. (defun org-agenda-skip-entry-if (&rest conditions)
  16876. "Skip entry is any of CONDITIONS is true.
  16877. See `org-agenda-skip-if for details."
  16878. (org-agenda-skip-if nil conditions))
  16879. (defun org-agenda-skip-subtree-if (&rest conditions)
  16880. "Skip entry is any of CONDITIONS is true.
  16881. See `org-agenda-skip-if for details."
  16882. (org-agenda-skip-if t conditions))
  16883. (defun org-agenda-skip-if (subtree conditions)
  16884. "Checks current entity for CONDITIONS.
  16885. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  16886. the entry, i.e. the text before the next heading is checked.
  16887. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  16888. from different tests. Valid conditions are:
  16889. scheduled Check if there is a scheduled cookie
  16890. notscheduled Check if there is no scheduled cookie
  16891. deadline Check if there is a deadline
  16892. notdeadline Check if there is no deadline
  16893. regexp Check if regexp matches
  16894. notregexp Check if regexp does not match.
  16895. The regexp is taken from the conditions list, it must com right after the
  16896. `regexp' of `notregexp' element.
  16897. If any of these conditions is met, this function returns the end point of
  16898. the entity, causing the search to continue from there. This is a function
  16899. that can be put into `org-agenda-skip-function' for the duration of a command."
  16900. (let (beg end m r)
  16901. (org-back-to-heading t)
  16902. (setq beg (point)
  16903. end (if subtree
  16904. (progn (org-end-of-subtree t) (point))
  16905. (progn (outline-next-heading) (1- (point)))))
  16906. (goto-char beg)
  16907. (and
  16908. (or
  16909. (and (memq 'scheduled conditions)
  16910. (re-search-forward org-scheduled-time-regexp end t))
  16911. (and (memq 'notscheduled conditions)
  16912. (not (re-search-forward org-scheduled-time-regexp end t)))
  16913. (and (memq 'deadline conditions)
  16914. (re-search-forward org-deadline-time-regexp end t))
  16915. (and (memq 'notdeadline conditions)
  16916. (not (re-search-forward org-deadline-time-regexp end t)))
  16917. (and (setq m (memq 'regexp conditions))
  16918. (stringp (setq r (nth 1 m)))
  16919. (re-search-forward (nth 1 m) end t))
  16920. (and (setq m (memq 'notregexp conditions))
  16921. (stringp (setq r (nth 1 m)))
  16922. (not (re-search-forward (nth 1 m) end t))))
  16923. end)))
  16924. (defun org-agenda-list-stuck-projects (&rest ignore)
  16925. "Create agenda view for projects that are stuck.
  16926. Stuck projects are project that have no next actions. For the definitions
  16927. of what a project is and how to check if it stuck, customize the variable
  16928. `org-stuck-projects'.
  16929. MATCH is being ignored."
  16930. (interactive)
  16931. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  16932. ;; FIXME: we could have used org-agenda-skip-if here.
  16933. (org-agenda-overriding-header "List of stuck projects: ")
  16934. (matcher (nth 0 org-stuck-projects))
  16935. (todo (nth 1 org-stuck-projects))
  16936. (todo-wds (if (member "*" todo)
  16937. (progn
  16938. (org-prepare-agenda-buffers (org-agenda-files))
  16939. (org-delete-all
  16940. org-done-keywords-for-agenda
  16941. (copy-sequence org-todo-keywords-for-agenda)))
  16942. todo))
  16943. (todo-re (concat "^\\*+[ \t]+\\("
  16944. (mapconcat 'identity todo-wds "\\|")
  16945. "\\)\\>"))
  16946. (tags (nth 2 org-stuck-projects))
  16947. (tags-re (if (member "*" tags)
  16948. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  16949. (concat "^\\*+ .*:\\("
  16950. (mapconcat 'identity tags "\\|")
  16951. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  16952. (gen-re (nth 3 org-stuck-projects))
  16953. (re-list
  16954. (delq nil
  16955. (list
  16956. (if todo todo-re)
  16957. (if tags tags-re)
  16958. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  16959. gen-re)))))
  16960. (setq org-agenda-skip-regexp
  16961. (if re-list
  16962. (mapconcat 'identity re-list "\\|")
  16963. (error "No information how to identify unstuck projects")))
  16964. (org-tags-view nil matcher)
  16965. (with-current-buffer org-agenda-buffer-name
  16966. (setq org-agenda-redo-command
  16967. '(org-agenda-list-stuck-projects
  16968. (or current-prefix-arg org-last-arg))))))
  16969. ;;; Diary integration
  16970. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  16971. (defun org-get-entries-from-diary (date)
  16972. "Get the (Emacs Calendar) diary entries for DATE."
  16973. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  16974. (diary-display-hook '(fancy-diary-display))
  16975. (pop-up-frames nil)
  16976. (list-diary-entries-hook
  16977. (cons 'org-diary-default-entry list-diary-entries-hook))
  16978. (diary-file-name-prefix-function nil) ; turn this feature off
  16979. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  16980. entries
  16981. (org-disable-agenda-to-diary t))
  16982. (save-excursion
  16983. (save-window-excursion
  16984. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  16985. (if (not (get-buffer fancy-diary-buffer))
  16986. (setq entries nil)
  16987. (with-current-buffer fancy-diary-buffer
  16988. (setq buffer-read-only nil)
  16989. (if (zerop (buffer-size))
  16990. ;; No entries
  16991. (setq entries nil)
  16992. ;; Omit the date and other unnecessary stuff
  16993. (org-agenda-cleanup-fancy-diary)
  16994. ;; Add prefix to each line and extend the text properties
  16995. (if (zerop (buffer-size))
  16996. (setq entries nil)
  16997. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  16998. (set-buffer-modified-p nil)
  16999. (kill-buffer fancy-diary-buffer)))
  17000. (when entries
  17001. (setq entries (org-split-string entries "\n"))
  17002. (setq entries
  17003. (mapcar
  17004. (lambda (x)
  17005. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  17006. ;; Extend the text properties to the beginning of the line
  17007. (org-add-props x (text-properties-at (1- (length x)) x)
  17008. 'type "diary" 'date date))
  17009. entries)))))
  17010. (defun org-agenda-cleanup-fancy-diary ()
  17011. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  17012. This gets rid of the date, the underline under the date, and
  17013. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  17014. date. It also removes lines that contain only whitespace."
  17015. (goto-char (point-min))
  17016. (if (looking-at ".*?:[ \t]*")
  17017. (progn
  17018. (replace-match "")
  17019. (re-search-forward "\n=+$" nil t)
  17020. (replace-match "")
  17021. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  17022. (re-search-forward "\n=+$" nil t)
  17023. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  17024. (goto-char (point-min))
  17025. (while (re-search-forward "^ +\n" nil t)
  17026. (replace-match ""))
  17027. (goto-char (point-min))
  17028. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  17029. (replace-match "")))
  17030. ;; Make sure entries from the diary have the right text properties.
  17031. (eval-after-load "diary-lib"
  17032. '(if (boundp 'diary-modify-entry-list-string-function)
  17033. ;; We can rely on the hook, nothing to do
  17034. nil
  17035. ;; Hook not avaiable, must use advice to make this work
  17036. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  17037. "Make the position visible."
  17038. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  17039. (stringp string)
  17040. buffer-file-name)
  17041. (setq string (org-modify-diary-entry-string string))))))
  17042. (defun org-modify-diary-entry-string (string)
  17043. "Add text properties to string, allowing org-mode to act on it."
  17044. (org-add-props string nil
  17045. 'mouse-face 'highlight
  17046. 'keymap org-agenda-keymap
  17047. 'help-echo (if buffer-file-name
  17048. (format "mouse-2 or RET jump to diary file %s"
  17049. (abbreviate-file-name buffer-file-name))
  17050. "")
  17051. 'org-agenda-diary-link t
  17052. 'org-marker (org-agenda-new-marker (point-at-bol))))
  17053. (defun org-diary-default-entry ()
  17054. "Add a dummy entry to the diary.
  17055. Needed to avoid empty dates which mess up holiday display."
  17056. ;; Catch the error if dealing with the new add-to-diary-alist
  17057. (when org-disable-agenda-to-diary
  17058. (condition-case nil
  17059. (add-to-diary-list original-date "Org-mode dummy" "")
  17060. (error
  17061. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  17062. ;;;###autoload
  17063. (defun org-diary (&rest args)
  17064. "Return diary information from org-files.
  17065. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  17066. It accesses org files and extracts information from those files to be
  17067. listed in the diary. The function accepts arguments specifying what
  17068. items should be listed. The following arguments are allowed:
  17069. :timestamp List the headlines of items containing a date stamp or
  17070. date range matching the selected date. Deadlines will
  17071. also be listed, on the expiration day.
  17072. :sexp FIXME
  17073. :deadline List any deadlines past due, or due within
  17074. `org-deadline-warning-days'. The listing occurs only
  17075. in the diary for *today*, not at any other date. If
  17076. an entry is marked DONE, it is no longer listed.
  17077. :scheduled List all items which are scheduled for the given date.
  17078. The diary for *today* also contains items which were
  17079. scheduled earlier and are not yet marked DONE.
  17080. :todo List all TODO items from the org-file. This may be a
  17081. long list - so this is not turned on by default.
  17082. Like deadlines, these entries only show up in the
  17083. diary for *today*, not at any other date.
  17084. The call in the diary file should look like this:
  17085. &%%(org-diary) ~/path/to/some/orgfile.org
  17086. Use a separate line for each org file to check. Or, if you omit the file name,
  17087. all files listed in `org-agenda-files' will be checked automatically:
  17088. &%%(org-diary)
  17089. If you don't give any arguments (as in the example above), the default
  17090. arguments (:deadline :scheduled :timestamp :sexp) are used.
  17091. So the example above may also be written as
  17092. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  17093. The function expects the lisp variables `entry' and `date' to be provided
  17094. by the caller, because this is how the calendar works. Don't use this
  17095. function from a program - use `org-agenda-get-day-entries' instead."
  17096. (org-agenda-maybe-reset-markers)
  17097. (org-compile-prefix-format 'agenda)
  17098. (org-set-sorting-strategy 'agenda)
  17099. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  17100. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  17101. (list entry)
  17102. (org-agenda-files t)))
  17103. file rtn results)
  17104. (org-prepare-agenda-buffers files)
  17105. ;; If this is called during org-agenda, don't return any entries to
  17106. ;; the calendar. Org Agenda will list these entries itself.
  17107. (if org-disable-agenda-to-diary (setq files nil))
  17108. (while (setq file (pop files))
  17109. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  17110. (setq results (append results rtn)))
  17111. (if results
  17112. (concat (org-finalize-agenda-entries results) "\n"))))
  17113. ;;; Agenda entry finders
  17114. (defun org-agenda-get-day-entries (file date &rest args)
  17115. "Does the work for `org-diary' and `org-agenda'.
  17116. FILE is the path to a file to be checked for entries. DATE is date like
  17117. the one returned by `calendar-current-date'. ARGS are symbols indicating
  17118. which kind of entries should be extracted. For details about these, see
  17119. the documentation of `org-diary'."
  17120. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  17121. (let* ((org-startup-folded nil)
  17122. (org-startup-align-all-tables nil)
  17123. (buffer (if (file-exists-p file)
  17124. (org-get-agenda-file-buffer file)
  17125. (error "No such file %s" file)))
  17126. arg results rtn)
  17127. (if (not buffer)
  17128. ;; If file does not exist, make sure an error message ends up in diary
  17129. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  17130. (with-current-buffer buffer
  17131. (unless (org-mode-p)
  17132. (error "Agenda file %s is not in `org-mode'" file))
  17133. (let ((case-fold-search nil))
  17134. (save-excursion
  17135. (save-restriction
  17136. (if org-agenda-restrict
  17137. (narrow-to-region org-agenda-restrict-begin
  17138. org-agenda-restrict-end)
  17139. (widen))
  17140. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  17141. (while (setq arg (pop args))
  17142. (cond
  17143. ((and (eq arg :todo)
  17144. (equal date (calendar-current-date)))
  17145. (setq rtn (org-agenda-get-todos))
  17146. (setq results (append results rtn)))
  17147. ((eq arg :timestamp)
  17148. (setq rtn (org-agenda-get-blocks))
  17149. (setq results (append results rtn))
  17150. (setq rtn (org-agenda-get-timestamps))
  17151. (setq results (append results rtn)))
  17152. ((eq arg :sexp)
  17153. (setq rtn (org-agenda-get-sexps))
  17154. (setq results (append results rtn)))
  17155. ((eq arg :scheduled)
  17156. (setq rtn (org-agenda-get-scheduled))
  17157. (setq results (append results rtn)))
  17158. ((eq arg :closed)
  17159. (setq rtn (org-agenda-get-closed))
  17160. (setq results (append results rtn)))
  17161. ((eq arg :deadline)
  17162. (setq rtn (org-agenda-get-deadlines))
  17163. (setq results (append results rtn))))))))
  17164. results))))
  17165. ;; FIXME: this works only if the cursor is not at the
  17166. ;; beginning of the entry
  17167. (defun org-entry-is-done-p ()
  17168. "Is the current entry marked DONE?"
  17169. (save-excursion
  17170. (and (re-search-backward "[\r\n]\\*+ " nil t)
  17171. (looking-at org-nl-done-regexp))))
  17172. (defun org-at-date-range-p (&optional inactive-ok)
  17173. "Is the cursor inside a date range?"
  17174. (interactive)
  17175. (save-excursion
  17176. (catch 'exit
  17177. (let ((pos (point)))
  17178. (skip-chars-backward "^[<\r\n")
  17179. (skip-chars-backward "<[")
  17180. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  17181. (>= (match-end 0) pos)
  17182. (throw 'exit t))
  17183. (skip-chars-backward "^<[\r\n")
  17184. (skip-chars-backward "<[")
  17185. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  17186. (>= (match-end 0) pos)
  17187. (throw 'exit t)))
  17188. nil)))
  17189. (defun org-agenda-get-todos ()
  17190. "Return the TODO information for agenda display."
  17191. (let* ((props (list 'face nil
  17192. 'done-face 'org-done
  17193. 'org-not-done-regexp org-not-done-regexp
  17194. 'org-todo-regexp org-todo-regexp
  17195. 'mouse-face 'highlight
  17196. 'keymap org-agenda-keymap
  17197. 'help-echo
  17198. (format "mouse-2 or RET jump to org file %s"
  17199. (abbreviate-file-name buffer-file-name))))
  17200. ;; FIXME: get rid of the \n at some point but watch out
  17201. (regexp (concat "^\\*+[ \t]+\\("
  17202. (if org-select-this-todo-keyword
  17203. (if (equal org-select-this-todo-keyword "*")
  17204. org-todo-regexp
  17205. (concat "\\<\\("
  17206. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  17207. "\\)\\>"))
  17208. org-not-done-regexp)
  17209. "[^\n\r]*\\)"))
  17210. marker priority category tags
  17211. ee txt beg end)
  17212. (goto-char (point-min))
  17213. (while (re-search-forward regexp nil t)
  17214. (catch :skip
  17215. (save-match-data
  17216. (beginning-of-line)
  17217. (setq beg (point) end (progn (outline-next-heading) (point)))
  17218. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  17219. (re-search-forward org-scheduled-time-regexp end t))
  17220. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  17221. (re-search-forward org-deadline-time-regexp end t)
  17222. (org-deadline-close (match-string 1))))
  17223. (goto-char (1+ beg))
  17224. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  17225. (throw :skip nil)))
  17226. (goto-char beg)
  17227. (org-agenda-skip)
  17228. (goto-char (match-beginning 1))
  17229. (setq marker (org-agenda-new-marker (match-beginning 0))
  17230. category (org-get-category)
  17231. tags (org-get-tags-at (point))
  17232. txt (org-format-agenda-item "" (match-string 1) category tags)
  17233. priority (1+ (org-get-priority txt)))
  17234. (org-add-props txt props
  17235. 'org-marker marker 'org-hd-marker marker
  17236. 'priority priority 'org-category category
  17237. 'type "todo")
  17238. (push txt ee)
  17239. (if org-agenda-todo-list-sublevels
  17240. (goto-char (match-end 1))
  17241. (org-end-of-subtree 'invisible))))
  17242. (nreverse ee)))
  17243. (defconst org-agenda-no-heading-message
  17244. "No heading for this item in buffer or region.")
  17245. (defun org-agenda-get-timestamps ()
  17246. "Return the date stamp information for agenda display."
  17247. (let* ((props (list 'face nil
  17248. 'org-not-done-regexp org-not-done-regexp
  17249. 'org-todo-regexp org-todo-regexp
  17250. 'mouse-face 'highlight
  17251. 'keymap org-agenda-keymap
  17252. 'help-echo
  17253. (format "mouse-2 or RET jump to org file %s"
  17254. (abbreviate-file-name buffer-file-name))))
  17255. (d1 (calendar-absolute-from-gregorian date))
  17256. (remove-re
  17257. (concat
  17258. (regexp-quote
  17259. (format-time-string
  17260. "<%Y-%m-%d"
  17261. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17262. ".*?>"))
  17263. (regexp
  17264. (concat
  17265. (regexp-quote
  17266. (substring
  17267. (format-time-string
  17268. (car org-time-stamp-formats)
  17269. (apply 'encode-time ; DATE bound by calendar
  17270. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  17271. 0 11))
  17272. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  17273. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  17274. marker hdmarker deadlinep scheduledp donep tmp priority category
  17275. ee txt timestr tags b0 b3 e3)
  17276. (goto-char (point-min))
  17277. (while (re-search-forward regexp nil t)
  17278. (setq b0 (match-beginning 0)
  17279. b3 (match-beginning 3) e3 (match-end 3))
  17280. (catch :skip
  17281. (and (org-at-date-range-p) (throw :skip nil))
  17282. (org-agenda-skip)
  17283. (if (and (match-end 1)
  17284. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  17285. (throw :skip nil))
  17286. (if (and e3
  17287. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  17288. (throw :skip nil))
  17289. (setq marker (org-agenda-new-marker b0)
  17290. category (org-get-category b0)
  17291. tmp (buffer-substring (max (point-min)
  17292. (- b0 org-ds-keyword-length))
  17293. b0)
  17294. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  17295. deadlinep (string-match org-deadline-regexp tmp)
  17296. scheduledp (string-match org-scheduled-regexp tmp)
  17297. donep (org-entry-is-done-p))
  17298. (if (or scheduledp deadlinep) (throw :skip t))
  17299. (if (string-match ">" timestr)
  17300. ;; substring should only run to end of time stamp
  17301. (setq timestr (substring timestr 0 (match-end 0))))
  17302. (save-excursion
  17303. (if (re-search-backward "^\\*+ " nil t)
  17304. (progn
  17305. (goto-char (match-beginning 0))
  17306. (setq hdmarker (org-agenda-new-marker)
  17307. tags (org-get-tags-at))
  17308. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17309. (setq txt (org-format-agenda-item
  17310. nil (match-string 1) category tags timestr nil
  17311. remove-re)))
  17312. (setq txt org-agenda-no-heading-message))
  17313. (setq priority (org-get-priority txt))
  17314. (org-add-props txt props
  17315. 'org-marker marker 'org-hd-marker hdmarker)
  17316. (org-add-props txt nil 'priority priority
  17317. 'org-category category 'date date
  17318. 'type "timestamp")
  17319. (push txt ee))
  17320. (outline-next-heading)))
  17321. (nreverse ee)))
  17322. (defun org-agenda-get-sexps ()
  17323. "Return the sexp information for agenda display."
  17324. (require 'diary-lib)
  17325. (let* ((props (list 'face nil
  17326. 'mouse-face 'highlight
  17327. 'keymap org-agenda-keymap
  17328. 'help-echo
  17329. (format "mouse-2 or RET jump to org file %s"
  17330. (abbreviate-file-name buffer-file-name))))
  17331. (regexp "^&?%%(")
  17332. marker category ee txt tags entry result beg b sexp sexp-entry)
  17333. (goto-char (point-min))
  17334. (while (re-search-forward regexp nil t)
  17335. (catch :skip
  17336. (org-agenda-skip)
  17337. (setq beg (match-beginning 0))
  17338. (goto-char (1- (match-end 0)))
  17339. (setq b (point))
  17340. (forward-sexp 1)
  17341. (setq sexp (buffer-substring b (point)))
  17342. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  17343. (org-trim (match-string 1))
  17344. ""))
  17345. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  17346. (when result
  17347. (setq marker (org-agenda-new-marker beg)
  17348. category (org-get-category beg))
  17349. (if (string-match "\\S-" result)
  17350. (setq txt result)
  17351. (setq txt "SEXP entry returned empty string"))
  17352. (setq txt (org-format-agenda-item
  17353. "" txt category tags 'time))
  17354. (org-add-props txt props 'org-marker marker)
  17355. (org-add-props txt nil
  17356. 'org-category category 'date date
  17357. 'type "sexp")
  17358. (push txt ee))))
  17359. (nreverse ee)))
  17360. (defun org-agenda-get-closed ()
  17361. "Return the logged TODO entries for agenda display."
  17362. (let* ((props (list 'mouse-face 'highlight
  17363. 'org-not-done-regexp org-not-done-regexp
  17364. 'org-todo-regexp org-todo-regexp
  17365. 'keymap org-agenda-keymap
  17366. 'help-echo
  17367. (format "mouse-2 or RET jump to org file %s"
  17368. (abbreviate-file-name buffer-file-name))))
  17369. (regexp (concat
  17370. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  17371. (regexp-quote
  17372. (substring
  17373. (format-time-string
  17374. (car org-time-stamp-formats)
  17375. (apply 'encode-time ; DATE bound by calendar
  17376. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  17377. 1 11))))
  17378. marker hdmarker priority category tags closedp
  17379. ee txt timestr)
  17380. (goto-char (point-min))
  17381. (while (re-search-forward regexp nil t)
  17382. (catch :skip
  17383. (org-agenda-skip)
  17384. (setq marker (org-agenda-new-marker (match-beginning 0))
  17385. closedp (equal (match-string 1) org-closed-string)
  17386. category (org-get-category (match-beginning 0))
  17387. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  17388. ;; donep (org-entry-is-done-p)
  17389. )
  17390. (if (string-match "\\]" timestr)
  17391. ;; substring should only run to end of time stamp
  17392. (setq timestr (substring timestr 0 (match-end 0))))
  17393. (save-excursion
  17394. (if (re-search-backward "^\\*+ " nil t)
  17395. (progn
  17396. (goto-char (match-beginning 0))
  17397. (setq hdmarker (org-agenda-new-marker)
  17398. tags (org-get-tags-at))
  17399. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17400. (setq txt (org-format-agenda-item
  17401. (if closedp "Closed: " "Clocked: ")
  17402. (match-string 1) category tags timestr)))
  17403. (setq txt org-agenda-no-heading-message))
  17404. (setq priority 100000)
  17405. (org-add-props txt props
  17406. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  17407. 'priority priority 'org-category category
  17408. 'type "closed" 'date date
  17409. 'undone-face 'org-warning 'done-face 'org-done)
  17410. (push txt ee))
  17411. (outline-next-heading)))
  17412. (nreverse ee)))
  17413. (defun org-agenda-get-deadlines ()
  17414. "Return the deadline information for agenda display."
  17415. (let* ((props (list 'mouse-face 'highlight
  17416. 'org-not-done-regexp org-not-done-regexp
  17417. 'org-todo-regexp org-todo-regexp
  17418. 'keymap org-agenda-keymap
  17419. 'help-echo
  17420. (format "mouse-2 or RET jump to org file %s"
  17421. (abbreviate-file-name buffer-file-name))))
  17422. (regexp org-deadline-time-regexp)
  17423. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  17424. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  17425. d2 diff dfrac wdays pos pos1 category tags
  17426. ee txt head face s upcomingp donep timestr)
  17427. (goto-char (point-min))
  17428. (while (re-search-forward regexp nil t)
  17429. (catch :skip
  17430. (org-agenda-skip)
  17431. (setq s (match-string 1)
  17432. pos (1- (match-beginning 1))
  17433. d2 (org-time-string-to-absolute (match-string 1) d1)
  17434. diff (- d2 d1)
  17435. wdays (org-get-wdays s)
  17436. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  17437. upcomingp (and todayp (> diff 0)))
  17438. ;; When to show a deadline in the calendar:
  17439. ;; If the expiration is within wdays warning time.
  17440. ;; Past-due deadlines are only shown on the current date
  17441. (if (or (and (<= diff wdays) todayp)
  17442. (= diff 0))
  17443. (save-excursion
  17444. (setq category (org-get-category))
  17445. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17446. (progn
  17447. (goto-char (match-end 0))
  17448. (setq pos1 (match-beginning 0))
  17449. (setq tags (org-get-tags-at pos1))
  17450. (setq head (buffer-substring-no-properties
  17451. (point)
  17452. (progn (skip-chars-forward "^\r\n")
  17453. (point))))
  17454. (setq donep (string-match org-looking-at-done-regexp head))
  17455. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17456. (setq timestr
  17457. (concat (substring s (match-beginning 1)) " "))
  17458. (setq timestr 'time))
  17459. (if (and donep
  17460. (or org-agenda-skip-deadline-if-done
  17461. (not (= diff 0))))
  17462. (setq txt nil)
  17463. (setq txt (org-format-agenda-item
  17464. (if (= diff 0)
  17465. "Deadline: "
  17466. (format "In %3d d.: " diff))
  17467. head category tags timestr))))
  17468. (setq txt org-agenda-no-heading-message))
  17469. (when txt
  17470. (setq face (org-agenda-deadline-face dfrac))
  17471. (org-add-props txt props
  17472. 'org-marker (org-agenda-new-marker pos)
  17473. 'org-hd-marker (org-agenda-new-marker pos1)
  17474. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  17475. (org-get-priority txt))
  17476. 'org-category category
  17477. 'type (if upcomingp "upcoming-deadline" "deadline")
  17478. 'date (if upcomingp date d2)
  17479. 'face (if donep 'org-done face)
  17480. 'undone-face face 'done-face 'org-done)
  17481. (push txt ee))))))
  17482. (nreverse ee)))
  17483. (defun org-agenda-deadline-face (fraction)
  17484. "Return the face to displaying a deadline item.
  17485. FRACTION is what fraction of the head-warning time has passed."
  17486. (let ((faces org-agenda-deadline-faces) f)
  17487. (catch 'exit
  17488. (while (setq f (pop faces))
  17489. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  17490. (defun org-agenda-get-scheduled ()
  17491. "Return the scheduled information for agenda display."
  17492. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  17493. 'org-todo-regexp org-todo-regexp
  17494. 'done-face 'org-done
  17495. 'mouse-face 'highlight
  17496. 'keymap org-agenda-keymap
  17497. 'help-echo
  17498. (format "mouse-2 or RET jump to org file %s"
  17499. (abbreviate-file-name buffer-file-name))))
  17500. (regexp org-scheduled-time-regexp)
  17501. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  17502. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  17503. d2 diff pos pos1 category tags
  17504. ee txt head pastschedp donep face timestr s)
  17505. (goto-char (point-min))
  17506. (while (re-search-forward regexp nil t)
  17507. (catch :skip
  17508. (org-agenda-skip)
  17509. (setq s (match-string 1)
  17510. pos (1- (match-beginning 1))
  17511. d2 (org-time-string-to-absolute (match-string 1) d1)
  17512. diff (- d2 d1))
  17513. (setq pastschedp (and todayp (< diff 0)))
  17514. ;; When to show a scheduled item in the calendar:
  17515. ;; If it is on or past the date.
  17516. (if (or (and (< diff 0) todayp)
  17517. (= diff 0))
  17518. (save-excursion
  17519. (setq category (org-get-category))
  17520. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17521. (progn
  17522. (goto-char (match-end 0))
  17523. (setq pos1 (match-beginning 0))
  17524. (setq tags (org-get-tags-at))
  17525. (setq head (buffer-substring-no-properties
  17526. (point)
  17527. (progn (skip-chars-forward "^\r\n") (point))))
  17528. (setq donep (string-match org-looking-at-done-regexp head))
  17529. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17530. (setq timestr
  17531. (concat (substring s (match-beginning 1)) " "))
  17532. (setq timestr 'time))
  17533. (if (and donep
  17534. (or org-agenda-skip-scheduled-if-done
  17535. (not (= diff 0))))
  17536. (setq txt nil)
  17537. (setq txt (org-format-agenda-item
  17538. (if (= diff 0)
  17539. "Scheduled: "
  17540. (format "Sched.%2dx: " (- 1 diff)))
  17541. head category tags timestr))))
  17542. (setq txt org-agenda-no-heading-message))
  17543. (when txt
  17544. (setq face (if pastschedp
  17545. 'org-scheduled-previously
  17546. 'org-scheduled-today))
  17547. (org-add-props txt props
  17548. 'undone-face face
  17549. 'face (if donep 'org-done face)
  17550. 'org-marker (org-agenda-new-marker pos)
  17551. 'org-hd-marker (org-agenda-new-marker pos1)
  17552. 'type (if pastschedp "past-scheduled" "scheduled")
  17553. 'date (if pastschedp d2 date)
  17554. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  17555. 'org-category category)
  17556. (push txt ee))))))
  17557. (nreverse ee)))
  17558. (defun org-agenda-get-blocks ()
  17559. "Return the date-range information for agenda display."
  17560. (let* ((props (list 'face nil
  17561. 'org-not-done-regexp org-not-done-regexp
  17562. 'org-todo-regexp org-todo-regexp
  17563. 'mouse-face 'highlight
  17564. 'keymap org-agenda-keymap
  17565. 'help-echo
  17566. (format "mouse-2 or RET jump to org file %s"
  17567. (abbreviate-file-name buffer-file-name))))
  17568. (regexp org-tr-regexp)
  17569. (d0 (calendar-absolute-from-gregorian date))
  17570. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  17571. (goto-char (point-min))
  17572. (while (re-search-forward regexp nil t)
  17573. (catch :skip
  17574. (org-agenda-skip)
  17575. (setq pos (point))
  17576. (setq timestr (match-string 0)
  17577. s1 (match-string 1)
  17578. s2 (match-string 2)
  17579. d1 (time-to-days (org-time-string-to-time s1))
  17580. d2 (time-to-days (org-time-string-to-time s2)))
  17581. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  17582. ;; Only allow days between the limits, because the normal
  17583. ;; date stamps will catch the limits.
  17584. (save-excursion
  17585. (setq marker (org-agenda-new-marker (point)))
  17586. (setq category (org-get-category))
  17587. (if (re-search-backward "^\\*+ " nil t)
  17588. (progn
  17589. (goto-char (match-beginning 0))
  17590. (setq hdmarker (org-agenda-new-marker (point)))
  17591. (setq tags (org-get-tags-at))
  17592. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17593. (setq txt (org-format-agenda-item
  17594. (format (if (= d1 d2) "" "(%d/%d): ")
  17595. (1+ (- d0 d1)) (1+ (- d2 d1)))
  17596. (match-string 1) category tags
  17597. (if (= d0 d1) timestr))))
  17598. (setq txt org-agenda-no-heading-message))
  17599. (org-add-props txt props
  17600. 'org-marker marker 'org-hd-marker hdmarker
  17601. 'type "block" 'date date
  17602. 'priority (org-get-priority txt) 'org-category category)
  17603. (push txt ee)))
  17604. (goto-char pos)))
  17605. ;; Sort the entries by expiration date.
  17606. (nreverse ee)))
  17607. ;;; Agenda presentation and sorting
  17608. (defconst org-plain-time-of-day-regexp
  17609. (concat
  17610. "\\(\\<[012]?[0-9]"
  17611. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17612. "\\(--?"
  17613. "\\(\\<[012]?[0-9]"
  17614. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17615. "\\)?")
  17616. "Regular expression to match a plain time or time range.
  17617. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  17618. groups carry important information:
  17619. 0 the full match
  17620. 1 the first time, range or not
  17621. 8 the second time, if it is a range.")
  17622. (defconst org-plain-time-extension-regexp
  17623. (concat
  17624. "\\(\\<[012]?[0-9]"
  17625. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17626. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  17627. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  17628. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  17629. groups carry important information:
  17630. 0 the full match
  17631. 7 hours of duration
  17632. 9 minutes of duration")
  17633. (defconst org-stamp-time-of-day-regexp
  17634. (concat
  17635. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  17636. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  17637. "\\(--?"
  17638. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  17639. "Regular expression to match a timestamp time or time range.
  17640. After a match, the following groups carry important information:
  17641. 0 the full match
  17642. 1 date plus weekday, for backreferencing to make sure both times on same day
  17643. 2 the first time, range or not
  17644. 4 the second time, if it is a range.")
  17645. (defvar org-prefix-has-time nil
  17646. "A flag, set by `org-compile-prefix-format'.
  17647. The flag is set if the currently compiled format contains a `%t'.")
  17648. (defvar org-prefix-has-tag nil
  17649. "A flag, set by `org-compile-prefix-format'.
  17650. The flag is set if the currently compiled format contains a `%T'.")
  17651. (defun org-format-agenda-item (extra txt &optional category tags dotime
  17652. noprefix remove-re)
  17653. "Format TXT to be inserted into the agenda buffer.
  17654. In particular, it adds the prefix and corresponding text properties. EXTRA
  17655. must be a string and replaces the `%s' specifier in the prefix format.
  17656. CATEGORY (string, symbol or nil) may be used to overrule the default
  17657. category taken from local variable or file name. It will replace the `%c'
  17658. specifier in the format. DOTIME, when non-nil, indicates that a
  17659. time-of-day should be extracted from TXT for sorting of this entry, and for
  17660. the `%t' specifier in the format. When DOTIME is a string, this string is
  17661. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  17662. only the correctly processes TXT should be returned - this is used by
  17663. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  17664. Any match of REMOVE-RE will be removed from TXT."
  17665. (save-match-data
  17666. ;; Diary entries sometimes have extra whitespace at the beginning
  17667. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  17668. (let* ((category (or category
  17669. org-category
  17670. (if buffer-file-name
  17671. (file-name-sans-extension
  17672. (file-name-nondirectory buffer-file-name))
  17673. "")))
  17674. (tag (if tags (nth (1- (length tags)) tags) ""))
  17675. time ; time and tag are needed for the eval of the prefix format
  17676. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  17677. (time-of-day (and dotime (org-get-time-of-day ts)))
  17678. stamp plain s0 s1 s2 rtn srp)
  17679. (when (and dotime time-of-day org-prefix-has-time)
  17680. ;; Extract starting and ending time and move them to prefix
  17681. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  17682. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  17683. (setq s0 (match-string 0 ts)
  17684. srp (and stamp (match-end 3))
  17685. s1 (match-string (if plain 1 2) ts)
  17686. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  17687. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  17688. ;; them, we might want to remove them there to avoid duplication.
  17689. ;; The user can turn this off with a variable.
  17690. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  17691. (string-match (concat (regexp-quote s0) " *") txt)
  17692. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  17693. (= (match-beginning 0) 0)
  17694. t))
  17695. (setq txt (replace-match "" nil nil txt))))
  17696. ;; Normalize the time(s) to 24 hour
  17697. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  17698. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  17699. (when (and s1 (not s2) org-agenda-default-appointment-duration
  17700. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  17701. (let ((m (+ (string-to-number (match-string 2 s1))
  17702. (* 60 (string-to-number (match-string 1 s1)))
  17703. org-agenda-default-appointment-duration))
  17704. h)
  17705. (setq h (/ m 60) m (- m (* h 60)))
  17706. (setq s2 (format "%02d:%02d" h m))))
  17707. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  17708. txt)
  17709. ;; Tags are in the string
  17710. (if (or (eq org-agenda-remove-tags t)
  17711. (and org-agenda-remove-tags
  17712. org-prefix-has-tag))
  17713. (setq txt (replace-match "" t t txt))
  17714. (setq txt (replace-match
  17715. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  17716. (match-string 2 txt))
  17717. t t txt))))
  17718. (when remove-re
  17719. (while (string-match remove-re txt)
  17720. (setq txt (replace-match "" t t txt))))
  17721. ;; Create the final string
  17722. (if noprefix
  17723. (setq rtn txt)
  17724. ;; Prepare the variables needed in the eval of the compiled format
  17725. (setq time (cond (s2 (concat s1 "-" s2))
  17726. (s1 (concat s1 "......"))
  17727. (t ""))
  17728. extra (or extra "")
  17729. category (if (symbolp category) (symbol-name category) category))
  17730. ;; Evaluate the compiled format
  17731. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  17732. ;; And finally add the text properties
  17733. (org-add-props rtn nil
  17734. 'org-category (downcase category) 'tags tags
  17735. 'prefix-length (- (length rtn) (length txt))
  17736. 'time-of-day time-of-day
  17737. 'txt txt
  17738. 'time time
  17739. 'extra extra
  17740. 'dotime dotime))))
  17741. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  17742. (defvar org-agenda-sorting-strategy-selected nil)
  17743. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  17744. (catch 'exit
  17745. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  17746. ((and todayp (member 'today (car org-agenda-time-grid))))
  17747. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  17748. ((member 'weekly (car org-agenda-time-grid)))
  17749. (t (throw 'exit list)))
  17750. (let* ((have (delq nil (mapcar
  17751. (lambda (x) (get-text-property 1 'time-of-day x))
  17752. list)))
  17753. (string (nth 1 org-agenda-time-grid))
  17754. (gridtimes (nth 2 org-agenda-time-grid))
  17755. (req (car org-agenda-time-grid))
  17756. (remove (member 'remove-match req))
  17757. new time)
  17758. (if (and (member 'require-timed req) (not have))
  17759. ;; don't show empty grid
  17760. (throw 'exit list))
  17761. (while (setq time (pop gridtimes))
  17762. (unless (and remove (member time have))
  17763. (setq time (int-to-string time))
  17764. (push (org-format-agenda-item
  17765. nil string "" nil
  17766. (concat (substring time 0 -2) ":" (substring time -2)))
  17767. new)
  17768. (put-text-property
  17769. 1 (length (car new)) 'face 'org-time-grid (car new))))
  17770. (if (member 'time-up org-agenda-sorting-strategy-selected)
  17771. (append new list)
  17772. (append list new)))))
  17773. (defun org-compile-prefix-format (key)
  17774. "Compile the prefix format into a Lisp form that can be evaluated.
  17775. The resulting form is returned and stored in the variable
  17776. `org-prefix-format-compiled'."
  17777. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  17778. (let ((s (cond
  17779. ((stringp org-agenda-prefix-format)
  17780. org-agenda-prefix-format)
  17781. ((assq key org-agenda-prefix-format)
  17782. (cdr (assq key org-agenda-prefix-format)))
  17783. (t " %-12:c%?-12t% s")))
  17784. (start 0)
  17785. varform vars var e c f opt)
  17786. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  17787. s start)
  17788. (setq var (cdr (assoc (match-string 4 s)
  17789. '(("c" . category) ("t" . time) ("s" . extra)
  17790. ("T" . tag))))
  17791. c (or (match-string 3 s) "")
  17792. opt (match-beginning 1)
  17793. start (1+ (match-beginning 0)))
  17794. (if (equal var 'time) (setq org-prefix-has-time t))
  17795. (if (equal var 'tag) (setq org-prefix-has-tag t))
  17796. (setq f (concat "%" (match-string 2 s) "s"))
  17797. (if opt
  17798. (setq varform
  17799. `(if (equal "" ,var)
  17800. ""
  17801. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  17802. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  17803. (setq s (replace-match "%s" t nil s))
  17804. (push varform vars))
  17805. (setq vars (nreverse vars))
  17806. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  17807. (defun org-set-sorting-strategy (key)
  17808. (if (symbolp (car org-agenda-sorting-strategy))
  17809. ;; the old format
  17810. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  17811. (setq org-agenda-sorting-strategy-selected
  17812. (or (cdr (assq key org-agenda-sorting-strategy))
  17813. (cdr (assq 'agenda org-agenda-sorting-strategy))
  17814. '(time-up category-keep priority-down)))))
  17815. (defun org-get-time-of-day (s &optional string mod24)
  17816. "Check string S for a time of day.
  17817. If found, return it as a military time number between 0 and 2400.
  17818. If not found, return nil.
  17819. The optional STRING argument forces conversion into a 5 character wide string
  17820. HH:MM."
  17821. (save-match-data
  17822. (when
  17823. (or
  17824. (string-match
  17825. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  17826. (string-match
  17827. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  17828. (let* ((h (string-to-number (match-string 1 s)))
  17829. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  17830. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  17831. (am-p (equal ampm "am"))
  17832. (h1 (cond ((not ampm) h)
  17833. ((= h 12) (if am-p 0 12))
  17834. (t (+ h (if am-p 0 12)))))
  17835. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  17836. (mod h1 24) h1))
  17837. (t0 (+ (* 100 h2) m))
  17838. (t1 (concat (if (>= h1 24) "+" " ")
  17839. (if (< t0 100) "0" "")
  17840. (if (< t0 10) "0" "")
  17841. (int-to-string t0))))
  17842. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  17843. (defun org-finalize-agenda-entries (list &optional nosort)
  17844. "Sort and concatenate the agenda items."
  17845. (setq list (mapcar 'org-agenda-highlight-todo list))
  17846. (if nosort
  17847. list
  17848. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  17849. (defun org-agenda-highlight-todo (x)
  17850. (let (re pl)
  17851. (if (eq x 'line)
  17852. (save-excursion
  17853. (beginning-of-line 1)
  17854. (setq re (get-text-property (point) 'org-todo-regexp))
  17855. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  17856. (and (looking-at (concat "[ \t]*\\.*" re))
  17857. (add-text-properties (match-beginning 0) (match-end 0)
  17858. (list 'face (org-get-todo-face 0)))))
  17859. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  17860. pl (get-text-property 0 'prefix-length x))
  17861. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  17862. (add-text-properties
  17863. (or (match-end 1) (match-end 0)) (match-end 0)
  17864. (list 'face (org-get-todo-face (match-string 2 x)))
  17865. x))
  17866. x)))
  17867. (defsubst org-cmp-priority (a b)
  17868. "Compare the priorities of string A and B."
  17869. (let ((pa (or (get-text-property 1 'priority a) 0))
  17870. (pb (or (get-text-property 1 'priority b) 0)))
  17871. (cond ((> pa pb) +1)
  17872. ((< pa pb) -1)
  17873. (t nil))))
  17874. (defsubst org-cmp-category (a b)
  17875. "Compare the string values of categories of strings A and B."
  17876. (let ((ca (or (get-text-property 1 'org-category a) ""))
  17877. (cb (or (get-text-property 1 'org-category b) "")))
  17878. (cond ((string-lessp ca cb) -1)
  17879. ((string-lessp cb ca) +1)
  17880. (t nil))))
  17881. (defsubst org-cmp-tag (a b)
  17882. "Compare the string values of categories of strings A and B."
  17883. (let ((ta (car (last (get-text-property 1 'tags a))))
  17884. (tb (car (last (get-text-property 1 'tags b)))))
  17885. (cond ((not ta) +1)
  17886. ((not tb) -1)
  17887. ((string-lessp ta tb) -1)
  17888. ((string-lessp tb ta) +1)
  17889. (t nil))))
  17890. (defsubst org-cmp-time (a b)
  17891. "Compare the time-of-day values of strings A and B."
  17892. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  17893. (ta (or (get-text-property 1 'time-of-day a) def))
  17894. (tb (or (get-text-property 1 'time-of-day b) def)))
  17895. (cond ((< ta tb) -1)
  17896. ((< tb ta) +1)
  17897. (t nil))))
  17898. (defun org-entries-lessp (a b)
  17899. "Predicate for sorting agenda entries."
  17900. ;; The following variables will be used when the form is evaluated.
  17901. ;; So even though the compiler complains, keep them.
  17902. (let* ((time-up (org-cmp-time a b))
  17903. (time-down (if time-up (- time-up) nil))
  17904. (priority-up (org-cmp-priority a b))
  17905. (priority-down (if priority-up (- priority-up) nil))
  17906. (category-up (org-cmp-category a b))
  17907. (category-down (if category-up (- category-up) nil))
  17908. (category-keep (if category-up +1 nil))
  17909. (tag-up (org-cmp-tag a b))
  17910. (tag-down (if tag-up (- tag-up) nil)))
  17911. (cdr (assoc
  17912. (eval (cons 'or org-agenda-sorting-strategy-selected))
  17913. '((-1 . t) (1 . nil) (nil . nil))))))
  17914. ;;; Agenda commands
  17915. (defun org-agenda-check-type (error &rest types)
  17916. "Check if agenda buffer is of allowed type.
  17917. If ERROR is non-nil, throw an error, otherwise just return nil."
  17918. (if (memq org-agenda-type types)
  17919. t
  17920. (if error
  17921. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  17922. nil)))
  17923. (defun org-agenda-quit ()
  17924. "Exit agenda by removing the window or the buffer."
  17925. (interactive)
  17926. (let ((buf (current-buffer)))
  17927. (if (not (one-window-p)) (delete-window))
  17928. (kill-buffer buf)
  17929. (org-agenda-maybe-reset-markers 'force)
  17930. (org-columns-remove-overlays))
  17931. ;; Maybe restore the pre-agenda window configuration.
  17932. (and org-agenda-restore-windows-after-quit
  17933. (not (eq org-agenda-window-setup 'other-frame))
  17934. org-pre-agenda-window-conf
  17935. (set-window-configuration org-pre-agenda-window-conf)))
  17936. (defun org-agenda-exit ()
  17937. "Exit agenda by removing the window or the buffer.
  17938. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  17939. Org-mode buffers visited directly by the user will not be touched."
  17940. (interactive)
  17941. (org-release-buffers org-agenda-new-buffers)
  17942. (setq org-agenda-new-buffers nil)
  17943. (org-agenda-quit))
  17944. (defun org-save-all-org-buffers ()
  17945. "Save all Org-mode buffers without user confirmation."
  17946. (interactive)
  17947. (message "Saving all Org-mode buffers...")
  17948. (save-some-buffers t 'org-mode-p)
  17949. (message "Saving all Org-mode buffers... done"))
  17950. (defun org-agenda-redo ()
  17951. "Rebuild Agenda.
  17952. When this is the global TODO list, a prefix argument will be interpreted."
  17953. (interactive)
  17954. (let* ((org-agenda-keep-modes t)
  17955. (line (org-current-line))
  17956. (window-line (- line (org-current-line (window-start))))
  17957. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  17958. (message "Rebuilding agenda buffer...")
  17959. (org-let lprops '(eval org-agenda-redo-command))
  17960. (setq org-agenda-undo-list nil
  17961. org-agenda-pending-undo-list nil)
  17962. (message "Rebuilding agenda buffer...done")
  17963. (goto-line line)
  17964. (recenter window-line)))
  17965. (defun org-agenda-goto-date (date)
  17966. "Jump to DATE in agenda."
  17967. (interactive (list (org-read-date)))
  17968. (org-agenda-list nil date))
  17969. (defun org-agenda-goto-today ()
  17970. "Go to today."
  17971. (interactive)
  17972. (org-agenda-check-type t 'timeline 'agenda)
  17973. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  17974. (cond
  17975. (tdpos (goto-char tdpos))
  17976. ((eq org-agenda-type 'agenda)
  17977. (let* ((sd (time-to-days (current-time)))
  17978. (comp (org-agenda-compute-time-span sd org-agenda-span))
  17979. (org-agenda-overriding-arguments org-agenda-last-arguments))
  17980. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  17981. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  17982. (org-agenda-redo)
  17983. (org-agenda-find-today-or-agenda)))
  17984. (t (error "Cannot find today")))))
  17985. (defun org-agenda-find-today-or-agenda ()
  17986. (goto-char
  17987. (or (text-property-any (point-min) (point-max) 'org-today t)
  17988. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  17989. (point-min))))
  17990. (defun org-agenda-later (arg)
  17991. "Go forward in time by thee current span.
  17992. With prefix ARG, go forward that many times the current span."
  17993. (interactive "p")
  17994. (org-agenda-check-type t 'agenda)
  17995. (let* ((span org-agenda-span)
  17996. (sd org-starting-day)
  17997. (greg (calendar-gregorian-from-absolute sd))
  17998. greg2 nd)
  17999. (cond
  18000. ((eq span 'day)
  18001. (setq sd (+ arg sd) nd 1))
  18002. ((eq span 'week)
  18003. (setq sd (+ (* 7 arg) sd) nd 7))
  18004. ((eq span 'month)
  18005. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  18006. sd (calendar-absolute-from-gregorian greg2))
  18007. (setcar greg2 (1+ (car greg2)))
  18008. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  18009. ((eq span 'year)
  18010. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  18011. sd (calendar-absolute-from-gregorian greg2))
  18012. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  18013. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  18014. (let ((org-agenda-overriding-arguments
  18015. (list (car org-agenda-last-arguments) sd nd t)))
  18016. (org-agenda-redo)
  18017. (org-agenda-find-today-or-agenda))))
  18018. (defun org-agenda-earlier (arg)
  18019. "Go backward in time by the current span.
  18020. With prefix ARG, go backward that many times the current span."
  18021. (interactive "p")
  18022. (org-agenda-later (- arg)))
  18023. (defun org-agenda-day-view ()
  18024. "Switch to daily view for agenda."
  18025. (interactive)
  18026. (setq org-agenda-ndays 1)
  18027. (org-agenda-change-time-span 'day))
  18028. (defun org-agenda-week-view ()
  18029. "Switch to daily view for agenda."
  18030. (interactive)
  18031. (setq org-agenda-ndays 7)
  18032. (org-agenda-change-time-span 'week))
  18033. (defun org-agenda-month-view ()
  18034. "Switch to daily view for agenda."
  18035. (interactive)
  18036. (org-agenda-change-time-span 'month))
  18037. (defun org-agenda-year-view ()
  18038. "Switch to daily view for agenda."
  18039. (interactive)
  18040. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  18041. (org-agenda-change-time-span 'year)
  18042. (error "Abort")))
  18043. (defun org-agenda-change-time-span (span)
  18044. "Change the agenda view to SPAN.
  18045. SPAN may be `day', `week', `month', `year'."
  18046. (org-agenda-check-type t 'agenda)
  18047. (if (equal org-agenda-span span)
  18048. (error "Viewing span is already \"%s\"" span))
  18049. (let* ((sd (or (get-text-property (point) 'day)
  18050. org-starting-day))
  18051. (computed (org-agenda-compute-time-span sd span))
  18052. (org-agenda-overriding-arguments
  18053. (list (car org-agenda-last-arguments)
  18054. (car computed) (cdr computed) t)))
  18055. (org-agenda-redo)
  18056. (org-agenda-find-today-or-agenda))
  18057. (org-agenda-set-mode-name)
  18058. (message "Switched to %s view" span))
  18059. (defun org-agenda-compute-time-span (sd span)
  18060. "Compute starting date and number of days for agenda.
  18061. SPAN may be `day', `week', `month', `year'. The return value
  18062. is a cons cell with the starting date and the number of days,
  18063. so that the date SD will be in that range."
  18064. (let* ((greg (calendar-gregorian-from-absolute sd))
  18065. nd)
  18066. (cond
  18067. ((eq span 'day)
  18068. (setq nd 1))
  18069. ((eq span 'week)
  18070. (let* ((nt (calendar-day-of-week
  18071. (calendar-gregorian-from-absolute sd)))
  18072. (d (if org-agenda-start-on-weekday
  18073. (- nt org-agenda-start-on-weekday)
  18074. 0)))
  18075. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  18076. (setq nd 7)))
  18077. ((eq span 'month)
  18078. (setq sd (calendar-absolute-from-gregorian
  18079. (list (car greg) 1 (nth 2 greg)))
  18080. nd (- (calendar-absolute-from-gregorian
  18081. (list (1+ (car greg)) 1 (nth 2 greg)))
  18082. sd)))
  18083. ((eq span 'year)
  18084. (setq sd (calendar-absolute-from-gregorian
  18085. (list 1 1 (nth 2 greg)))
  18086. nd (- (calendar-absolute-from-gregorian
  18087. (list 1 1 (1+ (nth 2 greg))))
  18088. sd))))
  18089. (cons sd nd)))
  18090. ;; FIXME: this no longer works if user make date format that starts with a blank
  18091. (defun org-agenda-next-date-line (&optional arg)
  18092. "Jump to the next line indicating a date in agenda buffer."
  18093. (interactive "p")
  18094. (org-agenda-check-type t 'agenda 'timeline)
  18095. (beginning-of-line 1)
  18096. (if (looking-at "^\\S-") (forward-char 1))
  18097. (if (not (re-search-forward "^\\S-" nil t arg))
  18098. (progn
  18099. (backward-char 1)
  18100. (error "No next date after this line in this buffer")))
  18101. (goto-char (match-beginning 0)))
  18102. (defun org-agenda-previous-date-line (&optional arg)
  18103. "Jump to the previous line indicating a date in agenda buffer."
  18104. (interactive "p")
  18105. (org-agenda-check-type t 'agenda 'timeline)
  18106. (beginning-of-line 1)
  18107. (if (not (re-search-backward "^\\S-" nil t arg))
  18108. (error "No previous date before this line in this buffer")))
  18109. ;; Initialize the highlight
  18110. (defvar org-hl (org-make-overlay 1 1))
  18111. (org-overlay-put org-hl 'face 'highlight)
  18112. (defun org-highlight (begin end &optional buffer)
  18113. "Highlight a region with overlay."
  18114. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  18115. org-hl begin end (or buffer (current-buffer))))
  18116. (defun org-unhighlight ()
  18117. "Detach overlay INDEX."
  18118. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  18119. ;; FIXME this is currently not used.
  18120. (defun org-highlight-until-next-command (beg end &optional buffer)
  18121. (org-highlight beg end buffer)
  18122. (add-hook 'pre-command-hook 'org-unhighlight-once))
  18123. (defun org-unhighlight-once ()
  18124. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  18125. (org-unhighlight))
  18126. (defun org-agenda-follow-mode ()
  18127. "Toggle follow mode in an agenda buffer."
  18128. (interactive)
  18129. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  18130. (org-agenda-set-mode-name)
  18131. (message "Follow mode is %s"
  18132. (if org-agenda-follow-mode "on" "off")))
  18133. (defun org-agenda-log-mode ()
  18134. "Toggle log mode in an agenda buffer."
  18135. (interactive)
  18136. (org-agenda-check-type t 'agenda 'timeline)
  18137. (setq org-agenda-show-log (not org-agenda-show-log))
  18138. (org-agenda-set-mode-name)
  18139. (org-agenda-redo)
  18140. (message "Log mode is %s"
  18141. (if org-agenda-show-log "on" "off")))
  18142. (defun org-agenda-toggle-diary ()
  18143. "Toggle diary inclusion in an agenda buffer."
  18144. (interactive)
  18145. (org-agenda-check-type t 'agenda)
  18146. (setq org-agenda-include-diary (not org-agenda-include-diary))
  18147. (org-agenda-redo)
  18148. (org-agenda-set-mode-name)
  18149. (message "Diary inclusion turned %s"
  18150. (if org-agenda-include-diary "on" "off")))
  18151. (defun org-agenda-toggle-time-grid ()
  18152. "Toggle time grid in an agenda buffer."
  18153. (interactive)
  18154. (org-agenda-check-type t 'agenda)
  18155. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  18156. (org-agenda-redo)
  18157. (org-agenda-set-mode-name)
  18158. (message "Time-grid turned %s"
  18159. (if org-agenda-use-time-grid "on" "off")))
  18160. (defun org-agenda-set-mode-name ()
  18161. "Set the mode name to indicate all the small mode settings."
  18162. (setq mode-name
  18163. (concat "Org-Agenda"
  18164. (if (equal org-agenda-ndays 1) " Day" "")
  18165. (if (equal org-agenda-ndays 7) " Week" "")
  18166. (if org-agenda-follow-mode " Follow" "")
  18167. (if org-agenda-include-diary " Diary" "")
  18168. (if org-agenda-use-time-grid " Grid" "")
  18169. (if org-agenda-show-log " Log" "")))
  18170. (force-mode-line-update))
  18171. (defun org-agenda-post-command-hook ()
  18172. (and (eolp) (not (bolp)) (backward-char 1))
  18173. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18174. (if (and org-agenda-follow-mode
  18175. (get-text-property (point) 'org-marker))
  18176. (org-agenda-show)))
  18177. (defun org-agenda-show-priority ()
  18178. "Show the priority of the current item.
  18179. This priority is composed of the main priority given with the [#A] cookies,
  18180. and by additional input from the age of a schedules or deadline entry."
  18181. (interactive)
  18182. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  18183. (message "Priority is %d" (if pri pri -1000))))
  18184. (defun org-agenda-show-tags ()
  18185. "Show the tags applicable to the current item."
  18186. (interactive)
  18187. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  18188. (if tags
  18189. (message "Tags are :%s:"
  18190. (org-no-properties (mapconcat 'identity tags ":")))
  18191. (message "No tags associated with this line"))))
  18192. (defun org-agenda-goto (&optional highlight)
  18193. "Go to the Org-mode file which contains the item at point."
  18194. (interactive)
  18195. (let* ((marker (or (get-text-property (point) 'org-marker)
  18196. (org-agenda-error)))
  18197. (buffer (marker-buffer marker))
  18198. (pos (marker-position marker)))
  18199. (switch-to-buffer-other-window buffer)
  18200. (widen)
  18201. (goto-char pos)
  18202. (when (org-mode-p)
  18203. (org-show-context 'agenda)
  18204. (save-excursion
  18205. (and (outline-next-heading)
  18206. (org-flag-heading nil)))) ; show the next heading
  18207. (run-hooks 'org-agenda-after-show-hook)
  18208. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  18209. (defvar org-agenda-after-show-hook nil
  18210. "Normal hook run after an item has been shown from the agenda.
  18211. Point is in the buffer where the item originated.")
  18212. (defun org-agenda-kill ()
  18213. "Kill the entry or subtree belonging to the current agenda entry."
  18214. (interactive)
  18215. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  18216. (let* ((marker (or (get-text-property (point) 'org-marker)
  18217. (org-agenda-error)))
  18218. (buffer (marker-buffer marker))
  18219. (pos (marker-position marker))
  18220. (type (get-text-property (point) 'type))
  18221. dbeg dend (n 0) conf)
  18222. (org-with-remote-undo buffer
  18223. (with-current-buffer buffer
  18224. (save-excursion
  18225. (goto-char pos)
  18226. (if (and (org-mode-p) (not (member type '("sexp"))))
  18227. (setq dbeg (progn (org-back-to-heading t) (point))
  18228. dend (org-end-of-subtree t t))
  18229. (setq dbeg (point-at-bol)
  18230. dend (min (point-max) (1+ (point-at-eol)))))
  18231. (goto-char dbeg)
  18232. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  18233. (setq conf (or (eq t org-agenda-confirm-kill)
  18234. (and (numberp org-agenda-confirm-kill)
  18235. (> n org-agenda-confirm-kill))))
  18236. (and conf
  18237. (not (y-or-n-p
  18238. (format "Delete entry with %d lines in buffer \"%s\"? "
  18239. n (buffer-name buffer))))
  18240. (error "Abort"))
  18241. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  18242. (with-current-buffer buffer (delete-region dbeg dend))
  18243. (message "Agenda item and source killed"))))
  18244. (defun org-agenda-archive ()
  18245. "Kill the entry or subtree belonging to the current agenda entry."
  18246. (interactive)
  18247. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  18248. (let* ((marker (or (get-text-property (point) 'org-marker)
  18249. (org-agenda-error)))
  18250. (buffer (marker-buffer marker))
  18251. (pos (marker-position marker)))
  18252. (org-with-remote-undo buffer
  18253. (with-current-buffer buffer
  18254. (if (org-mode-p)
  18255. (save-excursion
  18256. (goto-char pos)
  18257. (org-remove-subtree-entries-from-agenda)
  18258. (org-back-to-heading t)
  18259. (org-archive-subtree))
  18260. (error "Archiving works only in Org-mode files"))))))
  18261. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  18262. "Remove all lines in the agenda that correspond to a given subtree.
  18263. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  18264. If this information is not given, the function uses the tree at point."
  18265. (let ((buf (or buf (current-buffer))) m p)
  18266. (save-excursion
  18267. (unless (and beg end)
  18268. (org-back-to-heading t)
  18269. (setq beg (point))
  18270. (org-end-of-subtree t)
  18271. (setq end (point)))
  18272. (set-buffer (get-buffer org-agenda-buffer-name))
  18273. (save-excursion
  18274. (goto-char (point-max))
  18275. (beginning-of-line 1)
  18276. (while (not (bobp))
  18277. (when (and (setq m (get-text-property (point) 'org-marker))
  18278. (equal buf (marker-buffer m))
  18279. (setq p (marker-position m))
  18280. (>= p beg)
  18281. (<= p end))
  18282. (let ((inhibit-read-only t))
  18283. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  18284. (beginning-of-line 0))))))
  18285. (defun org-agenda-open-link ()
  18286. "Follow the link in the current line, if any."
  18287. (interactive)
  18288. (let ((eol (point-at-eol)))
  18289. (save-excursion
  18290. (if (or (re-search-forward org-bracket-link-regexp eol t)
  18291. (re-search-forward org-angle-link-re eol t)
  18292. (re-search-forward org-plain-link-re eol t))
  18293. (call-interactively 'org-open-at-point)
  18294. (error "No link in current line")))))
  18295. (defun org-agenda-switch-to (&optional delete-other-windows)
  18296. "Go to the Org-mode file which contains the item at point."
  18297. (interactive)
  18298. (let* ((marker (or (get-text-property (point) 'org-marker)
  18299. (org-agenda-error)))
  18300. (buffer (marker-buffer marker))
  18301. (pos (marker-position marker)))
  18302. (switch-to-buffer buffer)
  18303. (and delete-other-windows (delete-other-windows))
  18304. (widen)
  18305. (goto-char pos)
  18306. (when (org-mode-p)
  18307. (org-show-context 'agenda)
  18308. (save-excursion
  18309. (and (outline-next-heading)
  18310. (org-flag-heading nil)))))) ; show the next heading
  18311. (defun org-agenda-goto-mouse (ev)
  18312. "Go to the Org-mode file which contains the item at the mouse click."
  18313. (interactive "e")
  18314. (mouse-set-point ev)
  18315. (org-agenda-goto))
  18316. (defun org-agenda-show ()
  18317. "Display the Org-mode file which contains the item at point."
  18318. (interactive)
  18319. (let ((win (selected-window)))
  18320. (org-agenda-goto t)
  18321. (select-window win)))
  18322. (defun org-agenda-recenter (arg)
  18323. "Display the Org-mode file which contains the item at point and recenter."
  18324. (interactive "P")
  18325. (let ((win (selected-window)))
  18326. (org-agenda-goto t)
  18327. (recenter arg)
  18328. (select-window win)))
  18329. (defun org-agenda-show-mouse (ev)
  18330. "Display the Org-mode file which contains the item at the mouse click."
  18331. (interactive "e")
  18332. (mouse-set-point ev)
  18333. (org-agenda-show))
  18334. (defun org-agenda-check-no-diary ()
  18335. "Check if the entry is a diary link and abort if yes."
  18336. (if (get-text-property (point) 'org-agenda-diary-link)
  18337. (org-agenda-error)))
  18338. (defun org-agenda-error ()
  18339. (error "Command not allowed in this line"))
  18340. (defun org-agenda-tree-to-indirect-buffer ()
  18341. "Show the subtree corresponding to the current entry in an indirect buffer.
  18342. This calls the command `org-tree-to-indirect-buffer' from the original
  18343. Org-mode buffer.
  18344. With numerical prefix arg ARG, go up to this level and then take that tree.
  18345. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  18346. dedicated frame)."
  18347. (interactive)
  18348. (org-agenda-check-no-diary)
  18349. (let* ((marker (or (get-text-property (point) 'org-marker)
  18350. (org-agenda-error)))
  18351. (buffer (marker-buffer marker))
  18352. (pos (marker-position marker)))
  18353. (with-current-buffer buffer
  18354. (save-excursion
  18355. (goto-char pos)
  18356. (call-interactively 'org-tree-to-indirect-buffer)))))
  18357. (defvar org-last-heading-marker (make-marker)
  18358. "Marker pointing to the headline that last changed its TODO state
  18359. by a remote command from the agenda.")
  18360. (defun org-agenda-todo-nextset ()
  18361. "Switch TODO entry to next sequence."
  18362. (interactive)
  18363. (org-agenda-todo 'nextset))
  18364. (defun org-agenda-todo-previousset ()
  18365. "Switch TODO entry to previous sequence."
  18366. (interactive)
  18367. (org-agenda-todo 'previousset))
  18368. (defun org-agenda-todo (&optional arg)
  18369. "Cycle TODO state of line at point, also in Org-mode file.
  18370. This changes the line at point, all other lines in the agenda referring to
  18371. the same tree node, and the headline of the tree node in the Org-mode file."
  18372. (interactive "P")
  18373. (org-agenda-check-no-diary)
  18374. (let* ((col (current-column))
  18375. (marker (or (get-text-property (point) 'org-marker)
  18376. (org-agenda-error)))
  18377. (buffer (marker-buffer marker))
  18378. (pos (marker-position marker))
  18379. (hdmarker (get-text-property (point) 'org-hd-marker))
  18380. (inhibit-read-only t)
  18381. newhead)
  18382. (org-with-remote-undo buffer
  18383. (with-current-buffer buffer
  18384. (widen)
  18385. (goto-char pos)
  18386. (org-show-context 'agenda)
  18387. (save-excursion
  18388. (and (outline-next-heading)
  18389. (org-flag-heading nil))) ; show the next heading
  18390. (org-todo arg)
  18391. (and (bolp) (forward-char 1))
  18392. (setq newhead (org-get-heading))
  18393. (save-excursion
  18394. (org-back-to-heading)
  18395. (move-marker org-last-heading-marker (point))))
  18396. (beginning-of-line 1)
  18397. (save-excursion
  18398. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  18399. (move-to-column col))))
  18400. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  18401. "Change all lines in the agenda buffer which match HDMARKER.
  18402. The new content of the line will be NEWHEAD (as modified by
  18403. `org-format-agenda-item'). HDMARKER is checked with
  18404. `equal' against all `org-hd-marker' text properties in the file.
  18405. If FIXFACE is non-nil, the face of each item is modified acording to
  18406. the new TODO state."
  18407. (let* ((inhibit-read-only t)
  18408. props m pl undone-face done-face finish new dotime cat tags)
  18409. (save-excursion
  18410. (goto-char (point-max))
  18411. (beginning-of-line 1)
  18412. (while (not finish)
  18413. (setq finish (bobp))
  18414. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  18415. (equal m hdmarker))
  18416. (setq props (text-properties-at (point))
  18417. dotime (get-text-property (point) 'dotime)
  18418. cat (get-text-property (point) 'org-category)
  18419. tags (get-text-property (point) 'tags)
  18420. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  18421. pl (get-text-property (point) 'prefix-length)
  18422. undone-face (get-text-property (point) 'undone-face)
  18423. done-face (get-text-property (point) 'done-face))
  18424. (move-to-column pl)
  18425. (cond
  18426. ((equal new "")
  18427. (beginning-of-line 1)
  18428. (and (looking-at ".*\n?") (replace-match "")))
  18429. ((looking-at ".*")
  18430. (replace-match new t t)
  18431. (beginning-of-line 1)
  18432. (add-text-properties (point-at-bol) (point-at-eol) props)
  18433. (when fixface
  18434. (add-text-properties
  18435. (point-at-bol) (point-at-eol)
  18436. (list 'face
  18437. (if org-last-todo-state-is-todo
  18438. undone-face done-face))))
  18439. (org-agenda-highlight-todo 'line)
  18440. (beginning-of-line 1))
  18441. (t (error "Line update did not work"))))
  18442. (beginning-of-line 0)))
  18443. (org-finalize-agenda)))
  18444. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  18445. ;; See the code in set-tags for the way to do this.
  18446. (defun org-agenda-align-tags (&optional line)
  18447. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  18448. (let ((inhibit-read-only t))
  18449. (save-excursion
  18450. (goto-char (if line (point-at-bol) (point-min)))
  18451. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  18452. (if line (point-at-eol) nil) t)
  18453. (delete-region (match-beginning 1) (match-end 1))
  18454. (goto-char (match-beginning 1))
  18455. (insert (org-add-props
  18456. (make-string (max 1 (- org-agenda-align-tags-to-column
  18457. (current-column))) ?\ )
  18458. (text-properties-at (point))))))))
  18459. (defun org-agenda-priority-up ()
  18460. "Increase the priority of line at point, also in Org-mode file."
  18461. (interactive)
  18462. (org-agenda-priority 'up))
  18463. (defun org-agenda-priority-down ()
  18464. "Decrease the priority of line at point, also in Org-mode file."
  18465. (interactive)
  18466. (org-agenda-priority 'down))
  18467. (defun org-agenda-priority (&optional force-direction)
  18468. "Set the priority of line at point, also in Org-mode file.
  18469. This changes the line at point, all other lines in the agenda referring to
  18470. the same tree node, and the headline of the tree node in the Org-mode file."
  18471. (interactive)
  18472. (org-agenda-check-no-diary)
  18473. (let* ((marker (or (get-text-property (point) 'org-marker)
  18474. (org-agenda-error)))
  18475. (hdmarker (get-text-property (point) 'org-hd-marker))
  18476. (buffer (marker-buffer hdmarker))
  18477. (pos (marker-position hdmarker))
  18478. (inhibit-read-only t)
  18479. newhead)
  18480. (org-with-remote-undo buffer
  18481. (with-current-buffer buffer
  18482. (widen)
  18483. (goto-char pos)
  18484. (org-show-context 'agenda)
  18485. (save-excursion
  18486. (and (outline-next-heading)
  18487. (org-flag-heading nil))) ; show the next heading
  18488. (funcall 'org-priority force-direction)
  18489. (end-of-line 1)
  18490. (setq newhead (org-get-heading)))
  18491. (org-agenda-change-all-lines newhead hdmarker)
  18492. (beginning-of-line 1))))
  18493. (defun org-get-tags-at (&optional pos)
  18494. "Get a list of all headline tags applicable at POS.
  18495. POS defaults to point. If tags are inherited, the list contains
  18496. the targets in the same sequence as the headlines appear, i.e.
  18497. the tags of the current headline come last."
  18498. (interactive)
  18499. (let (tags lastpos)
  18500. (save-excursion
  18501. (save-restriction
  18502. (widen)
  18503. (goto-char (or pos (point)))
  18504. (save-match-data
  18505. (org-back-to-heading t)
  18506. (condition-case nil
  18507. (while (not (equal lastpos (point)))
  18508. (setq lastpos (point))
  18509. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  18510. (setq tags (append (org-split-string
  18511. (org-match-string-no-properties 1) ":")
  18512. tags)))
  18513. (or org-use-tag-inheritance (error ""))
  18514. (org-up-heading-all 1))
  18515. (error nil))))
  18516. tags)))
  18517. ;; FIXME: should fix the tags property of the agenda line.
  18518. (defun org-agenda-set-tags ()
  18519. "Set tags for the current headline."
  18520. (interactive)
  18521. (org-agenda-check-no-diary)
  18522. (if (and (org-region-active-p) (interactive-p))
  18523. (call-interactively 'org-change-tag-in-region)
  18524. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18525. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18526. (org-agenda-error)))
  18527. (buffer (marker-buffer hdmarker))
  18528. (pos (marker-position hdmarker))
  18529. (inhibit-read-only t)
  18530. newhead)
  18531. (org-with-remote-undo buffer
  18532. (with-current-buffer buffer
  18533. (widen)
  18534. (goto-char pos)
  18535. (save-excursion
  18536. (org-show-context 'agenda))
  18537. (save-excursion
  18538. (and (outline-next-heading)
  18539. (org-flag-heading nil))) ; show the next heading
  18540. (goto-char pos)
  18541. (call-interactively 'org-set-tags)
  18542. (end-of-line 1)
  18543. (setq newhead (org-get-heading)))
  18544. (org-agenda-change-all-lines newhead hdmarker)
  18545. (beginning-of-line 1)))))
  18546. (defun org-agenda-toggle-archive-tag ()
  18547. "Toggle the archive tag for the current entry."
  18548. (interactive)
  18549. (org-agenda-check-no-diary)
  18550. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18551. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18552. (org-agenda-error)))
  18553. (buffer (marker-buffer hdmarker))
  18554. (pos (marker-position hdmarker))
  18555. (inhibit-read-only t)
  18556. newhead)
  18557. (org-with-remote-undo buffer
  18558. (with-current-buffer buffer
  18559. (widen)
  18560. (goto-char pos)
  18561. (org-show-context 'agenda)
  18562. (save-excursion
  18563. (and (outline-next-heading)
  18564. (org-flag-heading nil))) ; show the next heading
  18565. (call-interactively 'org-toggle-archive-tag)
  18566. (end-of-line 1)
  18567. (setq newhead (org-get-heading)))
  18568. (org-agenda-change-all-lines newhead hdmarker)
  18569. (beginning-of-line 1))))
  18570. (defun org-agenda-date-later (arg &optional what)
  18571. "Change the date of this item to one day later."
  18572. (interactive "p")
  18573. (org-agenda-check-type t 'agenda 'timeline)
  18574. (org-agenda-check-no-diary)
  18575. (let* ((marker (or (get-text-property (point) 'org-marker)
  18576. (org-agenda-error)))
  18577. (buffer (marker-buffer marker))
  18578. (pos (marker-position marker)))
  18579. (org-with-remote-undo buffer
  18580. (with-current-buffer buffer
  18581. (widen)
  18582. (goto-char pos)
  18583. (if (not (org-at-timestamp-p))
  18584. (error "Cannot find time stamp"))
  18585. (org-timestamp-change arg (or what 'day)))
  18586. (org-agenda-show-new-time marker org-last-changed-timestamp))
  18587. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  18588. (defun org-agenda-date-earlier (arg &optional what)
  18589. "Change the date of this item to one day earlier."
  18590. (interactive "p")
  18591. (org-agenda-date-later (- arg) what))
  18592. (defun org-agenda-show-new-time (marker stamp)
  18593. "Show new date stamp via text properties."
  18594. ;; We use text properties to make this undoable
  18595. (let ((inhibit-read-only t))
  18596. (setq stamp (concat " => " stamp))
  18597. (save-excursion
  18598. (goto-char (point-max))
  18599. (while (not (bobp))
  18600. (when (equal marker (get-text-property (point) 'org-marker))
  18601. (move-to-column (- (window-width) (length stamp)) t)
  18602. (if (featurep 'xemacs)
  18603. ;; Use `duplicable' property to trigger undo recording
  18604. (let ((ex (make-extent nil nil))
  18605. (gl (make-glyph stamp)))
  18606. (set-glyph-face gl 'secondary-selection)
  18607. (set-extent-properties
  18608. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  18609. (insert-extent ex (1- (point)) (point-at-eol)))
  18610. (add-text-properties
  18611. (1- (point)) (point-at-eol)
  18612. (list 'display (org-add-props stamp nil
  18613. 'face 'secondary-selection))))
  18614. (beginning-of-line 1))
  18615. (beginning-of-line 0)))))
  18616. (defun org-agenda-date-prompt (arg)
  18617. "Change the date of this item. Date is prompted for, with default today.
  18618. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  18619. be used to request time specification in the time stamp."
  18620. (interactive "P")
  18621. (org-agenda-check-type t 'agenda 'timeline)
  18622. (org-agenda-check-no-diary)
  18623. (let* ((marker (or (get-text-property (point) 'org-marker)
  18624. (org-agenda-error)))
  18625. (buffer (marker-buffer marker))
  18626. (pos (marker-position marker)))
  18627. (org-with-remote-undo buffer
  18628. (with-current-buffer buffer
  18629. (widen)
  18630. (goto-char pos)
  18631. (if (not (org-at-timestamp-p))
  18632. (error "Cannot find time stamp"))
  18633. (org-time-stamp arg)
  18634. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  18635. (defun org-agenda-schedule (arg)
  18636. "Schedule the item at point."
  18637. (interactive "P")
  18638. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18639. (org-agenda-check-no-diary)
  18640. (let* ((marker (or (get-text-property (point) 'org-marker)
  18641. (org-agenda-error)))
  18642. (buffer (marker-buffer marker))
  18643. (pos (marker-position marker))
  18644. (org-insert-labeled-timestamps-at-point nil)
  18645. ts)
  18646. (org-with-remote-undo buffer
  18647. (with-current-buffer buffer
  18648. (widen)
  18649. (goto-char pos)
  18650. (setq ts (org-schedule))
  18651. (message "Item scheduled for %s" ts)))))
  18652. (defun org-agenda-deadline (arg)
  18653. "Schedule the item at point."
  18654. (interactive "P")
  18655. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18656. (org-agenda-check-no-diary)
  18657. (let* ((marker (or (get-text-property (point) 'org-marker)
  18658. (org-agenda-error)))
  18659. (buffer (marker-buffer marker))
  18660. (pos (marker-position marker))
  18661. (org-insert-labeled-timestamps-at-point nil)
  18662. ts)
  18663. (org-with-remote-undo buffer
  18664. (with-current-buffer buffer
  18665. (widen)
  18666. (goto-char pos)
  18667. (setq ts (org-deadline))
  18668. (message "Deadline for this item set to %s" ts)))))
  18669. (defun org-get-heading (&optional no-tags)
  18670. "Return the heading of the current entry, without the stars."
  18671. (save-excursion
  18672. (org-back-to-heading t)
  18673. (if (looking-at
  18674. (if no-tags
  18675. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  18676. "\\*+[ \t]+\\([^\r\n]*\\)"))
  18677. (match-string 1) "")))
  18678. (defun org-agenda-clock-in (&optional arg)
  18679. "Start the clock on the currently selected item."
  18680. (interactive "P")
  18681. (org-agenda-check-no-diary)
  18682. (let* ((marker (or (get-text-property (point) 'org-marker)
  18683. (org-agenda-error)))
  18684. (pos (marker-position marker)))
  18685. (org-with-remote-undo (marker-buffer marker)
  18686. (with-current-buffer (marker-buffer marker)
  18687. (widen)
  18688. (goto-char pos)
  18689. (org-clock-in)))))
  18690. (defun org-agenda-clock-out (&optional arg)
  18691. "Stop the currently running clock."
  18692. (interactive "P")
  18693. (unless (marker-buffer org-clock-marker)
  18694. (error "No running clock"))
  18695. (org-with-remote-undo (marker-buffer org-clock-marker)
  18696. (org-clock-out)))
  18697. (defun org-agenda-clock-cancel (&optional arg)
  18698. "Cancel the currently running clock."
  18699. (interactive "P")
  18700. (unless (marker-buffer org-clock-marker)
  18701. (error "No running clock"))
  18702. (org-with-remote-undo (marker-buffer org-clock-marker)
  18703. (org-clock-cancel)))
  18704. (defun org-agenda-diary-entry ()
  18705. "Make a diary entry, like the `i' command from the calendar.
  18706. All the standard commands work: block, weekly etc."
  18707. (interactive)
  18708. (org-agenda-check-type t 'agenda 'timeline)
  18709. (require 'diary-lib)
  18710. (let* ((char (progn
  18711. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  18712. (read-char-exclusive)))
  18713. (cmd (cdr (assoc char
  18714. '((?d . insert-diary-entry)
  18715. (?w . insert-weekly-diary-entry)
  18716. (?m . insert-monthly-diary-entry)
  18717. (?y . insert-yearly-diary-entry)
  18718. (?a . insert-anniversary-diary-entry)
  18719. (?b . insert-block-diary-entry)
  18720. (?c . insert-cyclic-diary-entry)))))
  18721. (oldf (symbol-function 'calendar-cursor-to-date))
  18722. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  18723. (point (point))
  18724. (mark (or (mark t) (point))))
  18725. (unless cmd
  18726. (error "No command associated with <%c>" char))
  18727. (unless (and (get-text-property point 'day)
  18728. (or (not (equal ?b char))
  18729. (get-text-property mark 'day)))
  18730. (error "Don't know which date to use for diary entry"))
  18731. ;; We implement this by hacking the `calendar-cursor-to-date' function
  18732. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  18733. (let ((calendar-mark-ring
  18734. (list (calendar-gregorian-from-absolute
  18735. (or (get-text-property mark 'day)
  18736. (get-text-property point 'day))))))
  18737. (unwind-protect
  18738. (progn
  18739. (fset 'calendar-cursor-to-date
  18740. (lambda (&optional error)
  18741. (calendar-gregorian-from-absolute
  18742. (get-text-property point 'day))))
  18743. (call-interactively cmd))
  18744. (fset 'calendar-cursor-to-date oldf)))))
  18745. (defun org-agenda-execute-calendar-command (cmd)
  18746. "Execute a calendar command from the agenda, with the date associated to
  18747. the cursor position."
  18748. (org-agenda-check-type t 'agenda 'timeline)
  18749. (require 'diary-lib)
  18750. (unless (get-text-property (point) 'day)
  18751. (error "Don't know which date to use for calendar command"))
  18752. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  18753. (point (point))
  18754. (date (calendar-gregorian-from-absolute
  18755. (get-text-property point 'day)))
  18756. ;; the following 3 vars are needed in the calendar
  18757. (displayed-day (extract-calendar-day date))
  18758. (displayed-month (extract-calendar-month date))
  18759. (displayed-year (extract-calendar-year date)))
  18760. (unwind-protect
  18761. (progn
  18762. (fset 'calendar-cursor-to-date
  18763. (lambda (&optional error)
  18764. (calendar-gregorian-from-absolute
  18765. (get-text-property point 'day))))
  18766. (call-interactively cmd))
  18767. (fset 'calendar-cursor-to-date oldf))))
  18768. (defun org-agenda-phases-of-moon ()
  18769. "Display the phases of the moon for the 3 months around the cursor date."
  18770. (interactive)
  18771. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  18772. (defun org-agenda-holidays ()
  18773. "Display the holidays for the 3 months around the cursor date."
  18774. (interactive)
  18775. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  18776. (defun org-agenda-sunrise-sunset (arg)
  18777. "Display sunrise and sunset for the cursor date.
  18778. Latitude and longitude can be specified with the variables
  18779. `calendar-latitude' and `calendar-longitude'. When called with prefix
  18780. argument, latitude and longitude will be prompted for."
  18781. (interactive "P")
  18782. (let ((calendar-longitude (if arg nil calendar-longitude))
  18783. (calendar-latitude (if arg nil calendar-latitude))
  18784. (calendar-location-name
  18785. (if arg "the given coordinates" calendar-location-name)))
  18786. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  18787. (defun org-agenda-goto-calendar ()
  18788. "Open the Emacs calendar with the date at the cursor."
  18789. (interactive)
  18790. (org-agenda-check-type t 'agenda 'timeline)
  18791. (let* ((day (or (get-text-property (point) 'day)
  18792. (error "Don't know which date to open in calendar")))
  18793. (date (calendar-gregorian-from-absolute day))
  18794. (calendar-move-hook nil)
  18795. (view-calendar-holidays-initially nil)
  18796. (view-diary-entries-initially nil))
  18797. (calendar)
  18798. (calendar-goto-date date)))
  18799. (defun org-calendar-goto-agenda ()
  18800. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  18801. This is a command that has to be installed in `calendar-mode-map'."
  18802. (interactive)
  18803. (org-agenda-list nil (calendar-absolute-from-gregorian
  18804. (calendar-cursor-to-date))
  18805. nil))
  18806. (defun org-agenda-convert-date ()
  18807. (interactive)
  18808. (org-agenda-check-type t 'agenda 'timeline)
  18809. (let ((day (get-text-property (point) 'day))
  18810. date s)
  18811. (unless day
  18812. (error "Don't know which date to convert"))
  18813. (setq date (calendar-gregorian-from-absolute day))
  18814. (setq s (concat
  18815. "Gregorian: " (calendar-date-string date) "\n"
  18816. "ISO: " (calendar-iso-date-string date) "\n"
  18817. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  18818. "Julian: " (calendar-julian-date-string date) "\n"
  18819. "Astron. JD: " (calendar-astro-date-string date)
  18820. " (Julian date number at noon UTC)\n"
  18821. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  18822. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  18823. "French: " (calendar-french-date-string date) "\n"
  18824. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  18825. "Mayan: " (calendar-mayan-date-string date) "\n"
  18826. "Coptic: " (calendar-coptic-date-string date) "\n"
  18827. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  18828. "Persian: " (calendar-persian-date-string date) "\n"
  18829. "Chinese: " (calendar-chinese-date-string date) "\n"))
  18830. (with-output-to-temp-buffer "*Dates*"
  18831. (princ s))
  18832. (if (fboundp 'fit-window-to-buffer)
  18833. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  18834. ;;;; Embedded LaTeX
  18835. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  18836. "Keymap for the minor `org-cdlatex-mode'.")
  18837. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  18838. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  18839. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  18840. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  18841. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  18842. (defvar org-cdlatex-texmathp-advice-is-done nil
  18843. "Flag remembering if we have applied the advice to texmathp already.")
  18844. (define-minor-mode org-cdlatex-mode
  18845. "Toggle the minor `org-cdlatex-mode'.
  18846. This mode supports entering LaTeX environment and math in LaTeX fragments
  18847. in Org-mode.
  18848. \\{org-cdlatex-mode-map}"
  18849. nil " OCDL" nil
  18850. (when org-cdlatex-mode (require 'cdlatex))
  18851. (unless org-cdlatex-texmathp-advice-is-done
  18852. (setq org-cdlatex-texmathp-advice-is-done t)
  18853. (defadvice texmathp (around org-math-always-on activate)
  18854. "Always return t in org-mode buffers.
  18855. This is because we want to insert math symbols without dollars even outside
  18856. the LaTeX math segments. If Orgmode thinks that point is actually inside
  18857. en embedded LaTeX fragement, let texmathp do its job.
  18858. \\[org-cdlatex-mode-map]"
  18859. (interactive)
  18860. (let (p)
  18861. (cond
  18862. ((not (org-mode-p)) ad-do-it)
  18863. ((eq this-command 'cdlatex-math-symbol)
  18864. (setq ad-return-value t
  18865. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  18866. (t
  18867. (let ((p (org-inside-LaTeX-fragment-p)))
  18868. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  18869. (setq ad-return-value t
  18870. texmathp-why '("Org-mode embedded math" . 0))
  18871. (if p ad-do-it)))))))))
  18872. (defun turn-on-org-cdlatex ()
  18873. "Unconditionally turn on `org-cdlatex-mode'."
  18874. (org-cdlatex-mode 1))
  18875. (defun org-inside-LaTeX-fragment-p ()
  18876. "Test if point is inside a LaTeX fragment.
  18877. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  18878. sequence appearing also before point.
  18879. Even though the matchers for math are configurable, this function assumes
  18880. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  18881. delimiters are skipped when they have been removed by customization.
  18882. The return value is nil, or a cons cell with the delimiter and
  18883. and the position of this delimiter.
  18884. This function does a reasonably good job, but can locally be fooled by
  18885. for example currency specifications. For example it will assume being in
  18886. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  18887. fragments that are properly closed, but during editing, we have to live
  18888. with the uncertainty caused by missing closing delimiters. This function
  18889. looks only before point, not after."
  18890. (catch 'exit
  18891. (let ((pos (point))
  18892. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  18893. (lim (progn
  18894. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  18895. (point)))
  18896. dd-on str (start 0) m re)
  18897. (goto-char pos)
  18898. (when dodollar
  18899. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  18900. re (nth 1 (assoc "$" org-latex-regexps)))
  18901. (while (string-match re str start)
  18902. (cond
  18903. ((= (match-end 0) (length str))
  18904. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  18905. ((= (match-end 0) (- (length str) 5))
  18906. (throw 'exit nil))
  18907. (t (setq start (match-end 0))))))
  18908. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  18909. (goto-char pos)
  18910. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  18911. (and (match-beginning 2) (throw 'exit nil))
  18912. ;; count $$
  18913. (while (re-search-backward "\\$\\$" lim t)
  18914. (setq dd-on (not dd-on)))
  18915. (goto-char pos)
  18916. (if dd-on (cons "$$" m))))))
  18917. (defun org-try-cdlatex-tab ()
  18918. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  18919. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  18920. - inside a LaTeX fragment, or
  18921. - after the first word in a line, where an abbreviation expansion could
  18922. insert a LaTeX environment."
  18923. (when org-cdlatex-mode
  18924. (cond
  18925. ((save-excursion
  18926. (skip-chars-backward "a-zA-Z0-9*")
  18927. (skip-chars-backward " \t")
  18928. (bolp))
  18929. (cdlatex-tab) t)
  18930. ((org-inside-LaTeX-fragment-p)
  18931. (cdlatex-tab) t)
  18932. (t nil))))
  18933. (defun org-cdlatex-underscore-caret (&optional arg)
  18934. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  18935. Revert to the normal definition outside of these fragments."
  18936. (interactive "P")
  18937. (if (org-inside-LaTeX-fragment-p)
  18938. (call-interactively 'cdlatex-sub-superscript)
  18939. (let (org-cdlatex-mode)
  18940. (call-interactively (key-binding (vector last-input-event))))))
  18941. (defun org-cdlatex-math-modify (&optional arg)
  18942. "Execute `cdlatex-math-modify' in LaTeX fragments.
  18943. Revert to the normal definition outside of these fragments."
  18944. (interactive "P")
  18945. (if (org-inside-LaTeX-fragment-p)
  18946. (call-interactively 'cdlatex-math-modify)
  18947. (let (org-cdlatex-mode)
  18948. (call-interactively (key-binding (vector last-input-event))))))
  18949. (defvar org-latex-fragment-image-overlays nil
  18950. "List of overlays carrying the images of latex fragments.")
  18951. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  18952. (defun org-remove-latex-fragment-image-overlays ()
  18953. "Remove all overlays with LaTeX fragment images in current buffer."
  18954. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  18955. (setq org-latex-fragment-image-overlays nil))
  18956. (defun org-preview-latex-fragment (&optional subtree)
  18957. "Preview the LaTeX fragment at point, or all locally or globally.
  18958. If the cursor is in a LaTeX fragment, create the image and overlay
  18959. it over the source code. If there is no fragment at point, display
  18960. all fragments in the current text, from one headline to the next. With
  18961. prefix SUBTREE, display all fragments in the current subtree. With a
  18962. double prefix `C-u C-u', or when the cursor is before the first headline,
  18963. display all fragments in the buffer.
  18964. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  18965. (interactive "P")
  18966. (org-remove-latex-fragment-image-overlays)
  18967. (save-excursion
  18968. (save-restriction
  18969. (let (beg end at msg)
  18970. (cond
  18971. ((or (equal subtree '(16))
  18972. (not (save-excursion
  18973. (re-search-backward (concat "^" outline-regexp) nil t))))
  18974. (setq beg (point-min) end (point-max)
  18975. msg "Creating images for buffer...%s"))
  18976. ((equal subtree '(4))
  18977. (org-back-to-heading)
  18978. (setq beg (point) end (org-end-of-subtree t)
  18979. msg "Creating images for subtree...%s"))
  18980. (t
  18981. (if (setq at (org-inside-LaTeX-fragment-p))
  18982. (goto-char (max (point-min) (- (cdr at) 2)))
  18983. (org-back-to-heading))
  18984. (setq beg (point) end (progn (outline-next-heading) (point))
  18985. msg (if at "Creating image...%s"
  18986. "Creating images for entry...%s"))))
  18987. (message msg "")
  18988. (narrow-to-region beg end)
  18989. (goto-char beg)
  18990. (org-format-latex
  18991. (concat "ltxpng/" (file-name-sans-extension
  18992. (file-name-nondirectory
  18993. buffer-file-name)))
  18994. default-directory 'overlays msg at 'forbuffer)
  18995. (message msg "done. Use `C-c C-c' to remove images.")))))
  18996. (defvar org-latex-regexps
  18997. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  18998. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  18999. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  19000. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  19001. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  19002. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  19003. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  19004. "Regular expressions for matching embedded LaTeX.")
  19005. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  19006. "Replace LaTeX fragments with links to an image, and produce images."
  19007. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  19008. (let* ((prefixnodir (file-name-nondirectory prefix))
  19009. (absprefix (expand-file-name prefix dir))
  19010. (todir (file-name-directory absprefix))
  19011. (opt org-format-latex-options)
  19012. (matchers (plist-get opt :matchers))
  19013. (re-list org-latex-regexps)
  19014. (cnt 0) txt link beg end re e checkdir
  19015. m n block linkfile movefile ov)
  19016. ;; Check if there are old images files with this prefix, and remove them
  19017. (when (file-directory-p todir)
  19018. (mapc 'delete-file
  19019. (directory-files
  19020. todir 'full
  19021. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  19022. ;; Check the different regular expressions
  19023. (while (setq e (pop re-list))
  19024. (setq m (car e) re (nth 1 e) n (nth 2 e)
  19025. block (if (nth 3 e) "\n\n" ""))
  19026. (when (member m matchers)
  19027. (goto-char (point-min))
  19028. (while (re-search-forward re nil t)
  19029. (when (or (not at) (equal (cdr at) (match-beginning n)))
  19030. (setq txt (match-string n)
  19031. beg (match-beginning n) end (match-end n)
  19032. cnt (1+ cnt)
  19033. linkfile (format "%s_%04d.png" prefix cnt)
  19034. movefile (format "%s_%04d.png" absprefix cnt)
  19035. link (concat block "[[file:" linkfile "]]" block))
  19036. (if msg (message msg cnt))
  19037. (goto-char beg)
  19038. (unless checkdir ; make sure the directory exists
  19039. (setq checkdir t)
  19040. (or (file-directory-p todir) (make-directory todir)))
  19041. (org-create-formula-image
  19042. txt movefile opt forbuffer)
  19043. (if overlays
  19044. (progn
  19045. (setq ov (org-make-overlay beg end))
  19046. (if (featurep 'xemacs)
  19047. (progn
  19048. (org-overlay-put ov 'invisible t)
  19049. (org-overlay-put
  19050. ov 'end-glyph
  19051. (make-glyph (vector 'png :file movefile))))
  19052. (org-overlay-put
  19053. ov 'display
  19054. (list 'image :type 'png :file movefile :ascent 'center)))
  19055. (push ov org-latex-fragment-image-overlays)
  19056. (goto-char end))
  19057. (delete-region beg end)
  19058. (insert link))))))))
  19059. ;; This function borrows from Ganesh Swami's latex2png.el
  19060. (defun org-create-formula-image (string tofile options buffer)
  19061. (let* ((tmpdir (if (featurep 'xemacs)
  19062. (temp-directory)
  19063. temporary-file-directory))
  19064. (texfilebase (make-temp-name
  19065. (expand-file-name "orgtex" tmpdir)))
  19066. (texfile (concat texfilebase ".tex"))
  19067. (dvifile (concat texfilebase ".dvi"))
  19068. (pngfile (concat texfilebase ".png"))
  19069. (fnh (face-attribute 'default :height nil))
  19070. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  19071. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  19072. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  19073. "Black"))
  19074. (bg (or (plist-get options (if buffer :background :html-background))
  19075. "Transparent")))
  19076. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  19077. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  19078. (with-temp-file texfile
  19079. (insert org-format-latex-header
  19080. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  19081. (let ((dir default-directory))
  19082. (condition-case nil
  19083. (progn
  19084. (cd tmpdir)
  19085. (call-process "latex" nil nil nil texfile))
  19086. (error nil))
  19087. (cd dir))
  19088. (if (not (file-exists-p dvifile))
  19089. (progn (message "Failed to create dvi file from %s" texfile) nil)
  19090. (call-process "dvipng" nil nil nil
  19091. "-E" "-fg" fg "-bg" bg
  19092. "-D" dpi
  19093. ;;"-x" scale "-y" scale
  19094. "-T" "tight"
  19095. "-o" pngfile
  19096. dvifile)
  19097. (if (not (file-exists-p pngfile))
  19098. (progn (message "Failed to create png file from %s" texfile) nil)
  19099. ;; Use the requested file name and clean up
  19100. (copy-file pngfile tofile 'replace)
  19101. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  19102. (delete-file (concat texfilebase e)))
  19103. pngfile))))
  19104. (defun org-dvipng-color (attr)
  19105. "Return an rgb color specification for dvipng."
  19106. (apply 'format "rgb %s %s %s"
  19107. (mapcar 'org-normalize-color
  19108. (color-values (face-attribute 'default attr nil)))))
  19109. (defun org-normalize-color (value)
  19110. "Return string to be used as color value for an RGB component."
  19111. (format "%g" (/ value 65535.0)))
  19112. ;;;; Exporting
  19113. ;;; Variables, constants, and parameter plists
  19114. (defconst org-level-max 20)
  19115. (defvar org-export-html-preamble nil
  19116. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  19117. (defvar org-export-html-postamble nil
  19118. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  19119. (defvar org-export-html-auto-preamble t
  19120. "Should default preamble be inserted? Set by publishing functions.")
  19121. (defvar org-export-html-auto-postamble t
  19122. "Should default postamble be inserted? Set by publishing functions.")
  19123. (defvar org-current-export-file nil) ; dynamically scoped parameter
  19124. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  19125. (defconst org-export-plist-vars
  19126. '((:language . org-export-default-language)
  19127. (:customtime . org-display-custom-times)
  19128. (:headline-levels . org-export-headline-levels)
  19129. (:section-numbers . org-export-with-section-numbers)
  19130. (:table-of-contents . org-export-with-toc)
  19131. (:preserve-breaks . org-export-preserve-breaks)
  19132. (:archived-trees . org-export-with-archived-trees)
  19133. (:emphasize . org-export-with-emphasize)
  19134. (:sub-superscript . org-export-with-sub-superscripts)
  19135. (:footnotes . org-export-with-footnotes)
  19136. (:property-drawer . org-export-with-property-drawer)
  19137. (:TeX-macros . org-export-with-TeX-macros)
  19138. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  19139. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  19140. (:fixed-width . org-export-with-fixed-width)
  19141. (:timestamps . org-export-with-timestamps)
  19142. (:author-info . org-export-author-info)
  19143. (:time-stamp-file . org-export-time-stamp-file)
  19144. (:tables . org-export-with-tables)
  19145. (:table-auto-headline . org-export-highlight-first-table-line)
  19146. (:style . org-export-html-style)
  19147. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  19148. (:convert-org-links . org-export-html-link-org-files-as-html)
  19149. (:inline-images . org-export-html-inline-images)
  19150. (:html-extension . org-export-html-extension)
  19151. (:expand-quoted-html . org-export-html-expand)
  19152. (:timestamp . org-export-html-with-timestamp)
  19153. (:publishing-directory . org-export-publishing-directory)
  19154. (:preamble . org-export-html-preamble)
  19155. (:postamble . org-export-html-postamble)
  19156. (:auto-preamble . org-export-html-auto-preamble)
  19157. (:auto-postamble . org-export-html-auto-postamble)
  19158. (:author . user-full-name)
  19159. (:email . user-mail-address)))
  19160. (defun org-default-export-plist ()
  19161. "Return the property list with default settings for the export variables."
  19162. (let ((l org-export-plist-vars) rtn e)
  19163. (while (setq e (pop l))
  19164. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  19165. rtn))
  19166. (defun org-infile-export-plist ()
  19167. "Return the property list with file-local settings for export."
  19168. (save-excursion
  19169. (goto-char 0)
  19170. (let ((re (org-make-options-regexp
  19171. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  19172. p key val text options)
  19173. (while (re-search-forward re nil t)
  19174. (setq key (org-match-string-no-properties 1)
  19175. val (org-match-string-no-properties 2))
  19176. (cond
  19177. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  19178. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  19179. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  19180. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  19181. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  19182. ((string-equal key "TEXT")
  19183. (setq text (if text (concat text "\n" val) val)))
  19184. ((string-equal key "OPTIONS") (setq options val))))
  19185. (setq p (plist-put p :text text))
  19186. (when options
  19187. (let ((op '(("H" . :headline-levels)
  19188. ("num" . :section-numbers)
  19189. ("toc" . :table-of-contents)
  19190. ("\\n" . :preserve-breaks)
  19191. ("@" . :expand-quoted-html)
  19192. (":" . :fixed-width)
  19193. ("|" . :tables)
  19194. ("^" . :sub-superscript)
  19195. ("f" . :footnotes)
  19196. ("p" . :property-drawer)
  19197. ("*" . :emphasize)
  19198. ("TeX" . :TeX-macros)
  19199. ("LaTeX" . :LaTeX-fragments)
  19200. ("skip" . :skip-before-1st-heading)
  19201. ("author" . :author-info)
  19202. ("timestamp" . :time-stamp-file)))
  19203. o)
  19204. (while (setq o (pop op))
  19205. (if (string-match (concat (regexp-quote (car o))
  19206. ":\\([^ \t\n\r;,.]*\\)")
  19207. options)
  19208. (setq p (plist-put p (cdr o)
  19209. (car (read-from-string
  19210. (match-string 1 options)))))))))
  19211. p)))
  19212. (defun org-export-directory (type plist)
  19213. (let* ((val (plist-get plist :publishing-directory))
  19214. (dir (if (listp val)
  19215. (or (cdr (assoc type val)) ".")
  19216. val)))
  19217. dir))
  19218. (defun org-skip-comments (lines)
  19219. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  19220. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  19221. (re2 "^\\(\\*+\\)[ \t\n\r]")
  19222. (case-fold-search nil)
  19223. rtn line level)
  19224. (while (setq line (pop lines))
  19225. (cond
  19226. ((and (string-match re1 line)
  19227. (setq level (- (match-end 1) (match-beginning 1))))
  19228. ;; Beginning of a COMMENT subtree. Skip it.
  19229. (while (and (setq line (pop lines))
  19230. (or (not (string-match re2 line))
  19231. (> (- (match-end 1) (match-beginning 1)) level))))
  19232. (setq lines (cons line lines)))
  19233. ((string-match "^#" line)
  19234. ;; an ordinary comment line
  19235. )
  19236. ((and org-export-table-remove-special-lines
  19237. (string-match "^[ \t]*|" line)
  19238. (or (string-match "^[ \t]*| *[!_^] *|" line)
  19239. (and (string-match "| *<[0-9]+> *|" line)
  19240. (not (string-match "| *[^ <|]" line)))))
  19241. ;; a special table line that should be removed
  19242. )
  19243. (t (setq rtn (cons line rtn)))))
  19244. (nreverse rtn)))
  19245. (defun org-export (&optional arg)
  19246. (interactive)
  19247. (let ((help "[t] insert the export option template
  19248. \[v] limit export to visible part of outline tree
  19249. \[a] export as ASCII
  19250. \[h] export as HTML
  19251. \[H] export as HTML to temporary buffer
  19252. \[R] export region as HTML
  19253. \[b] export as HTML and browse immediately
  19254. \[x] export as XOXO
  19255. \[l] export as LaTeX
  19256. \[L] export as LaTeX to temporary buffer
  19257. \[i] export current file as iCalendar file
  19258. \[I] export all agenda files as iCalendar files
  19259. \[c] export agenda files into combined iCalendar file
  19260. \[F] publish current file
  19261. \[P] publish current project
  19262. \[X] publish... (project will be prompted for)
  19263. \[A] publish all projects")
  19264. (cmds
  19265. '((?t . org-insert-export-options-template)
  19266. (?v . org-export-visible)
  19267. (?a . org-export-as-ascii)
  19268. (?h . org-export-as-html)
  19269. (?b . org-export-as-html-and-open)
  19270. (?H . org-export-as-html-to-buffer)
  19271. (?R . org-export-region-as-html)
  19272. (?x . org-export-as-xoxo)
  19273. (?l . org-export-as-latex)
  19274. (?L . org-export-as-latex-to-buffer)
  19275. (?i . org-export-icalendar-this-file)
  19276. (?I . org-export-icalendar-all-agenda-files)
  19277. (?c . org-export-icalendar-combine-agenda-files)
  19278. (?F . org-publish-current-file)
  19279. (?P . org-publish-current-project)
  19280. (?X . org-publish)
  19281. (?A . org-publish-all)))
  19282. r1 r2 ass)
  19283. (save-window-excursion
  19284. (delete-other-windows)
  19285. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  19286. (princ help))
  19287. (message "Select command: ")
  19288. (setq r1 (read-char-exclusive)))
  19289. (setq r2 (if (< r1 27) (+ r1 96) r1))
  19290. (if (setq ass (assq r2 cmds))
  19291. (call-interactively (cdr ass))
  19292. (error "No command associated with key %c" r1))))
  19293. (defconst org-html-entities
  19294. '(("nbsp")
  19295. ("iexcl")
  19296. ("cent")
  19297. ("pound")
  19298. ("curren")
  19299. ("yen")
  19300. ("brvbar")
  19301. ("vert" . "&#124;")
  19302. ("sect")
  19303. ("uml")
  19304. ("copy")
  19305. ("ordf")
  19306. ("laquo")
  19307. ("not")
  19308. ("shy")
  19309. ("reg")
  19310. ("macr")
  19311. ("deg")
  19312. ("plusmn")
  19313. ("sup2")
  19314. ("sup3")
  19315. ("acute")
  19316. ("micro")
  19317. ("para")
  19318. ("middot")
  19319. ("odot"."o")
  19320. ("star"."*")
  19321. ("cedil")
  19322. ("sup1")
  19323. ("ordm")
  19324. ("raquo")
  19325. ("frac14")
  19326. ("frac12")
  19327. ("frac34")
  19328. ("iquest")
  19329. ("Agrave")
  19330. ("Aacute")
  19331. ("Acirc")
  19332. ("Atilde")
  19333. ("Auml")
  19334. ("Aring") ("AA"."&Aring;")
  19335. ("AElig")
  19336. ("Ccedil")
  19337. ("Egrave")
  19338. ("Eacute")
  19339. ("Ecirc")
  19340. ("Euml")
  19341. ("Igrave")
  19342. ("Iacute")
  19343. ("Icirc")
  19344. ("Iuml")
  19345. ("ETH")
  19346. ("Ntilde")
  19347. ("Ograve")
  19348. ("Oacute")
  19349. ("Ocirc")
  19350. ("Otilde")
  19351. ("Ouml")
  19352. ("times")
  19353. ("Oslash")
  19354. ("Ugrave")
  19355. ("Uacute")
  19356. ("Ucirc")
  19357. ("Uuml")
  19358. ("Yacute")
  19359. ("THORN")
  19360. ("szlig")
  19361. ("agrave")
  19362. ("aacute")
  19363. ("acirc")
  19364. ("atilde")
  19365. ("auml")
  19366. ("aring")
  19367. ("aelig")
  19368. ("ccedil")
  19369. ("egrave")
  19370. ("eacute")
  19371. ("ecirc")
  19372. ("euml")
  19373. ("igrave")
  19374. ("iacute")
  19375. ("icirc")
  19376. ("iuml")
  19377. ("eth")
  19378. ("ntilde")
  19379. ("ograve")
  19380. ("oacute")
  19381. ("ocirc")
  19382. ("otilde")
  19383. ("ouml")
  19384. ("divide")
  19385. ("oslash")
  19386. ("ugrave")
  19387. ("uacute")
  19388. ("ucirc")
  19389. ("uuml")
  19390. ("yacute")
  19391. ("thorn")
  19392. ("yuml")
  19393. ("fnof")
  19394. ("Alpha")
  19395. ("Beta")
  19396. ("Gamma")
  19397. ("Delta")
  19398. ("Epsilon")
  19399. ("Zeta")
  19400. ("Eta")
  19401. ("Theta")
  19402. ("Iota")
  19403. ("Kappa")
  19404. ("Lambda")
  19405. ("Mu")
  19406. ("Nu")
  19407. ("Xi")
  19408. ("Omicron")
  19409. ("Pi")
  19410. ("Rho")
  19411. ("Sigma")
  19412. ("Tau")
  19413. ("Upsilon")
  19414. ("Phi")
  19415. ("Chi")
  19416. ("Psi")
  19417. ("Omega")
  19418. ("alpha")
  19419. ("beta")
  19420. ("gamma")
  19421. ("delta")
  19422. ("epsilon")
  19423. ("varepsilon"."&epsilon;")
  19424. ("zeta")
  19425. ("eta")
  19426. ("theta")
  19427. ("iota")
  19428. ("kappa")
  19429. ("lambda")
  19430. ("mu")
  19431. ("nu")
  19432. ("xi")
  19433. ("omicron")
  19434. ("pi")
  19435. ("rho")
  19436. ("sigmaf") ("varsigma"."&sigmaf;")
  19437. ("sigma")
  19438. ("tau")
  19439. ("upsilon")
  19440. ("phi")
  19441. ("chi")
  19442. ("psi")
  19443. ("omega")
  19444. ("thetasym") ("vartheta"."&thetasym;")
  19445. ("upsih")
  19446. ("piv")
  19447. ("bull") ("bullet"."&bull;")
  19448. ("hellip") ("dots"."&hellip;")
  19449. ("prime")
  19450. ("Prime")
  19451. ("oline")
  19452. ("frasl")
  19453. ("weierp")
  19454. ("image")
  19455. ("real")
  19456. ("trade")
  19457. ("alefsym")
  19458. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  19459. ("uarr") ("uparrow"."&uarr;")
  19460. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  19461. ("darr")("downarrow"."&darr;")
  19462. ("harr") ("leftrightarrow"."&harr;")
  19463. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  19464. ("lArr") ("Leftarrow"."&lArr;")
  19465. ("uArr") ("Uparrow"."&uArr;")
  19466. ("rArr") ("Rightarrow"."&rArr;")
  19467. ("dArr") ("Downarrow"."&dArr;")
  19468. ("hArr") ("Leftrightarrow"."&hArr;")
  19469. ("forall")
  19470. ("part") ("partial"."&part;")
  19471. ("exist") ("exists"."&exist;")
  19472. ("empty") ("emptyset"."&empty;")
  19473. ("nabla")
  19474. ("isin") ("in"."&isin;")
  19475. ("notin")
  19476. ("ni")
  19477. ("prod")
  19478. ("sum")
  19479. ("minus")
  19480. ("lowast") ("ast"."&lowast;")
  19481. ("radic")
  19482. ("prop") ("proptp"."&prop;")
  19483. ("infin") ("infty"."&infin;")
  19484. ("ang") ("angle"."&ang;")
  19485. ("and") ("vee"."&and;")
  19486. ("or") ("wedge"."&or;")
  19487. ("cap")
  19488. ("cup")
  19489. ("int")
  19490. ("there4")
  19491. ("sim")
  19492. ("cong") ("simeq"."&cong;")
  19493. ("asymp")("approx"."&asymp;")
  19494. ("ne") ("neq"."&ne;")
  19495. ("equiv")
  19496. ("le")
  19497. ("ge")
  19498. ("sub") ("subset"."&sub;")
  19499. ("sup") ("supset"."&sup;")
  19500. ("nsub")
  19501. ("sube")
  19502. ("supe")
  19503. ("oplus")
  19504. ("otimes")
  19505. ("perp")
  19506. ("sdot") ("cdot"."&sdot;")
  19507. ("lceil")
  19508. ("rceil")
  19509. ("lfloor")
  19510. ("rfloor")
  19511. ("lang")
  19512. ("rang")
  19513. ("loz") ("Diamond"."&loz;")
  19514. ("spades") ("spadesuit"."&spades;")
  19515. ("clubs") ("clubsuit"."&clubs;")
  19516. ("hearts") ("diamondsuit"."&hearts;")
  19517. ("diams") ("diamondsuit"."&diams;")
  19518. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  19519. ("quot")
  19520. ("amp")
  19521. ("lt")
  19522. ("gt")
  19523. ("OElig")
  19524. ("oelig")
  19525. ("Scaron")
  19526. ("scaron")
  19527. ("Yuml")
  19528. ("circ")
  19529. ("tilde")
  19530. ("ensp")
  19531. ("emsp")
  19532. ("thinsp")
  19533. ("zwnj")
  19534. ("zwj")
  19535. ("lrm")
  19536. ("rlm")
  19537. ("ndash")
  19538. ("mdash")
  19539. ("lsquo")
  19540. ("rsquo")
  19541. ("sbquo")
  19542. ("ldquo")
  19543. ("rdquo")
  19544. ("bdquo")
  19545. ("dagger")
  19546. ("Dagger")
  19547. ("permil")
  19548. ("lsaquo")
  19549. ("rsaquo")
  19550. ("euro")
  19551. ("arccos"."arccos")
  19552. ("arcsin"."arcsin")
  19553. ("arctan"."arctan")
  19554. ("arg"."arg")
  19555. ("cos"."cos")
  19556. ("cosh"."cosh")
  19557. ("cot"."cot")
  19558. ("coth"."coth")
  19559. ("csc"."csc")
  19560. ("deg"."deg")
  19561. ("det"."det")
  19562. ("dim"."dim")
  19563. ("exp"."exp")
  19564. ("gcd"."gcd")
  19565. ("hom"."hom")
  19566. ("inf"."inf")
  19567. ("ker"."ker")
  19568. ("lg"."lg")
  19569. ("lim"."lim")
  19570. ("liminf"."liminf")
  19571. ("limsup"."limsup")
  19572. ("ln"."ln")
  19573. ("log"."log")
  19574. ("max"."max")
  19575. ("min"."min")
  19576. ("Pr"."Pr")
  19577. ("sec"."sec")
  19578. ("sin"."sin")
  19579. ("sinh"."sinh")
  19580. ("sup"."sup")
  19581. ("tan"."tan")
  19582. ("tanh"."tanh")
  19583. )
  19584. "Entities for TeX->HTML translation.
  19585. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  19586. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  19587. In that case, \"\\ent\" will be translated to \"&other;\".
  19588. The list contains HTML entities for Latin-1, Greek and other symbols.
  19589. It is supplemented by a number of commonly used TeX macros with appropriate
  19590. translations. There is currently no way for users to extend this.")
  19591. ;;; General functions for all backends
  19592. (defun org-cleaned-string-for-export (string &rest parameters)
  19593. "Cleanup a buffer STRING so that links can be created safely."
  19594. (interactive)
  19595. (let* ((re-radio (and org-target-link-regexp
  19596. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  19597. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  19598. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  19599. (re-archive (concat ":" org-archive-tag ":"))
  19600. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  19601. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  19602. (htmlp (plist-get parameters :for-html))
  19603. (asciip (plist-get parameters :for-ascii))
  19604. (latexp (plist-get parameters :for-LaTeX))
  19605. (commentsp (plist-get parameters :comments))
  19606. (archived-trees (plist-get parameters :archived-trees))
  19607. (inhibit-read-only t)
  19608. (outline-regexp "\\*+ ")
  19609. a b xx
  19610. rtn p)
  19611. (with-current-buffer (get-buffer-create " org-mode-tmp")
  19612. (erase-buffer)
  19613. (insert string)
  19614. ;; Remove license-to-kill stuff
  19615. (while (setq p (text-property-any (point-min) (point-max)
  19616. :org-license-to-kill t))
  19617. (delete-region p (next-single-property-change p :org-license-to-kill)))
  19618. (let ((org-inhibit-startup t)) (org-mode))
  19619. (untabify (point-min) (point-max))
  19620. ;; Get the correct stuff before the first headline
  19621. (when (plist-get parameters :skip-before-1st-heading)
  19622. (goto-char (point-min))
  19623. (when (re-search-forward "^\\*+[ \t]" nil t)
  19624. (delete-region (point-min) (match-beginning 0))
  19625. (goto-char (point-min))
  19626. (insert "\n")))
  19627. (when (plist-get parameters :add-text)
  19628. (goto-char (point-min))
  19629. (insert (plist-get parameters :add-text) "\n"))
  19630. ;; Get rid of archived trees
  19631. (when (not (eq archived-trees t))
  19632. (goto-char (point-min))
  19633. (while (re-search-forward re-archive nil t)
  19634. (if (not (org-on-heading-p t))
  19635. (org-end-of-subtree t)
  19636. (beginning-of-line 1)
  19637. (setq a (if archived-trees
  19638. (1+ (point-at-eol)) (point))
  19639. b (org-end-of-subtree t))
  19640. (if (> b a) (delete-region a b)))))
  19641. ;; Get rid of property drawers
  19642. (unless org-export-with-property-drawer
  19643. (goto-char (point-min))
  19644. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  19645. (replace-match "")))
  19646. ;; Find targets in comments and move them out of comments,
  19647. ;; but mark them as targets that should be invisible
  19648. (goto-char (point-min))
  19649. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  19650. (replace-match "\\1(INVISIBLE)"))
  19651. ;; Protect backend specific stuff, throw away the others.
  19652. (goto-char (point-min))
  19653. (let ((formatters
  19654. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  19655. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  19656. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  19657. fmt)
  19658. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  19659. (add-text-properties (match-beginning 0) (match-end 0)
  19660. '(org-protected t)))
  19661. (while formatters
  19662. (setq fmt (pop formatters))
  19663. (when (car fmt)
  19664. (goto-char (point-min))
  19665. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  19666. ":[ \t]*\\(.*\\)") nil t)
  19667. (replace-match "\\1" t)
  19668. (add-text-properties
  19669. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  19670. '(org-protected t))))
  19671. (goto-char (point-min))
  19672. (while (re-search-forward
  19673. (concat "^#\\+"
  19674. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  19675. (cadddr fmt) "\\>.*\n?") nil t)
  19676. (if (car fmt)
  19677. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  19678. '(org-protected t))
  19679. (delete-region (match-beginning 0) (match-end 0))))))
  19680. ;; Protect quoted subtrees
  19681. (goto-char (point-min))
  19682. (while (re-search-forward re-quote nil t)
  19683. (goto-char (match-beginning 0))
  19684. (end-of-line 1)
  19685. (add-text-properties (point) (org-end-of-subtree t)
  19686. '(org-protected t)))
  19687. ;; Remove subtrees that are commented
  19688. (goto-char (point-min))
  19689. (while (re-search-forward re-commented nil t)
  19690. (goto-char (match-beginning 0))
  19691. (delete-region (point) (org-end-of-subtree t)))
  19692. ;; Remove special table lines
  19693. (when org-export-table-remove-special-lines
  19694. (goto-char (point-min))
  19695. (while (re-search-forward "^[ \t]*|" nil t)
  19696. (beginning-of-line 1)
  19697. (if (or (looking-at "[ \t]*| *[!_^] *|")
  19698. (and (looking-at ".*?| *<[0-9]+> *|")
  19699. (not (looking-at ".*?| *[^ <|]"))))
  19700. (delete-region (max (point-min) (1- (point-at-bol)))
  19701. (point-at-eol))
  19702. (end-of-line 1))))
  19703. ;; Specific LaTeX stuff
  19704. (when latexp
  19705. (require 'org-export-latex nil)
  19706. (org-export-latex-cleaned-string))
  19707. ;; Specific HTML stuff
  19708. (when htmlp
  19709. ;; Convert LaTeX fragments to images
  19710. (when (plist-get parameters :LaTeX-fragments)
  19711. (org-format-latex
  19712. (concat "ltxpng/" (file-name-sans-extension
  19713. (file-name-nondirectory
  19714. org-current-export-file)))
  19715. org-current-export-dir nil "Creating LaTeX image %s"))
  19716. (message "Exporting..."))
  19717. ;; Remove or replace comments
  19718. (goto-char (point-min))
  19719. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  19720. (if commentsp
  19721. (progn (add-text-properties
  19722. (match-beginning 0) (match-end 0) '(org-protected t))
  19723. (replace-match (format commentsp (match-string 1)) t t))
  19724. (replace-match "")))
  19725. ;; Find matches for radio targets and turn them into internal links
  19726. (goto-char (point-min))
  19727. (when re-radio
  19728. (while (re-search-forward re-radio nil t)
  19729. (org-if-unprotected
  19730. (replace-match "\\1[[\\2]]"))))
  19731. ;; Find all links that contain a newline and put them into a single line
  19732. (goto-char (point-min))
  19733. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  19734. (org-if-unprotected
  19735. (replace-match "\\1 \\3")
  19736. (goto-char (match-beginning 0))))
  19737. ;; Normalize links: Convert angle and plain links into bracket links
  19738. ;; Expand link abbreviations
  19739. (goto-char (point-min))
  19740. (while (re-search-forward re-plain-link nil t)
  19741. (goto-char (1- (match-end 0)))
  19742. (org-if-unprotected
  19743. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19744. ":" (match-string 3) "]]")))
  19745. ;; added 'org-link face to links
  19746. (put-text-property 0 (length s) 'face 'org-link s)
  19747. (replace-match s t t))))
  19748. (goto-char (point-min))
  19749. (while (re-search-forward re-angle-link nil t)
  19750. (goto-char (1- (match-end 0)))
  19751. (org-if-unprotected
  19752. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19753. ":" (match-string 3) "]]")))
  19754. (put-text-property 0 (length s) 'face 'org-link s)
  19755. (replace-match s t t))))
  19756. (goto-char (point-min))
  19757. (while (re-search-forward org-bracket-link-regexp nil t)
  19758. (org-if-unprotected
  19759. (let* ((s (concat "[[" (setq xx (save-match-data
  19760. (org-link-expand-abbrev (match-string 1))))
  19761. "]"
  19762. (if (match-end 3)
  19763. (match-string 2)
  19764. (concat "[" xx "]"))
  19765. "]")))
  19766. (put-text-property 0 (length s) 'face 'org-link s)
  19767. (replace-match s t t))))
  19768. ;; Find multiline emphasis and put them into single line
  19769. (when (plist-get parameters :emph-multiline)
  19770. (goto-char (point-min))
  19771. (while (re-search-forward org-emph-re nil t)
  19772. (if (not (= (char-after (match-beginning 3))
  19773. (char-after (match-beginning 4))))
  19774. (org-if-unprotected
  19775. (subst-char-in-region (match-beginning 0) (match-end 0)
  19776. ?\n ?\ t)
  19777. (goto-char (1- (match-end 0))))
  19778. (goto-char (1+ (match-beginning 0))))))
  19779. (setq rtn (buffer-string)))
  19780. (kill-buffer " org-mode-tmp")
  19781. rtn))
  19782. (defun org-export-grab-title-from-buffer ()
  19783. "Get a title for the current document, from looking at the buffer."
  19784. (let ((inhibit-read-only t))
  19785. (save-excursion
  19786. (goto-char (point-min))
  19787. (let ((end (save-excursion (outline-next-heading) (point))))
  19788. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  19789. ;; Mark the line so that it will not be exported as normal text.
  19790. (org-unmodified
  19791. (add-text-properties (match-beginning 0) (match-end 0)
  19792. (list :org-license-to-kill t)))
  19793. ;; Return the title string
  19794. (org-trim (match-string 0)))))))
  19795. (defun org-export-get-title-from-subtree ()
  19796. "Return subtree title and exclude it from export."
  19797. (let (title (m (mark)))
  19798. (save-excursion
  19799. (goto-char (region-beginning))
  19800. (when (and (org-at-heading-p)
  19801. (>= (org-end-of-subtree t t) (region-end)))
  19802. ;; This is a subtree, we take the title from the first heading
  19803. (goto-char (region-beginning))
  19804. (looking-at org-todo-line-regexp)
  19805. (setq title (match-string 3))
  19806. (org-unmodified
  19807. (add-text-properties (point) (1+ (point-at-eol))
  19808. (list :org-license-to-kill t)))))
  19809. title))
  19810. (defun org-solidify-link-text (s &optional alist)
  19811. "Take link text and make a safe target out of it."
  19812. (save-match-data
  19813. (let* ((rtn
  19814. (mapconcat
  19815. 'identity
  19816. (org-split-string s "[ \t\r\n]+") "--"))
  19817. (a (assoc rtn alist)))
  19818. (or (cdr a) rtn))))
  19819. (defun org-get-min-level (lines)
  19820. "Get the minimum level in LINES."
  19821. (let ((re "^\\(\\*+\\) ") l min)
  19822. (catch 'exit
  19823. (while (setq l (pop lines))
  19824. (if (string-match re l)
  19825. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  19826. 1)))
  19827. ;; Variable holding the vector with section numbers
  19828. (defvar org-section-numbers (make-vector org-level-max 0))
  19829. (defun org-init-section-numbers ()
  19830. "Initialize the vector for the section numbers."
  19831. (let* ((level -1)
  19832. (numbers (nreverse (org-split-string "" "\\.")))
  19833. (depth (1- (length org-section-numbers)))
  19834. (i depth) number-string)
  19835. (while (>= i 0)
  19836. (if (> i level)
  19837. (aset org-section-numbers i 0)
  19838. (setq number-string (or (car numbers) "0"))
  19839. (if (string-match "\\`[A-Z]\\'" number-string)
  19840. (aset org-section-numbers i
  19841. (- (string-to-char number-string) ?A -1))
  19842. (aset org-section-numbers i (string-to-number number-string)))
  19843. (pop numbers))
  19844. (setq i (1- i)))))
  19845. (defun org-section-number (&optional level)
  19846. "Return a string with the current section number.
  19847. When LEVEL is non-nil, increase section numbers on that level."
  19848. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  19849. (when level
  19850. (when (> level -1)
  19851. (aset org-section-numbers
  19852. level (1+ (aref org-section-numbers level))))
  19853. (setq idx (1+ level))
  19854. (while (<= idx depth)
  19855. (if (not (= idx 1))
  19856. (aset org-section-numbers idx 0))
  19857. (setq idx (1+ idx))))
  19858. (setq idx 0)
  19859. (while (<= idx depth)
  19860. (setq n (aref org-section-numbers idx))
  19861. (setq string (concat string (if (not (string= string "")) "." "")
  19862. (int-to-string n)))
  19863. (setq idx (1+ idx)))
  19864. (save-match-data
  19865. (if (string-match "\\`\\([@0]\\.\\)+" string)
  19866. (setq string (replace-match "" t nil string)))
  19867. (if (string-match "\\(\\.0\\)+\\'" string)
  19868. (setq string (replace-match "" t nil string))))
  19869. string))
  19870. ;;; ASCII export
  19871. (defvar org-last-level nil) ; dynamically scoped variable
  19872. (defvar org-min-level nil) ; dynamically scoped variable
  19873. (defvar org-levels-open nil) ; dynamically scoped parameter
  19874. (defvar org-ascii-current-indentation nil) ; For communication
  19875. (defun org-export-as-ascii (arg)
  19876. "Export the outline as a pretty ASCII file.
  19877. If there is an active region, export only the region.
  19878. The prefix ARG specifies how many levels of the outline should become
  19879. underlined headlines. The default is 3."
  19880. (interactive "P")
  19881. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19882. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19883. (org-infile-export-plist)))
  19884. (region-p (org-region-active-p))
  19885. (subtree-p
  19886. (when region-p
  19887. (save-excursion
  19888. (goto-char (region-beginning))
  19889. (and (org-at-heading-p)
  19890. (>= (org-end-of-subtree t t) (region-end))))))
  19891. (custom-times org-display-custom-times)
  19892. (org-ascii-current-indentation '(0 . 0))
  19893. (level 0) line txt
  19894. (umax nil)
  19895. (umax-toc nil)
  19896. (case-fold-search nil)
  19897. (filename (concat (file-name-as-directory
  19898. (org-export-directory :ascii opt-plist))
  19899. (file-name-sans-extension
  19900. (or (and subtree-p
  19901. (org-entry-get (region-beginning)
  19902. "EXPORT_FILE_NAME" t))
  19903. (file-name-nondirectory buffer-file-name)))
  19904. ".txt"))
  19905. (filename (if (equal (file-truename filename)
  19906. (file-truename buffer-file-name))
  19907. (concat filename ".txt")
  19908. filename))
  19909. (buffer (find-file-noselect filename))
  19910. (org-levels-open (make-vector org-level-max nil))
  19911. (odd org-odd-levels-only)
  19912. (date (plist-get opt-plist :date))
  19913. (author (plist-get opt-plist :author))
  19914. (title (or (and subtree-p (org-export-get-title-from-subtree))
  19915. (plist-get opt-plist :title)
  19916. (and (not
  19917. (plist-get opt-plist :skip-before-1st-heading))
  19918. (org-export-grab-title-from-buffer))
  19919. (file-name-sans-extension
  19920. (file-name-nondirectory buffer-file-name))))
  19921. (email (plist-get opt-plist :email))
  19922. (language (plist-get opt-plist :language))
  19923. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19924. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  19925. (todo nil)
  19926. (lang-words nil)
  19927. (region
  19928. (buffer-substring
  19929. (if (org-region-active-p) (region-beginning) (point-min))
  19930. (if (org-region-active-p) (region-end) (point-max))))
  19931. (lines (org-split-string
  19932. (org-cleaned-string-for-export
  19933. region
  19934. :for-ascii t
  19935. :skip-before-1st-heading
  19936. (plist-get opt-plist :skip-before-1st-heading)
  19937. :archived-trees
  19938. (plist-get opt-plist :archived-trees)
  19939. :add-text (plist-get opt-plist :text))
  19940. "[\r\n]")) ;; FIXME: why \r here???/
  19941. thetoc have-headings first-heading-pos
  19942. table-open table-buffer)
  19943. (let ((inhibit-read-only t))
  19944. (org-unmodified
  19945. (remove-text-properties (point-min) (point-max)
  19946. '(:org-license-to-kill t))))
  19947. (setq org-min-level (org-get-min-level lines))
  19948. (setq org-last-level org-min-level)
  19949. (org-init-section-numbers)
  19950. (find-file-noselect filename)
  19951. (setq lang-words (or (assoc language org-export-language-setup)
  19952. (assoc "en" org-export-language-setup)))
  19953. (switch-to-buffer-other-window buffer)
  19954. (erase-buffer)
  19955. (fundamental-mode)
  19956. ;; create local variables for all options, to make sure all called
  19957. ;; functions get the correct information
  19958. (mapcar (lambda (x)
  19959. (set (make-local-variable (cdr x))
  19960. (plist-get opt-plist (car x))))
  19961. org-export-plist-vars)
  19962. (org-set-local 'org-odd-levels-only odd)
  19963. (setq umax (if arg (prefix-numeric-value arg)
  19964. org-export-headline-levels))
  19965. (setq umax-toc (if (integerp org-export-with-toc)
  19966. (min org-export-with-toc umax)
  19967. umax))
  19968. ;; File header
  19969. (if title (org-insert-centered title ?=))
  19970. (insert "\n")
  19971. (if (and (or author email)
  19972. org-export-author-info)
  19973. (insert (concat (nth 1 lang-words) ": " (or author "")
  19974. (if email (concat " <" email ">") "")
  19975. "\n")))
  19976. (cond
  19977. ((and date (string-match "%" date))
  19978. (setq date (format-time-string date (current-time))))
  19979. (date)
  19980. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  19981. (if (and date org-export-time-stamp-file)
  19982. (insert (concat (nth 2 lang-words) ": " date"\n")))
  19983. (insert "\n\n")
  19984. (if org-export-with-toc
  19985. (progn
  19986. (push (concat (nth 3 lang-words) "\n") thetoc)
  19987. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  19988. (mapcar '(lambda (line)
  19989. (if (string-match org-todo-line-regexp
  19990. line)
  19991. ;; This is a headline
  19992. (progn
  19993. (setq have-headings t)
  19994. (setq level (- (match-end 1) (match-beginning 1))
  19995. level (org-tr-level level)
  19996. txt (match-string 3 line)
  19997. todo
  19998. (or (and org-export-mark-todo-in-toc
  19999. (match-beginning 2)
  20000. (not (member (match-string 2 line)
  20001. org-done-keywords)))
  20002. ; TODO, not DONE
  20003. (and org-export-mark-todo-in-toc
  20004. (= level umax-toc)
  20005. (org-search-todo-below
  20006. line lines level))))
  20007. (setq txt (org-html-expand-for-ascii txt))
  20008. (if (and (memq org-export-with-tags '(not-in-toc nil))
  20009. (string-match
  20010. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  20011. txt))
  20012. (setq txt (replace-match "" t t txt)))
  20013. (if (string-match quote-re0 txt)
  20014. (setq txt (replace-match "" t t txt)))
  20015. (if org-export-with-section-numbers
  20016. (setq txt (concat (org-section-number level)
  20017. " " txt)))
  20018. (if (<= level umax-toc)
  20019. (progn
  20020. (push
  20021. (concat
  20022. (make-string
  20023. (* (max 0 (- level org-min-level)) 4) ?\ )
  20024. (format (if todo "%s (*)\n" "%s\n") txt))
  20025. thetoc)
  20026. (setq org-last-level level))
  20027. ))))
  20028. lines)
  20029. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  20030. (org-init-section-numbers)
  20031. (while (setq line (pop lines))
  20032. ;; Remove the quoted HTML tags.
  20033. (setq line (org-html-expand-for-ascii line))
  20034. ;; Remove targets
  20035. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  20036. (setq line (replace-match "" t t line)))
  20037. ;; Replace internal links
  20038. (while (string-match org-bracket-link-regexp line)
  20039. (setq line (replace-match
  20040. (if (match-end 3) "[\\3]" "[\\1]")
  20041. t nil line)))
  20042. (when custom-times
  20043. (setq line (org-translate-time line)))
  20044. (cond
  20045. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20046. ;; a Headline
  20047. (setq first-heading-pos (or first-heading-pos (point)))
  20048. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20049. txt (match-string 2 line))
  20050. (org-ascii-level-start level txt umax lines))
  20051. ((and org-export-with-tables
  20052. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20053. (if (not table-open)
  20054. ;; New table starts
  20055. (setq table-open t table-buffer nil))
  20056. ;; Accumulate lines
  20057. (setq table-buffer (cons line table-buffer))
  20058. (when (or (not lines)
  20059. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20060. (car lines))))
  20061. (setq table-open nil
  20062. table-buffer (nreverse table-buffer))
  20063. (insert (mapconcat
  20064. (lambda (x)
  20065. (org-fix-indentation x org-ascii-current-indentation))
  20066. (org-format-table-ascii table-buffer)
  20067. "\n") "\n")))
  20068. (t
  20069. (setq line (org-fix-indentation line org-ascii-current-indentation))
  20070. (if (and org-export-with-fixed-width
  20071. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  20072. (setq line (replace-match "\\1" nil nil line)))
  20073. (insert line "\n"))))
  20074. (normal-mode)
  20075. ;; insert the table of contents
  20076. (when thetoc
  20077. (goto-char (point-min))
  20078. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  20079. (progn
  20080. (goto-char (match-beginning 0))
  20081. (replace-match ""))
  20082. (goto-char first-heading-pos))
  20083. (mapc 'insert thetoc)
  20084. (or (looking-at "[ \t]*\n[ \t]*\n")
  20085. (insert "\n\n")))
  20086. (save-buffer)
  20087. ;; remove display and invisible chars
  20088. (let (beg end)
  20089. (goto-char (point-min))
  20090. (while (setq beg (next-single-property-change (point) 'display))
  20091. (setq end (next-single-property-change beg 'display))
  20092. (delete-region beg end)
  20093. (goto-char beg)
  20094. (insert "=>"))
  20095. (goto-char (point-min))
  20096. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  20097. (setq end (next-single-property-change beg 'org-cwidth))
  20098. (delete-region beg end)
  20099. (goto-char beg)))
  20100. (goto-char (point-min))))
  20101. (defun org-search-todo-below (line lines level)
  20102. "Search the subtree below LINE for any TODO entries."
  20103. (let ((rest (cdr (memq line lines)))
  20104. (re org-todo-line-regexp)
  20105. line lv todo)
  20106. (catch 'exit
  20107. (while (setq line (pop rest))
  20108. (if (string-match re line)
  20109. (progn
  20110. (setq lv (- (match-end 1) (match-beginning 1))
  20111. todo (and (match-beginning 2)
  20112. (not (member (match-string 2 line)
  20113. org-done-keywords))))
  20114. ; TODO, not DONE
  20115. (if (<= lv level) (throw 'exit nil))
  20116. (if todo (throw 'exit t))))))))
  20117. (defun org-html-expand-for-ascii (line)
  20118. "Handle quoted HTML for ASCII export."
  20119. (if org-export-html-expand
  20120. (while (string-match "@<[^<>\n]*>" line)
  20121. ;; We just remove the tags for now.
  20122. (setq line (replace-match "" nil nil line))))
  20123. line)
  20124. (defun org-insert-centered (s &optional underline)
  20125. "Insert the string S centered and underline it with character UNDERLINE."
  20126. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  20127. (insert (make-string ind ?\ ) s "\n")
  20128. (if underline
  20129. (insert (make-string ind ?\ )
  20130. (make-string (string-width s) underline)
  20131. "\n"))))
  20132. (defun org-ascii-level-start (level title umax &optional lines)
  20133. "Insert a new level in ASCII export."
  20134. (let (char (n (- level umax 1)) (ind 0))
  20135. (if (> level umax)
  20136. (progn
  20137. (insert (make-string (* 2 n) ?\ )
  20138. (char-to-string (nth (% n (length org-export-ascii-bullets))
  20139. org-export-ascii-bullets))
  20140. " " title "\n")
  20141. ;; find the indentation of the next non-empty line
  20142. (catch 'stop
  20143. (while lines
  20144. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  20145. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  20146. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  20147. (pop lines)))
  20148. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  20149. (if (or (not (equal (char-before) ?\n))
  20150. (not (equal (char-before (1- (point))) ?\n)))
  20151. (insert "\n"))
  20152. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  20153. (unless org-export-with-tags
  20154. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20155. (setq title (replace-match "" t t title))))
  20156. (if org-export-with-section-numbers
  20157. (setq title (concat (org-section-number level) " " title)))
  20158. (insert title "\n" (make-string (string-width title) char) "\n")
  20159. (setq org-ascii-current-indentation '(0 . 0)))))
  20160. (defun org-export-visible (type arg)
  20161. "Create a copy of the visible part of the current buffer, and export it.
  20162. The copy is created in a temporary buffer and removed after use.
  20163. TYPE is the final key (as a string) that also select the export command in
  20164. the `C-c C-e' export dispatcher.
  20165. As a special case, if the you type SPC at the prompt, the temporary
  20166. org-mode file will not be removed but presented to you so that you can
  20167. continue to use it. The prefix arg ARG is passed through to the exporting
  20168. command."
  20169. (interactive
  20170. (list (progn
  20171. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  20172. (read-char-exclusive))
  20173. current-prefix-arg))
  20174. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  20175. (error "Invalid export key"))
  20176. (let* ((binding (cdr (assoc type
  20177. '((?a . org-export-as-ascii)
  20178. (?\C-a . org-export-as-ascii)
  20179. (?b . org-export-as-html-and-open)
  20180. (?\C-b . org-export-as-html-and-open)
  20181. (?h . org-export-as-html)
  20182. (?H . org-export-as-html-to-buffer)
  20183. (?R . org-export-region-as-html)
  20184. (?x . org-export-as-xoxo)))))
  20185. (keepp (equal type ?\ ))
  20186. (file buffer-file-name)
  20187. (buffer (get-buffer-create "*Org Export Visible*"))
  20188. s e)
  20189. ;; Need to hack the drawers here.
  20190. (save-excursion
  20191. (goto-char (point-min))
  20192. (while (re-search-forward org-drawer-regexp nil t)
  20193. (goto-char (match-beginning 1))
  20194. (or (org-invisible-p) (org-flag-drawer nil))))
  20195. (with-current-buffer buffer (erase-buffer))
  20196. (save-excursion
  20197. (setq s (goto-char (point-min)))
  20198. (while (not (= (point) (point-max)))
  20199. (goto-char (org-find-invisible))
  20200. (append-to-buffer buffer s (point))
  20201. (setq s (goto-char (org-find-visible))))
  20202. (org-cycle-hide-drawers 'all)
  20203. (goto-char (point-min))
  20204. (unless keepp
  20205. ;; Copy all comment lines to the end, to make sure #+ settings are
  20206. ;; still available for the second export step. Kind of a hack, but
  20207. ;; does do the trick.
  20208. (if (looking-at "#[^\r\n]*")
  20209. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  20210. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  20211. (append-to-buffer buffer (1+ (match-beginning 0))
  20212. (min (point-max) (1+ (match-end 0))))))
  20213. (set-buffer buffer)
  20214. (let ((buffer-file-name file)
  20215. (org-inhibit-startup t))
  20216. (org-mode)
  20217. (show-all)
  20218. (unless keepp (funcall binding arg))))
  20219. (if (not keepp)
  20220. (kill-buffer buffer)
  20221. (switch-to-buffer-other-window buffer)
  20222. (goto-char (point-min)))))
  20223. (defun org-find-visible ()
  20224. (let ((s (point)))
  20225. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  20226. (get-char-property s 'invisible)))
  20227. s))
  20228. (defun org-find-invisible ()
  20229. (let ((s (point)))
  20230. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  20231. (not (get-char-property s 'invisible))))
  20232. s))
  20233. ;;; HTML export
  20234. (defun org-get-current-options ()
  20235. "Return a string with current options as keyword options.
  20236. Does include HTML export options as well as TODO and CATEGORY stuff."
  20237. (format
  20238. "#+TITLE: %s
  20239. #+AUTHOR: %s
  20240. #+EMAIL: %s
  20241. #+LANGUAGE: %s
  20242. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  20243. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  20244. #+CATEGORY: %s
  20245. #+SEQ_TODO: %s
  20246. #+TYP_TODO: %s
  20247. #+PRIORITIES: %c %c %c
  20248. #+STARTUP: %s %s %s %s %s
  20249. #+TAGS: %s
  20250. #+ARCHIVE: %s
  20251. #+LINK: %s
  20252. "
  20253. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  20254. org-export-headline-levels
  20255. org-export-with-section-numbers
  20256. org-export-with-toc
  20257. org-export-preserve-breaks
  20258. org-export-html-expand
  20259. org-export-with-fixed-width
  20260. org-export-with-tables
  20261. org-export-with-sub-superscripts
  20262. org-export-with-footnotes
  20263. org-export-with-emphasize
  20264. org-export-with-TeX-macros
  20265. org-export-with-LaTeX-fragments
  20266. org-export-skip-text-before-1st-heading
  20267. org-export-with-property-drawer
  20268. (file-name-nondirectory buffer-file-name)
  20269. "TODO FEEDBACK VERIFY DONE"
  20270. "Me Jason Marie DONE"
  20271. org-highest-priority org-lowest-priority org-default-priority
  20272. (cdr (assoc org-startup-folded
  20273. '((nil . "showall") (t . "overview") (content . "content"))))
  20274. (if org-odd-levels-only "odd" "oddeven")
  20275. (if org-hide-leading-stars "hidestars" "showstars")
  20276. (if org-startup-align-all-tables "align" "noalign")
  20277. (cond ((eq t org-log-done) "logdone")
  20278. ((not org-log-done) "nologging")
  20279. ((listp org-log-done)
  20280. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  20281. org-log-done " ")))
  20282. (or (mapconcat (lambda (x)
  20283. (cond
  20284. ((equal '(:startgroup) x) "{")
  20285. ((equal '(:endgroup) x) "}")
  20286. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  20287. (t (car x))))
  20288. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  20289. org-archive-location
  20290. "org file:~/org/%s.org"
  20291. ))
  20292. (defun org-insert-export-options-template ()
  20293. "Insert into the buffer a template with information for exporting."
  20294. (interactive)
  20295. (if (not (bolp)) (newline))
  20296. (let ((s (org-get-current-options)))
  20297. (and (string-match "#\\+CATEGORY" s)
  20298. (setq s (substring s 0 (match-beginning 0))))
  20299. (insert s)))
  20300. (defun org-toggle-fixed-width-section (arg)
  20301. "Toggle the fixed-width export.
  20302. If there is no active region, the QUOTE keyword at the current headline is
  20303. inserted or removed. When present, it causes the text between this headline
  20304. and the next to be exported as fixed-width text, and unmodified.
  20305. If there is an active region, this command adds or removes a colon as the
  20306. first character of this line. If the first character of a line is a colon,
  20307. this line is also exported in fixed-width font."
  20308. (interactive "P")
  20309. (let* ((cc 0)
  20310. (regionp (org-region-active-p))
  20311. (beg (if regionp (region-beginning) (point)))
  20312. (end (if regionp (region-end)))
  20313. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20314. (re "[ \t]*\\(:\\)")
  20315. off)
  20316. (if regionp
  20317. (save-excursion
  20318. (goto-char beg)
  20319. (setq cc (current-column))
  20320. (beginning-of-line 1)
  20321. (setq off (looking-at re))
  20322. (while (> nlines 0)
  20323. (setq nlines (1- nlines))
  20324. (beginning-of-line 1)
  20325. (cond
  20326. (arg
  20327. (move-to-column cc t)
  20328. (insert ":\n")
  20329. (forward-line -1))
  20330. ((and off (looking-at re))
  20331. (replace-match "" t t nil 1))
  20332. ((not off) (move-to-column cc t) (insert ":")))
  20333. (forward-line 1)))
  20334. (save-excursion
  20335. (org-back-to-heading)
  20336. (if (looking-at (concat outline-regexp
  20337. "\\( *\\<" org-quote-string "\\>\\)"))
  20338. (replace-match "" t t nil 1)
  20339. (if (looking-at outline-regexp)
  20340. (progn
  20341. (goto-char (match-end 0))
  20342. (insert org-quote-string " "))))))))
  20343. (defun org-export-as-html-and-open (arg)
  20344. "Export the outline as HTML and immediately open it with a browser.
  20345. If there is an active region, export only the region.
  20346. The prefix ARG specifies how many levels of the outline should become
  20347. headlines. The default is 3. Lower levels will become bulleted lists."
  20348. (interactive "P")
  20349. (org-export-as-html arg 'hidden)
  20350. (org-open-file buffer-file-name))
  20351. (defun org-export-as-html-batch ()
  20352. "Call `org-export-as-html', may be used in batch processing as
  20353. emacs --batch
  20354. --load=$HOME/lib/emacs/org.el
  20355. --eval \"(setq org-export-headline-levels 2)\"
  20356. --visit=MyFile --funcall org-export-as-html-batch"
  20357. (org-export-as-html org-export-headline-levels 'hidden))
  20358. (defun org-export-as-html-to-buffer (arg)
  20359. "Call `org-exort-as-html` with output to a temporary buffer.
  20360. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  20361. (interactive "P")
  20362. (org-export-as-html arg nil nil "*Org HTML Export*")
  20363. (switch-to-buffer-other-window "*Org HTML Export*"))
  20364. (defun org-replace-region-by-html (beg end)
  20365. "Assume the current region has org-mode syntax, and convert it to HTML.
  20366. This can be used in any buffer. For example, you could write an
  20367. itemized list in org-mode syntax in an HTML buffer and then use this
  20368. command to convert it."
  20369. (interactive "r")
  20370. (let (reg html buf pop-up-frames)
  20371. (save-window-excursion
  20372. (if (org-mode-p)
  20373. (setq html (org-export-region-as-html
  20374. beg end t 'string))
  20375. (setq reg (buffer-substring beg end)
  20376. buf (get-buffer-create "*Org tmp*"))
  20377. (with-current-buffer buf
  20378. (erase-buffer)
  20379. (insert reg)
  20380. (org-mode)
  20381. (setq html (org-export-region-as-html
  20382. (point-min) (point-max) t 'string)))
  20383. (kill-buffer buf)))
  20384. (delete-region beg end)
  20385. (insert html)))
  20386. (defun org-export-region-as-html (beg end &optional body-only buffer)
  20387. "Convert region from BEG to END in org-mode buffer to HTML.
  20388. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  20389. contents, and only produce the region of converted text, useful for
  20390. cut-and-paste operations.
  20391. If BUFFER is a buffer or a string, use/create that buffer as a target
  20392. of the converted HTML. If BUFFER is the symbol `string', return the
  20393. produced HTML as a string and leave not buffer behind. For example,
  20394. a Lisp program could call this function in the following way:
  20395. (setq html (org-export-region-as-html beg end t 'string))
  20396. When called interactively, the output buffer is selected, and shown
  20397. in a window. A non-interactive call will only retunr the buffer."
  20398. (interactive "r\nP")
  20399. (when (interactive-p)
  20400. (setq buffer "*Org HTML Export*"))
  20401. (let ((transient-mark-mode t) (zmacs-regions t)
  20402. rtn)
  20403. (goto-char end)
  20404. (set-mark (point)) ;; to activate the region
  20405. (goto-char beg)
  20406. (setq rtn (org-export-as-html
  20407. nil nil nil
  20408. buffer body-only))
  20409. (if (fboundp 'deactivate-mark) (deactivate-mark))
  20410. (if (and (interactive-p) (bufferp rtn))
  20411. (switch-to-buffer-other-window rtn)
  20412. rtn)))
  20413. (defun org-export-as-html (arg &optional hidden ext-plist
  20414. to-buffer body-only)
  20415. "Export the outline as a pretty HTML file.
  20416. If there is an active region, export only the region. The prefix
  20417. ARG specifies how many levels of the outline should become
  20418. headlines. The default is 3. Lower levels will become bulleted
  20419. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  20420. EXT-PLIST is a property list with external parameters overriding
  20421. org-mode's default settings, but still inferior to file-local
  20422. settings. When TO-BUFFER is non-nil, create a buffer with that
  20423. name and export to that buffer. If TO-BUFFER is the symbol `string',
  20424. don't leave any buffer behind but just return the resulting HTML as
  20425. a string. When BODY-ONLY is set, don't produce the file header and footer,
  20426. simply return the content of <body>...</body>, without even
  20427. the body tags themselves."
  20428. (interactive "P")
  20429. ;; Make sure we have a file name when we need it.
  20430. (when (and (not (or to-buffer body-only))
  20431. (not buffer-file-name))
  20432. (if (buffer-base-buffer)
  20433. (org-set-local 'buffer-file-name
  20434. (with-current-buffer (buffer-base-buffer)
  20435. buffer-file-name))
  20436. (error "Need a file name to be able to export.")))
  20437. (message "Exporting...")
  20438. (setq-default org-todo-line-regexp org-todo-line-regexp)
  20439. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  20440. (setq-default org-done-keywords org-done-keywords)
  20441. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  20442. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  20443. ext-plist
  20444. (org-infile-export-plist)))
  20445. (style (plist-get opt-plist :style))
  20446. (link-validate (plist-get opt-plist :link-validation-function))
  20447. valid thetoc have-headings first-heading-pos
  20448. (odd org-odd-levels-only)
  20449. (region-p (org-region-active-p))
  20450. (subtree-p
  20451. (when region-p
  20452. (save-excursion
  20453. (goto-char (region-beginning))
  20454. (and (org-at-heading-p)
  20455. (>= (org-end-of-subtree t t) (region-end))))))
  20456. ;; The following two are dynamically scoped into other
  20457. ;; routines below.
  20458. (org-current-export-dir (org-export-directory :html opt-plist))
  20459. (org-current-export-file buffer-file-name)
  20460. (level 0) (line "") (origline "") txt todo
  20461. (umax nil)
  20462. (umax-toc nil)
  20463. (filename (if to-buffer nil
  20464. (concat (file-name-as-directory
  20465. (org-export-directory :html opt-plist))
  20466. (file-name-sans-extension
  20467. (or (and subtree-p
  20468. (org-entry-get (region-beginning)
  20469. "EXPORT_FILE_NAME" t))
  20470. (file-name-nondirectory buffer-file-name)))
  20471. "." org-export-html-extension)))
  20472. (current-dir (if buffer-file-name
  20473. (file-name-directory buffer-file-name)
  20474. default-directory))
  20475. (buffer (if to-buffer
  20476. (cond
  20477. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  20478. (t (get-buffer-create to-buffer)))
  20479. (find-file-noselect filename)))
  20480. (org-levels-open (make-vector org-level-max nil))
  20481. (date (plist-get opt-plist :date))
  20482. (author (plist-get opt-plist :author))
  20483. (title (or (and subtree-p (org-export-get-title-from-subtree))
  20484. (plist-get opt-plist :title)
  20485. (and (not
  20486. (plist-get opt-plist :skip-before-1st-heading))
  20487. (org-export-grab-title-from-buffer))
  20488. (and buffer-file-name
  20489. (file-name-sans-extension
  20490. (file-name-nondirectory buffer-file-name)))
  20491. "UNTITLED"))
  20492. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  20493. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  20494. (inquote nil)
  20495. (infixed nil)
  20496. (in-local-list nil)
  20497. (local-list-num nil)
  20498. (local-list-indent nil)
  20499. (llt org-plain-list-ordered-item-terminator)
  20500. (email (plist-get opt-plist :email))
  20501. (language (plist-get opt-plist :language))
  20502. (lang-words nil)
  20503. (target-alist nil) tg
  20504. (head-count 0) cnt
  20505. (start 0)
  20506. (coding-system (and (boundp 'buffer-file-coding-system)
  20507. buffer-file-coding-system))
  20508. (coding-system-for-write (or org-export-html-coding-system
  20509. coding-system))
  20510. (save-buffer-coding-system (or org-export-html-coding-system
  20511. coding-system))
  20512. (charset (and coding-system-for-write
  20513. (fboundp 'coding-system-get)
  20514. (coding-system-get coding-system-for-write
  20515. 'mime-charset)))
  20516. (region
  20517. (buffer-substring
  20518. (if region-p (region-beginning) (point-min))
  20519. (if region-p (region-end) (point-max))))
  20520. (lines
  20521. (org-split-string
  20522. (org-cleaned-string-for-export
  20523. region
  20524. :emph-multiline t
  20525. :for-html t
  20526. :skip-before-1st-heading
  20527. (plist-get opt-plist :skip-before-1st-heading)
  20528. :archived-trees
  20529. (plist-get opt-plist :archived-trees)
  20530. :add-text
  20531. (plist-get opt-plist :text)
  20532. :LaTeX-fragments
  20533. (plist-get opt-plist :LaTeX-fragments))
  20534. "[\r\n]"))
  20535. table-open type
  20536. table-buffer table-orig-buffer
  20537. ind start-is-num starter didclose
  20538. rpl path desc descp desc1 desc2 link
  20539. )
  20540. (let ((inhibit-read-only t))
  20541. (org-unmodified
  20542. (remove-text-properties (point-min) (point-max)
  20543. '(:org-license-to-kill t))))
  20544. (message "Exporting...")
  20545. (setq org-min-level (org-get-min-level lines))
  20546. (setq org-last-level org-min-level)
  20547. (org-init-section-numbers)
  20548. (cond
  20549. ((and date (string-match "%" date))
  20550. (setq date (format-time-string date (current-time))))
  20551. (date)
  20552. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  20553. ;; Get the language-dependent settings
  20554. (setq lang-words (or (assoc language org-export-language-setup)
  20555. (assoc "en" org-export-language-setup)))
  20556. ;; Switch to the output buffer
  20557. (set-buffer buffer)
  20558. (erase-buffer)
  20559. (fundamental-mode)
  20560. (and (fboundp 'set-buffer-file-coding-system)
  20561. (set-buffer-file-coding-system coding-system-for-write))
  20562. (let ((case-fold-search nil)
  20563. (org-odd-levels-only odd))
  20564. ;; create local variables for all options, to make sure all called
  20565. ;; functions get the correct information
  20566. (mapcar (lambda (x)
  20567. (set (make-local-variable (cdr x))
  20568. (plist-get opt-plist (car x))))
  20569. org-export-plist-vars)
  20570. (setq umax (if arg (prefix-numeric-value arg)
  20571. org-export-headline-levels))
  20572. (setq umax-toc (if (integerp org-export-with-toc)
  20573. (min org-export-with-toc umax)
  20574. umax))
  20575. (unless body-only
  20576. ;; File header
  20577. (insert (format
  20578. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  20579. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  20580. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  20581. lang=\"%s\" xml:lang=\"%s\">
  20582. <head>
  20583. <title>%s</title>
  20584. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  20585. <meta name=\"generator\" content=\"Org-mode\"/>
  20586. <meta name=\"generated\" content=\"%s\"/>
  20587. <meta name=\"author\" content=\"%s\"/>
  20588. %s
  20589. </head><body>
  20590. "
  20591. language language (org-html-expand title)
  20592. (or charset "iso-8859-1") date author style))
  20593. (insert (or (plist-get opt-plist :preamble) ""))
  20594. (when (plist-get opt-plist :auto-preamble)
  20595. (if title (insert (format org-export-html-title-format
  20596. (org-html-expand title))))))
  20597. (if (and org-export-with-toc (not body-only))
  20598. (progn
  20599. (push (format "<h%d>%s</h%d>\n"
  20600. org-export-html-toplevel-hlevel
  20601. (nth 3 lang-words)
  20602. org-export-html-toplevel-hlevel)
  20603. thetoc)
  20604. (push "<ul>\n<li>" thetoc)
  20605. (setq lines
  20606. (mapcar '(lambda (line)
  20607. (if (string-match org-todo-line-regexp line)
  20608. ;; This is a headline
  20609. (progn
  20610. (setq have-headings t)
  20611. (setq level (- (match-end 1) (match-beginning 1))
  20612. level (org-tr-level level)
  20613. txt (save-match-data
  20614. (org-html-expand
  20615. (org-export-cleanup-toc-line
  20616. (match-string 3 line))))
  20617. todo
  20618. (or (and org-export-mark-todo-in-toc
  20619. (match-beginning 2)
  20620. (not (member (match-string 2 line)
  20621. org-done-keywords)))
  20622. ; TODO, not DONE
  20623. (and org-export-mark-todo-in-toc
  20624. (= level umax-toc)
  20625. (org-search-todo-below
  20626. line lines level))))
  20627. (if (and (memq org-export-with-tags '(not-in-toc nil))
  20628. (string-match
  20629. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  20630. txt))
  20631. (setq txt (replace-match "" t t txt)))
  20632. (if (string-match quote-re0 txt)
  20633. (setq txt (replace-match "" t t txt)))
  20634. (if org-export-with-section-numbers
  20635. (setq txt (concat (org-section-number level)
  20636. " " txt)))
  20637. (if (<= level (max umax umax-toc))
  20638. (setq head-count (+ head-count 1)))
  20639. (if (<= level umax-toc)
  20640. (progn
  20641. (if (> level org-last-level)
  20642. (progn
  20643. (setq cnt (- level org-last-level))
  20644. (while (>= (setq cnt (1- cnt)) 0)
  20645. (push "\n<ul>\n<li>" thetoc))
  20646. (push "\n" thetoc)))
  20647. (if (< level org-last-level)
  20648. (progn
  20649. (setq cnt (- org-last-level level))
  20650. (while (>= (setq cnt (1- cnt)) 0)
  20651. (push "</li>\n</ul>" thetoc))
  20652. (push "\n" thetoc)))
  20653. ;; Check for targets
  20654. (while (string-match org-target-regexp line)
  20655. (setq tg (match-string 1 line)
  20656. line (replace-match
  20657. (concat "@<span class=\"target\">" tg "@</span> ")
  20658. t t line))
  20659. (push (cons (org-solidify-link-text tg)
  20660. (format "sec-%d" head-count))
  20661. target-alist))
  20662. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  20663. (setq txt (replace-match "" t t txt)))
  20664. (push
  20665. (format
  20666. (if todo
  20667. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  20668. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  20669. head-count txt) thetoc)
  20670. (setq org-last-level level))
  20671. )))
  20672. line)
  20673. lines))
  20674. (while (> org-last-level (1- org-min-level))
  20675. (setq org-last-level (1- org-last-level))
  20676. (push "</li>\n</ul>\n" thetoc))
  20677. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  20678. (setq head-count 0)
  20679. (org-init-section-numbers)
  20680. (while (setq line (pop lines) origline line)
  20681. (catch 'nextline
  20682. ;; end of quote section?
  20683. (when (and inquote (string-match "^\\*+ " line))
  20684. (insert "</pre>\n")
  20685. (setq inquote nil))
  20686. ;; inside a quote section?
  20687. (when inquote
  20688. (insert (org-html-protect line) "\n")
  20689. (throw 'nextline nil))
  20690. ;; verbatim lines
  20691. (when (and org-export-with-fixed-width
  20692. (string-match "^[ \t]*:\\(.*\\)" line))
  20693. (when (not infixed)
  20694. (setq infixed t)
  20695. (insert "<pre>\n"))
  20696. (insert (org-html-protect (match-string 1 line)) "\n")
  20697. (when (and lines
  20698. (not (string-match "^[ \t]*\\(:.*\\)"
  20699. (car lines))))
  20700. (setq infixed nil)
  20701. (insert "</pre>\n"))
  20702. (throw 'nextline nil))
  20703. ;; Protected HTML
  20704. (when (get-text-property 0 'org-protected line)
  20705. (let (par)
  20706. (when (re-search-backward
  20707. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  20708. (setq par (match-string 1))
  20709. (replace-match "\\2\n"))
  20710. (insert line "\n")
  20711. (while (and lines
  20712. (get-text-property 0 'org-protected (car lines)))
  20713. (insert (pop lines) "\n"))
  20714. (and par (insert "<p>\n")))
  20715. (throw 'nextline nil))
  20716. ;; Horizontal line
  20717. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  20718. (insert "\n<hr/>\n")
  20719. (throw 'nextline nil))
  20720. ;; make targets to anchors
  20721. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  20722. (cond
  20723. ((match-end 2)
  20724. (setq line (replace-match
  20725. (concat "@<a name=\""
  20726. (org-solidify-link-text (match-string 1 line))
  20727. "\">\\nbsp@</a>")
  20728. t t line)))
  20729. ((and org-export-with-toc (equal (string-to-char line) ?*))
  20730. (setq line (replace-match
  20731. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  20732. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  20733. t t line)))
  20734. (t
  20735. (setq line (replace-match
  20736. (concat "@<a name=\""
  20737. (org-solidify-link-text (match-string 1 line))
  20738. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  20739. t t line)))))
  20740. (setq line (org-html-handle-time-stamps line))
  20741. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  20742. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  20743. ;; Also handle sub_superscripts and checkboxes
  20744. (setq line (org-html-expand line))
  20745. ;; Format the links
  20746. (setq start 0)
  20747. (while (string-match org-bracket-link-analytic-regexp line start)
  20748. (setq start (match-beginning 0))
  20749. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  20750. (setq path (match-string 3 line))
  20751. (setq desc1 (if (match-end 5) (match-string 5 line))
  20752. desc2 (if (match-end 2) (concat type ":" path) path)
  20753. descp (and desc1 (not (equal desc1 desc2)))
  20754. desc (or desc1 desc2))
  20755. ;; Make an image out of the description if that is so wanted
  20756. (when (and descp (org-file-image-p desc))
  20757. (save-match-data
  20758. (if (string-match "^file:" desc)
  20759. (setq desc (substring desc (match-end 0)))))
  20760. (setq desc (concat "<img src=\"" desc "\"/>")))
  20761. ;; FIXME: do we need to unescape here somewhere?
  20762. (cond
  20763. ((equal type "internal")
  20764. (setq rpl
  20765. (concat
  20766. "<a href=\"#"
  20767. (org-solidify-link-text
  20768. (save-match-data (org-link-unescape path)) target-alist)
  20769. "\">" desc "</a>")))
  20770. ((member type '("http" "https")) ; FIXME: need to test this.
  20771. ;; standard URL, just check if we need to inline an image
  20772. (if (and (or (eq t org-export-html-inline-images)
  20773. (and org-export-html-inline-images (not descp)))
  20774. (org-file-image-p path))
  20775. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  20776. (setq link (concat type ":" path))
  20777. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  20778. ((member type '("ftp" "mailto" "news"))
  20779. ;; standard URL
  20780. (setq link (concat type ":" path))
  20781. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  20782. ((string= type "file")
  20783. ;; FILE link
  20784. (let* ((filename path)
  20785. (abs-p (file-name-absolute-p filename))
  20786. thefile file-is-image-p search)
  20787. (save-match-data
  20788. (if (string-match "::\\(.*\\)" filename)
  20789. (setq search (match-string 1 filename)
  20790. filename (replace-match "" t nil filename)))
  20791. (setq valid
  20792. (if (functionp link-validate)
  20793. (funcall link-validate filename current-dir)
  20794. t))
  20795. (setq file-is-image-p (org-file-image-p filename))
  20796. (setq thefile (if abs-p (expand-file-name filename) filename))
  20797. (when (and org-export-html-link-org-files-as-html
  20798. (string-match "\\.org$" thefile))
  20799. (setq thefile (concat (substring thefile 0
  20800. (match-beginning 0))
  20801. "." org-export-html-extension))
  20802. (if (and search
  20803. ;; make sure this is can be used as target search
  20804. (not (string-match "^[0-9]*$" search))
  20805. (not (string-match "^\\*" search))
  20806. (not (string-match "^/.*/$" search)))
  20807. (setq thefile (concat thefile "#"
  20808. (org-solidify-link-text
  20809. (org-link-unescape search)))))
  20810. (when (string-match "^file:" desc)
  20811. (setq desc (replace-match "" t t desc))
  20812. (if (string-match "\\.org$" desc)
  20813. (setq desc (replace-match "" t t desc))))))
  20814. (setq rpl (if (and file-is-image-p
  20815. (or (eq t org-export-html-inline-images)
  20816. (and org-export-html-inline-images
  20817. (not descp))))
  20818. (concat "<img src=\"" thefile "\"/>")
  20819. (concat "<a href=\"" thefile "\">" desc "</a>")))
  20820. (if (not valid) (setq rpl desc))))
  20821. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  20822. (setq rpl (concat "<i>&lt;" type ":"
  20823. (save-match-data (org-link-unescape path))
  20824. "&gt;</i>"))))
  20825. (setq line (replace-match rpl t t line)
  20826. start (+ start (length rpl))))
  20827. ;; TODO items
  20828. (if (and (string-match org-todo-line-regexp line)
  20829. (match-beginning 2))
  20830. (setq line
  20831. (concat (substring line 0 (match-beginning 2))
  20832. "<span class=\""
  20833. (if (member (match-string 2 line)
  20834. org-done-keywords)
  20835. "done" "todo")
  20836. "\">" (match-string 2 line)
  20837. "</span>" (substring line (match-end 2)))))
  20838. ;; Does this contain a reference to a footnote?
  20839. (when org-export-with-footnotes
  20840. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  20841. (let ((n (match-string 2 line)))
  20842. (setq line
  20843. (replace-match
  20844. (format
  20845. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  20846. (match-string 1 line) n n n)
  20847. t t line)))))
  20848. (cond
  20849. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20850. ;; This is a headline
  20851. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20852. txt (match-string 2 line))
  20853. (if (string-match quote-re0 txt)
  20854. (setq txt (replace-match "" t t txt)))
  20855. (if (<= level (max umax umax-toc))
  20856. (setq head-count (+ head-count 1)))
  20857. (when in-local-list
  20858. ;; Close any local lists before inserting a new header line
  20859. (while local-list-num
  20860. (org-close-li)
  20861. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20862. (pop local-list-num))
  20863. (setq local-list-indent nil
  20864. in-local-list nil))
  20865. (setq first-heading-pos (or first-heading-pos (point)))
  20866. (org-html-level-start level txt umax
  20867. (and org-export-with-toc (<= level umax))
  20868. head-count)
  20869. ;; QUOTES
  20870. (when (string-match quote-re line)
  20871. (insert "<pre>")
  20872. (setq inquote t)))
  20873. ((and org-export-with-tables
  20874. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20875. (if (not table-open)
  20876. ;; New table starts
  20877. (setq table-open t table-buffer nil table-orig-buffer nil))
  20878. ;; Accumulate lines
  20879. (setq table-buffer (cons line table-buffer)
  20880. table-orig-buffer (cons origline table-orig-buffer))
  20881. (when (or (not lines)
  20882. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20883. (car lines))))
  20884. (setq table-open nil
  20885. table-buffer (nreverse table-buffer)
  20886. table-orig-buffer (nreverse table-orig-buffer))
  20887. (org-close-par-maybe)
  20888. (insert (org-format-table-html table-buffer table-orig-buffer))))
  20889. (t
  20890. ;; Normal lines
  20891. (when (string-match
  20892. (cond
  20893. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20894. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20895. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20896. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  20897. line)
  20898. (setq ind (org-get-string-indentation line)
  20899. start-is-num (match-beginning 4)
  20900. starter (if (match-beginning 2)
  20901. (substring (match-string 2 line) 0 -1))
  20902. line (substring line (match-beginning 5)))
  20903. (unless (string-match "[^ \t]" line)
  20904. ;; empty line. Pretend indentation is large.
  20905. (setq ind (if org-empty-line-terminates-plain-lists
  20906. 0
  20907. (1+ (or (car local-list-indent) 1)))))
  20908. (setq didclose nil)
  20909. (while (and in-local-list
  20910. (or (and (= ind (car local-list-indent))
  20911. (not starter))
  20912. (< ind (car local-list-indent))))
  20913. (setq didclose t)
  20914. (org-close-li)
  20915. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20916. (pop local-list-num) (pop local-list-indent)
  20917. (setq in-local-list local-list-indent))
  20918. (cond
  20919. ((and starter
  20920. (or (not in-local-list)
  20921. (> ind (car local-list-indent))))
  20922. ;; Start new (level of) list
  20923. (org-close-par-maybe)
  20924. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  20925. (push start-is-num local-list-num)
  20926. (push ind local-list-indent)
  20927. (setq in-local-list t))
  20928. (starter
  20929. ;; continue current list
  20930. (org-close-li)
  20931. (insert "<li>\n"))
  20932. (didclose
  20933. ;; we did close a list, normal text follows: need <p>
  20934. (org-open-par)))
  20935. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  20936. (setq line
  20937. (replace-match
  20938. (if (equal (match-string 1 line) "X")
  20939. "<b>[X]</b>"
  20940. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  20941. t t line))))
  20942. ;; Empty lines start a new paragraph. If hand-formatted lists
  20943. ;; are not fully interpreted, lines starting with "-", "+", "*"
  20944. ;; also start a new paragraph.
  20945. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  20946. ;; Is this the start of a footnote?
  20947. (when org-export-with-footnotes
  20948. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  20949. (org-close-par-maybe)
  20950. (let ((n (match-string 1 line)))
  20951. (setq line (replace-match
  20952. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  20953. ;; Check if the line break needs to be conserved
  20954. (cond
  20955. ((string-match "\\\\\\\\[ \t]*$" line)
  20956. (setq line (replace-match "<br/>" t t line)))
  20957. (org-export-preserve-breaks
  20958. (setq line (concat line "<br/>"))))
  20959. (insert line "\n")))))
  20960. ;; Properly close all local lists and other lists
  20961. (when inquote (insert "</pre>\n"))
  20962. (when in-local-list
  20963. ;; Close any local lists before inserting a new header line
  20964. (while local-list-num
  20965. (org-close-li)
  20966. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  20967. (pop local-list-num))
  20968. (setq local-list-indent nil
  20969. in-local-list nil))
  20970. (org-html-level-start 1 nil umax
  20971. (and org-export-with-toc (<= level umax))
  20972. head-count)
  20973. (unless body-only
  20974. (when (plist-get opt-plist :auto-postamble)
  20975. (when (and org-export-author-info author)
  20976. (insert "<p class=\"author\"> "
  20977. (nth 1 lang-words) ": " author "\n")
  20978. (when email
  20979. (insert "<a href=\"mailto:" email "\">&lt;"
  20980. email "&gt;</a>\n"))
  20981. (insert "</p>\n"))
  20982. (when (and date org-export-time-stamp-file)
  20983. (insert "<p class=\"date\"> "
  20984. (nth 2 lang-words) ": "
  20985. date "</p>\n")))
  20986. (if org-export-html-with-timestamp
  20987. (insert org-export-html-html-helper-timestamp))
  20988. (insert (or (plist-get opt-plist :postamble) ""))
  20989. (insert "</body>\n</html>\n"))
  20990. (normal-mode)
  20991. (if (eq major-mode default-major-mode) (html-mode))
  20992. ;; insert the table of contents
  20993. (goto-char (point-min))
  20994. (when thetoc
  20995. (if (or (re-search-forward
  20996. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  20997. (re-search-forward
  20998. "\\[TABLE-OF-CONTENTS\\]" nil t))
  20999. (progn
  21000. (goto-char (match-beginning 0))
  21001. (replace-match ""))
  21002. (goto-char first-heading-pos)
  21003. (when (looking-at "\\s-*</p>")
  21004. (goto-char (match-end 0))
  21005. (insert "\n")))
  21006. (mapc 'insert thetoc))
  21007. ;; remove empty paragraphs and lists
  21008. (goto-char (point-min))
  21009. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  21010. (replace-match ""))
  21011. (goto-char (point-min))
  21012. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  21013. (replace-match ""))
  21014. (or to-buffer (save-buffer))
  21015. (goto-char (point-min))
  21016. (message "Exporting... done")
  21017. (if (eq to-buffer 'string)
  21018. (prog1 (buffer-substring (point-min) (point-max))
  21019. (kill-buffer (current-buffer)))
  21020. (current-buffer)))))
  21021. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  21022. (defun org-format-table-ascii (lines)
  21023. "Format a table for ascii export."
  21024. (if (stringp lines)
  21025. (setq lines (org-split-string lines "\n")))
  21026. (if (not (string-match "^[ \t]*|" (car lines)))
  21027. ;; Table made by table.el - test for spanning
  21028. lines
  21029. ;; A normal org table
  21030. ;; Get rid of hlines at beginning and end
  21031. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21032. (setq lines (nreverse lines))
  21033. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21034. (setq lines (nreverse lines))
  21035. (when org-export-table-remove-special-lines
  21036. ;; Check if the table has a marking column. If yes remove the
  21037. ;; column and the special lines
  21038. (setq lines (org-table-clean-before-export lines)))
  21039. ;; Get rid of the vertical lines except for grouping
  21040. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  21041. rtn line vl1 start)
  21042. (while (setq line (pop lines))
  21043. (if (string-match org-table-hline-regexp line)
  21044. (and (string-match "|\\(.*\\)|" line)
  21045. (setq line (replace-match " \\1" t nil line)))
  21046. (setq start 0 vl1 vl)
  21047. (while (string-match "|" line start)
  21048. (setq start (match-end 0))
  21049. (or (pop vl1) (setq line (replace-match " " t t line)))))
  21050. (push line rtn))
  21051. (nreverse rtn))))
  21052. (defun org-colgroup-info-to-vline-list (info)
  21053. (let (vl new last)
  21054. (while info
  21055. (setq last new new (pop info))
  21056. (if (or (memq last '(:end :startend))
  21057. (memq new '(:start :startend)))
  21058. (push t vl)
  21059. (push nil vl)))
  21060. (setq vl (cons nil (nreverse vl)))))
  21061. (defun org-format-table-html (lines olines)
  21062. "Find out which HTML converter to use and return the HTML code."
  21063. (if (stringp lines)
  21064. (setq lines (org-split-string lines "\n")))
  21065. (if (string-match "^[ \t]*|" (car lines))
  21066. ;; A normal org table
  21067. (org-format-org-table-html lines)
  21068. ;; Table made by table.el - test for spanning
  21069. (let* ((hlines (delq nil (mapcar
  21070. (lambda (x)
  21071. (if (string-match "^[ \t]*\\+-" x) x
  21072. nil))
  21073. lines)))
  21074. (first (car hlines))
  21075. (ll (and (string-match "\\S-+" first)
  21076. (match-string 0 first)))
  21077. (re (concat "^[ \t]*" (regexp-quote ll)))
  21078. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  21079. hlines))))
  21080. (if (and (not spanning)
  21081. (not org-export-prefer-native-exporter-for-tables))
  21082. ;; We can use my own converter with HTML conversions
  21083. (org-format-table-table-html lines)
  21084. ;; Need to use the code generator in table.el, with the original text.
  21085. (org-format-table-table-html-using-table-generate-source olines)))))
  21086. (defun org-format-org-table-html (lines &optional splice)
  21087. "Format a table into HTML."
  21088. ;; Get rid of hlines at beginning and end
  21089. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21090. (setq lines (nreverse lines))
  21091. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21092. (setq lines (nreverse lines))
  21093. (when org-export-table-remove-special-lines
  21094. ;; Check if the table has a marking column. If yes remove the
  21095. ;; column and the special lines
  21096. (setq lines (org-table-clean-before-export lines)))
  21097. (let ((head (and org-export-highlight-first-table-line
  21098. (delq nil (mapcar
  21099. (lambda (x) (string-match "^[ \t]*|-" x))
  21100. (cdr lines)))))
  21101. (nlines 0) fnum i
  21102. tbopen line fields html gr colgropen)
  21103. (if splice (setq head nil))
  21104. (unless splice (push (if head "<thead>" "<tbody>") html))
  21105. (setq tbopen t)
  21106. (while (setq line (pop lines))
  21107. (catch 'next-line
  21108. (if (string-match "^[ \t]*|-" line)
  21109. (progn
  21110. (unless splice
  21111. (push (if head "</thead>" "</tbody>") html)
  21112. (if lines (push "<tbody>" html) (setq tbopen nil)))
  21113. (setq head nil) ;; head ends here, first time around
  21114. ;; ignore this line
  21115. (throw 'next-line t)))
  21116. ;; Break the line into fields
  21117. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  21118. (unless fnum (setq fnum (make-vector (length fields) 0)))
  21119. (setq nlines (1+ nlines) i -1)
  21120. (push (concat "<tr>"
  21121. (mapconcat
  21122. (lambda (x)
  21123. (setq i (1+ i))
  21124. (if (and (< i nlines)
  21125. (string-match org-table-number-regexp x))
  21126. (incf (aref fnum i)))
  21127. (if head
  21128. (concat (car org-export-table-header-tags) x
  21129. (cdr org-export-table-header-tags))
  21130. (concat (car org-export-table-data-tags) x
  21131. (cdr org-export-table-data-tags))))
  21132. fields "")
  21133. "</tr>")
  21134. html)))
  21135. (unless splice (if tbopen (push "</tbody>" html)))
  21136. (unless splice (push "</table>\n" html))
  21137. (setq html (nreverse html))
  21138. (unless splice
  21139. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  21140. (push (mapconcat
  21141. (lambda (x)
  21142. (setq gr (pop org-table-colgroup-info))
  21143. (format "%s<COL align=\"%s\"></COL>%s"
  21144. (if (memq gr '(:start :startend))
  21145. (prog1
  21146. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  21147. (setq colgropen t))
  21148. "")
  21149. (if (> (/ (float x) nlines) org-table-number-fraction)
  21150. "right" "left")
  21151. (if (memq gr '(:end :startend))
  21152. (progn (setq colgropen nil) "</colgroup>")
  21153. "")))
  21154. fnum "")
  21155. html)
  21156. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  21157. (push org-export-html-table-tag html))
  21158. (concat (mapconcat 'identity html "\n") "\n")))
  21159. (defun org-table-clean-before-export (lines)
  21160. "Check if the table has a marking column.
  21161. If yes remove the column and the special lines."
  21162. (setq org-table-colgroup-info nil)
  21163. (if (memq nil
  21164. (mapcar
  21165. (lambda (x) (or (string-match "^[ \t]*|-" x)
  21166. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  21167. lines))
  21168. (progn
  21169. (setq org-table-clean-did-remove-column nil)
  21170. (delq nil
  21171. (mapcar
  21172. (lambda (x)
  21173. (cond
  21174. ((string-match "^[ \t]*| */ *|" x)
  21175. (setq org-table-colgroup-info
  21176. (mapcar (lambda (x)
  21177. (cond ((member x '("<" "&lt;")) :start)
  21178. ((member x '(">" "&gt;")) :end)
  21179. ((member x '("<>" "&lt;&gt;")) :startend)
  21180. (t nil)))
  21181. (org-split-string x "[ \t]*|[ \t]*")))
  21182. nil)
  21183. (t x)))
  21184. lines)))
  21185. (setq org-table-clean-did-remove-column t)
  21186. (delq nil
  21187. (mapcar
  21188. (lambda (x)
  21189. (cond
  21190. ((string-match "^[ \t]*| */ *|" x)
  21191. (setq org-table-colgroup-info
  21192. (mapcar (lambda (x)
  21193. (cond ((member x '("<" "&lt;")) :start)
  21194. ((member x '(">" "&gt;")) :end)
  21195. ((member x '("<>" "&lt;&gt;")) :startend)
  21196. (t nil)))
  21197. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  21198. nil)
  21199. ((string-match "^[ \t]*| *[!_^/] *|" x)
  21200. nil) ; ignore this line
  21201. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  21202. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  21203. ;; remove the first column
  21204. (replace-match "\\1|" t nil x))
  21205. (t (error "This should not happen"))))
  21206. lines))))
  21207. (defun org-format-table-table-html (lines)
  21208. "Format a table generated by table.el into HTML.
  21209. This conversion does *not* use `table-generate-source' from table.el.
  21210. This has the advantage that Org-mode's HTML conversions can be used.
  21211. But it has the disadvantage, that no cell- or row-spanning is allowed."
  21212. (let (line field-buffer
  21213. (head org-export-highlight-first-table-line)
  21214. fields html empty)
  21215. (setq html (concat org-export-html-table-tag "\n"))
  21216. (while (setq line (pop lines))
  21217. (setq empty "&nbsp;")
  21218. (catch 'next-line
  21219. (if (string-match "^[ \t]*\\+-" line)
  21220. (progn
  21221. (if field-buffer
  21222. (progn
  21223. (setq
  21224. html
  21225. (concat
  21226. html
  21227. "<tr>"
  21228. (mapconcat
  21229. (lambda (x)
  21230. (if (equal x "") (setq x empty))
  21231. (if head
  21232. (concat (car org-export-table-header-tags) x
  21233. (cdr org-export-table-header-tags))
  21234. (concat (car org-export-table-data-tags) x
  21235. (cdr org-export-table-data-tags))))
  21236. field-buffer "\n")
  21237. "</tr>\n"))
  21238. (setq head nil)
  21239. (setq field-buffer nil)))
  21240. ;; Ignore this line
  21241. (throw 'next-line t)))
  21242. ;; Break the line into fields and store the fields
  21243. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  21244. (if field-buffer
  21245. (setq field-buffer (mapcar
  21246. (lambda (x)
  21247. (concat x "<br/>" (pop fields)))
  21248. field-buffer))
  21249. (setq field-buffer fields))))
  21250. (setq html (concat html "</table>\n"))
  21251. html))
  21252. (defun org-format-table-table-html-using-table-generate-source (lines)
  21253. "Format a table into html, using `table-generate-source' from table.el.
  21254. This has the advantage that cell- or row-spanning is allowed.
  21255. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  21256. (require 'table)
  21257. (with-current-buffer (get-buffer-create " org-tmp1 ")
  21258. (erase-buffer)
  21259. (insert (mapconcat 'identity lines "\n"))
  21260. (goto-char (point-min))
  21261. (if (not (re-search-forward "|[^+]" nil t))
  21262. (error "Error processing table"))
  21263. (table-recognize-table)
  21264. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  21265. (table-generate-source 'html " org-tmp2 ")
  21266. (set-buffer " org-tmp2 ")
  21267. (buffer-substring (point-min) (point-max))))
  21268. (defun org-html-handle-time-stamps (s)
  21269. "Format time stamps in string S, or remove them."
  21270. (catch 'exit
  21271. (let (r b)
  21272. (while (string-match org-maybe-keyword-time-regexp s)
  21273. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  21274. ;; never export CLOCK
  21275. (throw 'exit ""))
  21276. (or b (setq b (substring s 0 (match-beginning 0))))
  21277. (if (not org-export-with-timestamps)
  21278. (setq r (concat r (substring s 0 (match-beginning 0)))
  21279. s (substring s (match-end 0)))
  21280. (setq r (concat
  21281. r (substring s 0 (match-beginning 0))
  21282. (if (match-end 1)
  21283. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  21284. (match-string 1 s)))
  21285. (format " @<span class=\"timestamp\">%s@</span>"
  21286. (substring
  21287. (org-translate-time (match-string 3 s)) 1 -1)))
  21288. s (substring s (match-end 0)))))
  21289. ;; Line break if line started and ended with time stamp stuff
  21290. (if (not r)
  21291. s
  21292. (setq r (concat r s))
  21293. (unless (string-match "\\S-" (concat b s))
  21294. (setq r (concat r "@<br/>")))
  21295. r))))
  21296. (defun org-html-protect (s)
  21297. ;; convert & to &amp;, < to &lt; and > to &gt;
  21298. (let ((start 0))
  21299. (while (string-match "&" s start)
  21300. (setq s (replace-match "&amp;" t t s)
  21301. start (1+ (match-beginning 0))))
  21302. (while (string-match "<" s)
  21303. (setq s (replace-match "&lt;" t t s)))
  21304. (while (string-match ">" s)
  21305. (setq s (replace-match "&gt;" t t s))))
  21306. s)
  21307. (defun org-export-cleanup-toc-line (s)
  21308. "Remove tags and time staps from lines going into the toc."
  21309. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  21310. (setq s (replace-match "" t t s)))
  21311. (when org-export-remove-timestamps-from-toc
  21312. (while (string-match org-maybe-keyword-time-regexp s)
  21313. (setq s (replace-match "" t t s))))
  21314. (while (string-match org-bracket-link-regexp s)
  21315. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  21316. t t s)))
  21317. s)
  21318. (defun org-html-expand (string)
  21319. "Prepare STRING for HTML export. Applies all active conversions.
  21320. If there are links in the string, don't modify these."
  21321. (let* (m s l res)
  21322. (while (setq m (string-match org-bracket-link-regexp string))
  21323. (setq s (substring string 0 m)
  21324. l (match-string 0 string)
  21325. string (substring string (match-end 0)))
  21326. (push (org-html-do-expand s) res)
  21327. (push l res))
  21328. (push (org-html-do-expand string) res)
  21329. (apply 'concat (nreverse res))))
  21330. (defun org-html-do-expand (s)
  21331. "Apply all active conversions to translate special ASCII to HTML."
  21332. (setq s (org-html-protect s))
  21333. (if org-export-html-expand
  21334. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  21335. (setq s (replace-match "<\\1>" t nil s))))
  21336. (if org-export-with-emphasize
  21337. (setq s (org-export-html-convert-emphasize s)))
  21338. (if org-export-with-sub-superscripts
  21339. (setq s (org-export-html-convert-sub-super s)))
  21340. (if org-export-with-TeX-macros
  21341. (let ((start 0) wd ass)
  21342. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  21343. (setq wd (match-string 1 s))
  21344. (if (setq ass (assoc wd org-html-entities))
  21345. (setq s (replace-match (or (cdr ass)
  21346. (concat "&" (car ass) ";"))
  21347. t t s))
  21348. (setq start (+ start (length wd)))))))
  21349. s)
  21350. (defun org-create-multibrace-regexp (left right n)
  21351. "Create a regular expression which will match a balanced sexp.
  21352. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  21353. as single character strings.
  21354. The regexp returned will match the entire expression including the
  21355. delimiters. It will also define a single group which contains the
  21356. match except for the outermost delimiters. The maximum depth of
  21357. stacked delimiters is N. Escaping delimiters is not possible."
  21358. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  21359. (or "\\|")
  21360. (re nothing)
  21361. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  21362. (while (> n 1)
  21363. (setq n (1- n)
  21364. re (concat re or next)
  21365. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  21366. (concat left "\\(" re "\\)" right)))
  21367. (defvar org-match-substring-regexp
  21368. (concat
  21369. "\\([^\\]\\)\\([_^]\\)\\("
  21370. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  21371. "\\|"
  21372. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  21373. "\\|"
  21374. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  21375. "The regular expression matching a sub- or superscript.")
  21376. ;(let ((s "a\\_b"))
  21377. ; (and (string-match org-match-substring-regexp s)
  21378. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  21379. (defun org-export-html-convert-sub-super (string)
  21380. "Convert sub- and superscripts in STRING to HTML."
  21381. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  21382. (while (string-match org-match-substring-regexp string s)
  21383. (if (and requireb (match-end 8))
  21384. (setq s (match-end 2))
  21385. (setq s (match-end 1)
  21386. key (if (string= (match-string 2 string) "_") "sub" "sup")
  21387. c (or (match-string 8 string)
  21388. (match-string 6 string)
  21389. (match-string 5 string))
  21390. string (replace-match
  21391. (concat (match-string 1 string)
  21392. "<" key ">" c "</" key ">")
  21393. t t string))))
  21394. (while (string-match "\\\\\\([_^]\\)" string)
  21395. (setq string (replace-match (match-string 1 string) t t string)))
  21396. string))
  21397. (defun org-export-html-convert-emphasize (string)
  21398. "Apply emphasis."
  21399. (let ((s 0))
  21400. (while (string-match org-emph-re string s)
  21401. (if (not (equal
  21402. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  21403. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  21404. (setq string (replace-match
  21405. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  21406. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  21407. "\\5") t nil string))
  21408. (setq s (1+ s))))
  21409. string))
  21410. (defvar org-par-open nil)
  21411. (defun org-open-par ()
  21412. "Insert <p>, but first close previous paragraph if any."
  21413. (org-close-par-maybe)
  21414. (insert "\n<p>")
  21415. (setq org-par-open t))
  21416. (defun org-close-par-maybe ()
  21417. "Close paragraph if there is one open."
  21418. (when org-par-open
  21419. (insert "</p>")
  21420. (setq org-par-open nil)))
  21421. (defun org-close-li ()
  21422. "Close <li> if necessary."
  21423. (org-close-par-maybe)
  21424. (insert "</li>\n"))
  21425. (defvar body-only) ; dynamically scoped into this.
  21426. (defun org-html-level-start (level title umax with-toc head-count)
  21427. "Insert a new level in HTML export.
  21428. When TITLE is nil, just close all open levels."
  21429. (org-close-par-maybe)
  21430. (let ((l (1+ (max level umax))))
  21431. (while (<= l org-level-max)
  21432. (if (aref org-levels-open (1- l))
  21433. (progn
  21434. (org-html-level-close l)
  21435. (aset org-levels-open (1- l) nil)))
  21436. (setq l (1+ l)))
  21437. (when title
  21438. ;; If title is nil, this means this function is called to close
  21439. ;; all levels, so the rest is done only if title is given
  21440. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  21441. (setq title (replace-match
  21442. (if org-export-with-tags
  21443. (save-match-data
  21444. (concat
  21445. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  21446. (mapconcat 'identity (org-split-string
  21447. (match-string 1 title) ":")
  21448. "&nbsp;")
  21449. "</span>"))
  21450. "")
  21451. t t title)))
  21452. (if (> level umax)
  21453. (progn
  21454. (if (aref org-levels-open (1- level))
  21455. (progn
  21456. (org-close-li)
  21457. (insert "<li>" title "<br/>\n"))
  21458. (aset org-levels-open (1- level) t)
  21459. (org-close-par-maybe)
  21460. (insert "<ul>\n<li>" title "<br/>\n")))
  21461. (if (and org-export-with-section-numbers (not body-only))
  21462. (setq title (concat (org-section-number level) " " title)))
  21463. (setq level (+ level org-export-html-toplevel-hlevel -1))
  21464. (if with-toc
  21465. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  21466. level head-count title level))
  21467. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  21468. (org-open-par)))))
  21469. (defun org-html-level-close (&rest args)
  21470. "Terminate one level in HTML export."
  21471. (org-close-li)
  21472. (insert "</ul>\n"))
  21473. ;;; iCalendar export
  21474. ;;;###autoload
  21475. (defun org-export-icalendar-this-file ()
  21476. "Export current file as an iCalendar file.
  21477. The iCalendar file will be located in the same directory as the Org-mode
  21478. file, but with extension `.ics'."
  21479. (interactive)
  21480. (org-export-icalendar nil buffer-file-name))
  21481. ;;;###autoload
  21482. (defun org-export-icalendar-all-agenda-files ()
  21483. "Export all files in `org-agenda-files' to iCalendar .ics files.
  21484. Each iCalendar file will be located in the same directory as the Org-mode
  21485. file, but with extension `.ics'."
  21486. (interactive)
  21487. (apply 'org-export-icalendar nil (org-agenda-files t)))
  21488. ;;;###autoload
  21489. (defun org-export-icalendar-combine-agenda-files ()
  21490. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  21491. The file is stored under the name `org-combined-agenda-icalendar-file'."
  21492. (interactive)
  21493. (apply 'org-export-icalendar t (org-agenda-files t)))
  21494. (defun org-export-icalendar (combine &rest files)
  21495. "Create iCalendar files for all elements of FILES.
  21496. If COMBINE is non-nil, combine all calendar entries into a single large
  21497. file and store it under the name `org-combined-agenda-icalendar-file'."
  21498. (save-excursion
  21499. (org-prepare-agenda-buffers files)
  21500. (let* ((dir (org-export-directory
  21501. :ical (list :publishing-directory
  21502. org-export-publishing-directory)))
  21503. file ical-file ical-buffer category started org-agenda-new-buffers)
  21504. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  21505. (when combine
  21506. (setq ical-file
  21507. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  21508. org-combined-agenda-icalendar-file
  21509. (expand-file-name org-combined-agenda-icalendar-file dir))
  21510. ical-buffer (org-get-agenda-file-buffer ical-file))
  21511. (set-buffer ical-buffer) (erase-buffer))
  21512. (while (setq file (pop files))
  21513. (catch 'nextfile
  21514. (org-check-agenda-file file)
  21515. (set-buffer (org-get-agenda-file-buffer file))
  21516. (unless combine
  21517. (setq ical-file (concat (file-name-as-directory dir)
  21518. (file-name-sans-extension
  21519. (file-name-nondirectory buffer-file-name))
  21520. ".ics"))
  21521. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  21522. (with-current-buffer ical-buffer (erase-buffer)))
  21523. (setq category (or org-category
  21524. (file-name-sans-extension
  21525. (file-name-nondirectory buffer-file-name))))
  21526. (if (symbolp category) (setq category (symbol-name category)))
  21527. (let ((standard-output ical-buffer))
  21528. (if combine
  21529. (and (not started) (setq started t)
  21530. (org-start-icalendar-file org-icalendar-combined-name))
  21531. (org-start-icalendar-file category))
  21532. (org-print-icalendar-entries combine)
  21533. (when (or (and combine (not files)) (not combine))
  21534. (org-finish-icalendar-file)
  21535. (set-buffer ical-buffer)
  21536. (save-buffer)
  21537. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  21538. (org-release-buffers org-agenda-new-buffers))))
  21539. (defvar org-after-save-iCalendar-file-hook nil
  21540. "Hook run after an iCalendar file has been saved.
  21541. The iCalendar buffer is still current when this hook is run.
  21542. A good way to use this is to tell a desktop calenndar application to re-read
  21543. the iCalendar file.")
  21544. (defun org-print-icalendar-entries (&optional combine)
  21545. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  21546. When COMBINE is non nil, add the category to each line."
  21547. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  21548. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  21549. (dts (org-ical-ts-to-string
  21550. (format-time-string (cdr org-time-stamp-formats) (current-time))
  21551. "DTSTART"))
  21552. hd ts ts2 state status (inc t) pos b sexp rrule
  21553. scheduledp deadlinep tmp pri category entry
  21554. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  21555. (org-refresh-category-properties)
  21556. (save-excursion
  21557. (goto-char (point-min))
  21558. (while (re-search-forward re1 nil t)
  21559. (catch :skip
  21560. (org-agenda-skip)
  21561. (setq pos (match-beginning 0)
  21562. ts (match-string 0)
  21563. inc t
  21564. hd (org-get-heading)
  21565. entry (org-get-cleaned-entry org-icalendar-include-body)
  21566. category (org-get-category))
  21567. (if (looking-at re2)
  21568. (progn
  21569. (goto-char (match-end 0))
  21570. (setq ts2 (match-string 1) inc nil))
  21571. (setq tmp (buffer-substring (max (point-min)
  21572. (- pos org-ds-keyword-length))
  21573. pos)
  21574. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  21575. (progn
  21576. (setq inc nil)
  21577. (replace-match "\\1" t nil ts))
  21578. ts)
  21579. deadlinep (string-match org-deadline-regexp tmp)
  21580. scheduledp (string-match org-scheduled-regexp tmp)
  21581. ;; donep (org-entry-is-done-p)
  21582. ))
  21583. (if (or (string-match org-tr-regexp hd)
  21584. (string-match org-ts-regexp hd))
  21585. (setq hd (replace-match "" t t hd)))
  21586. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  21587. (setq rrule
  21588. (concat "\nRRULE:FREQ="
  21589. (cdr (assoc
  21590. (match-string 2 ts)
  21591. '(("d" . "DAILY")("w" . "WEEKLY")
  21592. ("m" . "MONTHLY")("y" . "YEARLY"))))
  21593. ";INTERVAL=" (match-string 1 ts)))
  21594. (setq rrule ""))
  21595. (if (string-match org-bracket-link-regexp hd)
  21596. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  21597. (match-string 1 hd))
  21598. t t hd)))
  21599. (if deadlinep (setq hd (concat "DL: " hd)))
  21600. (if scheduledp (setq hd (concat "S: " hd)))
  21601. (if (string-match "\\`<%%" ts)
  21602. (with-current-buffer sexp-buffer
  21603. (insert (substring ts 1 -1) " " hd "\n"))
  21604. (princ (format "BEGIN:VEVENT
  21605. %s
  21606. %s%s
  21607. SUMMARY:%s%s
  21608. CATEGORIES:%s
  21609. END:VEVENT\n"
  21610. (org-ical-ts-to-string ts "DTSTART")
  21611. (org-ical-ts-to-string ts2 "DTEND" inc)
  21612. rrule hd
  21613. (if (and entry (string-match "\\S-" entry))
  21614. (concat "\nDESCRIPTION: " entry) "")
  21615. category)))))
  21616. (when (and org-icalendar-include-sexps
  21617. (condition-case nil (require 'icalendar) (error nil))
  21618. (fboundp 'icalendar-export-region))
  21619. ;; Get all the literal sexps
  21620. (goto-char (point-min))
  21621. (while (re-search-forward "^&?%%(" nil t)
  21622. (catch :skip
  21623. (org-agenda-skip)
  21624. (setq b (match-beginning 0))
  21625. (goto-char (1- (match-end 0)))
  21626. (forward-sexp 1)
  21627. (end-of-line 1)
  21628. (setq sexp (buffer-substring b (point)))
  21629. (with-current-buffer sexp-buffer
  21630. (insert sexp "\n"))
  21631. (princ (org-diary-to-ical-string sexp-buffer)))))
  21632. (when org-icalendar-include-todo
  21633. (goto-char (point-min))
  21634. (while (re-search-forward org-todo-line-regexp nil t)
  21635. (catch :skip
  21636. (org-agenda-skip)
  21637. (setq state (match-string 2))
  21638. (setq status (if (member state org-done-keywords)
  21639. "COMPLETED" "NEEDS-ACTION"))
  21640. (when (and state
  21641. (or (not (member state org-done-keywords))
  21642. (eq org-icalendar-include-todo 'all))
  21643. (not (member org-archive-tag (org-get-tags-at)))
  21644. )
  21645. (setq hd (match-string 3)
  21646. entry (org-get-cleaned-entry org-icalendar-include-body))
  21647. (if (string-match org-bracket-link-regexp hd)
  21648. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  21649. (match-string 1 hd))
  21650. t t hd)))
  21651. (if (string-match org-priority-regexp hd)
  21652. (setq pri (string-to-char (match-string 2 hd))
  21653. hd (concat (substring hd 0 (match-beginning 1))
  21654. (substring hd (match-end 1))))
  21655. (setq pri org-default-priority))
  21656. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  21657. (- org-lowest-priority org-highest-priority))))))
  21658. (princ (format "BEGIN:VTODO
  21659. %s
  21660. SUMMARY:%s%s
  21661. CATEGORIES:%s
  21662. SEQUENCE:1
  21663. PRIORITY:%d
  21664. STATUS:%s
  21665. END:VTODO\n"
  21666. dts hd
  21667. (if (and entry (string-match "\\S-" entry))
  21668. (concat "\nDESCRIPTION: " entry) "")
  21669. category pri status)))))))))
  21670. (defun org-get-cleaned-entry (what)
  21671. "Clean-up description string."
  21672. (when what
  21673. (save-excursion
  21674. (org-back-to-heading t)
  21675. (let ((s (buffer-substring (point-at-bol 2) (org-end-of-subtree t)))
  21676. (re (concat org-drawer-regexp "[^\000]*?:END:.*\n?"))
  21677. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  21678. (while (string-match re s) (setq s (replace-match "" t t s)))
  21679. (while (string-match re2 s) (setq s (replace-match "" t t s)))
  21680. (if (string-match "[ \t\r\n]+\\'" s) (setq s (replace-match "" t t s)))
  21681. (while (string-match "[ \t]*\n[ \t]*" s)
  21682. (setq s (replace-match "\\n" t t s)))
  21683. (if (and (numberp what)
  21684. (> (length s) what))
  21685. (substring s 0 what)
  21686. s)))))
  21687. (defun org-start-icalendar-file (name)
  21688. "Start an iCalendar file by inserting the header."
  21689. (let ((user user-full-name)
  21690. (name (or name "unknown"))
  21691. (timezone (cadr (current-time-zone))))
  21692. (princ
  21693. (format "BEGIN:VCALENDAR
  21694. VERSION:2.0
  21695. X-WR-CALNAME:%s
  21696. PRODID:-//%s//Emacs with Org-mode//EN
  21697. X-WR-TIMEZONE:%s
  21698. CALSCALE:GREGORIAN\n" name user timezone))))
  21699. (defun org-finish-icalendar-file ()
  21700. "Finish an iCalendar file by inserting the END statement."
  21701. (princ "END:VCALENDAR\n"))
  21702. (defun org-ical-ts-to-string (s keyword &optional inc)
  21703. "Take a time string S and convert it to iCalendar format.
  21704. KEYWORD is added in front, to make a complete line like DTSTART....
  21705. When INC is non-nil, increase the hour by two (if time string contains
  21706. a time), or the day by one (if it does not contain a time)."
  21707. (let ((t1 (org-parse-time-string s 'nodefault))
  21708. t2 fmt have-time time)
  21709. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  21710. (setq t2 t1 have-time t)
  21711. (setq t2 (org-parse-time-string s)))
  21712. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  21713. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  21714. (when inc
  21715. (if have-time
  21716. (if org-agenda-default-appointment-duration
  21717. (setq mi (+ org-agenda-default-appointment-duration mi))
  21718. (setq h (+ 2 h)))
  21719. (setq d (1+ d))))
  21720. (setq time (encode-time s mi h d m y)))
  21721. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  21722. (concat keyword (format-time-string fmt time))))
  21723. ;;; XOXO export
  21724. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  21725. (with-current-buffer buffer
  21726. (apply 'insert output)))
  21727. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  21728. (defun org-export-as-xoxo (&optional buffer)
  21729. "Export the org buffer as XOXO.
  21730. The XOXO buffer is named *xoxo-<source buffer name>*"
  21731. (interactive (list (current-buffer)))
  21732. ;; A quickie abstraction
  21733. ;; Output everything as XOXO
  21734. (with-current-buffer (get-buffer buffer)
  21735. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  21736. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21737. (org-infile-export-plist)))
  21738. (filename (concat (file-name-as-directory
  21739. (org-export-directory :xoxo opt-plist))
  21740. (file-name-sans-extension
  21741. (file-name-nondirectory buffer-file-name))
  21742. ".html"))
  21743. (out (find-file-noselect filename))
  21744. (last-level 1)
  21745. (hanging-li nil))
  21746. ;; Check the output buffer is empty.
  21747. (with-current-buffer out (erase-buffer))
  21748. ;; Kick off the output
  21749. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  21750. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  21751. (let* ((hd (match-string-no-properties 1))
  21752. (level (length hd))
  21753. (text (concat
  21754. (match-string-no-properties 2)
  21755. (save-excursion
  21756. (goto-char (match-end 0))
  21757. (let ((str ""))
  21758. (catch 'loop
  21759. (while 't
  21760. (forward-line)
  21761. (if (looking-at "^[ \t]\\(.*\\)")
  21762. (setq str (concat str (match-string-no-properties 1)))
  21763. (throw 'loop str)))))))))
  21764. ;; Handle level rendering
  21765. (cond
  21766. ((> level last-level)
  21767. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  21768. ((< level last-level)
  21769. (dotimes (- (- last-level level) 1)
  21770. (if hanging-li
  21771. (org-export-as-xoxo-insert-into out "</li>\n"))
  21772. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21773. (when hanging-li
  21774. (org-export-as-xoxo-insert-into out "</li>\n")
  21775. (setq hanging-li nil)))
  21776. ((equal level last-level)
  21777. (if hanging-li
  21778. (org-export-as-xoxo-insert-into out "</li>\n")))
  21779. )
  21780. (setq last-level level)
  21781. ;; And output the new li
  21782. (setq hanging-li 't)
  21783. (if (equal ?+ (elt text 0))
  21784. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  21785. (org-export-as-xoxo-insert-into out "<li>" text))))
  21786. ;; Finally finish off the ol
  21787. (dotimes (- last-level 1)
  21788. (if hanging-li
  21789. (org-export-as-xoxo-insert-into out "</li>\n"))
  21790. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21791. ;; Finish the buffer off and clean it up.
  21792. (switch-to-buffer-other-window out)
  21793. (indent-region (point-min) (point-max) nil)
  21794. (save-buffer)
  21795. (goto-char (point-min))
  21796. )))
  21797. ;;;; Key bindings
  21798. ;; Make `C-c C-x' a prefix key
  21799. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  21800. ;; TAB key with modifiers
  21801. (org-defkey org-mode-map "\C-i" 'org-cycle)
  21802. (org-defkey org-mode-map [(tab)] 'org-cycle)
  21803. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  21804. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  21805. (org-defkey org-mode-map "\M-\t" 'org-complete)
  21806. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  21807. ;; The following line is necessary under Suse GNU/Linux
  21808. (unless (featurep 'xemacs)
  21809. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  21810. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  21811. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  21812. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  21813. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  21814. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  21815. ;; Cursor keys with modifiers
  21816. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  21817. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  21818. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  21819. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  21820. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  21821. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  21822. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  21823. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  21824. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  21825. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  21826. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  21827. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  21828. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  21829. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  21830. ;;; Extra keys for tty access.
  21831. ;; We only set them when really needed because otherwise the
  21832. ;; menus don't show the simple keys
  21833. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  21834. (not window-system))
  21835. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  21836. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  21837. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  21838. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  21839. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  21840. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  21841. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  21842. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  21843. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  21844. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  21845. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  21846. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  21847. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  21848. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  21849. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  21850. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  21851. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  21852. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  21853. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  21854. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  21855. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  21856. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  21857. ;; All the other keys
  21858. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  21859. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  21860. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  21861. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  21862. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  21863. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  21864. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  21865. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  21866. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  21867. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  21868. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  21869. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  21870. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  21871. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  21872. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  21873. (org-defkey org-mode-map "\C-c\C-x/" 'org-occur-in-agenda-files)
  21874. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  21875. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  21876. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  21877. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  21878. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  21879. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  21880. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  21881. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  21882. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  21883. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  21884. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  21885. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  21886. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  21887. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  21888. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  21889. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  21890. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  21891. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  21892. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  21893. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  21894. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  21895. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  21896. (org-defkey org-mode-map "\C-c^" 'org-sort)
  21897. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  21898. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  21899. (org-defkey org-mode-map "\C-m" 'org-return)
  21900. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  21901. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  21902. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  21903. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  21904. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  21905. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  21906. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  21907. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  21908. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  21909. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  21910. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  21911. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  21912. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  21913. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  21914. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  21915. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  21916. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  21917. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  21918. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  21919. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  21920. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  21921. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  21922. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  21923. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  21924. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  21925. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  21926. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  21927. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  21928. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  21929. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  21930. (when (featurep 'xemacs)
  21931. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  21932. (defsubst org-table-p () (org-at-table-p))
  21933. (defun org-self-insert-command (N)
  21934. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  21935. If the cursor is in a table looking at whitespace, the whitespace is
  21936. overwritten, and the table is not marked as requiring realignment."
  21937. (interactive "p")
  21938. (if (and (org-table-p)
  21939. (progn
  21940. ;; check if we blank the field, and if that triggers align
  21941. (and org-table-auto-blank-field
  21942. (member last-command
  21943. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  21944. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  21945. ;; got extra space, this field does not determine column width
  21946. (let (org-table-may-need-update) (org-table-blank-field))
  21947. ;; no extra space, this field may determine column width
  21948. (org-table-blank-field)))
  21949. t)
  21950. (eq N 1)
  21951. (looking-at "[^|\n]* |"))
  21952. (let (org-table-may-need-update)
  21953. (goto-char (1- (match-end 0)))
  21954. (delete-backward-char 1)
  21955. (goto-char (match-beginning 0))
  21956. (self-insert-command N))
  21957. (setq org-table-may-need-update t)
  21958. (self-insert-command N)
  21959. (org-fix-tags-on-the-fly)))
  21960. (defun org-fix-tags-on-the-fly ()
  21961. (when (and (equal (char-after (point-at-bol)) ?*)
  21962. (org-on-heading-p))
  21963. (org-align-tags-here org-tags-column)))
  21964. (defun org-delete-backward-char (N)
  21965. "Like `delete-backward-char', insert whitespace at field end in tables.
  21966. When deleting backwards, in tables this function will insert whitespace in
  21967. front of the next \"|\" separator, to keep the table aligned. The table will
  21968. still be marked for re-alignment if the field did fill the entire column,
  21969. because, in this case the deletion might narrow the column."
  21970. (interactive "p")
  21971. (if (and (org-table-p)
  21972. (eq N 1)
  21973. (string-match "|" (buffer-substring (point-at-bol) (point)))
  21974. (looking-at ".*?|"))
  21975. (let ((pos (point))
  21976. (noalign (looking-at "[^|\n\r]* |"))
  21977. (c org-table-may-need-update))
  21978. (backward-delete-char N)
  21979. (skip-chars-forward "^|")
  21980. (insert " ")
  21981. (goto-char (1- pos))
  21982. ;; noalign: if there were two spaces at the end, this field
  21983. ;; does not determine the width of the column.
  21984. (if noalign (setq org-table-may-need-update c)))
  21985. (backward-delete-char N)
  21986. (org-fix-tags-on-the-fly)))
  21987. (defun org-delete-char (N)
  21988. "Like `delete-char', but insert whitespace at field end in tables.
  21989. When deleting characters, in tables this function will insert whitespace in
  21990. front of the next \"|\" separator, to keep the table aligned. The table will
  21991. still be marked for re-alignment if the field did fill the entire column,
  21992. because, in this case the deletion might narrow the column."
  21993. (interactive "p")
  21994. (if (and (org-table-p)
  21995. (not (bolp))
  21996. (not (= (char-after) ?|))
  21997. (eq N 1))
  21998. (if (looking-at ".*?|")
  21999. (let ((pos (point))
  22000. (noalign (looking-at "[^|\n\r]* |"))
  22001. (c org-table-may-need-update))
  22002. (replace-match (concat
  22003. (substring (match-string 0) 1 -1)
  22004. " |"))
  22005. (goto-char pos)
  22006. ;; noalign: if there were two spaces at the end, this field
  22007. ;; does not determine the width of the column.
  22008. (if noalign (setq org-table-may-need-update c)))
  22009. (delete-char N))
  22010. (delete-char N)
  22011. (org-fix-tags-on-the-fly)))
  22012. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  22013. (put 'org-self-insert-command 'delete-selection t)
  22014. (put 'orgtbl-self-insert-command 'delete-selection t)
  22015. (put 'org-delete-char 'delete-selection 'supersede)
  22016. (put 'org-delete-backward-char 'delete-selection 'supersede)
  22017. ;; Make `flyspell-mode' delay after some commands
  22018. (put 'org-self-insert-command 'flyspell-delayed t)
  22019. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  22020. (put 'org-delete-char 'flyspell-delayed t)
  22021. (put 'org-delete-backward-char 'flyspell-delayed t)
  22022. (eval-after-load "pabbrev"
  22023. '(progn
  22024. (add-to-list 'pabbrev-expand-after-command-list
  22025. 'orgtbl-self-insert-command t)
  22026. (add-to-list 'pabbrev-expand-after-command-list
  22027. 'org-self-insert-command t)))
  22028. ;; How to do this: Measure non-white length of current string
  22029. ;; If equal to column width, we should realign.
  22030. (defun org-remap (map &rest commands)
  22031. "In MAP, remap the functions given in COMMANDS.
  22032. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  22033. (let (new old)
  22034. (while commands
  22035. (setq old (pop commands) new (pop commands))
  22036. (if (fboundp 'command-remapping)
  22037. (org-defkey map (vector 'remap old) new)
  22038. (substitute-key-definition old new map global-map)))))
  22039. (when (eq org-enable-table-editor 'optimized)
  22040. ;; If the user wants maximum table support, we need to hijack
  22041. ;; some standard editing functions
  22042. (org-remap org-mode-map
  22043. 'self-insert-command 'org-self-insert-command
  22044. 'delete-char 'org-delete-char
  22045. 'delete-backward-char 'org-delete-backward-char)
  22046. (org-defkey org-mode-map "|" 'org-force-self-insert))
  22047. (defun org-shiftcursor-error ()
  22048. "Throw an error because Shift-Cursor command was applied in wrong context."
  22049. (error "This command is active in special context like tables, headlines or timestamps"))
  22050. (defun org-shifttab (&optional arg)
  22051. "Global visibility cycling or move to previous table field.
  22052. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  22053. on context.
  22054. See the individual commands for more information."
  22055. (interactive "P")
  22056. (cond
  22057. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  22058. (arg (message "Content view to level: ")
  22059. (org-content (prefix-numeric-value arg))
  22060. (setq org-cycle-global-status 'overview))
  22061. (t (call-interactively 'org-global-cycle))))
  22062. (defun org-shiftmetaleft ()
  22063. "Promote subtree or delete table column.
  22064. Calls `org-promote-subtree', `org-outdent-item',
  22065. or `org-table-delete-column', depending on context.
  22066. See the individual commands for more information."
  22067. (interactive)
  22068. (cond
  22069. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  22070. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  22071. ((org-at-item-p) (call-interactively 'org-outdent-item))
  22072. (t (org-shiftcursor-error))))
  22073. (defun org-shiftmetaright ()
  22074. "Demote subtree or insert table column.
  22075. Calls `org-demote-subtree', `org-indent-item',
  22076. or `org-table-insert-column', depending on context.
  22077. See the individual commands for more information."
  22078. (interactive)
  22079. (cond
  22080. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  22081. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  22082. ((org-at-item-p) (call-interactively 'org-indent-item))
  22083. (t (org-shiftcursor-error))))
  22084. (defun org-shiftmetaup (&optional arg)
  22085. "Move subtree up or kill table row.
  22086. Calls `org-move-subtree-up' or `org-table-kill-row' or
  22087. `org-move-item-up' depending on context. See the individual commands
  22088. for more information."
  22089. (interactive "P")
  22090. (cond
  22091. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  22092. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  22093. ((org-at-item-p) (call-interactively 'org-move-item-up))
  22094. (t (org-shiftcursor-error))))
  22095. (defun org-shiftmetadown (&optional arg)
  22096. "Move subtree down or insert table row.
  22097. Calls `org-move-subtree-down' or `org-table-insert-row' or
  22098. `org-move-item-down', depending on context. See the individual
  22099. commands for more information."
  22100. (interactive "P")
  22101. (cond
  22102. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  22103. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  22104. ((org-at-item-p) (call-interactively 'org-move-item-down))
  22105. (t (org-shiftcursor-error))))
  22106. (defun org-metaleft (&optional arg)
  22107. "Promote heading or move table column to left.
  22108. Calls `org-do-promote' or `org-table-move-column', depending on context.
  22109. With no specific context, calls the Emacs default `backward-word'.
  22110. See the individual commands for more information."
  22111. (interactive "P")
  22112. (cond
  22113. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  22114. ((or (org-on-heading-p) (org-region-active-p))
  22115. (call-interactively 'org-do-promote))
  22116. ((org-at-item-p) (call-interactively 'org-outdent-item))
  22117. (t (call-interactively 'backward-word))))
  22118. (defun org-metaright (&optional arg)
  22119. "Demote subtree or move table column to right.
  22120. Calls `org-do-demote' or `org-table-move-column', depending on context.
  22121. With no specific context, calls the Emacs default `forward-word'.
  22122. See the individual commands for more information."
  22123. (interactive "P")
  22124. (cond
  22125. ((org-at-table-p) (call-interactively 'org-table-move-column))
  22126. ((or (org-on-heading-p) (org-region-active-p))
  22127. (call-interactively 'org-do-demote))
  22128. ((org-at-item-p) (call-interactively 'org-indent-item))
  22129. (t (call-interactively 'forward-word))))
  22130. (defun org-metaup (&optional arg)
  22131. "Move subtree up or move table row up.
  22132. Calls `org-move-subtree-up' or `org-table-move-row' or
  22133. `org-move-item-up', depending on context. See the individual commands
  22134. for more information."
  22135. (interactive "P")
  22136. (cond
  22137. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  22138. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  22139. ((org-at-item-p) (call-interactively 'org-move-item-up))
  22140. (t (org-shiftcursor-error))))
  22141. (defun org-metadown (&optional arg)
  22142. "Move subtree down or move table row down.
  22143. Calls `org-move-subtree-down' or `org-table-move-row' or
  22144. `org-move-item-down', depending on context. See the individual
  22145. commands for more information."
  22146. (interactive "P")
  22147. (cond
  22148. ((org-at-table-p) (call-interactively 'org-table-move-row))
  22149. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  22150. ((org-at-item-p) (call-interactively 'org-move-item-down))
  22151. (t (org-shiftcursor-error))))
  22152. (defun org-shiftup (&optional arg)
  22153. "Increase item in timestamp or increase priority of current headline.
  22154. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  22155. depending on context. See the individual commands for more information."
  22156. (interactive "P")
  22157. (cond
  22158. ((org-at-timestamp-p t)
  22159. (call-interactively (if org-edit-timestamp-down-means-later
  22160. 'org-timestamp-down 'org-timestamp-up)))
  22161. ((org-on-heading-p) (call-interactively 'org-priority-up))
  22162. ((org-at-item-p) (call-interactively 'org-previous-item))
  22163. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  22164. (defun org-shiftdown (&optional arg)
  22165. "Decrease item in timestamp or decrease priority of current headline.
  22166. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  22167. depending on context. See the individual commands for more information."
  22168. (interactive "P")
  22169. (cond
  22170. ((org-at-timestamp-p t)
  22171. (call-interactively (if org-edit-timestamp-down-means-later
  22172. 'org-timestamp-up 'org-timestamp-down)))
  22173. ((org-on-heading-p) (call-interactively 'org-priority-down))
  22174. (t (call-interactively 'org-next-item))))
  22175. (defun org-shiftright ()
  22176. "Next TODO keyword or timestamp one day later, depending on context."
  22177. (interactive)
  22178. (cond
  22179. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  22180. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  22181. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  22182. (t (org-shiftcursor-error))))
  22183. (defun org-shiftleft ()
  22184. "Previous TODO keyword or timestamp one day earlier, depending on context."
  22185. (interactive)
  22186. (cond
  22187. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  22188. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  22189. ((org-at-property-p)
  22190. (call-interactively 'org-property-previous-allowed-value))
  22191. (t (org-shiftcursor-error))))
  22192. (defun org-shiftcontrolright ()
  22193. "Switch to next TODO set."
  22194. (interactive)
  22195. (cond
  22196. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  22197. (t (org-shiftcursor-error))))
  22198. (defun org-shiftcontrolleft ()
  22199. "Switch to previous TODO set."
  22200. (interactive)
  22201. (cond
  22202. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  22203. (t (org-shiftcursor-error))))
  22204. (defun org-ctrl-c-ret ()
  22205. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  22206. (interactive)
  22207. (cond
  22208. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  22209. (t (call-interactively 'org-insert-heading))))
  22210. (defun org-copy-special ()
  22211. "Copy region in table or copy current subtree.
  22212. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  22213. See the individual commands for more information."
  22214. (interactive)
  22215. (call-interactively
  22216. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  22217. (defun org-cut-special ()
  22218. "Cut region in table or cut current subtree.
  22219. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  22220. See the individual commands for more information."
  22221. (interactive)
  22222. (call-interactively
  22223. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  22224. (defun org-paste-special (arg)
  22225. "Paste rectangular region into table, or past subtree relative to level.
  22226. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  22227. See the individual commands for more information."
  22228. (interactive "P")
  22229. (if (org-at-table-p)
  22230. (org-table-paste-rectangle)
  22231. (org-paste-subtree arg)))
  22232. (defun org-ctrl-c-ctrl-c (&optional arg)
  22233. "Set tags in headline, or update according to changed information at point.
  22234. This command does many different things, depending on context:
  22235. - If the cursor is in a headline, prompt for tags and insert them
  22236. into the current line, aligned to `org-tags-column'. When called
  22237. with prefix arg, realign all tags in the current buffer.
  22238. - If the cursor is in one of the special #+KEYWORD lines, this
  22239. triggers scanning the buffer for these lines and updating the
  22240. information.
  22241. - If the cursor is inside a table, realign the table. This command
  22242. works even if the automatic table editor has been turned off.
  22243. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  22244. the entire table.
  22245. - If the cursor is a the beginning of a dynamic block, update it.
  22246. - If the cursor is inside a table created by the table.el package,
  22247. activate that table.
  22248. - If the current buffer is a remember buffer, close note and file it.
  22249. with a prefix argument, file it without further interaction to the default
  22250. location.
  22251. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  22252. links in this buffer.
  22253. - If the cursor is on a numbered item in a plain list, renumber the
  22254. ordered list."
  22255. (interactive "P")
  22256. (let ((org-enable-table-editor t))
  22257. (cond
  22258. ((or org-clock-overlays
  22259. org-occur-highlights
  22260. org-latex-fragment-image-overlays)
  22261. (org-remove-clock-overlays)
  22262. (org-remove-occur-highlights)
  22263. (org-remove-latex-fragment-image-overlays)
  22264. (message "Temporary highlights/overlays removed from current buffer"))
  22265. ((and (local-variable-p 'org-finish-function (current-buffer))
  22266. (fboundp org-finish-function))
  22267. (funcall org-finish-function))
  22268. ((org-at-property-p)
  22269. (call-interactively 'org-property-action))
  22270. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  22271. ((org-on-heading-p) (call-interactively 'org-set-tags))
  22272. ((org-at-table.el-p)
  22273. (require 'table)
  22274. (beginning-of-line 1)
  22275. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  22276. (call-interactively 'table-recognize-table))
  22277. ((org-at-table-p)
  22278. (org-table-maybe-eval-formula)
  22279. (if arg
  22280. (call-interactively 'org-table-recalculate)
  22281. (org-table-maybe-recalculate-line))
  22282. (call-interactively 'org-table-align))
  22283. ((org-at-item-checkbox-p)
  22284. (call-interactively 'org-toggle-checkbox))
  22285. ((org-at-item-p)
  22286. (call-interactively 'org-maybe-renumber-ordered-list))
  22287. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  22288. ;; Dynamic block
  22289. (call-interactively 'org-update-dblock))
  22290. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  22291. (cond
  22292. ((equal (match-string 1) "TBLFM")
  22293. ;; Recalculate the table before this line
  22294. (save-excursion
  22295. (beginning-of-line 1)
  22296. (skip-chars-backward " \r\n\t")
  22297. (if (org-at-table-p)
  22298. (org-call-with-arg 'org-table-recalculate t))))
  22299. (t
  22300. (call-interactively 'org-mode-restart))))
  22301. (t (error "C-c C-c can do nothing useful at this location.")))))
  22302. (defun org-mode-restart ()
  22303. "Restart Org-mode, to scan again for special lines.
  22304. Also updates the keyword regular expressions."
  22305. (interactive)
  22306. (let ((org-inhibit-startup t)) (org-mode))
  22307. (message "Org-mode restarted to refresh keyword and special line setup"))
  22308. (defun org-return ()
  22309. "Goto next table row or insert a newline.
  22310. Calls `org-table-next-row' or `newline', depending on context.
  22311. See the individual commands for more information."
  22312. (interactive)
  22313. (cond
  22314. ((bobp) (newline))
  22315. ((org-at-table-p)
  22316. (org-table-justify-field-maybe)
  22317. (call-interactively 'org-table-next-row))
  22318. (t (newline))))
  22319. (defun org-ctrl-c-minus ()
  22320. "Insert separator line in table or modify bullet type in list.
  22321. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  22322. depending on context."
  22323. (interactive)
  22324. (cond
  22325. ((org-at-table-p)
  22326. (call-interactively 'org-table-insert-hline))
  22327. ((org-in-item-p)
  22328. (call-interactively 'org-cycle-list-bullet))
  22329. (t (error "`C-c -' does have no function here."))))
  22330. (defun org-meta-return (&optional arg)
  22331. "Insert a new heading or wrap a region in a table.
  22332. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  22333. See the individual commands for more information."
  22334. (interactive "P")
  22335. (cond
  22336. ((org-at-table-p)
  22337. (call-interactively 'org-table-wrap-region))
  22338. (t (call-interactively 'org-insert-heading))))
  22339. ;;; Menu entries
  22340. ;; Define the Org-mode menus
  22341. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  22342. '("Tbl"
  22343. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  22344. ["Next Field" org-cycle (org-at-table-p)]
  22345. ["Previous Field" org-shifttab (org-at-table-p)]
  22346. ["Next Row" org-return (org-at-table-p)]
  22347. "--"
  22348. ["Blank Field" org-table-blank-field (org-at-table-p)]
  22349. ["Edit Field" org-table-edit-field (org-at-table-p)]
  22350. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  22351. "--"
  22352. ("Column"
  22353. ["Move Column Left" org-metaleft (org-at-table-p)]
  22354. ["Move Column Right" org-metaright (org-at-table-p)]
  22355. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  22356. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  22357. ("Row"
  22358. ["Move Row Up" org-metaup (org-at-table-p)]
  22359. ["Move Row Down" org-metadown (org-at-table-p)]
  22360. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  22361. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  22362. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  22363. "--"
  22364. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  22365. ("Rectangle"
  22366. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  22367. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  22368. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  22369. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  22370. "--"
  22371. ("Calculate"
  22372. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  22373. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  22374. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  22375. "--"
  22376. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  22377. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  22378. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  22379. "--"
  22380. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  22381. "--"
  22382. ["Sum Column/Rectangle" org-table-sum
  22383. (or (org-at-table-p) (org-region-active-p))]
  22384. ["Which Column?" org-table-current-column (org-at-table-p)])
  22385. ["Debug Formulas"
  22386. org-table-toggle-formula-debugger
  22387. :style toggle :selected org-table-formula-debug]
  22388. ["Show Col/Row Numbers"
  22389. org-table-toggle-coordinate-overlays
  22390. :style toggle :selected org-table-overlay-coordinates]
  22391. "--"
  22392. ["Create" org-table-create (and (not (org-at-table-p))
  22393. org-enable-table-editor)]
  22394. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  22395. ["Import from File" org-table-import (not (org-at-table-p))]
  22396. ["Export to File" org-table-export (org-at-table-p)]
  22397. "--"
  22398. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  22399. (easy-menu-define org-org-menu org-mode-map "Org menu"
  22400. '("Org"
  22401. ("Show/Hide"
  22402. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  22403. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  22404. ["Sparse Tree" org-occur t]
  22405. ["Reveal Context" org-reveal t]
  22406. ["Show All" show-all t]
  22407. "--"
  22408. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  22409. "--"
  22410. ["New Heading" org-insert-heading t]
  22411. ("Navigate Headings"
  22412. ["Up" outline-up-heading t]
  22413. ["Next" outline-next-visible-heading t]
  22414. ["Previous" outline-previous-visible-heading t]
  22415. ["Next Same Level" outline-forward-same-level t]
  22416. ["Previous Same Level" outline-backward-same-level t]
  22417. "--"
  22418. ["Jump" org-goto t])
  22419. ("Edit Structure"
  22420. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  22421. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  22422. "--"
  22423. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  22424. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  22425. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  22426. "--"
  22427. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  22428. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  22429. ["Demote Heading" org-metaright (not (org-at-table-p))]
  22430. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  22431. "--"
  22432. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  22433. "--"
  22434. ["Convert to odd levels" org-convert-to-odd-levels t]
  22435. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  22436. ("Editing"
  22437. ["Emphasis..." org-emphasize t])
  22438. ("Archive"
  22439. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  22440. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  22441. ; :active t :keys "C-u C-c C-x C-a"]
  22442. ["Sparse trees open ARCHIVE trees"
  22443. (setq org-sparse-tree-open-archived-trees
  22444. (not org-sparse-tree-open-archived-trees))
  22445. :style toggle :selected org-sparse-tree-open-archived-trees]
  22446. ["Cycling opens ARCHIVE trees"
  22447. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  22448. :style toggle :selected org-cycle-open-archived-trees]
  22449. ["Agenda includes ARCHIVE trees"
  22450. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  22451. :style toggle :selected (not org-agenda-skip-archived-trees)]
  22452. "--"
  22453. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  22454. ; ["Check and Move Children" (org-archive-subtree '(4))
  22455. ; :active t :keys "C-u C-c C-x C-s"]
  22456. )
  22457. "--"
  22458. ("TODO Lists"
  22459. ["TODO/DONE/-" org-todo t]
  22460. ("Select keyword"
  22461. ["Next keyword" org-shiftright (org-on-heading-p)]
  22462. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  22463. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  22464. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  22465. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  22466. ["Show TODO Tree" org-show-todo-tree t]
  22467. ["Global TODO list" org-todo-list t]
  22468. "--"
  22469. ["Set Priority" org-priority t]
  22470. ["Priority Up" org-shiftup t]
  22471. ["Priority Down" org-shiftdown t])
  22472. ("TAGS and Properties"
  22473. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  22474. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)] ;FIXME
  22475. ["Column view of properties" org-columns t])
  22476. ("Dates and Scheduling"
  22477. ["Timestamp" org-time-stamp t]
  22478. ["Timestamp (inactive)" org-time-stamp-inactive t]
  22479. ("Change Date"
  22480. ["1 Day Later" org-shiftright t]
  22481. ["1 Day Earlier" org-shiftleft t]
  22482. ["1 ... Later" org-shiftup t]
  22483. ["1 ... Earlier" org-shiftdown t])
  22484. ["Compute Time Range" org-evaluate-time-range t]
  22485. ["Schedule Item" org-schedule t]
  22486. ["Deadline" org-deadline t]
  22487. "--"
  22488. ["Custom time format" org-toggle-time-stamp-overlays
  22489. :style radio :selected org-display-custom-times]
  22490. "--"
  22491. ["Goto Calendar" org-goto-calendar t]
  22492. ["Date from Calendar" org-date-from-calendar t])
  22493. ("Logging work"
  22494. ["Clock in" org-clock-in t]
  22495. ["Clock out" org-clock-out t]
  22496. ["Clock cancel" org-clock-cancel t]
  22497. ["Display times" org-clock-display t]
  22498. ["Create clock table" org-clock-report t]
  22499. "--"
  22500. ["Record DONE time"
  22501. (progn (setq org-log-done (not org-log-done))
  22502. (message "Switching to %s will %s record a timestamp"
  22503. (car org-done-keywords)
  22504. (if org-log-done "automatically" "not")))
  22505. :style toggle :selected org-log-done])
  22506. "--"
  22507. ["Agenda Command..." org-agenda t]
  22508. ("File List for Agenda")
  22509. ("Special views current file"
  22510. ["TODO Tree" org-show-todo-tree t]
  22511. ["Check Deadlines" org-check-deadlines t]
  22512. ["Timeline" org-timeline t]
  22513. ["Tags Tree" org-tags-sparse-tree t])
  22514. "--"
  22515. ("Hyperlinks"
  22516. ["Store Link (Global)" org-store-link t]
  22517. ["Insert Link" org-insert-link t]
  22518. ["Follow Link" org-open-at-point t]
  22519. "--"
  22520. ["Next link" org-next-link t]
  22521. ["Previous link" org-previous-link t]
  22522. "--"
  22523. ["Descriptive Links"
  22524. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  22525. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  22526. ["Literal Links"
  22527. (progn
  22528. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  22529. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  22530. "--"
  22531. ["Export/Publish..." org-export t]
  22532. ("LaTeX"
  22533. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  22534. :selected org-cdlatex-mode]
  22535. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  22536. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  22537. ["Modify math symbol" org-cdlatex-math-modify
  22538. (org-inside-LaTeX-fragment-p)]
  22539. ["Export LaTeX fragments as images"
  22540. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  22541. :style toggle :selected org-export-with-LaTeX-fragments])
  22542. "--"
  22543. ("Documentation"
  22544. ["Show Version" org-version t]
  22545. ["Info Documentation" org-info t])
  22546. ("Customize"
  22547. ["Browse Org Group" org-customize t]
  22548. "--"
  22549. ["Expand This Menu" org-create-customize-menu
  22550. (fboundp 'customize-menu-create)])
  22551. "--"
  22552. ["Refresh setup" org-mode-restart t]
  22553. ))
  22554. (defun org-info (&optional node)
  22555. "Read documentation for Org-mode in the info system.
  22556. With optional NODE, go directly to that node."
  22557. (interactive)
  22558. (require 'info)
  22559. (Info-goto-node (format "(org)%s" (or node ""))))
  22560. (defun org-install-agenda-files-menu ()
  22561. (let ((bl (buffer-list)))
  22562. (save-excursion
  22563. (while bl
  22564. (set-buffer (pop bl))
  22565. (if (org-mode-p) (setq bl nil)))
  22566. (when (org-mode-p)
  22567. (easy-menu-change
  22568. '("Org") "File List for Agenda"
  22569. (append
  22570. (list
  22571. ["Edit File List" (org-edit-agenda-file-list) t]
  22572. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  22573. ["Remove Current File from List" org-remove-file t]
  22574. ["Cycle through agenda files" org-cycle-agenda-files t]
  22575. ["Occur in all agenda files" org-occur-in-agenda-files t]
  22576. "--")
  22577. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  22578. ;;;; Documentation
  22579. (defun org-customize ()
  22580. "Call the customize function with org as argument."
  22581. (interactive)
  22582. (customize-browse 'org))
  22583. (defun org-create-customize-menu ()
  22584. "Create a full customization menu for Org-mode, insert it into the menu."
  22585. (interactive)
  22586. (if (fboundp 'customize-menu-create)
  22587. (progn
  22588. (easy-menu-change
  22589. '("Org") "Customize"
  22590. `(["Browse Org group" org-customize t]
  22591. "--"
  22592. ,(customize-menu-create 'org)
  22593. ["Set" Custom-set t]
  22594. ["Save" Custom-save t]
  22595. ["Reset to Current" Custom-reset-current t]
  22596. ["Reset to Saved" Custom-reset-saved t]
  22597. ["Reset to Standard Settings" Custom-reset-standard t]))
  22598. (message "\"Org\"-menu now contains full customization menu"))
  22599. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  22600. ;;;; Miscellaneous stuff
  22601. ;;; Generally useful functions
  22602. (defun org-context ()
  22603. "Return a list of contexts of the current cursor position.
  22604. If several contexts apply, all are returned.
  22605. Each context entry is a list with a symbol naming the context, and
  22606. two positions indicating start and end of the context. Possible
  22607. contexts are:
  22608. :headline anywhere in a headline
  22609. :headline-stars on the leading stars in a headline
  22610. :todo-keyword on a TODO keyword (including DONE) in a headline
  22611. :tags on the TAGS in a headline
  22612. :priority on the priority cookie in a headline
  22613. :item on the first line of a plain list item
  22614. :item-bullet on the bullet/number of a plain list item
  22615. :checkbox on the checkbox in a plain list item
  22616. :table in an org-mode table
  22617. :table-special on a special filed in a table
  22618. :table-table in a table.el table
  22619. :link on a hyperlink
  22620. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  22621. :target on a <<target>>
  22622. :radio-target on a <<<radio-target>>>
  22623. :latex-fragment on a LaTeX fragment
  22624. :latex-preview on a LaTeX fragment with overlayed preview image
  22625. This function expects the position to be visible because it uses font-lock
  22626. faces as a help to recognize the following contexts: :table-special, :link,
  22627. and :keyword."
  22628. (let* ((f (get-text-property (point) 'face))
  22629. (faces (if (listp f) f (list f)))
  22630. (p (point)) clist o)
  22631. ;; First the large context
  22632. (cond
  22633. ((org-on-heading-p t)
  22634. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  22635. (when (progn
  22636. (beginning-of-line 1)
  22637. (looking-at org-todo-line-tags-regexp))
  22638. (push (org-point-in-group p 1 :headline-stars) clist)
  22639. (push (org-point-in-group p 2 :todo-keyword) clist)
  22640. (push (org-point-in-group p 4 :tags) clist))
  22641. (goto-char p)
  22642. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  22643. (if (looking-at "\\[#[A-Z0-9]\\]")
  22644. (push (org-point-in-group p 0 :priority) clist)))
  22645. ((org-at-item-p)
  22646. (push (org-point-in-group p 2 :item-bullet) clist)
  22647. (push (list :item (point-at-bol)
  22648. (save-excursion (org-end-of-item) (point)))
  22649. clist)
  22650. (and (org-at-item-checkbox-p)
  22651. (push (org-point-in-group p 0 :checkbox) clist)))
  22652. ((org-at-table-p)
  22653. (push (list :table (org-table-begin) (org-table-end)) clist)
  22654. (if (memq 'org-formula faces)
  22655. (push (list :table-special
  22656. (previous-single-property-change p 'face)
  22657. (next-single-property-change p 'face)) clist)))
  22658. ((org-at-table-p 'any)
  22659. (push (list :table-table) clist)))
  22660. (goto-char p)
  22661. ;; Now the small context
  22662. (cond
  22663. ((org-at-timestamp-p)
  22664. (push (org-point-in-group p 0 :timestamp) clist))
  22665. ((memq 'org-link faces)
  22666. (push (list :link
  22667. (previous-single-property-change p 'face)
  22668. (next-single-property-change p 'face)) clist))
  22669. ((memq 'org-special-keyword faces)
  22670. (push (list :keyword
  22671. (previous-single-property-change p 'face)
  22672. (next-single-property-change p 'face)) clist))
  22673. ((org-on-target-p)
  22674. (push (org-point-in-group p 0 :target) clist)
  22675. (goto-char (1- (match-beginning 0)))
  22676. (if (looking-at org-radio-target-regexp)
  22677. (push (org-point-in-group p 0 :radio-target) clist))
  22678. (goto-char p))
  22679. ((setq o (car (delq nil
  22680. (mapcar
  22681. (lambda (x)
  22682. (if (memq x org-latex-fragment-image-overlays) x))
  22683. (org-overlays-at (point))))))
  22684. (push (list :latex-fragment
  22685. (org-overlay-start o) (org-overlay-end o)) clist)
  22686. (push (list :latex-preview
  22687. (org-overlay-start o) (org-overlay-end o)) clist))
  22688. ((org-inside-LaTeX-fragment-p)
  22689. ;; FIXME: positions wrong.
  22690. (push (list :latex-fragment (point) (point)) clist)))
  22691. (setq clist (nreverse (delq nil clist)))
  22692. clist))
  22693. ;; FIXME Compare with at-regexp-p
  22694. (defun org-in-regexp (re &optional nlines visually)
  22695. "Check if point is inside a match of regexp.
  22696. Normally only the current line is checked, but you can include NLINES extra
  22697. lines both before and after point into the search.
  22698. If VISUALLY is set, require that the cursor is not after the match but
  22699. really on, so that the block visually is on the match."
  22700. (catch 'exit
  22701. (let ((pos (point))
  22702. (eol (point-at-eol (+ 1 (or nlines 0))))
  22703. (inc (if visually 1 0)))
  22704. (save-excursion
  22705. (beginning-of-line (- 1 (or nlines 0)))
  22706. (while (re-search-forward re eol t)
  22707. (if (and (<= (match-beginning 0) pos)
  22708. (>= (+ inc (match-end 0)) pos))
  22709. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  22710. (defun org-at-regexp-p (regexp)
  22711. "Is point inside a match of REGEXP in the current line?"
  22712. (catch 'exit
  22713. (save-excursion
  22714. (let ((pos (point)) (end (point-at-eol)))
  22715. (beginning-of-line 1)
  22716. (while (re-search-forward regexp end t)
  22717. (if (and (<= (match-beginning 0) pos)
  22718. (>= (match-end 0) pos))
  22719. (throw 'exit t)))
  22720. nil))))
  22721. (defun org-occur-in-agenda-files (regexp)
  22722. "Call `multi-occur' with buffers for all agenda files."
  22723. (interactive "sList all lines matching: ")
  22724. (multi-occur
  22725. (mapcar
  22726. (lambda (x) (or (get-file-buffer x) (find-file-noselect x)))
  22727. (org-agenda-files))
  22728. regexp))
  22729. (defun org-uniquify (list)
  22730. "Remove duplicate elements from LIST."
  22731. (let (res)
  22732. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  22733. res))
  22734. (defun org-delete-all (elts list)
  22735. "Remove all elements in ELTS from LIST."
  22736. (while elts
  22737. (setq list (delete (pop elts) list)))
  22738. list)
  22739. (defun org-point-in-group (point group &optional context)
  22740. "Check if POINT is in match-group GROUP.
  22741. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  22742. match. If the match group does ot exist or point is not inside it,
  22743. return nil."
  22744. (and (match-beginning group)
  22745. (>= point (match-beginning group))
  22746. (<= point (match-end group))
  22747. (if context
  22748. (list context (match-beginning group) (match-end group))
  22749. t)))
  22750. (defun org-switch-to-buffer-other-window (&rest args)
  22751. "Switch to buffer in a second window on the current frame.
  22752. In particular, do not allow pop-up frames."
  22753. (let (pop-up-frames special-display-buffer-names special-display-regexps
  22754. special-display-function)
  22755. (apply 'switch-to-buffer-other-window args)))
  22756. (defun org-combine-plists (&rest plists)
  22757. "Create a single property list from all plists in PLISTS.
  22758. The process starts by copying the first list, and then setting properties
  22759. from the other lists. Settings in the last list are the most significant
  22760. ones and overrule settings in the other lists."
  22761. (let ((rtn (copy-sequence (pop plists)))
  22762. p v ls)
  22763. (while plists
  22764. (setq ls (pop plists))
  22765. (while ls
  22766. (setq p (pop ls) v (pop ls))
  22767. (setq rtn (plist-put rtn p v))))
  22768. rtn))
  22769. (defun org-move-line-down (arg)
  22770. "Move the current line down. With prefix argument, move it past ARG lines."
  22771. (interactive "p")
  22772. (let ((col (current-column))
  22773. beg end pos)
  22774. (beginning-of-line 1) (setq beg (point))
  22775. (beginning-of-line 2) (setq end (point))
  22776. (beginning-of-line (+ 1 arg))
  22777. (setq pos (move-marker (make-marker) (point)))
  22778. (insert (delete-and-extract-region beg end))
  22779. (goto-char pos)
  22780. (move-to-column col)))
  22781. (defun org-move-line-up (arg)
  22782. "Move the current line up. With prefix argument, move it past ARG lines."
  22783. (interactive "p")
  22784. (let ((col (current-column))
  22785. beg end pos)
  22786. (beginning-of-line 1) (setq beg (point))
  22787. (beginning-of-line 2) (setq end (point))
  22788. (beginning-of-line (- arg))
  22789. (setq pos (move-marker (make-marker) (point)))
  22790. (insert (delete-and-extract-region beg end))
  22791. (goto-char pos)
  22792. (move-to-column col)))
  22793. (defun org-replace-escapes (string table)
  22794. "Replace %-escapes in STRING with values in TABLE.
  22795. TABLE is an association list with keys line \"%a\" and string values.
  22796. The sequences in STRING may contain normal field width and padding information,
  22797. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  22798. so values can contain further %-escapes if they are define later in TABLE."
  22799. (let ((case-fold-search nil)
  22800. e re rpl)
  22801. (while (setq e (pop table))
  22802. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  22803. (while (string-match re string)
  22804. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  22805. (cdr e)))
  22806. (setq string (replace-match rpl t t string))))
  22807. string))
  22808. (defun org-sublist (list start end)
  22809. "Return a section of LIST, from START to END.
  22810. Counting starts at 1."
  22811. (let (rtn (c start))
  22812. (setq list (nthcdr (1- start) list))
  22813. (while (and list (<= c end))
  22814. (push (pop list) rtn)
  22815. (setq c (1+ c)))
  22816. (nreverse rtn)))
  22817. (defun org-find-base-buffer-visiting (file)
  22818. "Like `find-buffer-visiting' but alway return the base buffer and
  22819. not an indirect buffer"
  22820. (let ((buf (find-buffer-visiting file)))
  22821. (or (buffer-base-buffer buf) buf)))
  22822. (defun org-image-file-name-regexp ()
  22823. "Return regexp matching the file names of images."
  22824. (if (fboundp 'image-file-name-regexp)
  22825. (image-file-name-regexp)
  22826. (let ((image-file-name-extensions
  22827. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  22828. "xbm" "xpm" "pbm" "pgm" "ppm")))
  22829. (concat "\\."
  22830. (regexp-opt (nconc (mapcar 'upcase
  22831. image-file-name-extensions)
  22832. image-file-name-extensions)
  22833. t)
  22834. "\\'"))))
  22835. (defun org-file-image-p (file)
  22836. "Return non-nil if FILE is an image."
  22837. (save-match-data
  22838. (string-match (org-image-file-name-regexp) file)))
  22839. ;;; Paragraph filling stuff.
  22840. ;; We want this to be just right, so use the full arsenal.
  22841. (defun org-indent-line-function ()
  22842. "Indent line like previous, but further if previous was headline or item."
  22843. (interactive)
  22844. (let* ((pos (point))
  22845. (itemp (org-at-item-p))
  22846. column bpos bcol tpos tcol bullet btype bullet-type)
  22847. ;; Find the previous relevant line
  22848. (beginning-of-line 1)
  22849. (cond
  22850. ((looking-at "#") (setq column 0))
  22851. ((looking-at "\\*+ ") (setq column 0))
  22852. (t
  22853. (beginning-of-line 0)
  22854. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  22855. (beginning-of-line 0))
  22856. (cond
  22857. ((looking-at "\\*+[ \t]+")
  22858. (goto-char (match-end 0))
  22859. (setq column (current-column)))
  22860. ((org-in-item-p)
  22861. (org-beginning-of-item)
  22862. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22863. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  22864. (setq bpos (match-beginning 1) tpos (match-end 0)
  22865. bcol (progn (goto-char bpos) (current-column))
  22866. tcol (progn (goto-char tpos) (current-column))
  22867. bullet (match-string 1)
  22868. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  22869. (if (not itemp)
  22870. (setq column tcol)
  22871. (goto-char pos)
  22872. (beginning-of-line 1)
  22873. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22874. (setq bullet (match-string 1)
  22875. btype (if (string-match "[0-9]" bullet) "n" bullet))
  22876. (setq column (if (equal btype bullet-type) bcol tcol))))
  22877. (t (setq column (org-get-indentation))))))
  22878. (goto-char pos)
  22879. (if (<= (current-column) (current-indentation))
  22880. (indent-line-to column)
  22881. (save-excursion (indent-line-to column)))
  22882. (setq column (current-column))
  22883. (beginning-of-line 1)
  22884. (if (looking-at
  22885. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  22886. (replace-match (concat "\\1" (format org-property-format
  22887. (match-string 2) (match-string 3)))
  22888. t nil))
  22889. (move-to-column column)))
  22890. (defun org-set-autofill-regexps ()
  22891. (interactive)
  22892. ;; In the paragraph separator we include headlines, because filling
  22893. ;; text in a line directly attached to a headline would otherwise
  22894. ;; fill the headline as well.
  22895. (org-set-local 'comment-start-skip "^#+[ \t]*")
  22896. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  22897. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  22898. ;; The paragraph starter includes hand-formatted lists.
  22899. (org-set-local 'paragraph-start
  22900. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  22901. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  22902. ;; But only if the user has not turned off tables or fixed-width regions
  22903. (org-set-local
  22904. 'auto-fill-inhibit-regexp
  22905. (concat "\\*+ \\|#\\+"
  22906. "\\|[ \t]*" org-keyword-time-regexp
  22907. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  22908. (concat
  22909. "\\|[ \t]*["
  22910. (if org-enable-table-editor "|" "")
  22911. (if org-enable-fixed-width-editor ":" "")
  22912. "]"))))
  22913. ;; We use our own fill-paragraph function, to make sure that tables
  22914. ;; and fixed-width regions are not wrapped. That function will pass
  22915. ;; through to `fill-paragraph' when appropriate.
  22916. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  22917. ; Adaptive filling: To get full control, first make sure that
  22918. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  22919. (org-set-local 'adaptive-fill-regexp "\000")
  22920. (org-set-local 'adaptive-fill-function
  22921. 'org-adaptive-fill-function))
  22922. (defun org-fill-paragraph (&optional justify)
  22923. "Re-align a table, pass through to fill-paragraph if no table."
  22924. (let ((table-p (org-at-table-p))
  22925. (table.el-p (org-at-table.el-p)))
  22926. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22927. (table.el-p t) ; skip table.el tables
  22928. (table-p (org-table-align) t) ; align org-mode tables
  22929. (t nil)))) ; call paragraph-fill
  22930. ;; For reference, this is the default value of adaptive-fill-regexp
  22931. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  22932. (defun org-adaptive-fill-function ()
  22933. "Return a fill prefix for org-mode files.
  22934. In particular, this makes sure hanging paragraphs for hand-formatted lists
  22935. work correctly."
  22936. (cond ((looking-at "#[ \t]+")
  22937. (match-string 0))
  22938. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  22939. (save-excursion
  22940. (goto-char (match-end 0))
  22941. (make-string (current-column) ?\ )))
  22942. (t nil)))
  22943. ;;;; Functions extending outline functionality
  22944. (defun org-beginning-of-line (&optional arg)
  22945. "Go to the beginning of the current line. If that is invisible, continue
  22946. to a visible line beginning. This makes the function of C-a more intuitive.
  22947. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22948. first attempt, and only move to after the tags when the cursor is already
  22949. beyond the end of the headline."
  22950. (interactive "P")
  22951. (let ((pos (point)))
  22952. (beginning-of-line 1)
  22953. (if (bobp)
  22954. nil
  22955. (backward-char 1)
  22956. (if (org-invisible-p)
  22957. (while (and (not (bobp)) (org-invisible-p))
  22958. (backward-char 1)
  22959. (beginning-of-line 1))
  22960. (forward-char 1)))
  22961. (when org-special-ctrl-a/e
  22962. (cond
  22963. ((and (looking-at org-todo-line-regexp)
  22964. (= (char-after (match-end 1)) ?\ ))
  22965. (goto-char
  22966. (if (eq org-special-ctrl-a/e t)
  22967. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  22968. ((= pos (point)) (match-beginning 3))
  22969. (t (point)))
  22970. (cond ((> pos (point)) (point))
  22971. ((not (eq last-command this-command)) (point))
  22972. (t (match-beginning 3))))))
  22973. ((org-at-item-p)
  22974. (goto-char
  22975. (if (eq org-special-ctrl-a/e t)
  22976. (cond ((> pos (match-end 4)) (match-end 4))
  22977. ((= pos (point)) (match-end 4))
  22978. (t (point)))
  22979. (cond ((> pos (point)) (point))
  22980. ((not (eq last-command this-command)) (point))
  22981. (t (match-end 4))))))))))
  22982. (defun org-end-of-line (&optional arg)
  22983. "Go to the end of the line.
  22984. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22985. first attempt, and only move to after the tags when the cursor is already
  22986. beyond the end of the headline."
  22987. (interactive "P")
  22988. (if (or (not org-special-ctrl-a/e)
  22989. (not (org-on-heading-p)))
  22990. (end-of-line arg)
  22991. (let ((pos (point)))
  22992. (beginning-of-line 1)
  22993. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  22994. (if (eq org-special-ctrl-a/e t)
  22995. (if (or (< pos (match-beginning 1))
  22996. (= pos (match-end 0)))
  22997. (goto-char (match-beginning 1))
  22998. (goto-char (match-end 0)))
  22999. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  23000. (goto-char (match-end 0))
  23001. (goto-char (match-beginning 1))))
  23002. (end-of-line arg)))))
  23003. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  23004. (define-key org-mode-map "\C-e" 'org-end-of-line)
  23005. (defun org-invisible-p ()
  23006. "Check if point is at a character currently not visible."
  23007. ;; Early versions of noutline don't have `outline-invisible-p'.
  23008. (if (fboundp 'outline-invisible-p)
  23009. (outline-invisible-p)
  23010. (get-char-property (point) 'invisible)))
  23011. (defun org-invisible-p2 ()
  23012. "Check if point is at a character currently not visible."
  23013. (save-excursion
  23014. (if (and (eolp) (not (bobp))) (backward-char 1))
  23015. ;; Early versions of noutline don't have `outline-invisible-p'.
  23016. (if (fboundp 'outline-invisible-p)
  23017. (outline-invisible-p)
  23018. (get-char-property (point) 'invisible))))
  23019. (defalias 'org-back-to-heading 'outline-back-to-heading)
  23020. (defalias 'org-on-heading-p 'outline-on-heading-p)
  23021. (defalias 'org-at-heading-p 'outline-on-heading-p)
  23022. (defun org-at-heading-or-item-p ()
  23023. (or (org-on-heading-p) (org-at-item-p)))
  23024. (defun org-on-target-p ()
  23025. (or (org-in-regexp org-radio-target-regexp)
  23026. (org-in-regexp org-target-regexp)))
  23027. (defun org-up-heading-all (arg)
  23028. "Move to the heading line of which the present line is a subheading.
  23029. This function considers both visible and invisible heading lines.
  23030. With argument, move up ARG levels."
  23031. (if (fboundp 'outline-up-heading-all)
  23032. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  23033. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  23034. (defun org-up-heading-safe ()
  23035. "Move to the heading line of which the present line is a subheading.
  23036. This version will not throw an error. It will return the level of the
  23037. headline found, or nil if no higher level is found."
  23038. (let ((pos (point)) start-level level
  23039. (re (concat "^" outline-regexp)))
  23040. (catch 'exit
  23041. (outline-back-to-heading t)
  23042. (setq start-level (funcall outline-level))
  23043. (if (equal start-level 1) (throw 'exit nil))
  23044. (while (re-search-backward re nil t)
  23045. (setq level (funcall outline-level))
  23046. (if (< level start-level) (throw 'exit level)))
  23047. nil)))
  23048. (defun org-goto-sibling (&optional previous)
  23049. "Goto the next sibling, even if it is invisible.
  23050. When PREVIOUS is set, go to the previous sibling instead. Returns t
  23051. when a sibling was found. When none is found, return nil and don't
  23052. move point."
  23053. (let ((fun (if previous 're-search-backward 're-search-forward))
  23054. (pos (point))
  23055. (re (concat "^" outline-regexp))
  23056. level l)
  23057. (when (condition-case nil (org-back-to-heading t) (error nil))
  23058. (setq level (funcall outline-level))
  23059. (catch 'exit
  23060. (or previous (forward-char 1))
  23061. (while (funcall fun re nil t)
  23062. (setq l (funcall outline-level))
  23063. (when (< l level) (goto-char pos) (throw 'exit nil))
  23064. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  23065. (goto-char pos)
  23066. nil))))
  23067. (defun org-show-siblings ()
  23068. "Show all siblings of the current headline."
  23069. (save-excursion
  23070. (while (org-goto-sibling) (org-flag-heading nil)))
  23071. (save-excursion
  23072. (while (org-goto-sibling 'previous)
  23073. (org-flag-heading nil))))
  23074. (defun org-show-hidden-entry ()
  23075. "Show an entry where even the heading is hidden."
  23076. (save-excursion
  23077. (org-show-entry)))
  23078. (defun org-flag-heading (flag &optional entry)
  23079. "Flag the current heading. FLAG non-nil means make invisible.
  23080. When ENTRY is non-nil, show the entire entry."
  23081. (save-excursion
  23082. (org-back-to-heading t)
  23083. ;; Check if we should show the entire entry
  23084. (if entry
  23085. (progn
  23086. (org-show-entry)
  23087. (save-excursion
  23088. (and (outline-next-heading)
  23089. (org-flag-heading nil))))
  23090. (outline-flag-region (max (point-min) (1- (point)))
  23091. (save-excursion (outline-end-of-heading) (point))
  23092. flag))))
  23093. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  23094. ;; This is an exact copy of the original function, but it uses
  23095. ;; `org-back-to-heading', to make it work also in invisible
  23096. ;; trees. And is uses an invisible-OK argument.
  23097. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  23098. (org-back-to-heading invisible-OK)
  23099. (let ((first t)
  23100. (level (funcall outline-level)))
  23101. (while (and (not (eobp))
  23102. (or first (> (funcall outline-level) level)))
  23103. (setq first nil)
  23104. (outline-next-heading))
  23105. (unless to-heading
  23106. (if (memq (preceding-char) '(?\n ?\^M))
  23107. (progn
  23108. ;; Go to end of line before heading
  23109. (forward-char -1)
  23110. (if (memq (preceding-char) '(?\n ?\^M))
  23111. ;; leave blank line before heading
  23112. (forward-char -1))))))
  23113. (point))
  23114. (defun org-show-subtree ()
  23115. "Show everything after this heading at deeper levels."
  23116. (outline-flag-region
  23117. (point)
  23118. (save-excursion
  23119. (outline-end-of-subtree) (outline-next-heading) (point))
  23120. nil))
  23121. (defun org-show-entry ()
  23122. "Show the body directly following this heading.
  23123. Show the heading too, if it is currently invisible."
  23124. (interactive)
  23125. (save-excursion
  23126. (condition-case nil
  23127. (progn
  23128. (org-back-to-heading t)
  23129. (outline-flag-region
  23130. (max (point-min) (1- (point)))
  23131. (save-excursion
  23132. (re-search-forward
  23133. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  23134. (or (match-beginning 1) (point-max)))
  23135. nil))
  23136. (error nil))))
  23137. (defun org-make-options-regexp (kwds)
  23138. "Make a regular expression for keyword lines."
  23139. (concat
  23140. "^"
  23141. "#?[ \t]*\\+\\("
  23142. (mapconcat 'regexp-quote kwds "\\|")
  23143. "\\):[ \t]*"
  23144. "\\(.+\\)"))
  23145. ;; Make isearch reveal the necessary context
  23146. (defun org-isearch-end ()
  23147. "Reveal context after isearch exits."
  23148. (when isearch-success ; only if search was successful
  23149. (if (featurep 'xemacs)
  23150. ;; Under XEmacs, the hook is run in the correct place,
  23151. ;; we directly show the context.
  23152. (org-show-context 'isearch)
  23153. ;; In Emacs the hook runs *before* restoring the overlays.
  23154. ;; So we have to use a one-time post-command-hook to do this.
  23155. ;; (Emacs 22 has a special variable, see function `org-mode')
  23156. (unless (and (boundp 'isearch-mode-end-hook-quit)
  23157. isearch-mode-end-hook-quit)
  23158. ;; Only when the isearch was not quitted.
  23159. (org-add-hook 'post-command-hook 'org-isearch-post-command
  23160. 'append 'local)))))
  23161. (defun org-isearch-post-command ()
  23162. "Remove self from hook, and show context."
  23163. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  23164. (org-show-context 'isearch))
  23165. ;;;; Address problems with some other packages
  23166. ;; Make flyspell not check words in links, to not mess up our keymap
  23167. (defun org-mode-flyspell-verify ()
  23168. "Don't let flyspell put overlays at active buttons."
  23169. (not (get-text-property (point) 'keymap)))
  23170. ;; Make `bookmark-jump' show the jump location if it was hidden.
  23171. (eval-after-load "bookmark"
  23172. '(if (boundp 'bookmark-after-jump-hook)
  23173. ;; We can use the hook
  23174. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23175. ;; Hook not available, use advice
  23176. (defadvice bookmark-jump (after org-make-visible activate)
  23177. "Make the position visible."
  23178. (org-bookmark-jump-unhide))))
  23179. (defun org-bookmark-jump-unhide ()
  23180. "Unhide the current position, to show the bookmark location."
  23181. (and (org-mode-p)
  23182. (or (org-invisible-p)
  23183. (save-excursion (goto-char (max (point-min) (1- (point))))
  23184. (org-invisible-p)))
  23185. (org-show-context 'bookmark-jump)))
  23186. ;; Make session.el ignore our circular variable
  23187. (eval-after-load "session"
  23188. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23189. ;;;; Experimental code
  23190. ;; Make appt aware of appointments from the agenda
  23191. (defun org-agenda-to-appt ()
  23192. "Activate appointments found in `org-agenda-files'."
  23193. (interactive)
  23194. (require 'org)
  23195. (let* ((today (org-date-to-gregorian
  23196. (time-to-days (current-time))))
  23197. (files org-agenda-files) entries file)
  23198. (while (setq file (pop files))
  23199. (setq entries (append entries (org-agenda-get-day-entries
  23200. file today :timestamp))))
  23201. (setq entries (delq nil entries))
  23202. (mapc (lambda(x)
  23203. (let* ((event (org-trim (get-text-property 1 'txt x)))
  23204. (time-of-day (get-text-property 1 'time-of-day x)) tod)
  23205. (when time-of-day
  23206. (setq tod (number-to-string time-of-day)
  23207. tod (when (string-match
  23208. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  23209. (concat (match-string 1 tod) ":"
  23210. (match-string 2 tod))))
  23211. (if tod (appt-add tod event))))) entries)))
  23212. (defun org-closed-in-range ()
  23213. "Sparse tree of items closed in a certain time range.
  23214. Still experimental, may disappear in the furture."
  23215. (interactive)
  23216. ;; Get the time interval from the user.
  23217. (let* ((time1 (time-to-seconds
  23218. (org-read-date nil 'to-time nil "Starting date: ")))
  23219. (time2 (time-to-seconds
  23220. (org-read-date nil 'to-time nil "End date:")))
  23221. ;; callback function
  23222. (callback (lambda ()
  23223. (let ((time
  23224. (time-to-seconds
  23225. (apply 'encode-time
  23226. (org-parse-time-string
  23227. (match-string 1))))))
  23228. ;; check if time in interval
  23229. (and (>= time time1) (<= time time2))))))
  23230. ;; make tree, check each match with the callback
  23231. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  23232. (defun org-fill-paragraph-experimental (&optional justify)
  23233. "Re-align a table, pass through to fill-paragraph if no table."
  23234. (let ((table-p (org-at-table-p))
  23235. (table.el-p (org-at-table.el-p)))
  23236. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  23237. (table.el-p t) ; skip table.el tables
  23238. (table-p (org-table-align) t) ; align org-mode tables
  23239. ((save-excursion
  23240. (let ((pos (1+ (point-at-eol))))
  23241. (backward-paragraph 1)
  23242. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  23243. (save-excursion
  23244. (save-restriction
  23245. (narrow-to-region (1+ (match-end 0)) (point-max))
  23246. (fill-paragraph nil)
  23247. t)))
  23248. (t nil)))) ; call paragraph-fill
  23249. ;; FIXME: this needs a much better algorithm
  23250. (defun org-assign-fast-keys (alist)
  23251. "Assign fast keys to a keyword-key alist.
  23252. Respect keys that are already there."
  23253. (let (new e k c c1 c2 (char ?a))
  23254. (while (setq e (pop alist))
  23255. (cond
  23256. ((equal e '(:startgroup)) (push e new))
  23257. ((equal e '(:endgroup)) (push e new))
  23258. (t
  23259. (setq k (car e) c2 nil)
  23260. (if (cdr e)
  23261. (setq c (cdr e))
  23262. ;; automatically assign a character.
  23263. (setq c1 (string-to-char
  23264. (downcase (substring
  23265. k (if (= (string-to-char k) ?@) 1 0)))))
  23266. (if (or (rassoc c1 new) (rassoc c1 alist))
  23267. (while (or (rassoc char new) (rassoc char alist))
  23268. (setq char (1+ char)))
  23269. (setq c2 c1))
  23270. (setq c (or c2 char)))
  23271. (push (cons k c) new))))
  23272. (nreverse new)))
  23273. ;;;; Finish up
  23274. (provide 'org)
  23275. (run-hooks 'org-load-hook)
  23276. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  23277. ;;; org.el ends here